packethost 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +1 -0
- data/.rubocop.yml +15 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +3 -0
- data/LICENSE +340 -0
- data/README.md +56 -0
- data/Rakefile +37 -0
- data/lib/packet/client/devices.rb +34 -0
- data/lib/packet/client/facilities.rb +9 -0
- data/lib/packet/client/operating_systems.rb +9 -0
- data/lib/packet/client/plans.rb +9 -0
- data/lib/packet/client/projects.rb +34 -0
- data/lib/packet/client/ssh_keys.rb +34 -0
- data/lib/packet/client/users.rb +9 -0
- data/lib/packet/client.rb +70 -0
- data/lib/packet/configuration.rb +9 -0
- data/lib/packet/device.rb +9 -0
- data/lib/packet/entity/associations.rb +28 -0
- data/lib/packet/entity/base.rb +58 -0
- data/lib/packet/entity/finders.rb +20 -0
- data/lib/packet/entity/persistence.rb +34 -0
- data/lib/packet/entity/serialization.rb +16 -0
- data/lib/packet/entity/timestamps.rb +17 -0
- data/lib/packet/entity.rb +22 -0
- data/lib/packet/errors.rb +4 -0
- data/lib/packet/facility.rb +9 -0
- data/lib/packet/global_id.rb +27 -0
- data/lib/packet/operating_system.rb +11 -0
- data/lib/packet/plan.rb +12 -0
- data/lib/packet/project.rb +10 -0
- data/lib/packet/ssh_key.rb +10 -0
- data/lib/packet/user.rb +14 -0
- data/lib/packet/version.rb +3 -0
- data/lib/packet.rb +49 -0
- data/packethost.gemspec +34 -0
- metadata +252 -0
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
require 'bundler/gem_tasks'
|
6
|
+
|
7
|
+
begin
|
8
|
+
Bundler.setup(:default, :development)
|
9
|
+
rescue Bundler::BundlerError => e
|
10
|
+
$stderr.puts e.message
|
11
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
12
|
+
exit e.status_code
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'rspec/core'
|
16
|
+
require 'rspec/core/rake_task'
|
17
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
18
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rubocop/rake_task'
|
22
|
+
RuboCop::RakeTask.new
|
23
|
+
|
24
|
+
desc 'Run the CI suite'
|
25
|
+
task ci: [:spec, :rubocop]
|
26
|
+
|
27
|
+
task default: :ci
|
28
|
+
|
29
|
+
require 'rdoc/task'
|
30
|
+
Rake::RDocTask.new do |rdoc|
|
31
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
32
|
+
|
33
|
+
rdoc.rdoc_dir = 'rdoc'
|
34
|
+
rdoc.title = "packet #{version}"
|
35
|
+
rdoc.rdoc_files.include('README*')
|
36
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
37
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Packet
|
2
|
+
class Client
|
3
|
+
module Devices
|
4
|
+
def list_devices(project_id, *args)
|
5
|
+
get("projects/#{project_id}/devices", *args).body['devices'].map { |p| Packet::Device.new(p, self) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_device(id, *args)
|
9
|
+
Packet::Device.new(get("devices/#{id}", *args).body, self)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_device(device)
|
13
|
+
post("projects/#{device.project_id}/devices", device.to_hash).tap do |response|
|
14
|
+
device.update_attributes(response.body)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_device(device)
|
19
|
+
patch("devices/#{device.id}", device.to_hash).tap do |response|
|
20
|
+
device.update_attributes(response.body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete_device(device_or_id)
|
25
|
+
id = if device_or_id.is_a?(Packet::Device)
|
26
|
+
device_or_id.id
|
27
|
+
else
|
28
|
+
device_or_id
|
29
|
+
end
|
30
|
+
delete("devices/#{id}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Packet
|
2
|
+
class Client
|
3
|
+
module Projects
|
4
|
+
def list_projects(*args)
|
5
|
+
get('projects', *args).body['projects'].map { |p| Packet::Project.new(p, self) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_project(id, *args)
|
9
|
+
Packet::Project.new(get("projects/#{id}", *args).body, self)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_project(project)
|
13
|
+
post('projects', project.to_hash).tap do |response|
|
14
|
+
project.update_attributes(response.body)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_project(project)
|
19
|
+
patch("projects/#{project.id}", project.to_hash).tap do |response|
|
20
|
+
project.update_attributes(response.body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete_project(project_or_id)
|
25
|
+
id = if project_or_id.is_a?(Packet::Project)
|
26
|
+
project_or_id.id
|
27
|
+
else
|
28
|
+
project_or_id
|
29
|
+
end
|
30
|
+
delete("projects/#{id}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Packet
|
2
|
+
class Client
|
3
|
+
module SshKeys
|
4
|
+
def list_ssh_keys(*args)
|
5
|
+
get('ssh-keys', *args).body['ssh_keys'].map { |p| Packet::SshKey.new(p, self) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def get_ssh_key(id, *args)
|
9
|
+
Packet::SshKey.new(get("ssh-keys/#{id}", *args).body, self)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_ssh_key(ssh_key)
|
13
|
+
post('ssh-keys', ssh_key.to_hash).tap do |response|
|
14
|
+
ssh_key.update_attributes(response.body)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_ssh_key(ssh_key)
|
19
|
+
patch("ssh-keys/#{ssh_key.id}", ssh_key.to_hash).tap do |response|
|
20
|
+
ssh_key.update_attributes(response.body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete_ssh_key(ssh_key_or_id)
|
25
|
+
id = if ssh_key_or_id.is_a?(Packet::SshKey)
|
26
|
+
ssh_key_or_id.id
|
27
|
+
else
|
28
|
+
ssh_key_or_id
|
29
|
+
end
|
30
|
+
delete("ssh-keys/#{id}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
|
4
|
+
require 'packet/entity'
|
5
|
+
|
6
|
+
%w(device facility operating_system plan project ssh_key user).each do |f|
|
7
|
+
require "packet/#{f}"
|
8
|
+
require "packet/client/#{f.pluralize}"
|
9
|
+
end
|
10
|
+
|
11
|
+
module Packet
|
12
|
+
class Client
|
13
|
+
attr_accessor :auth_token, :consumer_token, :url
|
14
|
+
|
15
|
+
def initialize(auth_token = nil, consumer_token = nil, url = nil)
|
16
|
+
self.url = url || Packet.configuration.url
|
17
|
+
self.auth_token = auth_token || Packet.configuration.auth_token
|
18
|
+
self.consumer_token = consumer_token || Packet.configuration.consumer_token
|
19
|
+
end
|
20
|
+
|
21
|
+
[:get, :post, :patch, :head, :delete].each do |method|
|
22
|
+
define_method(method) do |*args|
|
23
|
+
response = client.send(method, *args)
|
24
|
+
fail_on_error(response) || response
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def inspect
|
29
|
+
%(#<#{self.class}:#{format('%014x', (object_id << 1))}>)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def client
|
35
|
+
@client ||= Faraday.new(url: url, headers: headers, ssl: { verify: true }) do |faraday|
|
36
|
+
faraday.request :json
|
37
|
+
faraday.response :json, content_type: /\bjson$/
|
38
|
+
faraday.adapter Faraday.default_adapter
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def headers
|
43
|
+
{
|
44
|
+
'X-Consumer-Token' => consumer_token,
|
45
|
+
'X-Auth-Token' => auth_token,
|
46
|
+
# 'X-Packet-Staff' => '1',
|
47
|
+
'Content-Type' => 'application/json',
|
48
|
+
'Accept' => 'application/json'
|
49
|
+
}.reject { |_, v| v.nil? }
|
50
|
+
end
|
51
|
+
|
52
|
+
def fail_on_error(response)
|
53
|
+
return if response.success?
|
54
|
+
|
55
|
+
klass = case response.status
|
56
|
+
when 404 then NotFound
|
57
|
+
else Error
|
58
|
+
end
|
59
|
+
fail klass, response.body
|
60
|
+
end
|
61
|
+
|
62
|
+
include Packet::Client::Devices
|
63
|
+
include Packet::Client::Facilities
|
64
|
+
include Packet::Client::OperatingSystems
|
65
|
+
include Packet::Client::Plans
|
66
|
+
include Packet::Client::Projects
|
67
|
+
include Packet::Client::SshKeys
|
68
|
+
include Packet::Client::Users
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module Packet
|
5
|
+
module Entity
|
6
|
+
module Associations
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def has_one(association)
|
11
|
+
require "packet/#{association}"
|
12
|
+
casts(association, lambda do |value|
|
13
|
+
klass = "Packet::#{association.to_s.classify}".constantize
|
14
|
+
klass.new(value, client)
|
15
|
+
end)
|
16
|
+
end
|
17
|
+
|
18
|
+
def has_many(association)
|
19
|
+
require "packet/#{association.to_s.singularize}"
|
20
|
+
casts(association, lambda do |value|
|
21
|
+
klass = "Packet::#{association.to_s.classify}".constantize
|
22
|
+
value.map { |v| klass.new(v, client) }
|
23
|
+
end)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
4
|
+
require 'active_support/core_ext/module/delegation'
|
5
|
+
|
6
|
+
module Packet
|
7
|
+
module Entity
|
8
|
+
module Base
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
class_attribute :_casts
|
13
|
+
self._casts = {}
|
14
|
+
|
15
|
+
attr_accessor :client, :id
|
16
|
+
delegate :resource_name, to: :class
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(attributes = {}, client = nil)
|
20
|
+
self.client = client
|
21
|
+
update_attributes(attributes)
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_attributes(attributes = {})
|
25
|
+
attributes.each_pair do |attribute, value|
|
26
|
+
setter = "#{attribute}="
|
27
|
+
send(setter, _cast_value(attribute, value)) if respond_to?(setter)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def client
|
32
|
+
@client || Packet.client
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
def casts(attribute, transformer)
|
37
|
+
_casts[attribute.to_sym] = transformer
|
38
|
+
attr_accessor attribute.to_sym
|
39
|
+
end
|
40
|
+
|
41
|
+
def resource_name
|
42
|
+
to_s.demodulize.underscore
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def _cast_value(attribute, value)
|
49
|
+
if self.class._casts.key?(attribute.to_sym)
|
50
|
+
callback = self.class._casts[attribute.to_sym]
|
51
|
+
instance_exec(value, &callback)
|
52
|
+
else
|
53
|
+
value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module Packet
|
5
|
+
module Entity
|
6
|
+
module Finders
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def all(params = {}, client = nil)
|
11
|
+
(client || Packet.client).send(:"list_#{resource_name.pluralize}", params)
|
12
|
+
end
|
13
|
+
|
14
|
+
def find(id, params = {}, client = nil)
|
15
|
+
(client || Packet.client).send(:"get_#{resource_name}", id, params)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Packet
|
4
|
+
module Entity
|
5
|
+
module Persistence
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def save!
|
9
|
+
if id
|
10
|
+
client.send("update_#{resource_name}", self)
|
11
|
+
else
|
12
|
+
client.send("create_#{resource_name}", self)
|
13
|
+
end
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
def destroy!
|
18
|
+
client.send("delete_#{resource_name}", self)
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def create!(attributes = {}, client = nil)
|
24
|
+
new(attributes, client).tap(&:save!)
|
25
|
+
end
|
26
|
+
|
27
|
+
def destroy!(id, client = nil)
|
28
|
+
(client || Packet.client).send("delete_#{resource_name}", id)
|
29
|
+
true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Packet
|
4
|
+
module Entity
|
5
|
+
module Serialization
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def to_hash
|
9
|
+
instance_variables.reject { |ivar| [:@client].include?(ivar) }.map do |ivar|
|
10
|
+
[ivar.to_s.tr('@', ''), instance_variable_get(ivar)]
|
11
|
+
end.to_h
|
12
|
+
end
|
13
|
+
alias_method :to_h, :to_hash
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|