do_snapshot 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -7
  3. data/lib/do_snapshot/adapter.rb +21 -0
  4. data/lib/do_snapshot/adapter/abstract.rb +44 -0
  5. data/lib/do_snapshot/adapter/digitalocean.rb +133 -0
  6. data/lib/do_snapshot/adapter/digitalocean_v2.rb +145 -0
  7. data/lib/do_snapshot/cli.rb +22 -12
  8. data/lib/do_snapshot/command.rb +7 -8
  9. data/lib/do_snapshot/version.rb +1 -1
  10. data/spec/do_snapshot/adapter/abstract_spec.rb +29 -0
  11. data/spec/do_snapshot/{api_spec.rb → adapter/digitalocean_spec.rb} +4 -3
  12. data/spec/do_snapshot/adapter/digitalocean_v2_spec.rb +223 -0
  13. data/spec/do_snapshot/cli_spec.rb +2 -1
  14. data/spec/do_snapshot/command_spec.rb +5 -8
  15. data/spec/fixtures/{error_message.json → digitalocean/v1/error_message.json} +0 -0
  16. data/spec/fixtures/{response_event.json → digitalocean/v1/response_event.json} +0 -0
  17. data/spec/fixtures/{show_droplet.json → digitalocean/v1/show_droplet.json} +0 -0
  18. data/spec/fixtures/{show_droplet_inactive.json → digitalocean/v1/show_droplet_inactive.json} +0 -0
  19. data/spec/fixtures/{show_droplets.json → digitalocean/v1/show_droplets.json} +0 -0
  20. data/spec/fixtures/{show_droplets_empty.json → digitalocean/v1/show_droplets_empty.json} +0 -0
  21. data/spec/fixtures/{show_event_done.json → digitalocean/v1/show_event_done.json} +0 -0
  22. data/spec/fixtures/{show_event_start.json → digitalocean/v1/show_event_start.json} +0 -0
  23. data/spec/fixtures/digitalocean/v2/empty.json +0 -0
  24. data/spec/fixtures/digitalocean/v2/error_message.json +4 -0
  25. data/spec/fixtures/digitalocean/v2/response_event.json +29 -0
  26. data/spec/fixtures/digitalocean/v2/show_droplet.json +102 -0
  27. data/spec/fixtures/digitalocean/v2/show_droplet_inactive.json +102 -0
  28. data/spec/fixtures/digitalocean/v2/show_droplets.json +284 -0
  29. data/spec/fixtures/digitalocean/v2/show_droplets_empty.json +5 -0
  30. data/spec/fixtures/digitalocean/v2/show_event_done.json +29 -0
  31. data/spec/fixtures/digitalocean/v2/show_event_power_off_done.json +29 -0
  32. data/spec/fixtures/digitalocean/v2/show_event_power_off_start.json +29 -0
  33. data/spec/fixtures/digitalocean/v2/show_event_power_on_done.json +29 -0
  34. data/spec/fixtures/digitalocean/v2/show_event_power_on_start.json +29 -0
  35. data/spec/fixtures/digitalocean/v2/show_event_start.json +29 -0
  36. data/spec/shared/api_helpers.rb +5 -80
  37. data/spec/shared/api_v1_helpers.rb +97 -0
  38. data/spec/shared/api_v2_helpers.rb +152 -0
  39. data/spec/shared/environment.rb +5 -14
  40. data/spec/shared/uri_helpers.rb +1 -0
  41. data/spec/spec_helper.rb +5 -3
  42. metadata +74 -23
  43. data/lib/do_snapshot/api.rb +0 -144
