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,31 @@
1
+ module DropletKit
2
+ class DomainRecordResource < ResourceKit::Resource
3
+ resources do
4
+ action :all, 'GET /v2/domains/:for_domain/records' do
5
+ handler(200) { |response| DomainRecordMapping.extract_collection(response.body, :read) }
6
+ end
7
+
8
+ action :create, 'POST /v2/domains/:for_domain/records' do
9
+ body {|object| DomainRecordMapping.representation_for(:create, object) }
10
+ handler(201) { |response| DomainRecordMapping.extract_single(response.body, :read) }
11
+ end
12
+
13
+ action :find, 'GET /v2/domains/:for_domain/records/:id' do
14
+ handler(200) { |response| DomainRecordMapping.extract_single(response.body, :read) }
15
+ end
16
+
17
+ action :delete, 'DELETE /v2/domains/:for_domain/records/:id' do
18
+ handler(204) { |response| true }
19
+ end
20
+
21
+ action :update, 'PUT /v2/domains/:for_domain/records/:id' do
22
+ body {|object| DomainRecordMapping.representation_for(:update, object) }
23
+ handler(200) { |response| DomainRecordMapping.extract_single(response.body, :read) }
24
+ end
25
+ end
26
+
27
+ def all(*args)
28
+ PaginatedResource.new(action_and_connection(:all), *args)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ module DropletKit
2
+ class DomainResource < ResourceKit::Resource
3
+ resources do
4
+ action :all, 'GET /v2/domains' do
5
+ handler(200) { |response| DomainMapping.extract_collection(response.body, :read) }
6
+ end
7
+
8
+ action :create, 'POST /v2/domains' do
9
+ body { |object| DomainMapping.representation_for(:create, object) }
10
+ handler(201) { |response| DomainMapping.extract_single(response.body, :read) }
11
+ end
12
+
13
+ action :find, 'GET /v2/domains/:name' do
14
+ handler(200) { |response| DomainMapping.extract_single(response.body, :read) }
15
+ end
16
+
17
+ action :delete, 'DELETE /v2/domains/:name' do
18
+ handler(204) { |response| true }
19
+ end
20
+ end
21
+
22
+ def all(*args)
23
+ PaginatedResource.new(action_and_connection(:all), *args)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,49 @@
1
+ module DropletKit
2
+ class DropletActionResource < ResourceKit::Resource
3
+ ACTIONS_WITHOUT_INPUT = %w(reboot power_cycle shutdown power_off
4
+ power_on password_reset enable_ipv6 disable_backups enable_private_networking)
5
+
6
+ resources do
7
+ ACTIONS_WITHOUT_INPUT.each do |action_name|
8
+ action action_name.to_sym, 'POST /v2/droplets/:droplet_id/actions' do
9
+ body { |_| { type: action_name }.to_json }
10
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
11
+ end
12
+ end
13
+
14
+ action :snapshot, 'POST /v2/droplets/:droplet_id/actions' do
15
+ body { |hash| { type: 'snapshot', name: hash[:name] }.to_json }
16
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
17
+ end
18
+
19
+ action :kernel, 'POST /v2/droplets/:droplet_id/actions' do
20
+ body { |hash| { type: 'kernel', kernel: hash[:kernel] }.to_json }
21
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
22
+ end
23
+
24
+ action :rename, 'POST /v2/droplets/:droplet_id/actions' do
25
+ body { |hash| { type: 'rename', name: hash[:name] }.to_json }
26
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
27
+ end
28
+
29
+ action :rebuild, 'POST /v2/droplets/:droplet_id/actions' do
30
+ body { |hash| { type: 'rebuild', image: hash[:image] }.to_json }
31
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
32
+ end
33
+
34
+ action :restore, 'POST /v2/droplets/:droplet_id/actions' do
35
+ body { |hash| { type: 'restore', image: hash[:image] }.to_json }
36
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
37
+ end
38
+
39
+ action :resize, 'POST /v2/droplets/:droplet_id/actions' do
40
+ body { |hash| { type: 'resize', size: hash[:size] }.to_json }
41
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
42
+ end
43
+
44
+ action :find, 'GET /v2/droplets/:droplet_id/actions/:id' do
45
+ handler(200) { |response| ActionMapping.extract_single(response.body, :read) }
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,44 @@
1
+ module DropletKit
2
+ class DropletResource < ResourceKit::Resource
3
+ resources do
4
+ action :all, 'GET /v2/droplets' do
5
+ query_keys :per_page, :page
6
+ handler(200) { |response| DropletMapping.extract_collection(response.body, :read) }
7
+ end
8
+
9
+ action :find, 'GET /v2/droplets/:id' do
10
+ handler(200) { |response| DropletMapping.extract_single(response.body, :read) }
11
+ end
12
+
13
+ action :create, 'POST /v2/droplets' do
14
+ body { |object| DropletMapping.representation_for(:create, object) }
15
+ handler(202) { |response| DropletMapping.extract_single(response.body, :read) }
16
+ handler(422) { |response| ErrorMapping.fail_with(FailedCreate, response.body) }
17
+ end
18
+
19
+ action :delete, 'DELETE /v2/droplets/:id' do
20
+ handler(204) { |response| true }
21
+ end
22
+
23
+ action :kernels, 'GET /v2/droplets/:id/kernels' do
24
+ handler(200) { |response| KernelMapping.extract_collection(response.body, :read) }
25
+ end
26
+
27
+ action :snapshots, 'GET /v2/droplets/:id/snapshots' do
28
+ handler(200) { |response| SnapshotMapping.extract_collection(response.body, :read) }
29
+ end
30
+
31
+ action :backups, 'GET /v2/droplets/:id/backups' do
32
+ handler(200) { |response| BackupMapping.extract_collection(response.body, :read) }
33
+ end
34
+
35
+ action :actions, 'GET /v2/droplets/:id/actions' do
36
+ handler(200) { |response| ActionMapping.extract_collection(response.body, :read) }
37
+ end
38
+ end
39
+
40
+ def all(*args)
41
+ PaginatedResource.new(action_and_connection(:all), *args)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,14 @@
1
+ module DropletKit
2
+ class ImageActionResource < ResourceKit::Resource
3
+ resources do
4
+ action :transfer, 'POST /v2/images/:image_id/actions' do
5
+ body { |object| { type: 'transfer', region: object[:region] }.to_json }
6
+ handler(200, 201) { |response| ImageActionMapping.extract_single(response.body, :read) }
7
+ end
8
+
9
+ action :find, 'GET /v2/images/:image_id/actions/:id' do
10
+ handler(200) { |response| ImageActionMapping.extract_single(response.body, :read) }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ module DropletKit
2
+ class ImageResource < ResourceKit::Resource
3
+ resources do
4
+ action :all, 'GET /v2/images' do
5
+ handler(200) { |response| ImageMapping.extract_collection(response.body, :read) }
6
+ end
7
+
8
+ action :find, 'GET /v2/images/:id' do
9
+ handler(200) { |response| ImageMapping.extract_single(response.body, :read) }
10
+ end
11
+
12
+ action :delete, 'DELETE /v2/images/:id' do
13
+ handler(204) { |response| true }
14
+ end
15
+
16
+ action :update, 'PUT /v2/images/:id' do
17
+ body {|image| ImageMapping.representation_for(:update, image) }
18
+ handler(200) { |response| ImageMapping.extract_single(response.body, :read) }
19
+ end
20
+ end
21
+
22
+ def all(*args)
23
+ PaginatedResource.new(action_and_connection(:all), *args)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module DropletKit
2
+ class RegionResource < ResourceKit::Resource
3
+ resources do
4
+ action :all, 'GET /v2/regions' do
5
+ handler(200) { |r| RegionMapping.extract_collection(r.body, :read) }
6
+ end
7
+ end
8
+
9
+ def all(*args)
10
+ PaginatedResource.new(action_and_connection(:all), *args)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module DropletKit
2
+ class SizeResource < ResourceKit::Resource
3
+ resources do
4
+ action :all, 'GET /v2/sizes' do
5
+ handler(200) { |r| SizeMapping.extract_collection(r.body, :read) }
6
+ end
7
+ end
8
+
9
+ def all(*args)
10
+ PaginatedResource.new(action_and_connection(:all), *args)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ module DropletKit
2
+ class SSHKeyResource < ResourceKit::Resource
3
+ resources do
4
+ default_handler(:ok, :created) {|r| SSHKeyMapping.extract_single(r.body, :read) }
5
+
6
+ action :all, 'GET /v2/account/keys' do
7
+ handler(:ok) { |response| SSHKeyMapping.extract_collection(response.body, :read) }
8
+ end
9
+
10
+ action :create, 'POST /v2/account/keys' do
11
+ body {|object| SSHKeyMapping.representation_for(:create, object) }
12
+ end
13
+
14
+ action :find, 'GET /v2/account/keys/:id'
15
+ action :delete, 'DELETE /v2/account/keys/:id'
16
+
17
+ action :update, 'PUT /v2/account/keys/:id' do
18
+ body {|ssh_key| SSHKeyMapping.representation_for(:update, ssh_key) }
19
+ end
20
+ end
21
+
22
+ def all(*args)
23
+ PaginatedResource.new(action_and_connection(:all), *args)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module DropletKit
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ {
2
+ "actions": [
3
+ {
4
+ "id": 1,
5
+ "status": "in-progress",
6
+ "type": "test",
7
+ "started_at": "2014-07-29T14:35:26Z",
8
+ "completed_at": null,
9
+ "resource_id": null,
10
+ "resource_type": "backend",
11
+ "region": "nyc1"
12
+ }
13
+ ],
14
+ "meta": {
15
+ "total": 1
16
+ }
17
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "action": {
3
+ "id": 2,
4
+ "status": "in-progress",
5
+ "type": "test",
6
+ "started_at": "2014-07-29T14:35:27Z",
7
+ "completed_at": null,
8
+ "resource_id": null,
9
+ "resource_type": "backend",
10
+ "region": "nyc1"
11
+ }
12
+ }
@@ -0,0 +1,52 @@
1
+ {
2
+ "domain_records": [
3
+ {
4
+ "id": 1,
5
+ "type": "A",
6
+ "name": "@",
7
+ "data": "8.8.8.8",
8
+ "priority": null,
9
+ "port": null,
10
+ "weight": null
11
+ },
12
+ {
13
+ "id": 2,
14
+ "type": "NS",
15
+ "name": null,
16
+ "data": "NS1.DIGITALOCEAN.COM.",
17
+ "priority": null,
18
+ "port": null,
19
+ "weight": null
20
+ },
21
+ {
22
+ "id": 3,
23
+ "type": "NS",
24
+ "name": null,
25
+ "data": "NS2.DIGITALOCEAN.COM.",
26
+ "priority": null,
27
+ "port": null,
28
+ "weight": null
29
+ },
30
+ {
31
+ "id": 4,
32
+ "type": "NS",
33
+ "name": null,
34
+ "data": "NS3.DIGITALOCEAN.COM.",
35
+ "priority": null,
36
+ "port": null,
37
+ "weight": null
38
+ },
39
+ {
40
+ "id": 5,
41
+ "type": "CNAME",
42
+ "name": "example",
43
+ "data": "@",
44
+ "priority": null,
45
+ "port": null,
46
+ "weight": null
47
+ }
48
+ ],
49
+ "meta": {
50
+ "total": 5
51
+ }
52
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "domain_record": {
3
+ "id": 16,
4
+ "type": "CNAME",
5
+ "name": "www",
6
+ "data": "@",
7
+ "priority": null,
8
+ "port": null,
9
+ "weight": null
10
+ }
11
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "domain_record": {
3
+ "id": 10,
4
+ "type": "CNAME",
5
+ "name": "example",
6
+ "data": "@",
7
+ "priority": null,
8
+ "port": null,
9
+ "weight": null
10
+ }
11
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "domain_record": {
3
+ "id": 1066,
4
+ "type": "CNAME",
5
+ "name": "lol",
6
+ "data": "@",
7
+ "priority": null,
8
+ "port": null,
9
+ "weight": null
10
+ }
11
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "domains": [
3
+ {
4
+ "name": "example.com",
5
+ "ttl": 1800,
6
+ "zone_file": "Example zone file text..."
7
+ }
8
+ ],
9
+ "meta": {
10
+ "total": 1
11
+ }
12
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "domain": {
3
+ "name": "example.com",
4
+ "ttl": 1800,
5
+ "zone_file": null
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "domain": {
3
+ "name": "example.com",
4
+ "ttl": 1800,
5
+ "zone_file": "Example zone file text..."
6
+ }
7
+ }
@@ -0,0 +1,84 @@
1
+ {
2
+ "droplets": [
3
+ {
4
+ "id": 19,
5
+ "name": "test.example.com",
6
+ "memory": 1024,
7
+ "vcpus": 2,
8
+ "disk": 20,
9
+ "region": {
10
+ "slug": "nyc1",
11
+ "name": "New York",
12
+ "sizes": [
13
+ "1024mb",
14
+ "512mb"
15
+ ],
16
+ "available": true,
17
+ "features": [
18
+ "virtio",
19
+ "private_networking",
20
+ "backups",
21
+ "ipv6"
22
+ ]
23
+ },
24
+ "image": {
25
+ "id": 119192817,
26
+ "name": "Ubuntu 13.04",
27
+ "distribution": "ubuntu",
28
+ "slug": "ubuntu1304",
29
+ "public": true,
30
+ "regions": [
31
+ "nyc1"
32
+ ],
33
+ "created_at": "2014-07-29T14:35:36Z"
34
+ },
35
+ "size": {
36
+ "slug": "1024mb",
37
+ "transfer": 2,
38
+ "price_monthly": 10.0,
39
+ "price_hourly": 0.01488
40
+ },
41
+ "locked": false,
42
+ "status": "active",
43
+ "networks": {
44
+ "v4": [
45
+ {
46
+ "ip_address": "127.0.0.19",
47
+ "netmask": "255.255.255.0",
48
+ "gateway": "127.0.0.20",
49
+ "type": "public"
50
+ }
51
+ ],
52
+ "v6": [
53
+ {
54
+ "ip_address": "2001::13",
55
+ "cidr": 124,
56
+ "gateway": "2400:6180:0000:00D0:0000:0000:0009:7000",
57
+ "type": "public"
58
+ }
59
+ ]
60
+ },
61
+ "kernel": {
62
+ "id": 485432985,
63
+ "name": "DO-recovery-static-fsck",
64
+ "version": "3.8.0-25-generic"
65
+ },
66
+ "created_at": "2014-07-29T14:35:36Z",
67
+ "features": [
68
+ "ipv6"
69
+ ],
70
+ "backup_ids": [
71
+ 449676382
72
+ ],
73
+ "snapshot_ids": [
74
+ 449676383
75
+ ],
76
+ "action_ids": [
77
+
78
+ ]
79
+ }
80
+ ],
81
+ "meta": {
82
+ "total": 1
83
+ }
84
+ }