droplet_kit 1.4.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +15 -0
  4. data/lib/droplet_kit.rb +1 -3
  5. data/lib/droplet_kit/client.rb +2 -1
  6. data/lib/droplet_kit/mappings/image_mapping.rb +9 -7
  7. data/lib/droplet_kit/mappings/snapshot_mapping.rb +4 -4
  8. data/lib/droplet_kit/mappings/volume_mapping.rb +1 -0
  9. data/lib/droplet_kit/models/snapshot.rb +4 -4
  10. data/lib/droplet_kit/models/volume.rb +3 -0
  11. data/lib/droplet_kit/resources/droplet_resource.rb +2 -2
  12. data/lib/droplet_kit/resources/image_action_resource.rb +4 -4
  13. data/lib/droplet_kit/resources/snapshot_resource.rb +24 -0
  14. data/lib/droplet_kit/resources/volume_action_resource.rb +11 -0
  15. data/lib/droplet_kit/resources/volume_resource.rb +15 -0
  16. data/lib/droplet_kit/version.rb +1 -1
  17. data/spec/fixtures/snapshots/all.json +32 -0
  18. data/spec/fixtures/snapshots/find.json +14 -0
  19. data/spec/fixtures/snapshots/resource_type.json +20 -0
  20. data/spec/fixtures/volume_actions/resize.json +34 -0
  21. data/spec/fixtures/volumes/create_snapshot.json +14 -0
  22. data/spec/lib/droplet_kit/client_spec.rb +13 -1
  23. data/spec/lib/droplet_kit/resources/droplet_resource_spec.rb +3 -5
  24. data/spec/lib/droplet_kit/resources/image_action_resource_spec.rb +3 -3
  25. data/spec/lib/droplet_kit/resources/snapshot_resource_spec.rb +61 -0
  26. data/spec/lib/droplet_kit/resources/volume_action_resource_spec.rb +31 -0
  27. data/spec/lib/droplet_kit/resources/volume_resource_spec.rb +45 -0
  28. metadata +15 -6
  29. data/lib/droplet_kit/mappings/backup_mapping.rb +0 -19
  30. data/lib/droplet_kit/mappings/image_action_mapping.rb +0 -20
  31. data/lib/droplet_kit/models/backup.rb +0 -12
  32. data/lib/droplet_kit/models/image_action.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c6c0a5bca047be0aab328d01e7cca4c3abd19c3
4
- data.tar.gz: a7737a18b385672e5e94069d53dad88aed3db638
3
+ metadata.gz: 03dcba690c63223939582769ffe69abcfa1c56ac
4
+ data.tar.gz: ae895f01786428f1643659da6dbc94299e69c284
5
5
  SHA512:
6
- metadata.gz: 3afbe6fef22f3f09d35978771cec474704ce69039c243b303f21f71e126dc3b260f1582d26c21a3142a6a270a74bfd26373e807ab83a7b67a5cd24e2a1ab3b75
7
- data.tar.gz: c913513dbf0adea27a22fb1c97c4d9ec5d543677eec2a2c1b89a212e09bf39d31e9834553e15a8250c7ef1aaa6dacc4bf485d72389c340758b6fdd39014fd1d8
6
+ metadata.gz: da75b47a74c59c5e56a198e487e587f5dab664365893fe9d94fbbcba7157323a9776dabb217d158e5039f7dd93eb1a55bc757a5bd22fd95bc66faebca26f1fb8
7
+ data.tar.gz: ab33f8e593bc48c5c52c40e916dd3266998080fb49dcaf5f55ecd9b99cd74ff4a8404e076bdd79974c88cec43e744f97d25b8ebd081f970adea9477c4ace7690
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ### Version 2.0.0
2
+ * Several duplicate classes have been deprecated and combined:
3
+ * Replace duplicate Backup model/mapping with Image.
4
+ * Remove duplicate ImageAction(Mapping), use Action.
5
+ * Redefine Snapshot model for new Snapshot object, use Image for Droplet snapshots.
6
+ * Added support for Snapshot endpoint with volume snapshots.
7
+
1
8
  ### Version 1.2.2
2
9
 
3
10
  * Add image convert action.
data/README.md CHANGED
@@ -282,8 +282,11 @@ Actions supported:
282
282
  * `client.volumes.all()`
283
283
  * `client.volumes.find(id: 'id')`
284
284
  * `client.volumes.create(volume)`
285
+ * `client.volumes.snapshots(id: 'id')`
286
+ * `client.volumes.create_snapshot(id: 'id', name: 'snapshot-name')`
285
287
  * `client.volumes.delete(id: 'id')`
286
288
 
289
+
287
290
  ## Volume Action resource
288
291
 
289
292
  client = DropletKit::Client.new(access_token: 'TOKEN')
@@ -293,6 +296,18 @@ Actions supported:
293
296
 
294
297
  * `client.volume_actions.attach(volume_id: volume.id, droplet_id: droplet.id, region: droplet.region.slug)`