@@ -0,0 +1,5 @@
1
+ {
2
+ "droplets": [],
3
+ "links": {},
4
+ "meta": {"total": 0}
5
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "action": {
3
+ "id": 7499,
4
+ "status": "completed",
5
+ "type": "snapshot",
6
+ "started_at": "2014-11-14T16:32:24Z",
7
+ "completed_at": "2014-11-14T16:34:15Z",
8
+ "resource_id": 100823,
9
+ "resource_type": "droplet",
10
+ "region": {
11
+ "name": "Amsterdam 1",
12
+ "slug": "ams1",
13
+ "sizes": [
14
+ "512mb",
15
+ "1gb",
16
+ "4gb",
17
+ "2gb",
18
+ "8gb",
19
+ "16gb"
20
+ ],
21
+ "features": [
22
+ "virtio",
23
+ "backups"
24
+ ],
25
+ "available": true
26
+ },
27
+ "region_slug": "ams1"
28
+ }
29
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "action": {
3
+ "id": 7499,
4
+ "status": "completed",
5
+ "type": "power_off",
6
+ "started_at": "2015-05-03T01:00:12Z",
7
+ "completed_at": "2015-05-03T01:35:51Z",
8
+ "resource_id": 100823,
9
+ "resource_type": "droplet",
10
+ "region": {
11
+ "name": "Amsterdam 1",
12
+ "slug": "ams1",
13
+ "sizes": [
14
+ "512mb",
15
+ "1gb",
16
+ "4gb",
17
+ "2gb",
18
+ "8gb",
19
+ "16gb"
20
+ ],
21
+ "features": [
22
+ "virtio",
23
+ "backups"
24
+ ],
25
+ "available": true
26
+ },
27
+ "region_slug": "ams1"
28
+ }
29
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "action": {
3
+ "id": 7499,
4
+ "status": "in-progress",
5
+ "type": "power_off",
6
+ "started_at": "2015-05-03T01:00:12Z",
7
+ "completed_at": "2015-05-03T01:35:51Z",
8
+ "resource_id": 100823,
9
+ "resource_type": "droplet",
10
+ "region": {
11
+ "name": "Amsterdam 1",
12
+ "slug": "ams1",
13
+ "sizes": [
14
+ "512mb",
15
+ "1gb",
16
+ "4gb",
17
+ "2gb",
18
+ "8gb",
19
+ "16gb"
20
+ ],
21
+ "features": [
22
+ "virtio",
23
+ "backups"
24
+ ],
25
+ "available": true
26
+ },
27
+ "region_slug": "ams1"
28
+ }
29
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "action": {
3
+ "id": 7499,
4
+ "status": "completed",
5
+ "type": "power_on",
6
+ "started_at": "2015-05-03T01:00:12Z",
7
+ "completed_at": "2015-05-03T01:35:51Z",
8
+ "resource_id": 100823,
9
+ "resource_type": "droplet",
10
+ "region": {
11
+ "name": "Amsterdam 1",
12
+ "slug": "ams1",
13
+ "sizes": [
14
+ "512mb",
15
+ "1gb",
16
+ "4gb",
17
+ "2gb",
18
+ "8gb",
19
+ "16gb"
20
+ ],
21
+ "features": [
22
+ "virtio",
23
+ "backups"
24
+ ],
25
+ "available": true
26
+ },
27
+ "region_slug": "ams1"
28
+ }
29
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "action": {
3
+ "id": 7499,
4
+ "status": "in-progress",
5
+ "type": "power_on",
6
+ "started_at": "2015-05-03T01:00:12Z",
7
+ "completed_at": "2015-05-03T01:35:51Z",
8
+ "resource_id": 100823,
9
+ "resource_type": "droplet",
10
+ "region": {
11
+ "name": "Amsterdam 1",
12
+ "slug": "ams1",
13
+ "sizes": [
14
+ "512mb",
15
+ "1gb",
16
+ "4gb",
17
+ "2gb",
18
+ "8gb",
19
+ "16gb"
20
+ ],
21
+ "features": [
22
+ "virtio",
23
+ "backups"
24
+ ],
25
+ "available": true
26
+ },
27
+ "region_slug": "ams1"
28
+ }
29
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "action": {
3
+ "id": 7499,
4
+ "status": "in-progress",
5
+ "type": "snapshot",
6
+ "started_at": "2014-11-14T16:32:24Z",
7
+ "completed_at": "2014-11-14T16:34:15Z",
8
+ "resource_id": 100823,
9
+ "resource_type": "droplet",
10
+ "region": {
11
+ "name": "Amsterdam 1",
12
+ "slug": "ams1",
13
+ "sizes": [
14
+ "512mb",
15
+ "1gb",
16
+ "4gb",
17
+ "2gb",
18
+ "8gb",
19
+ "16gb"
20
+ ],
21
+ "features": [
22
+ "virtio",
23
+ "backups"
24
+ ],
25
+ "available": true
26
+ },
27
+ "region_slug": "ams1"
28
+ }
29
+ }
@@ -2,86 +2,6 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  shared_context 'api_helpers' do
5
- # List of droplets
6
- #
7
- def stub_droplets
8
- stub_without_id(droplets_uri, 'show_droplets')
9
- end
10
-
11
- def stub_droplets_empty
12
- stub_without_id(droplets_uri, 'show_droplets_empty')
13
- end
14
-
15
- def stub_droplets_fail
16
- stub_without_id(droplets_uri, 'error_message')
17
- end
18
-
19
- # Droplet data
20
- #
21
- def stub_droplet(id)
22
- stub_with_id(droplet_find_uri, id, 'show_droplet')
23
- end
24
-
25
- def stub_droplet_fail(id)
26
- stub_with_id(droplet_find_uri, id, 'error_message')
27
- end
28
-
29
- def stub_droplet_inactive(id)
30
- stub_with_id(droplet_find_uri, id, 'show_droplet_inactive')
31
- end
32
-
33
- # Droplet actions
34
- #
35
- def stub_droplet_stop(id)
36
- stub_with_id(droplet_stop_uri, id, 'response_event')
37
- end
38
-
39
- def stub_droplet_stop_fail(id)
40
- stub_with_id(droplet_stop_uri, id, 'error_message')
41
- end
42
-
43
- def stub_droplet_start(id)
44
- stub_with_id(droplet_start_uri, id, 'response_event')
45
- end
46
-
47
- def stub_droplet_start_fail(id)
48
- stub_with_id(droplet_start_uri, id, 'error_message')
49
- end
50
-
51
- # Snapshot
52
- #
53
- def stub_droplet_snapshot(id, name)
54
- stub_with_id_name(snapshot_uri, id, name, 'response_event')
55
- end
56
-
57
- def stub_droplet_snapshot_fail(id, name)
58
- stub_with_id_name(snapshot_uri, id, name, 'error_message')
59
- end
60
-
61
- # Event status
62
- #
63
- def stub_event_done(id)
64
- stub_with_id(event_find_uri, id, 'show_event_done')
65
- end
66
-
67
- def stub_event_fail(id)
68
- stub_with_id(event_find_uri, id, 'error_message')
69
- end
70
-
71
- def stub_event_running(id)
72
- stub_with_id(event_find_uri, id, 'show_event_running')
73
- end
74
-
75
- # Image actions
76
- #
77
- def stub_image_destroy(id)
78
- stub_with_id(image_destroy_uri, id, 'response_event')
79
- end
80
-
81
- def stub_image_destroy_fail(id)
82
- stub_with_id(image_destroy_uri, id, 'error_message')
83
- end
84
-
85
5
  # Stub helpers
