droplet_kit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +70 -0
  7. data/Rakefile +2 -0
  8. data/droplet_kit.gemspec +33 -0
  9. data/lib/droplet_kit.rb +63 -0
  10. data/lib/droplet_kit/client.rb +58 -0
  11. data/lib/droplet_kit/mappings/action_mapping.rb +19 -0
  12. data/lib/droplet_kit/mappings/backup_mapping.rb +19 -0
  13. data/lib/droplet_kit/mappings/domain_mapping.rb +15 -0
  14. data/lib/droplet_kit/mappings/domain_record_mapping.rb +18 -0
  15. data/lib/droplet_kit/mappings/droplet_mapping.rb +38 -0
  16. data/lib/droplet_kit/mappings/error_mapping.rb +19 -0
  17. data/lib/droplet_kit/mappings/image_action_mapping.rb +19 -0
  18. data/lib/droplet_kit/mappings/image_mapping.rb +17 -0
  19. data/lib/droplet_kit/mappings/kernel_mapping.rb +14 -0
  20. data/lib/droplet_kit/mappings/network_detail_mapping.rb +15 -0
  21. data/lib/droplet_kit/mappings/network_mapping.rb +12 -0
  22. data/lib/droplet_kit/mappings/region_mapping.rb +16 -0
  23. data/lib/droplet_kit/mappings/size_mapping.rb +15 -0
  24. data/lib/droplet_kit/mappings/snapshot_mapping.rb +19 -0
  25. data/lib/droplet_kit/mappings/ssh_key_mapping.rb +17 -0
  26. data/lib/droplet_kit/models/action.rb +12 -0
  27. data/lib/droplet_kit/models/backup.rb +12 -0
  28. data/lib/droplet_kit/models/base_model.rb +13 -0
  29. data/lib/droplet_kit/models/domain.rb +10 -0
  30. data/lib/droplet_kit/models/domain_record.rb +11 -0
  31. data/lib/droplet_kit/models/droplet.rb +15 -0
  32. data/lib/droplet_kit/models/image.rb +10 -0
  33. data/lib/droplet_kit/models/image_action.rb +12 -0
  34. data/lib/droplet_kit/models/kernel.rb +7 -0
  35. data/lib/droplet_kit/models/meta_information.rb +14 -0
  36. data/lib/droplet_kit/models/network.rb +4 -0
  37. data/lib/droplet_kit/models/network_hash.rb +4 -0
  38. data/lib/droplet_kit/models/pagination_information.rb +17 -0
  39. data/lib/droplet_kit/models/region.rb +9 -0
  40. data/lib/droplet_kit/models/size.rb +8 -0
  41. data/lib/droplet_kit/models/snapshot.rb +12 -0
  42. data/lib/droplet_kit/models/ssh_key.rb +8 -0
  43. data/lib/droplet_kit/paginated_resource.rb +71 -0
  44. data/lib/droplet_kit/resources/action_resource.rb +17 -0
  45. data/lib/droplet_kit/resources/domain_record_resource.rb +31 -0
  46. data/lib/droplet_kit/resources/domain_resource.rb +26 -0
  47. data/lib/droplet_kit/resources/droplet_action_resource.rb +49 -0
  48. data/lib/droplet_kit/resources/droplet_resource.rb +44 -0
  49. data/lib/droplet_kit/resources/image_action_resource.rb +14 -0
  50. data/lib/droplet_kit/resources/image_resource.rb +26 -0
  51. data/lib/droplet_kit/resources/region_resource.rb +13 -0
  52. data/lib/droplet_kit/resources/size_resource.rb +13 -0
  53. data/lib/droplet_kit/resources/ssh_key_resource.rb +26 -0
  54. data/lib/droplet_kit/version.rb +3 -0
  55. data/spec/fixtures/actions/all.json +17 -0
  56. data/spec/fixtures/actions/find.json +12 -0
  57. data/spec/fixtures/domain_records/all.json +52 -0
  58. data/spec/fixtures/domain_records/create.json +11 -0
  59. data/spec/fixtures/domain_records/find.json +11 -0
  60. data/spec/fixtures/domain_records/update.json +11 -0
  61. data/spec/fixtures/domains/all.json +12 -0
  62. data/spec/fixtures/domains/create.json +7 -0
  63. data/spec/fixtures/domains/find.json +7 -0
  64. data/spec/fixtures/droplets/all.json +84 -0
  65. data/spec/fixtures/droplets/create.json +79 -0
  66. data/spec/fixtures/droplets/find.json +79 -0
  67. data/spec/fixtures/droplets/list_actions.json +17 -0
  68. data/spec/fixtures/droplets/list_backups.json +18 -0
  69. data/spec/fixtures/droplets/list_kernels.json +17 -0
  70. data/spec/fixtures/droplets/list_snapshots.json +18 -0
  71. data/spec/fixtures/image_actions/create.json +12 -0
  72. data/spec/fixtures/image_actions/find.json +12 -0
  73. data/spec/fixtures/images/all.json +29 -0
  74. data/spec/fixtures/images/find.json +13 -0
  75. data/spec/fixtures/regions/all.json +47 -0
  76. data/spec/fixtures/sizes/all.json +35 -0
  77. data/spec/fixtures/ssh_keys/all.json +13 -0
  78. data/spec/fixtures/ssh_keys/create.json +8 -0
  79. data/spec/fixtures/ssh_keys/find.json +8 -0
  80. data/spec/fixtures/ssh_keys/update.json +8 -0
  81. data/spec/lib/droplet_kit/client_spec.rb +22 -0
  82. data/spec/lib/droplet_kit/paginated_resource_spec.rb +69 -0
  83. data/spec/lib/droplet_kit/resources/action_resource_spec.rb +26 -0
  84. data/spec/lib/droplet_kit/resources/domain_record_resource_spec.rb +83 -0
  85. data/spec/lib/droplet_kit/resources/domain_resource_spec.rb +53 -0
  86. data/spec/lib/droplet_kit/resources/droplet_action_resource_spec.rb +150 -0
  87. data/spec/lib/droplet_kit/resources/droplet_resource_spec.rb +194 -0
  88. data/spec/lib/droplet_kit/resources/image_action_resource_spec.rb +45 -0
  89. data/spec/lib/droplet_kit/resources/image_resource_spec.rb +56 -0
  90. data/spec/lib/droplet_kit/resources/region_resource_spec.rb +16 -0
  91. data/spec/lib/droplet_kit/resources/size_resource_spec.rb +16 -0
  92. data/spec/lib/droplet_kit/resources/ssh_key_resource_spec.rb +80 -0
  93. data/spec/spec_helper.rb +11 -0
  94. data/spec/support/fake_server.rb +4 -0
  95. data/spec/support/null_hash_load.rb +9 -0
  96. data/spec/support/request_stub_helpers.rb +9 -0
  97. data/spec/support/resource_context.rb +4 -0
  98. 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,12 @@