295
298
  * `client.volume_actions.detach(volume_id: volume.id, droplet_id: droplet.id, region: droplet.region.slug)`
299
+ * `client.volume_actions.resize(volume_id: volume.id, size_gigabytes: 123, region: droplet.region.slug)`
300
+
301
+ ## Volume resource
302
+
303
+ client = DropletKit::Client.new(access_token: 'TOKEN')
304
+ client.snapshots #=> DropletKit::SnapshotResource
305
+
306
+ Actions supported:
307
+
308
+ * `client.snapshots.all(resource_type: 'droplet')`
309
+ * `client.snapshots.find(id: 'id')`
310
+ * `client.snapshots.delete(id: 'id')`
296
311
 
297
312
  ## Contributing
298
313
 
data/lib/droplet_kit.rb CHANGED
@@ -11,13 +11,11 @@ module DropletKit
11
11
  autoload :Droplet, 'droplet_kit/models/droplet'
12
12
  autoload :Region, 'droplet_kit/models/region'
13
13
  autoload :Image, 'droplet_kit/models/image'
14
- autoload :ImageAction, 'droplet_kit/models/image_action'
15
14
  autoload :Size, 'droplet_kit/models/size'
16
15
  autoload :NetworkHash, 'droplet_kit/models/network_hash'
17
16
  autoload :Network, 'droplet_kit/models/network'
18
17
  autoload :Kernel, 'droplet_kit/models/kernel'
19
18
  autoload :Snapshot, 'droplet_kit/models/snapshot'
20
- autoload :Backup, 'droplet_kit/models/backup'
21
19
  autoload :Action, 'droplet_kit/models/action'
22
20
  autoload :Domain, 'droplet_kit/models/domain'
23
21
  autoload :DomainRecord, 'droplet_kit/models/domain_record'
@@ -49,6 +47,7 @@ module DropletKit
49
47
  autoload :TagResource, 'droplet_kit/resources/tag_resource'
50
48
  autoload :VolumeResource, 'droplet_kit/resources/volume_resource'
51
49
  autoload :VolumeActionResource, 'droplet_kit/resources/volume_action_resource'
50
+ autoload :SnapshotResource, 'droplet_kit/resources/snapshot_resource'
52
51
 
53
52
  # JSON Maps
54
53
  autoload :DropletMapping, 'droplet_kit/mappings/droplet_mapping'
@@ -59,7 +58,6 @@ module DropletKit
59
58
  autoload :NetworkDetailMapping, 'droplet_kit/mappings/network_detail_mapping'
60
59
  autoload :KernelMapping, 'droplet_kit/mappings/kernel_mapping'
61
60
  autoload :SnapshotMapping, 'droplet_kit/mappings/snapshot_mapping'
62
- autoload :BackupMapping, 'droplet_kit/mappings/backup_mapping'
63
61
  autoload :ActionMapping, 'droplet_kit/mappings/action_mapping'
64
62
  autoload :DomainMapping, 'droplet_kit/mappings/domain_mapping'
65
63
  autoload :DomainRecordMapping, 'droplet_kit/mappings/domain_record_mapping'
@@ -11,7 +11,7 @@ module DropletKit
11
11
  end
12
12
 
13
13
  def connection
14
- Faraday.new(connection_options) do |req|
14
+ @faraday ||= Faraday.new connection_options do |req|
15
15
  req.adapter :net_http
16
16
  end
17
17
  end
@@ -28,6 +28,7 @@ module DropletKit
28
28
  regions: RegionResource,
29
29
  sizes: SizeResource,
30
30
  ssh_keys: SSHKeyResource,
31
+ snapshots: SnapshotResource,
31
32
  account: AccountResource,
32
33
  floating_ips: FloatingIpResource,
33
34
  floating_ip_actions: FloatingIpActionResource,
@@ -5,14 +5,16 @@ module DropletKit
5
5
  kartograph do
6
6
  mapping Image
7
7
  root_key plural: 'images', singular: 'image', scopes: [:read]
8
+ root_key plural: 'snapshots', singular: 'snapshot', scopes: [:read_snapshot]
9
+ root_key plural: 'backups', singular: 'backup', scopes: [:read_backup]
8
10
 
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
- property :type, scopes: [:read]
11
+ property :id, scopes: [:read, :read_snapshot, :read_backup]
12
+ property :name, scopes: [:read, :update, :read_snapshot, :read_backup]
13
+ property :distribution, scopes: [:read, :read_snapshot, :read_backup]
14
+ property :slug, scopes: [:read, :read_snapshot, :read_backup]
15
+ property :public, scopes: [:read, :read_snapshot, :read_backup]
16
+ property :regions, scopes: [:read, :read_snapshot, :read_backup]
17
+ property :type, scopes: [:read, :read_snapshot, :read_backup]
16
18
  end
17
19
  end
18
20
  end
@@ -8,12 +8,12 @@ module DropletKit
8
8
 
9
9
  property :id, scopes: [:read]
10
10
  property :name, scopes: [:read]
