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,69 @@
1
+ require 'spec_helper'
2
+ require 'addressable/uri'
3
+
4
+ RequestCounter = Struct.new(:count)
5
+
6
+ RSpec.describe DropletKit::PaginatedResource do
7
+ let(:request_count) { RequestCounter.new(0) }
8
+
9
+ let(:connection) { Faraday.new {|b| b.adapter :test, stubs } }
10
+ let(:stubs) do
11
+ Faraday::Adapter::Test::Stubs.new do |stub|
12
+ stub.get('/droplets') do |env|
13
+ request_count.count += 1
14
+ uri = Addressable::URI.parse(env[:url].to_s)
15
+ page = (uri.query_values['page'] || 1).to_i
16
+ per_page = (uri.query_values['per_page'] || 20).to_i
17
+ range = (0...per_page).map do |num|
18
+ num + ((page - 1) * per_page)
19
+ end
20
+
21
+ [200, {}, { objects: range, meta: { total: 40 } }.to_json ]
22
+ end
23
+ end
24
+ end
25
+ let(:action) { ResourceKit::Action.new(:find, :get, '/droplets') }
26
+ let(:action_connection) { ResourceKit::ActionConnection.new(action, connection) }
27
+
28
+ before do
29
+ action.query_keys :per_page, :page
30
+ action.handler(200) { |r| JSON.load(r.body)['objects'] }
31
+ end
32
+
33
+ describe '#initialize' do
34
+ it 'initializes with a action connection struct' do
35
+ instance = DropletKit::PaginatedResource.new(action_connection)
36
+ expect(instance.action).to be(action)
37
+ expect(instance.connection).to be(connection)
38
+ end
39
+ end
40
+
41
+ describe '#each' do
42
+ subject(:paginated) { DropletKit::PaginatedResource.new(action_connection) }
43
+
44
+ it 'iterates over every object returned from the API' do
45
+ total = 0
46
+ paginated.each do |object|
47
+ total += 1
48
+ end
49
+
50
+ expect(total).to eq(40)
51
+ end
52
+
53
+ it 'called the API twice' do
54
+ expect {|b| paginated.each {|c| c } }.to change { request_count.count }.to(2).from(0)
55
+ end
56
+
57
+ it 'returns the correct objects' do
58
+ expect(paginated.first(3)).to eq([0,1,2])
59
+ end
60
+
61
+ context 'for changing size' do
62
+ subject(:paginated) { DropletKit::PaginatedResource.new(action_connection, per_page: 40) }
63
+
64
+ it 'only calls the API once' do
65
+ expect {|b| paginated.each {|c| c } }.to change { request_count.count }.to(1).from(0)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DropletKit::ActionResource do
4
+ subject(:resource) { described_class.new(connection) }
5
+ include_context 'resources'
6
+
7
+ describe '#all' do
8
+ it 'returns all actions' do
9
+ response = api_fixture('actions/all')
10
+ stub_do_api('/v2/actions', :get).to_return(body: response)
11
+ expected_actions = DropletKit::ActionMapping.extract_collection(response, :read)
12
+
13
+ expect(resource.all).to eq(expected_actions)
14
+ end
15
+ end
16
+
17
+ describe '#find' do
18
+ it 'returns an action' do
19
+ response = api_fixture('actions/find')
20
+ stub_do_api('/v2/actions/123', :get).to_return(body: response)
21
+ expected_action = DropletKit::ActionMapping.extract_single(response, :read)
22
+
23
+ expect(resource.find(id: 123)).to eq(expected_action)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DropletKit::DomainRecordResource do
4
+ subject(:resource) { described_class.new(connection) }
5
+ include_context 'resources'
6
+
7
+ describe '#all' do
8
+ it 'returns all of the domain records for a domain' do
9
+ response = api_fixture('domain_records/all')
10
+ stub_do_api('/v2/domains/example.com/records', :get).to_return(body: response)
11
+
12
+ expected_records = DropletKit::DomainRecordMapping.extract_collection(response, :read)
13
+ returned_records = resource.all(for_domain: 'example.com')
14
+
15
+ expect(returned_records).to all(be_kind_of(DropletKit::DomainRecord))
16
+ expect(returned_records).to eq(expected_records)
17
+ end
18
+ end
19
+
20
+ describe '#create' do
21
+ it 'creates a domain record' do
22
+ response = api_fixture('domain_records/create')
23
+
24
+ domain_record = DropletKit::DomainRecord.new(
25
+ type: 'CNAME',
26
+ name: 'www',
27
+ data: '@'
28
+ )
29
+ as_hash = DropletKit::DomainRecordMapping.representation_for(:create, domain_record, NullHashLoad)
30
+ expect(as_hash[:type]).to eq('CNAME')
31
+ expect(as_hash[:name]).to eq('www')
32
+ expect(as_hash[:data]).to eq('@')
33
+
34
+ as_json = DropletKit::DomainRecordMapping.representation_for(:create, domain_record)
35
+ stub_do_api('/v2/domains/example.com/records', :post).with(body: as_json).to_return(body: response, status: 201)
36
+
37
+ created_domain_record = resource.create(domain_record, for_domain: 'example.com')
38
+ expect(created_domain_record.id).to eq(16)
39
+ expect(created_domain_record.name).to eq('www')
40
+ expect(created_domain_record.type).to eq('CNAME')
41
+ expect(created_domain_record.data).to eq('@')
42
+ end
43
+ end
44
+
45
+ describe '#find' do
46
+ it 'returns a domain record' do
47
+ response = api_fixture('domain_records/find')
48
+ stub_do_api('/v2/domains/example.com/records/12', :get).to_return(body: response)
49
+
50
+ expected_record = DropletKit::DomainRecordMapping.extract_single(response, :read)
51
+ expect(resource.find(id: 12, for_domain: 'example.com')).to eq(expected_record)
52
+ end
53
+ end
54
+
55
+ describe '#delete' do
56
+ it 'deletes the domain record for an id and domain name' do
57
+ request = stub_do_api('/v2/domains/example.com/records/12', :delete)
58
+ resource.delete(for_domain: 'example.com', id: 12)
59
+
60
+ expect(request).to have_been_made
61
+ end
62
+ end
63
+
64
+ describe '#update' do
65
+ it 'updates a record' do
66
+ response = api_fixture('domain_records/update')
67
+
68
+ domain_record = DropletKit::DomainRecord.new(name: 'lol')
69
+ as_hash = DropletKit::DomainRecordMapping.representation_for(:update, domain_record, NullHashLoad)
70
+ expect(as_hash[:name]).to eq('lol')
71
+
72
+ as_json = DropletKit::DomainRecordMapping.representation_for(:update, domain_record)
73
+ request = stub_do_api('/v2/domains/example.com/records/1066', :put).with(body: as_json).to_return(body: response, status: 200)
74
+ updated_domain_record = resource.update(domain_record, for_domain: 'example.com', id: 1066)
75
+
76
+ expect(request).to have_been_made
77
+ expect(updated_domain_record.id).to eq(1066)
78
+ expect(updated_domain_record.name).to eq('lol')
79
+ expect(updated_domain_record.type).to eq('CNAME')
80
+ expect(updated_domain_record.data).to eq('@')
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DropletKit::DomainResource do
4
+ subject(:resource) { described_class.new(connection) }
5
+ include_context 'resources'
6
+
7
+ describe '#all' do
8
+ it 'returns all of the domains' do
9
+ response = api_fixture('domains/all')
10
+ stub_do_api('/v2/domains', :get).to_return(body: response)
11
+ expected_domains = DropletKit::DomainMapping.extract_collection(response, :read)
12
+
13
+ expect(resource.all).to eq(expected_domains)
14
+ end
15
+ end
16
+
17
+ describe '#create' do
18
+ it 'send a POST request to create a domain' do
19
+ response = api_fixture('domains/create')
20
+
21
+ domain = DropletKit::Domain.new(ip_address: '1.1.1.1', name: 'example.com')
22
+ as_hash = DropletKit::DomainMapping.representation_for(:create, domain, NullHashLoad)
23
+ expect(as_hash[:ip_address]).to eq('1.1.1.1')
24
+ expect(as_hash[:name]).to eq('example.com')
25
+
26
+ as_json = DropletKit::DomainMapping.representation_for(:create, domain)
27
+ stub_do_api('/v2/domains', :post).with(body: as_json).to_return(body: response, status: 201)
28
+
29
+ created_domain = resource.create(domain)
30
+ expect(created_domain.name).to eq('example.com')
31
+ expect(created_domain.ttl).to eq(1800)
32
+ expect(created_domain.zone_file).to eq(nil)
33
+ end
34
+ end
35
+
36
+ describe '#find' do
37
+ it 'returns a single domain' do
38
+ response = api_fixture('domains/find')
39
+ stub_do_api('/v2/domains/example.com', :get).to_return(body: response)
40
+ expected_domain = DropletKit::DomainMapping.extract_single(response, :read)
41
+
42
+ expect(resource.find(name: 'example.com')).to eq(expected_domain)
43
+ end
44
+ end
45
+
46
+ describe '#delete' do
47
+ it 'deletes a single domain' do
48
+ request = stub_do_api('/v2/domains/example.com', :delete)
49
+ resource.delete(name: 'example.com')
50
+ expect(request).to have_been_made
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DropletKit::DropletActionResource do
4
+ subject(:resource) { described_class.new(connection) }
5
+ let(:droplet_id) { 1066 }
6
+ def json
7
+ {
8
+ "action" => {
9
+ "id" => 2,
10
+ "status" => "in-progress",
11
+ "type" => action,
12
+ "started_at" => "2014-07-29T14:35:27Z",
13
+ "completed_at" => nil,
14
+ "resource_id" => 12,
15
+ "resource_type" => "droplet",
16
+ "region" => "nyc1"
17
+ }
18
+ }.to_json
19
+ end
20
+
21
+ include_context 'resources'
22
+
23
+ ACTIONS_WITHOUT_INPUT = %w(reboot power_cycle shutdown power_off
24
+ power_on password_reset enable_ipv6 disable_backups enable_private_networking)
25
+
26
+ ACTIONS_WITHOUT_INPUT.each do |action_name|
27
+ describe "Action #{action_name}" do
28
+ let(:action) { action_name }
29
+
30
+ it 'performs the action' do
31
+ request = stub_do_api("/v2/droplets/#{droplet_id}/actions", :post).with(
32
+ body: { type: action_name }.to_json
33
+ ).to_return(body: json, status: 201)
34
+
35
+ returned_action = resource.send(action_name, droplet_id: droplet_id)
36
+
37
+ expect(request).to have_been_made
38
+ expect(returned_action.type).to eq(action_name)
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "Action snapshot" do
44
+ let(:action) { 'snapshot' }
45
+
46
+ it 'performs the action' do
47
+ request = stub_do_api("/v2/droplets/#{droplet_id}/actions", :post).with(
48
+ body: { type: action, name: 'Dwight' }.to_json
49
+ ).to_return(body: json, status: 201)
50
+
51
+ returned_action = resource.send(action, droplet_id: droplet_id, name: 'Dwight')
52
+
53
+ expect(request).to have_been_made
54
+ expect(returned_action.type).to eq(action)
55
+ end
56
+ end
57
+
58
+ describe "Action kernel" do
59
+ let(:action) { 'kernel' }
60
+
61
+ it 'performs the action' do
62
+ request = stub_do_api("/v2/droplets/#{droplet_id}/actions", :post).with(
63
+ body: { type: action, kernel: 1556 }.to_json
64
+ ).to_return(body: json, status: 201)
65
+
66
+ returned_action = resource.send(action, droplet_id: droplet_id, kernel: 1556)
67
+
68
+ expect(request).to have_been_made
69
+ expect(returned_action.type).to eq(action)
70
+ end
71
+ end
72
+
73
+ describe "Action rename" do
74
+ let(:action) { 'rename' }
75
+
76
+ it 'performs the action' do
77
+ request = stub_do_api("/v2/droplets/#{droplet_id}/actions", :post).with(
78
+ body: { type: action, name: 'newname.com' }.to_json
79
+ ).to_return(body: json, status: 201)
80
+
81
+ returned_action = resource.send(action, droplet_id: droplet_id, name: 'newname.com')
82
+
83
+ expect(request).to have_been_made
84
+ expect(returned_action.type).to eq(action)
85
+ end
86
+ end
87
+
88
+ describe "Action rebuild" do
89
+ let(:action) { 'rebuild' }
90
+
91
+ it 'performs the action' do
92
+ request = stub_do_api("/v2/droplets/#{droplet_id}/actions", :post).with(
93
+ body: { type: action, image: 'ubuntu-14-04-x86' }.to_json
94
+ ).to_return(body: json, status: 201)
95
+
96
+ returned_action = resource.send(action, droplet_id: droplet_id, image: 'ubuntu-14-04-x86')
97
+
98
+ expect(request).to have_been_made
99
+ expect(returned_action.type).to eq(action)
100
+ end
101
+ end
102
+
103
+ describe "Action restore" do
104
+ let(:action) { 'restore' }
105
+
106
+ it 'performs the action' do
107
+ request = stub_do_api("/v2/droplets/#{droplet_id}/actions", :post).with(
108
+ body: { type: action, image: 'ubuntu-14-04-x86' }.to_json
109
+ ).to_return(body: json, status: 201)
110
+
111
+ returned_action = resource.send(action, droplet_id: droplet_id, image: 'ubuntu-14-04-x86')
112
+
113
+ expect(request).to have_been_made
114
+ expect(returned_action.type).to eq(action)
115
+ end
116
+ end
117
+
118
+ describe "Action resize" do
119
+ let(:action) { 'resize' }
120
+
121
+ it 'performs the action' do
122
+ request = stub_do_api("/v2/droplets/#{droplet_id}/actions", :post).with(
123
+ body: { type: action, size: '1gb' }.to_json
124
+ ).to_return(body: json, status: 201)
125
+
126
+ returned_action = resource.send(action, droplet_id: droplet_id, size: '1gb')
127
+
128
+ expect(request).to have_been_made
129
+ expect(returned_action.type).to eq(action)
130
+ end
131
+ end
132
+
133
+ describe '#find' do
134
+ it 'returns an action' do
135
+ request = stub_do_api("/v2/droplets/1066/actions/123", :get).to_return(
136
+ body: api_fixture('actions/find')
137
+ )
138
+
139
+ returned_action = resource.find(droplet_id: 1066, id: 123)
140
+ expect(request).to have_been_made
141
+
142
+ expect(returned_action.type).to eq('test')
143
+ expect(returned_action.started_at).to eq("2014-07-29T14:35:27Z")
144
+ expect(returned_action.completed_at).to eq(nil)
145
+ expect(returned_action.resource_id).to eq(nil)
146
+ expect(returned_action.resource_type).to eq("backend")
147
+ expect(returned_action.region).to eq("nyc1")
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,194 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DropletKit::DropletResource do
4
+ subject(:resource) { described_class.new(connection) }
5
+ include_context 'resources'
6
+
7
+ # Theres a lot to check
8
+ def check_droplet(droplet)
9
+ expect(droplet.id).to eq(19)
10
+ expect(droplet.name).to eq('test.example.com')
11
+ expect(droplet.memory).to eq(1024)
12
+ expect(droplet.vcpus).to eq(2)
13
+ expect(droplet.disk).to eq(20)
14
+ expect(droplet.locked).to eq(false)
15
+ expect(droplet.status).to eq('active')
16
+ expect(droplet.created_at).to be_present
17
+ expect(droplet.backup_ids).to include(449676382)
18
+ expect(droplet.snapshot_ids).to include(449676383)
19
+ expect(droplet.action_ids).to be_empty
20
+ expect(droplet.features).to include('ipv6')
21
+
22
+ expect(droplet.region).to be_kind_of(DropletKit::Region)
23
+ expect(droplet.region.slug).to eq('nyc1')
24
+ expect(droplet.region.name).to eq('New York')
25
+ expect(droplet.region.sizes).to include('1024mb', '512mb')
26
+ expect(droplet.region.available).to be(true)
27
+ expect(droplet.region.features).to include("virtio", "private_networking", "backups", "ipv6")
28
+
29
+ expect(droplet.image).to be_kind_of(DropletKit::Image)
30
+ expect(droplet.image.id).to eq(119192817)
31
+ expect(droplet.image.name).to eq("Ubuntu 13.04")
32
+ expect(droplet.image.distribution).to eq("ubuntu")
33
+ expect(droplet.image.slug).to eq("ubuntu1304")
34
+ expect(droplet.image.public).to eq(true)
35
+ expect(droplet.image.regions).to include('nyc1')
36
+
37
+ expect(droplet.size).to be_kind_of(DropletKit::Size)
38
+ expect(droplet.size.slug).to eq("1024mb")
39
+ expect(droplet.size.transfer).to eq(2)
40
+ expect(droplet.size.price_monthly).to eq(10.0)
41
+ expect(droplet.size.price_hourly).to eq(0.01488)
42
+
43
+ expect(droplet.networks).to be_kind_of(DropletKit::NetworkHash)
44
+ v4_network = droplet.networks.v4.first
45
+ expect(v4_network.ip_address).to eq('127.0.0.19')
46
+ expect(v4_network.netmask).to eq("255.255.255.0")
47
+ expect(v4_network.gateway).to eq("127.0.0.20")
48
+ expect(v4_network.type).to eq("public")
49
+
50
+ v6_network = droplet.networks.v6.first
51
+ expect(v6_network.ip_address).to eq('2001::13')
52
+ expect(v6_network.gateway).to eq("2400:6180:0000:00D0:0000:0000:0009:7000")
53
+ expect(v6_network.cidr).to eq(124)
54
+ expect(v6_network.type).to eq("public")
55
+
56
+ expect(droplet.kernel).to be_kind_of(DropletKit::Kernel)
57
+ expect(droplet.kernel.id).to eq(485432985)
58
+ expect(droplet.kernel.name).to eq("DO-recovery-static-fsck")
59
+ expect(droplet.kernel.version).to eq("3.8.0-25-generic")
60
+ end
61
+
62
+ describe '#all' do
63
+ it 'returns all of the droplets' do
64
+ stub_do_api('/v2/droplets', :get).to_return(body: api_fixture('droplets/all'))
65
+ droplets = resource.all
66
+ expect(droplets).to all(be_kind_of(DropletKit::Droplet))
67
+
68
+ check_droplet(droplets.first)
69
+ end
70
+ end
71
+
72
+ describe '#find' do
73
+ it 'returns a singular droplet' do
74
+ stub_do_api('/v2/droplets/20', :get).to_return(body: api_fixture('droplets/find'))
75
+ droplet = resource.find(id: 20)
76
+ expect(droplet).to be_kind_of(DropletKit::Droplet)
77
+ check_droplet(droplet)
78
+ end
79
+ end
80
+
81
+ describe '#create' do
82
+ context 'for a successful create' do
83
+ it 'returns the created droplet' do
84
+ droplet = DropletKit::Droplet.new(
85
+ name: 'test.example.com',
86
+ region: 'nyc1',
87
+ size: '512mb',
88
+ image: 'ubuntu-14-04-x86',
89
+ ssh_keys: [123],
90
+ backups: true,
91
+ ipv6: true
92
+ )
93
+
94
+ as_hash = DropletKit::DropletMapping.representation_for(:create, droplet, NullHashLoad)
95
+ expect(as_hash[:name]).to eq(droplet.name)
96
+ expect(as_hash[:region]).to eq(droplet.region)
97
+ expect(as_hash[:size]).to eq(droplet.size)
98
+ expect(as_hash[:image]).to eq(droplet.image)
99
+ expect(as_hash[:ssh_keys]).to eq(droplet.ssh_keys)
100
+ expect(as_hash[:backups]).to eq(droplet.backups)
101
+ expect(as_hash[:ipv6]).to eq(droplet.ipv6)
102
+
103
+ as_string = DropletKit::DropletMapping.representation_for(:create, droplet)
104
+ stub_do_api('/v2/droplets', :post).with(body: as_string).to_return(body: api_fixture('droplets/create'), status: 202)
105
+ created_droplet = resource.create(droplet)
106
+ check_droplet(created_droplet)
107
+ end
108
+ end
109
+
110
+ context 'for an unsuccessful create' do
111
+ it 'raises a FailedCreate exception with the message attached' do
112
+ response_body = { id: :unprocessable_entity, message: 'Something is not right' }
113
+ stub_do_api('/v2/droplets', :post).to_return(body: response_body.to_json, status: 422)
114
+
115
+ expect { resource.create(DropletKit::Droplet.new) }.to raise_exception(DropletKit::FailedCreate).with_message(response_body[:message])
116
+ end
117
+ end
118
+ end
119
+
120
+ describe '#kernels' do
121
+ it 'returns a list of kernels for a droplet' do
122
+ stub_do_api('/v2/droplets/1066/kernels', :get).to_return(body: api_fixture('droplets/list_kernels'))
123
+ kernels = resource.kernels(id: 1066)
124
+
125
+ expect(kernels).to all(be_kind_of(DropletKit::Kernel))
126
+ expect(kernels[0].id).to eq(61833229)
127
+ expect(kernels[0].name).to eq('Ubuntu 14.04 x32 vmlinuz-3.13.0-24-generic')
128
+ expect(kernels[0].version).to eq('3.13.0-24-generic')
129
+
130
+ expect(kernels[1].id).to eq(485432972)
131
+ expect(kernels[1].name).to eq('Ubuntu 14.04 x64 vmlinuz-3.13.0-24-generic (1221)')
132
+ expect(kernels[1].version).to eq('3.13.0-24-generic')
133
+ end
134
+ end
135
+
136
+ describe '#snapshots' do
137
+ it 'returns a list of kernels for a droplet' do
138
+ stub_do_api('/v2/droplets/1066/snapshots', :get).to_return(body: api_fixture('droplets/list_snapshots'))
139
+ snapshots = resource.snapshots(id: 1066)
140
+
141
+ expect(snapshots).to all(be_kind_of(DropletKit::Snapshot))
142
+ expect(snapshots[0].id).to eq(449676387)
143
+ expect(snapshots[0].name).to eq("Ubuntu 13.04")
144
+ expect(snapshots[0].distribution).to eq("ubuntu")
145
+ expect(snapshots[0].slug).to eq(nil)
146
+ expect(snapshots[0].public).to eq(false)
147
+ expect(snapshots[0].regions).to eq(["nyc1"])
148
+ expect(snapshots[0].created_at).to eq("2014-07-29T14:35:38Z")
149
+ end
150
+ end
151
+
152
+ describe '#backups' do
153
+ it 'returns a list of backups for a droplet' do
154
+ stub_do_api('/v2/droplets/1066/backups', :get).to_return(body: api_fixture('droplets/list_backups'))
155
+ backups = resource.backups(id: 1066)
156
+
157
+ expect(backups).to all(be_kind_of(DropletKit::Backup))
158
+ expect(backups[0].id).to eq(449676388)
159
+ expect(backups[0].name).to eq("Ubuntu 13.04")
160
+ expect(backups[0].distribution).to eq("ubuntu")
161
+ expect(backups[0].slug).to eq(nil)
162
+ expect(backups[0].public).to eq(false)
163
+ expect(backups[0].regions).to eq(["nyc1"])
164
+ expect(backups[0].created_at).to eq("2014-07-29T14:35:38Z")
165
+ end
166
+ end
167
+
168
+ describe '#actions' do
169
+ it 'returns a list of actions for the droplet' do
170
+ stub_do_api('/v2/droplets/1066/actions', :get).to_return(body: api_fixture('droplets/list_actions'))
171
+ actions = resource.actions(id: 1066)
172
+
173
+ expect(actions).to all(be_kind_of(DropletKit::Action))
174
+
175
+ expect(actions[0].id).to eq(19)
176
+ expect(actions[0].status).to eq("in-progress")
177
+ expect(actions[0].type).to eq("create")
178
+ expect(actions[0].started_at).to eq("2014-07-29T14:35:39Z")
179
+ expect(actions[0].completed_at).to eq(nil)
180
+ expect(actions[0].resource_id).to eq(24)
181
+ expect(actions[0].resource_type).to eq("droplet")
182
+ expect(actions[0].region).to eq("nyc1")
183
+ end
184
+ end
185
+
186
+ describe '#delete' do
187
+ it 'sends a delete request for the droplet' do
188
+ request = stub_do_api('/v2/droplets/1066', :delete)
189
+ resource.delete(id: 1066)
190
+
191
+ expect(request).to have_been_made
192
+ end
193
+ end
194
+ end