86
6
  #
87
7
  def stub_with_id(request, id, fixture, status = 200)
@@ -109,6 +29,11 @@ shared_context 'api_helpers' do
109
29
  request.sub('[id]', id.to_s)
110
30
  end
111
31
 
32
+ def url_with_event_id(request, droplet_id, id)
33
+ return unless request && id && droplet_id
34
+ request.sub('[id]', id.to_s).sub('[droplet_id]', droplet_id.to_s)
35
+ end
36
+
112
37
  def url_with_id_name(request, id, name)
113
38
  return unless request && id && name
114
39
  request.sub('[id]', id.to_s).sub('[name]', name)
@@ -0,0 +1,97 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ shared_context 'api_v1_helpers' do
5
+ let(:api_base) { 'https://api.digitalocean.com/v1' }
6
+ let(:keys_uri) { "api_key=#{api_key}&client_id=#{client_key}" }
7
+ let(:droplets_api_base) { "#{api_base}/droplets" }
8
+ let(:events_api_base) { "#{api_base}/events" }
9
+ let(:images_api_base) { "#{api_base}/images" }
10
+ let(:image_destroy_uri) { "#{images_api_base}/[id]/destroy/?#{keys_uri}" }
11
+ let(:droplets_uri) { "#{droplets_api_base}/?#{keys_uri}" }
12
+ let(:droplet_find_uri) { "#{droplets_api_base}/[id]?#{keys_uri}" }
13
+ let(:droplet_stop_uri) { "#{droplets_api_base}/[id]/power_off/?#{keys_uri}" }
14
+ let(:droplet_start_uri) { "#{droplets_api_base}/[id]/power_on/?#{keys_uri}" }
15
+ let(:snapshot_uri) { "#{droplets_api_base}/[id]/snapshot/?name=[name]&#{keys_uri}" }
16
+ let(:event_find_uri) { "#{events_api_base}/[id]/?#{keys_uri}" }
17
+
18
+ # List of droplets
19
+ #
20
+ def stub_droplets
21
+ stub_without_id(droplets_uri, 'v1/show_droplets')
22
+ end
23
+
24
+ def stub_droplets_empty
25
+ stub_without_id(droplets_uri, 'v1/show_droplets_empty')
26
+ end
27
+
28
+ def stub_droplets_fail
29
+ stub_without_id(droplets_uri, 'v1/error_message')
30
+ end
31
+
32
+ # Droplet data
33
+ #
34
+ def stub_droplet(id)
35
+ stub_with_id(droplet_find_uri, id, 'v1/show_droplet')
36
+ end
37
+
38
+ def stub_droplet_fail(id)
39
+ stub_with_id(droplet_find_uri, id, 'v1/error_message')
40
+ end
41
+
42
+ def stub_droplet_inactive(id)
43
+ stub_with_id(droplet_find_uri, id, 'v1/show_droplet_inactive')
44
+ end
45
+
46
+ # Droplet actions
47
+ #
48
+ def stub_droplet_stop(id)
49
+ stub_with_id(droplet_stop_uri, id, 'v1/response_event')
50
+ end
51
+
52
+ def stub_droplet_stop_fail(id)
53
+ stub_with_id(droplet_stop_uri, id, 'v1/error_message')
54
+ end
55
+
56
+ def stub_droplet_start(id)
57
+ stub_with_id(droplet_start_uri, id, 'v1/response_event')
58
+ end
59
+
60
+ def stub_droplet_start_fail(id)
61
+ stub_with_id(droplet_start_uri, id, 'v1/error_message')
62
+ end
63
+
64
+ # Snapshot
65
+ #
66
+ def stub_droplet_snapshot(id, name)
67
+ stub_with_id_name(snapshot_uri, id, name, 'v1/response_event')
68
+ end
69
+
70
+ def stub_droplet_snapshot_fail(id, name)
71
+ stub_with_id_name(snapshot_uri, id, name, 'v1/error_message')
72
+ end
73
+
74
+ # Event status
75
+ #
76
+ def stub_event_done(id)
77
+ stub_with_id(event_find_uri, id, 'v1/show_event_done')
78
+ end
79
+
80
+ def stub_event_fail(id)
81
+ stub_with_id(event_find_uri, id, 'v1/error_message')
82
+ end
83
+
84
+ def stub_event_running(id)
85
+ stub_with_id(event_find_uri, id, 'v1/show_event_running')
86
+ end
87
+
88
+ # Image actions
89
+ #
90
+ def stub_image_destroy(id)
91
+ stub_with_id(image_destroy_uri, id, 'v1/response_event')
92
+ end
93
+
94
+ def stub_image_destroy_fail(id)
95
+ stub_with_id(image_destroy_uri, id, 'v1/error_message')
96
+ end
97
+ end
@@ -0,0 +1,152 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ shared_context 'api_v2_helpers' do
5
+ let(:api_base) { 'https://api.digitalocean.com/v2' }
6
+ let(:droplets_api_base) { "#{api_base}/droplets" }
7
+ let(:events_api_base) { "#{api_base}/droplets/[droplet_id]/actions" }
8
+ let(:actions_api_base) { "#{api_base}/actions" }
9
+ let(:images_api_base) { "#{api_base}/images" }
10
+ let(:image_destroy_uri) { "#{images_api_base}/[id]" }
11
+ let(:droplets_uri) { "#{droplets_api_base}?page=1&per_page=20" }
12
+ let(:droplet_find_uri) { "#{droplets_api_base}/[id]" }
13
+ let(:droplet_stop_uri) { "#{droplets_api_base}/[id]/actions" }
14
+ let(:droplet_start_uri) { "#{droplets_api_base}/[id]/actions" }
15
+ let(:snapshot_uri) { "#{droplets_api_base}/[id]/actions" }
16
+ let(:event_find_uri) { "#{events_api_base}/[id]" }
17
+ let(:action_find_uri) { "#{actions_api_base}/[id]" }
18
+
19
+ # List of droplets
20
+ #
21
+ def stub_droplets
22
+ stub_without_id(droplets_uri, 'v2/show_droplets')
23
+ end
24
+
25
+ def stub_droplets_empty
26
+ stub_without_id(droplets_uri, 'v2/show_droplets_empty')
27
+ end
28
+
29
+ def stub_droplets_fail
30
+ stub_without_id(droplets_uri, 'v2/error_message')
31
+ end
32
+
33
+ # Droplet data
34
+ #
35
+ def stub_droplet(id)
36
+ stub_with_id(droplet_find_uri, id, 'v2/show_droplet')
37
+ end
38
+
39
+ def stub_droplet_fail(id)
40
+ stub_with_id(droplet_find_uri, id, 'v2/error_message')
41
+ end
42
+
43
+ def stub_droplet_inactive(id)
44
+ stub_with_id(droplet_find_uri, id, 'v2/show_droplet_inactive')
45
+ end
46
+
47
+ # Droplet actions
48
+ #
49
+ def stub_droplet_stop(id)
50
+ stub_with_id(droplet_stop_uri, id, 'v2/show_event_power_off_start', :post,
51
+ type: 'power_off'
52
+
53
+ )
54
+ end
55
+
56
+ def stub_droplet_stop_fail(id)
57
+ stub_with_id(droplet_stop_uri, id, 'v2/error_message', :post,
58
+ {
59
+ type: 'power_off'
60
+ },
61
+ 404
62
+ )
63
+ end
64
+
65
+ def stub_droplet_start(id)
66
+ stub_with_id(droplet_start_uri, id, 'v2/show_event_power_on_start', :post,
67
+ type: 'power_on'
68
+
69
+ )
70
+ end
71
+
72
+ def stub_droplet_start_fail(id)
73
+ stub_with_id(droplet_start_uri, id, 'v2/error_message', :post,
74
+ {
75
+ type: 'power_on'
76
+ },
77
+ 404
78
+ )
79
+ end
80
+
81
+ # Snapshot
82
+ #
83
+ def stub_droplet_snapshot(id, name)
84
+ stub_with_id_name(snapshot_uri, id, name, 'v2/response_event', :post,
85
+ type: 'snapshot',
86
+ name: name
87
+
88
+ )
89
+ end
90
+
91
+ def stub_droplet_snapshot_fail(id, name)
92
+ stub_with_id_name(snapshot_uri, id, name, 'v2/error_message', :post,
93
+ {
94
+ type: 'snapshot',
95
+ name: name
96
+ },
97
+ 404
98
+ )
99
+ end
100
+
101
+ # Event status
102
+ #
103
+ def stub_event_done(id)
104
+ stub_with_id(action_find_uri, id, 'v2/show_event_done')
105
+ end
106
+
107
+ def stub_event_fail(id)
108
+ stub_with_id(action_find_uri, id, 'v2/error_message')
109
+ end
110
+
111
+ def stub_event_running(id)
112
+ stub_with_id(action_find_uri, id, 'v2/show_event_start')
113
+ end
114
+
115
+ # Image actions
116
+ #
117
+ def stub_image_destroy(id)
118
+ stub_with_id(image_destroy_uri, id, 'v2/empty', :delete, nil, 204)
119
+ end
120
+
121
+ def stub_image_destroy_fail(id)
122
+ stub_with_id(image_destroy_uri, id, 'v2/error_message', :delete, nil, 404)
123
+ end
124
+
125
+ # Stub helpers
126
+ #
127
+ def stub_with_id(request, id, fixture, type = :get, body = nil, status = 200) # rubocop:disable Metrics/ParameterLists
128
+ return unless request && fixture && id
129
+ stub_request_body(type, url_with_id(request, id), body)
130
+ .to_return(status: status, body: fixture(fixture))
131
+ end
132
+
133
+ def stub_without_id(request, fixture, type = :get, body = nil, status = 200)
134
+ return unless request && fixture
135
+ stub_request_body(type, request, body)
136
+ .to_return(status: status, body: fixture(fixture))
137
+ end
138
+
139
+ def stub_with_id_name(request, id, name, fixture, type = :get, body = nil, status = 200) # rubocop:disable Metrics/ParameterLists
140
+ return unless request && fixture && id && name
141
+ stub_request_body(type, url_with_id_name(request, id, name), body)
142
+ .to_return(status: status, body: fixture(fixture))
143
+ end
144
+
145
+ # Body Helpers
146
+ #
147
+ def stub_request_body(type, request, body)
148
+ stub_response = stub_request(type, request)
149
+ return stub_response.with(body: body) if body
150
+ stub_response
151
+ end
152
+ end