11
- property :distribution, scopes: [:read]
12
- property :slug, scopes: [:read]
13
- property :public, scopes: [:read]
14
11
  property :regions, scopes: [:read]
15
- property :action_ids, scopes: [:read]
16
12
  property :created_at, scopes: [:read]
13
+ property :resource_id, scopes: [:read]
14
+ property :resource_type, scopes: [:read]
15
+ property :min_disk_size, scopes: [:read]
16
+ property :size_gigabytes, scopes: [:read]
17
17
  end
18
18
  end
19
19
  end
@@ -15,6 +15,7 @@ module DropletKit
15
15
  property :created_at, scopes: [:read]
16
16
 
17
17
  property :region, scopes: [:create]
18
+ property :snapshot_id, scopes: [:create]
18
19
  end
19
20
  end
20
21
  end
@@ -2,11 +2,11 @@ module DropletKit
2
2
  class Snapshot < BaseModel
3
3
  attribute :id
4
4
  attribute :name
5
- attribute :distribution
6
- attribute :slug
7
- attribute :public
8
5
  attribute :regions
9
- attribute :action_ids
10
6
  attribute :created_at
7
+ attribute :resource_id
8
+ attribute :resource_type
9
+ attribute :min_disk_size
10
+ attribute :size_gigabytes
11
11
  end
12
12
  end
@@ -7,5 +7,8 @@ module DropletKit
7
7
  attribute :description
8
8
  attribute :size_gigabytes
9
9
  attribute :created_at
10
+
11
+ # Used for creates
12
+ attribute :snapshot_id
10
13
  end
11
14
  end
@@ -35,12 +35,12 @@ module DropletKit
35
35
 
36
36
  action :snapshots, 'GET /v2/droplets/:id/snapshots' do
37
37
  query_keys :per_page, :page
38
- handler(200) { |response| SnapshotMapping.extract_collection(response.body, :read) }
38
+ handler(200) { |response| ImageMapping.extract_collection(response.body, :read_snapshot) }
39
39
  end
40
40
 
41
41
  action :backups, 'GET /v2/droplets/:id/backups' do
42
42
  query_keys :per_page, :page
43
- handler(200) { |response| BackupMapping.extract_collection(response.body, :read) }
43
+ handler(200) { |response| ImageMapping.extract_collection(response.body, :read_backup) }
44
44
  end
45
45
 
46
46
  action :actions, 'GET /v2/droplets/:id/actions' do
@@ -7,21 +7,21 @@ module DropletKit
7
7
 
8
8
  action :transfer, 'POST /v2/images/:image_id/actions' do
9
9
  body { |object| { type: 'transfer', region: object[:region] }.to_json }
10
- handler(200, 201) { |response| ImageActionMapping.extract_single(response.body, :read) }
10
+ handler(200, 201) { |response| ActionMapping.extract_single(response.body, :read) }
11
11
  end
12
12
 
13
13
  action :convert, 'POST /v2/images/:image_id/actions' do
14
14
  body { |object| { type: 'convert' }.to_json }
15
- handler(200, 201) { |response| ImageActionMapping.extract_single(response.body, :read) }
15
+ handler(200, 201) { |response| ActionMapping.extract_single(response.body, :read) }
16
16
  end
17
17
 
18
18
  action :all, 'GET /v2/images/:image_id/actions' do
19
19
  query_keys :per_page, :page
20
- handler(200) { |response| ImageActionMapping.extract_collection(response.body, :read) }
20
+ handler(200) { |response| ActionMapping.extract_collection(response.body, :read) }
21
21
  end
22
22
 
23
23
  action :find, 'GET /v2/images/:image_id/actions/:id' do
24
- handler(200) { |response| ImageActionMapping.extract_single(response.body, :read) }
24
+ handler(200) { |response| ActionMapping.extract_single(response.body, :read) }
25
25
  end
26
26
  end
27
27
 
@@ -0,0 +1,24 @@
1
+ module DropletKit
2
+ class SnapshotResource < ResourceKit::Resource
3
+ include ErrorHandlingResourcable
4
+
5
+ resources do
6
+ action :all, 'GET /v2/snapshots' do
7
+ query_keys :page, :per_page, :resource_type
8
+ handler(200) { |response| SnapshotMapping.extract_collection(response.body, :read) }
9
+ end
10
+
11
+ action :find, 'GET /v2/snapshots/:id' do
12
+ handler(200) { |response| SnapshotMapping.extract_single(response.body, :read) }
13
+ end
14
+
15
+ action :delete, 'DELETE /v2/snapshots/:id' do
16
+ handler(204) { |_| true }
17
+ end
18
+ end
19
+
20
+ def all(*args)
21
+ PaginatedResource.new(action(:all), self, *args)
22
+ end
23
+ end
24
+ end
@@ -27,6 +27,17 @@ module DropletKit
27
27
  handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
28
28
  end
29
29
 