1
+ module DropletKit
2
+ class Action < BaseModel
3
+ attribute :id
4
+ attribute :status
5
+ attribute :type
6
+ attribute :started_at
7
+ attribute :completed_at
8
+ attribute :resource_id
9
+ attribute :resource_type
10
+ attribute :region
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module DropletKit
2
+ class Backup < BaseModel
3
+ attribute :id
4
+ attribute :name
5
+ attribute :distribution
6
+ attribute :slug
7
+ attribute :public
8
+ attribute :regions
9
+ attribute :action_ids
10
+ attribute :created_at
11
+ end
12
+ 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,10 @@
1
+ module DropletKit
2
+ class Domain < BaseModel
3
+ attribute :name
4
+ attribute :ttl
5
+ attribute :zone_file
6
+
7
+ # Used for creates
8
+ attribute :ip_address
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module DropletKit
2
+ class DomainRecord < BaseModel
3
+ attribute :id
4
+ attribute :type
5
+ attribute :name
6
+ attribute :data
7
+ attribute :priority
8
+ attribute :port
9
+ attribute :weight
10
+ end
11
+ 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,10 @@
1
+ module DropletKit
2
+ class Image < BaseModel
3
+ attribute :id
4
+ attribute :name
5
+ attribute :distribution
6
+ attribute :slug
7
+ attribute :public
8
+ attribute :regions
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module DropletKit
2
+ class ImageAction < BaseModel
3
+ attribute :id
4
+ attribute :status
5
+ attribute :type
6
+ attribute :started_at
7
+ attribute :completed_at
8
+ attribute :resource_id
9
+ attribute :resource_type
10
+ attribute :region
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module DropletKit
2
+ class Kernel < BaseModel
3
+ attribute :id
4
+ attribute :name
5
+ attribute :version
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ module DropletKit
2
+ class MetaInformation < BaseModel
3
+ include Kartograph::DSL
4
+
5
+ kartograph do
6
+ mapping MetaInformation
7
+ root_key singular: 'meta', scopes: [:read]
8
+
9
+ property :total, scopes: [:read]
10
+ end
11
+
12
+ attribute :total
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module DropletKit
2
+ class Network < Struct.new(:ip_address, :netmask, :gateway, :type, :cidr)
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module DropletKit
2
+ class NetworkHash < Struct.new(:v4, :v6)
3
+ end
4
+ end
@@ -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,9 @@
1
+ module DropletKit
2
+ class Region < BaseModel
3
+ attribute :slug
4
+ attribute :name
5
+ attribute :sizes
6
+ attribute :available
7
+ attribute :features
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module DropletKit
2
+ class Size < BaseModel
3
+ attribute :slug
4
+ attribute :transfer
5
+ attribute :price_monthly
6
+ attribute :price_hourly
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module DropletKit
2
+ class Snapshot < BaseModel
3
+ attribute :id
4
+ attribute :name
5
+ attribute :distribution
6
+ attribute :slug
7
+ attribute :public
8
+ attribute :regions
9
+ attribute :action_ids
10
+ attribute :created_at
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module DropletKit
2
+ class SSHKey < BaseModel
3
+ attribute :id
4
+ attribute :fingerprint
5
+ attribute :public_key
6
+ attribute :name
7
+ end
8
+ 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