droplet_kit 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +2 -0
- data/droplet_kit.gemspec +33 -0
- data/lib/droplet_kit.rb +63 -0
- data/lib/droplet_kit/client.rb +58 -0
- data/lib/droplet_kit/mappings/action_mapping.rb +19 -0
- data/lib/droplet_kit/mappings/backup_mapping.rb +19 -0
- data/lib/droplet_kit/mappings/domain_mapping.rb +15 -0
- data/lib/droplet_kit/mappings/domain_record_mapping.rb +18 -0
- data/lib/droplet_kit/mappings/droplet_mapping.rb +38 -0
- data/lib/droplet_kit/mappings/error_mapping.rb +19 -0
- data/lib/droplet_kit/mappings/image_action_mapping.rb +19 -0
- data/lib/droplet_kit/mappings/image_mapping.rb +17 -0
- data/lib/droplet_kit/mappings/kernel_mapping.rb +14 -0
- data/lib/droplet_kit/mappings/network_detail_mapping.rb +15 -0
- data/lib/droplet_kit/mappings/network_mapping.rb +12 -0
- data/lib/droplet_kit/mappings/region_mapping.rb +16 -0
- data/lib/droplet_kit/mappings/size_mapping.rb +15 -0
- data/lib/droplet_kit/mappings/snapshot_mapping.rb +19 -0
- data/lib/droplet_kit/mappings/ssh_key_mapping.rb +17 -0
- data/lib/droplet_kit/models/action.rb +12 -0
- data/lib/droplet_kit/models/backup.rb +12 -0
- data/lib/droplet_kit/models/base_model.rb +13 -0
- data/lib/droplet_kit/models/domain.rb +10 -0
- data/lib/droplet_kit/models/domain_record.rb +11 -0
- data/lib/droplet_kit/models/droplet.rb +15 -0
- data/lib/droplet_kit/models/image.rb +10 -0
- data/lib/droplet_kit/models/image_action.rb +12 -0
- data/lib/droplet_kit/models/kernel.rb +7 -0
- data/lib/droplet_kit/models/meta_information.rb +14 -0
- data/lib/droplet_kit/models/network.rb +4 -0
- data/lib/droplet_kit/models/network_hash.rb +4 -0
- data/lib/droplet_kit/models/pagination_information.rb +17 -0
- data/lib/droplet_kit/models/region.rb +9 -0
- data/lib/droplet_kit/models/size.rb +8 -0
- data/lib/droplet_kit/models/snapshot.rb +12 -0
- data/lib/droplet_kit/models/ssh_key.rb +8 -0
- data/lib/droplet_kit/paginated_resource.rb +71 -0
- data/lib/droplet_kit/resources/action_resource.rb +17 -0
- data/lib/droplet_kit/resources/domain_record_resource.rb +31 -0
- data/lib/droplet_kit/resources/domain_resource.rb +26 -0
- data/lib/droplet_kit/resources/droplet_action_resource.rb +49 -0
- data/lib/droplet_kit/resources/droplet_resource.rb +44 -0
- data/lib/droplet_kit/resources/image_action_resource.rb +14 -0
- data/lib/droplet_kit/resources/image_resource.rb +26 -0
- data/lib/droplet_kit/resources/region_resource.rb +13 -0
- data/lib/droplet_kit/resources/size_resource.rb +13 -0
- data/lib/droplet_kit/resources/ssh_key_resource.rb +26 -0
- data/lib/droplet_kit/version.rb +3 -0
- data/spec/fixtures/actions/all.json +17 -0
- data/spec/fixtures/actions/find.json +12 -0
- data/spec/fixtures/domain_records/all.json +52 -0
- data/spec/fixtures/domain_records/create.json +11 -0
- data/spec/fixtures/domain_records/find.json +11 -0
- data/spec/fixtures/domain_records/update.json +11 -0
- data/spec/fixtures/domains/all.json +12 -0
- data/spec/fixtures/domains/create.json +7 -0
- data/spec/fixtures/domains/find.json +7 -0
- data/spec/fixtures/droplets/all.json +84 -0
- data/spec/fixtures/droplets/create.json +79 -0
- data/spec/fixtures/droplets/find.json +79 -0
- data/spec/fixtures/droplets/list_actions.json +17 -0
- data/spec/fixtures/droplets/list_backups.json +18 -0
- data/spec/fixtures/droplets/list_kernels.json +17 -0
- data/spec/fixtures/droplets/list_snapshots.json +18 -0
- data/spec/fixtures/image_actions/create.json +12 -0
- data/spec/fixtures/image_actions/find.json +12 -0
- data/spec/fixtures/images/all.json +29 -0
- data/spec/fixtures/images/find.json +13 -0
- data/spec/fixtures/regions/all.json +47 -0
- data/spec/fixtures/sizes/all.json +35 -0
- data/spec/fixtures/ssh_keys/all.json +13 -0
- data/spec/fixtures/ssh_keys/create.json +8 -0
- data/spec/fixtures/ssh_keys/find.json +8 -0
- data/spec/fixtures/ssh_keys/update.json +8 -0
- data/spec/lib/droplet_kit/client_spec.rb +22 -0
- data/spec/lib/droplet_kit/paginated_resource_spec.rb +69 -0
- data/spec/lib/droplet_kit/resources/action_resource_spec.rb +26 -0
- data/spec/lib/droplet_kit/resources/domain_record_resource_spec.rb +83 -0
- data/spec/lib/droplet_kit/resources/domain_resource_spec.rb +53 -0
- data/spec/lib/droplet_kit/resources/droplet_action_resource_spec.rb +150 -0
- data/spec/lib/droplet_kit/resources/droplet_resource_spec.rb +194 -0
- data/spec/lib/droplet_kit/resources/image_action_resource_spec.rb +45 -0
- data/spec/lib/droplet_kit/resources/image_resource_spec.rb +56 -0
- data/spec/lib/droplet_kit/resources/region_resource_spec.rb +16 -0
- data/spec/lib/droplet_kit/resources/size_resource_spec.rb +16 -0
- data/spec/lib/droplet_kit/resources/ssh_key_resource_spec.rb +80 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/support/fake_server.rb +4 -0
- data/spec/support/null_hash_load.rb +9 -0
- data/spec/support/request_stub_helpers.rb +9 -0
- data/spec/support/resource_context.rb +4 -0
- metadata +309 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class ImageMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
mapping Image
|
7
|
+
root_key plural: 'images', singular: 'image', scopes: [:read]
|
8
|
+
|
9
|
+
property :id, scopes: [:read]
|
10
|
+
property :name, scopes: [:read, :update]
|
11
|
+
property :distribution, scopes: [:read]
|
12
|
+
property :slug, scopes: [:read]
|
13
|
+
property :public, scopes: [:read]
|
14
|
+
property :regions, scopes: [:read]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class KernelMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
mapping Kernel
|
7
|
+
root_key plural: 'kernels', singular: 'kernel', scopes: [:read]
|
8
|
+
|
9
|
+
property :id, scopes: [:read]
|
10
|
+
property :name, scopes: [:read]
|
11
|
+
property :version, scopes: [:read]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class NetworkDetailMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
mapping Network
|
7
|
+
|
8
|
+
property :ip_address, scopes: [:read]
|
9
|
+
property :netmask, scopes: [:read]
|
10
|
+
property :gateway, scopes: [:read]
|
11
|
+
property :type, scopes: [:read]
|
12
|
+
property :cidr, scopes: [:read]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class NetworkMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
mapping NetworkHash
|
7
|
+
|
8
|
+
property :v4, plural: true, scopes: [:read], include: NetworkDetailMapping
|
9
|
+
property :v6, plural: true, scopes: [:read], include: NetworkDetailMapping
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class RegionMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
mapping Region
|
7
|
+
root_key singular: 'region', plural: 'regions', scopes: [:read]
|
8
|
+
|
9
|
+
property :slug, scopes: [:read]
|
10
|
+
property :name, scopes: [:read]
|
11
|
+
property :sizes, scopes: [:read]
|
12
|
+
property :available, scopes: [:read]
|
13
|
+
property :features, scopes: [:read]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class SizeMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
mapping Size
|
7
|
+
root_key singular: 'size', plural: 'sizes', scopes: [:read]
|
8
|
+
|
9
|
+
property :slug, scopes: [:read]
|
10
|
+
property :transfer, scopes: [:read]
|
11
|
+
property :price_monthly, scopes: [:read]
|
12
|
+
property :price_hourly, scopes: [:read]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class SnapshotMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
root_key plural: 'snapshots', singular: 'snapshot', scopes: [:read]
|
7
|
+
mapping Snapshot
|
8
|
+
|
9
|
+
property :id, scopes: [:read]
|
10
|
+
property :name, scopes: [:read]
|
11
|
+
property :distribution, scopes: [:read]
|
12
|
+
property :slug, scopes: [:read]
|
13
|
+
property :public, scopes: [:read]
|
14
|
+
property :regions, scopes: [:read]
|
15
|
+
property :action_ids, scopes: [:read]
|
16
|
+
property :created_at, scopes: [:read]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class SSHKeyMapping
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
kartograph do
|
6
|
+
mapping SSHKey
|
7
|
+
root_key singular: 'ssh_key', plural: 'ssh_keys', scopes: [:read]
|
8
|
+
|
9
|
+
property :id, :fingerprint, :public_key, :name,
|
10
|
+
scopes: :read
|
11
|
+
|
12
|
+
property :name, :public_key, scopes: :create
|
13
|
+
|
14
|
+
property :name, scopes: :update
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
|
3
|
+
module DropletKit
|
4
|
+
class BaseModel
|
5
|
+
include Virtus.model
|
6
|
+
include Virtus::Equalizer.new(name || inspect)
|
7
|
+
|
8
|
+
def inspect
|
9
|
+
values = Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ]
|
10
|
+
"<#{self.class.name} #{values}>"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class Droplet < BaseModel
|
3
|
+
[:id, :name, :memory, :vcpus, :disk, :locked, :created_at,
|
4
|
+
:status, :backup_ids, :snapshot_ids, :action_ids, :features,
|
5
|
+
:region, :image, :size, :networks, :kernel].each do |key|
|
6
|
+
attribute(key)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Used for creates
|
10
|
+
attribute :ssh_keys
|
11
|
+
attribute :backups
|
12
|
+
attribute :ipv6
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class PaginationInformation < BaseModel
|
3
|
+
include Kartograph::DSL
|
4
|
+
|
5
|
+
Pages = Struct.new(:last, :next, :pref, :first)
|
6
|
+
|
7
|
+
kartograph do
|
8
|
+
mapping PaginationInformation
|
9
|
+
root_key singular: 'links', scopes: [:read]
|
10
|
+
|
11
|
+
property :pages do
|
12
|
+
mapping Pages
|
13
|
+
property :first, :prev, :next, :last, scopes: :read
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class PaginatedResource
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
PER_PAGE = 20
|
6
|
+
|
7
|
+
attr_reader :action, :connection, :collection
|
8
|
+
attr_accessor :total
|
9
|
+
|
10
|
+
def initialize(action_connection, *args)
|
11
|
+
@current_page = 0
|
12
|
+
@total = nil
|
13
|
+
@action = action_connection.action
|
14
|
+
@connection = action_connection.connection
|
15
|
+
@collection = []
|
16
|
+
@args = args
|
17
|
+
@options = args.last.kind_of?(Hash) ? args.last : {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def per_page
|
21
|
+
@options[:per_page] || PER_PAGE
|
22
|
+
end
|
23
|
+
|
24
|
+
def each(start = 0)
|
25
|
+
# Start off with the first page if we have no idea of anything yet
|
26
|
+
fetch_next_page if total.nil?
|
27
|
+
|
28
|
+
return to_enum(:each, start) unless block_given?
|
29
|
+
Array(@collection[start..-1]).each do |element|
|
30
|
+
yield(element)
|
31
|
+
end
|
32
|
+
|
33
|
+
unless last?
|
34
|
+
start = [@collection.size, start].max
|
35
|
+
fetch_next_page
|
36
|
+
each(start, &Proc.new)
|
37
|
+
end
|
38
|
+
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def last?
|
43
|
+
@current_page == (self.total.to_f / per_page.to_f).ceil
|
44
|
+
end
|
45
|
+
|
46
|
+
def ==(other)
|
47
|
+
return false if self.total != other.length
|
48
|
+
each_with_index.each.all? {|object, index| object == other[index] }
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def fetch_next_page
|
54
|
+
@current_page += 1
|
55
|
+
retrieve(@current_page)
|
56
|
+
end
|
57
|
+
|
58
|
+
def retrieve(page, per_page = self.per_page)
|
59
|
+
invoker = ResourceKit::ActionInvoker.new(action, connection, *@args)
|
60
|
+
invoker.options[:per_page] ||= per_page
|
61
|
+
invoker.options[:page] = page
|
62
|
+
|
63
|
+
@collection += invoker.handle_response
|
64
|
+
|
65
|
+
if total.nil?
|
66
|
+
meta = MetaInformation.extract_single(invoker.response.body, :read)
|
67
|
+
self.total = meta.total.to_i
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DropletKit
|
2
|
+
class ActionResource < ResourceKit::Resource
|
3
|
+
resources do
|
4
|
+
action :all, 'GET /v2/actions' do
|
5
|
+
handler(200) { |response| ActionMapping.extract_collection(response.body, :read) }
|
6
|
+
end
|
7
|
+
|
8
|
+
action :find, 'GET /v2/actions/:id' do
|
9
|
+
handler(200) { |response| ActionMapping.extract_single(response.body, :read) }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def all(*args)
|
14
|
+
PaginatedResource.new(action_and_connection(:all), *args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|