30
+ action :resize, 'POST /v2/volumes/:volume_id/actions' do
31
+ body do |hash|
32
+ {
33
+ type: 'resize',
34
+ size_gigabytes: hash[:size_gigabytes],
35
+ region: hash[:region],
36
+ }.to_json
37
+ end
38
+ handler(201, 200) { |response| ActionMapping.extract_single(response.body, :read) }
39
+ end
40
+
30
41
  action :find, 'GET /v2/volumes/:volume_id/actions/:id' do
31
42
  handler(200) { |response| ActionMapping.extract_single(response.body, :read) }
32
43
  end
@@ -21,10 +21,25 @@ module DropletKit
21
21
  action :delete, 'DELETE /v2/volumes/:id' do
22
22
  handler(204) { |response| true }
23
23
  end
24
+
25
+ action :snapshots, 'GET /v2/volumes/:id/snapshots' do
26
+ query_keys :per_page, :page
27
+ handler(200) { |response| SnapshotMapping.extract_collection(response.body, :read) }
28
+ end
29
+
30
+ action :create_snapshot, 'POST /v2/volumes/:id/snapshots' do
31
+ body { |hash| { name: hash[:name] }.to_json }
32
+ handler(201) { |response| SnapshotMapping.extract_single(response.body, :read) }
33
+ handler(422) { |response| ErrorMapping.fail_with(FailedCreate, response.body) }
34
+ end
24
35
  end
25
36
 
26
37
  def all(*args)
27
38
  PaginatedResource.new(action(:all), self, *args)
28
39
  end
40
+
41
+ def snapshots(*args)
42
+ PaginatedResource.new(action(:snapshots), self, *args)
43
+ end
29
44
  end
30
45
  end
@@ -1,3 +1,3 @@
1
1
  module DropletKit
2
- VERSION = "1.4.3"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -0,0 +1,32 @@
1
+ {
2
+ "snapshots": [
3
+ {
4
+ "id": "119192817",
5
+ "name": "Ubuntu 13.04",
6
+ "regions": [
7
+ "nyc1"
8
+ ],
9
+ "created_at": "2014-07-29T14:35:40Z",
10
+ "resource_type": "droplet",
11
+ "resource_id": "123",
12
+ "resource_type": "droplet",
13
+ "min_disk_size": 10,
14
+ "size_gigabytes": 0.4
15
+ },
16
+ {
17
+ "id": "7724db7c-e098-11e5-b522-000f53304e51",
18
+ "name": "Ubuntu Foo",
19
+ "regions": [
20
+ "nyc1"
21
+ ],
22
+ "created_at": "2014-07-29T14:35:40Z",
23
+ "resource_type": "volume",
24
+ "resource_id": "7724db7c-e098-11e5-b522-000f53304e51",
25
+ "min_disk_size": 10,
26
+ "size_gigabytes": 0.4
27
+ }
28
+ ],
29
+ "meta": {
30
+ "total": 2
31
+ }
32
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "snapshot": {
3
+ "id": "7724db7c-e098-11e5-b522-000f53304e51",
4
+ "name": "Ubuntu Foo",
5
+ "regions": [
6
+ "nyc1"
7
+ ],
8
+ "created_at": "2014-07-29T14:35:40Z",
9
+ "resource_type": "volume",
10
+ "resource_id": "7724db7c-e098-11e5-b522-000f53304e51",
11
+ "min_disk_size": 10,
12
+ "size_gigabytes": 0.4
13
+ }
14
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "snapshots": [
3
+ {
4
+ "id": "119192817",
5
+ "name": "Ubuntu 13.04",
6
+ "regions": [
7
+ "nyc1"
8
+ ],
9
+ "created_at": "2014-07-29T14:35:40Z",
10
+ "resource_type": "droplet",
11
+ "resource_id": "123",
12
+ "resource_type": "droplet",
13
+ "min_disk_size": 10,
14
+ "size_gigabytes": 0.4
15
+ }
16
+ ],
17
+ "meta": {
18
+ "total": 1
19
+ }
20
+ }
@@ -0,0 +1,34 @@
1
+ {
2
+ "action": {
3
+ "id": 72531856,
4
+ "status": "in-progress",
5
+ "type": "resize_volume",
6
+ "started_at": "2015-11-12T17:51:03Z",
7
+ "completed_at": null,
8
+ "resource_id": null,
9
+ "resource_type": "volume",
10
+ "region": {
11
+ "name": "New York 1",
12
+ "slug": "nyc1",
13
+ "sizes": [
14
+ "512mb",
15
+ "1gb",
16
+ "2gb",
17
+ "4gb",
18
+ "8gb",
19
+ "16gb",
20
+ "32gb",
21
+ "48gb",
22
+ "64gb"
23
+ ],
24
+ "features": [
25
+ "private_networking",
26
+ "backups",
27
+ "ipv6",
28
+ "metadata"
29
+ ],
30
+ "available": true
31
+ },
32
+ "region_slug": "nyc1"
33
+ }
34
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "snapshot": {
3
+ "id": "7724db7c-e098-11e5-b522-000f53304e51",
4
+ "name": "Ubuntu Foo",
5
+ "regions": [
6
+ "nyc1"
7
+ ],
8
+ "created_at": "2014-07-29T14:35:40Z",
9
+ "resource_type": "volume",
10
+ "resource_id": "7724db7c-e098-11e5-b522-000f53304e51",
11
+ "min_disk_size": 10,
12
+ "size_gigabytes": 0.4
13
+ }
14
+ }
@@ -26,6 +26,11 @@ RSpec.describe DropletKit::Client do
26
26
  end
27
27
 
28
28
  describe '#connection' do
29
+ module AcmeApp
30
+ class CustomLogger < Faraday::Middleware
31
+ end
32
+ end
33
+
29
34
  it 'populates the authorization header correctly' do
30
35
  expect(client.connection.headers['Authorization']).to eq("Bearer #{client.access_token}")
31
36
  end
@@ -33,5 +38,12 @@ RSpec.describe DropletKit::Client do
33
38
  it 'sets the content type' do
34
39
  expect(client.connection.headers['Content-Type']).to eq("application/json")
35
40
  end
41
+
42
+ it 'allows access to faraday instance' do
43
+ client.connection.use AcmeApp::CustomLogger
44
+ expect(client.connection.builder.handlers).to include(AcmeApp::CustomLogger)
45
+ end
36
46
  end
37
- end
47
+
48
+ end
49
+
@@ -244,18 +244,17 @@ RSpec.describe DropletKit::DropletResource do
244
244
  end
245
245
 
246
246
  describe '#snapshots' do
247
- it 'returns a list of kernels for a droplet' do
247
+ it 'returns a list of image snapshots for a droplet' do
248
248
  stub_do_api('/v2/droplets/1066/snapshots', :get).to_return(body: api_fixture('droplets/list_snapshots'))
249
249
  snapshots = resource.snapshots(id: 1066).take(20)
250
250
 
251
- expect(snapshots).to all(be_kind_of(DropletKit::Snapshot))
251
+ expect(snapshots).to all(be_kind_of(DropletKit::Image))
252
252
  expect(snapshots[0].id).to eq(449676387)
253
253
  expect(snapshots[0].name).to eq("Ubuntu 13.04")
254
254
  expect(snapshots[0].distribution).to eq("ubuntu")
255
255
  expect(snapshots[0].slug).to eq(nil)
256
256
  expect(snapshots[0].public).to eq(false)
257
257
  expect(snapshots[0].regions).to eq(["nyc1"])
258
- expect(snapshots[0].created_at).to eq("2014-07-29T14:35:38Z")
259
258
  end
260
259
 
261
260
  it 'returns a paginated resource' do
@@ -270,14 +269,13 @@ RSpec.describe DropletKit::DropletResource do
270
269
  stub_do_api('/v2/droplets/1066/backups', :get).to_return(body: api_fixture('droplets/list_backups'))
271
270
  backups = resource.backups(id: 1066).take(20)
272
271
 
273
- expect(backups).to all(be_kind_of(DropletKit::Backup))
272
+ expect(backups).to all(be_kind_of(DropletKit::Image))
274
273
  expect(backups[0].id).to eq(449676388)
275
274
  expect(backups[0].name).to eq("Ubuntu 13.04")
276
275
  expect(backups[0].distribution).to eq("ubuntu")
277
276
  expect(backups[0].slug).to eq(nil)
278
277
  expect(backups[0].public).to eq(false)
279
278
  expect(backups[0].regions).to eq(["nyc1"])
280
- expect(backups[0].created_at).to eq("2014-07-29T14:35:38Z")
281
279
  end
282
280
 
283
281
  it 'returns a paginated resource' do
@@ -20,7 +20,7 @@ RSpec.describe DropletKit::ImageActionResource do
20
20
 
21
21
  expect(request).to have_been_made
22
22
 
23
- expect(action).to be_kind_of(DropletKit::ImageAction)
23
+ expect(action).to be_kind_of(DropletKit::Action)
24
24
  expect(action.id).to eq(23)
25
25
  expect(action.status).to eq("in-progress")
26
26
  expect(action.type).to eq("transfer")
@@ -60,7 +60,7 @@ RSpec.describe DropletKit::ImageActionResource do
60
60
 
61
61
  expect(request).to have_been_made
62
62
 
63
- expect(action).to be_kind_of(DropletKit::ImageAction)
63
+ expect(action).to be_kind_of(DropletKit::Action)
64
64
  expect(action.id).to eq(23)
65
65
  expect(action.status).to eq("in-progress")
66
66
  expect(action.type).to eq("convert")
@@ -121,7 +121,7 @@ RSpec.describe DropletKit::ImageActionResource do
121
121
  stub_do_api('/v2/images/449676391/actions/23').to_return(body: api_fixture('image_actions/find'))
122
122
  action = resource.find(image_id: 449676391, id: 23)
123
123
 
124
- expect(action).to be_kind_of(DropletKit::ImageAction)
124
+ expect(action).to be_kind_of(DropletKit::Action)
125
125
  expect(action.id).to eq(23)
126
126
  expect(action.status).to eq("in-progress")
127
127
  expect(action.type).to eq("transfer")
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DropletKit::SnapshotResource do
4
+ subject(:resource) { described_class.new(connection: connection) }
5
+ include_context 'resources'
6
+
7
+ describe '#all' do
8
+ it 'returns all of the snapshots' do
9
+ snapshots_json = api_fixture('snapshots/all')
10
+ stub_do_api('/v2/snapshots', :get).to_return(body: snapshots_json)
11
+ expected_snapshots = DropletKit::SnapshotMapping.extract_collection(snapshots_json, :read)
12
+
13
+ expect(resource.all).to eq(expected_snapshots)
14
+ end
15
+
16
+ it 'returns snapshots of a type' do
17
+ snapshots_json = api_fixture('snapshots/resource_type')
18
+ stub_do_api('/v2/snapshots', :get)
19
+ .with(query: hash_including({ resource_type: 'droplet' }))
20
+ .to_return(body: snapshots_json)
21
+ expected_snapshots = DropletKit::SnapshotMapping.extract_collection(snapshots_json, :read)
22
+
23
+ expect(resource.all(resource_type: :droplet)).to eq(expected_snapshots)
24
+ end
25
+
26
+ it_behaves_like 'a paginated index' do
27
+ let(:fixture_path) { 'snapshots/all' }
28
+ let(:api_path) { '/v2/snapshots' }
29
+ end
30
+ end
31
+
32
+ describe '#find' do
33
+ it 'returns a singular snapshot' do
34
+ stub_do_api('/v2/snapshots/7724db7c-e098-11e5-b522-000f53304e51', :get).to_return(body: api_fixture('snapshots/find'))
35
+ snapshot = resource.find(id: "7724db7c-e098-11e5-b522-000f53304e51")
36
+
37
+ expect(snapshot.id).to eq("7724db7c-e098-11e5-b522-000f53304e51")
38
+ expect(snapshot.name).to eq("Ubuntu Foo")
39
+ expect(snapshot.regions).to eq(["nyc1"])
40
+ expect(snapshot.resource_type).to eq("volume")
41
+ expect(snapshot.resource_id).to eq("7724db7c-e098-11e5-b522-000f53304e51")
42
+ expect(snapshot.min_disk_size).to eq(10)
43
+ expect(snapshot.size_gigabytes).to eq(0.4)
44
+ end
45
+
46
+ it_behaves_like 'resource that handles common errors' do
47
+ let(:path) { '/v2/snapshots/123' }
48
+ let(:method) { :get }
49
+ let(:action) { :find }
50
+ let(:arguments) { { id: 123 } }
51
+ end
52
+ end
53
+
54
+ describe '#delete' do
55
+ it 'deletes an snapshot' do
56
+ request = stub_do_api('/v2/snapshots/146', :delete).to_return(body: '', status: 204)
57
+ resource.delete(id: 146)
58
+ expect(request).to have_been_made
59
+ end
60
+ end
61
+ end
@@ -86,6 +86,37 @@ RSpec.describe DropletKit::VolumeActionResource do
86
86
  end
87
87
  end
88
88
 
89
+ describe '#resize' do
90
+ let(:volume_id) { '7724db7c-e098-11e5-b522-000f53304e51' }
91
+ let(:region) { 'nyc1' }
92
+ let(:path) { "/v2/volumes/#{volume_id}/actions" }
93
+ let(:action) { 'resize' }
94
+ let(:fixture) { api_fixture("volume_actions/#{action}") }
95
+
96
+ it 'sends an resize request for a volume' do
97
+ request = stub_do_api(path).with(
98
+ body: {
99
+ type: action,
100
+ size_gigabytes: 100,
101
+ region: region
102
+ }.to_json
103
+ ).to_return(body: fixture, status: 201)
104
+
105
+ action = resource.resize(
106
+ volume_id: volume_id,
107
+ region: region,
108
+ size_gigabytes: 100
109
+ )
110
+
111
+ expect(request).to have_been_made
112
+ expect(action).to match_volume_action_fixture('resize_volume')
113
+ end
114
+
115
+ it_behaves_like 'an action that handles invalid parameters' do
116
+ let(:arguments) { { volume_id: volume_id } }
117
+ end
118
+ end
119
+
89
120
  describe '#all' do
90
121
  let(:volume_id) { '7724db7c-e098-11e5-b522-000f53304e51' }
91
122
 
@@ -62,12 +62,57 @@ RSpec.describe DropletKit::VolumeResource do
62
62
  end
63
63
  end
64
64
 
65
+ context 'with a snapshot id' do
66
+ it 'returns the created volume' do
67
+ volume = DropletKit::Volume.new(
68
+ size_gigabytes: 10,
69
+ name: "Example",
70
+ description: "Block store for examples",
71
+ snapshot_id: "7724db7c-e098-11e5-b522-000f53304e51"
72
+ )
73
+
74
+ as_string = DropletKit::VolumeMapping.representation_for(:create, volume)
75
+ stub_do_api(path, :post).with(body: as_string).to_return(body: api_fixture('volumes/create'), status: 201)
76
+ created_volume = resource.create(volume)
77
+
78
+ expect(created_volume).to match_volume_fixture
79
+ end
80
+ end
81
+
65
82
  it_behaves_like 'an action that handles invalid parameters' do
66
83
  let(:action) { 'create' }
67
84
  let(:arguments) { DropletKit::Volume.new }
68
85
  end
69
86
  end
70
87
 
88
+ describe '#create_snapshot' do
89
+ let(:id) { '7724db7c-e098-11e5-b522-000f53304e51' }
90
+ let(:path) { "/v2/volumes/#{id}/snapshot" }
91
+
92
+ context 'for a successful create' do
93
+ it 'returns the created volume' do
94
+ as_string = { name: 'foo' }.to_json
95
+ stub_do_api(path, :post).with(body: as_string).to_return(body: api_fixture('volumes/create_snapshot'), status: 201)
96
+ created_snapshot = resource.create_snapshot(id: id, name: 'foo')
97
+
98
+ expect(created_snapshot).to be_kind_of(DropletKit::Snapshot)
99
+
100
+ expect(created_snapshot.id).to eq("7724db7c-e098-11e5-b522-000f53304e51")
101
+ expect(created_snapshot.name).to eq("Ubuntu Foo")
102
+ expect(created_snapshot.regions).to eq(["nyc1"])
103
+ expect(created_snapshot.resource_type).to eq("volume")
104
+ expect(created_snapshot.resource_id).to eq("7724db7c-e098-11e5-b522-000f53304e51")
105
+ expect(created_snapshot.min_disk_size).to eq(10)
106
+ expect(created_snapshot.size_gigabytes).to eq(0.4)
107
+ end
108
+ end
109
+
110
+ it_behaves_like 'an action that handles invalid parameters' do
111
+ let(:action) { 'create_snapshot' }
112
+ let(:arguments) { { id: id } }
113
+ end
114
+ end
115
+
71
116
  describe '#delete' do
72
117
  it 'sends a delete request for the volume' do
73
118
  request = stub_do_api('/v2/volumes/7724db7c-e098-11e5-b522-000f53304e51', :delete)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: droplet_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Ross
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-26 00:00:00.000000000 Z
11
+ date: 2016-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -191,14 +191,12 @@ files:
191
191
  - lib/droplet_kit/error_handling_resourcable.rb
192
192
  - lib/droplet_kit/mappings/account_mapping.rb
193
193
  - lib/droplet_kit/mappings/action_mapping.rb
194
- - lib/droplet_kit/mappings/backup_mapping.rb
195
194
  - lib/droplet_kit/mappings/domain_mapping.rb
196
195
  - lib/droplet_kit/mappings/domain_record_mapping.rb
197
196
  - lib/droplet_kit/mappings/droplet_mapping.rb
198
197
  - lib/droplet_kit/mappings/droplet_upgrade_mapping.rb
199
198
  - lib/droplet_kit/mappings/error_mapping.rb
200
199
  - lib/droplet_kit/mappings/floating_ip_mapping.rb
201
- - lib/droplet_kit/mappings/image_action_mapping.rb
202
200
  - lib/droplet_kit/mappings/image_mapping.rb
203
201
  - lib/droplet_kit/mappings/kernel_mapping.rb
204
202
  - lib/droplet_kit/mappings/network_detail_mapping.rb
@@ -213,7 +211,6 @@ files:
213
211
  - lib/droplet_kit/mappings/volume_mapping.rb
214
212
  - lib/droplet_kit/models/account.rb
215
213
  - lib/droplet_kit/models/action.rb
216
- - lib/droplet_kit/models/backup.rb
217
214
  - lib/droplet_kit/models/base_model.rb
218
215
  - lib/droplet_kit/models/domain.rb
219
216
  - lib/droplet_kit/models/domain_record.rb
@@ -221,7 +218,6 @@ files:
221
218
  - lib/droplet_kit/models/droplet_upgrade.rb
222
219
  - lib/droplet_kit/models/floating_ip.rb
223
220
  - lib/droplet_kit/models/image.rb
224
- - lib/droplet_kit/models/image_action.rb
225
221
  - lib/droplet_kit/models/kernel.rb
226
222
  - lib/droplet_kit/models/meta_information.rb
227
223
  - lib/droplet_kit/models/network.rb
@@ -249,6 +245,7 @@ files:
249
245
  - lib/droplet_kit/resources/image_resource.rb
250
246
  - lib/droplet_kit/resources/region_resource.rb
251
247
  - lib/droplet_kit/resources/size_resource.rb
248
+ - lib/droplet_kit/resources/snapshot_resource.rb
252
249
  - lib/droplet_kit/resources/ssh_key_resource.rb
253
250
  - lib/droplet_kit/resources/tag_resource.rb
254
251
  - lib/droplet_kit/resources/volume_action_resource.rb
@@ -312,6 +309,9 @@ files:
312
309
  - spec/fixtures/images/type.json
313
310
  - spec/fixtures/regions/all.json
314
311
  - spec/fixtures/sizes/all.json
312
+ - spec/fixtures/snapshots/all.json
313
+ - spec/fixtures/snapshots/find.json
314
+ - spec/fixtures/snapshots/resource_type.json
315
315
  - spec/fixtures/ssh_keys/all.json
316
316
  - spec/fixtures/ssh_keys/create.json
317
317
  - spec/fixtures/ssh_keys/find.json
@@ -324,8 +324,10 @@ files:
324
324
  - spec/fixtures/volume_actions/attach.json
325
325
  - spec/fixtures/volume_actions/detach.json
326
326
  - spec/fixtures/volume_actions/find.json
327
+ - spec/fixtures/volume_actions/resize.json
327
328
  - spec/fixtures/volumes/all.json
328
329
  - spec/fixtures/volumes/create.json
330
+ - spec/fixtures/volumes/create_snapshot.json
329
331
  - spec/fixtures/volumes/find.json
330
332
  - spec/lib/droplet_kit/client_spec.rb
331
333
  - spec/lib/droplet_kit/models/base_model_spec.rb
@@ -344,6 +346,7 @@ files:
344
346
  - spec/lib/droplet_kit/resources/image_resource_spec.rb
345
347
  - spec/lib/droplet_kit/resources/region_resource_spec.rb
346
348
  - spec/lib/droplet_kit/resources/size_resource_spec.rb
349
+ - spec/lib/droplet_kit/resources/snapshot_resource_spec.rb
347
350
  - spec/lib/droplet_kit/resources/ssh_key_resource_spec.rb
348
351
  - spec/lib/droplet_kit/resources/tag_resource_spec.rb
349
352
  - spec/lib/droplet_kit/resources/volume_action_resource_spec.rb
@@ -437,6 +440,9 @@ test_files:
437
440
  - spec/fixtures/images/type.json
438
441
  - spec/fixtures/regions/all.json
439
442
  - spec/fixtures/sizes/all.json
443
+ - spec/fixtures/snapshots/all.json
444
+ - spec/fixtures/snapshots/find.json
445
+ - spec/fixtures/snapshots/resource_type.json
440
446
  - spec/fixtures/ssh_keys/all.json
441
447
  - spec/fixtures/ssh_keys/create.json
442
448
  - spec/fixtures/ssh_keys/find.json
@@ -449,8 +455,10 @@ test_files:
449
455
  - spec/fixtures/volume_actions/attach.json
450
456
  - spec/fixtures/volume_actions/detach.json
451
457
  - spec/fixtures/volume_actions/find.json
458
+ - spec/fixtures/volume_actions/resize.json
452
459
  - spec/fixtures/volumes/all.json
453
460
  - spec/fixtures/volumes/create.json
461
+ - spec/fixtures/volumes/create_snapshot.json
454
462
  - spec/fixtures/volumes/find.json
455
463
  - spec/lib/droplet_kit/client_spec.rb
456
464
  - spec/lib/droplet_kit/models/base_model_spec.rb
@@ -469,6 +477,7 @@ test_files:
469
477
  - spec/lib/droplet_kit/resources/image_resource_spec.rb
470
478
  - spec/lib/droplet_kit/resources/region_resource_spec.rb
471
479
  - spec/lib/droplet_kit/resources/size_resource_spec.rb
480
+ - spec/lib/droplet_kit/resources/snapshot_resource_spec.rb
472
481
  - spec/lib/droplet_kit/resources/ssh_key_resource_spec.rb
473
482
  - spec/lib/droplet_kit/resources/tag_resource_spec.rb
474
483
  - spec/lib/droplet_kit/resources/volume_action_resource_spec.rb
@@ -1,19 +0,0 @@
1
- module DropletKit
2
- class BackupMapping
3
- include Kartograph::DSL
4
-
5
- kartograph do
6
- root_key plural: 'backups', singular: 'backup', scopes: [:read]
7
- mapping Backup
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
@@ -1,20 +0,0 @@
1
- module DropletKit
2
- class ImageActionMapping
3
- include Kartograph::DSL
4
-
5
- kartograph do
6
- mapping ImageAction
7
- root_key singular: 'action', plural: 'actions', scopes: [:read]
8
-
9
- property :id, scopes: [:read]
10
- property :status, scopes: [:read]
11
- property :type, scopes: [:read]
12
- property :started_at, scopes: [:read]
13
- property :completed_at, scopes: [:read]
14
- property :resource_id, scopes: [:read]
15
- property :resource_type, scopes: [:read]
16
- property :region, scopes: [:read], include: RegionMapping
17
- property :region_slug, scopes: [:read]
18
- end
19
- end
20
- end
@@ -1,12 +0,0 @@
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
@@ -1,13 +0,0 @@
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
- attribute :region_slug
12
- end
13
- end