bbrowning-deltacloud-client 0.0.6.1 → 0.0.9.4
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.
- data/bin/deltacloudc +6 -7
- data/lib/deltacloud.rb +54 -29
- data/lib/documentation.rb +4 -3
- data/lib/plain_formatter.rb +1 -1
- data/specs/hardware_profiles_spec.rb +9 -3
- metadata +46 -44
data/bin/deltacloudc
CHANGED
@@ -20,8 +20,8 @@
|
|
20
20
|
require 'rubygems'
|
21
21
|
require 'optparse'
|
22
22
|
require 'uri'
|
23
|
-
require '
|
24
|
-
require '
|
23
|
+
require 'deltacloud'
|
24
|
+
require 'plain_formatter'
|
25
25
|
|
26
26
|
include DeltaCloud::PlainFormatter
|
27
27
|
|
@@ -80,8 +80,7 @@ collections.delete(:instance_states)
|
|
80
80
|
# with API documentation
|
81
81
|
if options[:list] and options[:collection].nil?
|
82
82
|
collections.each do |c|
|
83
|
-
|
84
|
-
puts sprintf("%-22s: %s", c.to_s[0, 22], doc[:description])
|
83
|
+
puts sprintf("%-22s", c.to_s[0, 22])
|
85
84
|
end
|
86
85
|
exit(0)
|
87
86
|
end
|
@@ -89,9 +88,9 @@ end
|
|
89
88
|
# If collection parameter is present and user requested list
|
90
89
|
# print all operation defined for collection with API documentation
|
91
90
|
if options[:list] and options[:collection]
|
92
|
-
doc = client.
|
93
|
-
doc
|
94
|
-
puts sprintf("%-20s: %s", c
|
91
|
+
doc = client.documentation(options[:collection])
|
92
|
+
doc.operations.each do |c|
|
93
|
+
puts sprintf("%-20s: %s", c.operation, c.description)
|
95
94
|
end
|
96
95
|
exit(0)
|
97
96
|
end
|
data/lib/deltacloud.rb
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
# License for the specific language governing permissions and limitations
|
17
17
|
# under the License.
|
18
18
|
|
19
|
-
require '
|
19
|
+
require 'nokogiri'
|
20
20
|
require 'rest_client'
|
21
21
|
require 'base64'
|
22
22
|
require 'logger'
|
@@ -116,7 +116,7 @@ module DeltaCloud
|
|
116
116
|
|
117
117
|
def base_object_collection(c, model, response)
|
118
118
|
collection = []
|
119
|
-
|
119
|
+
Nokogiri::XML(response).xpath("#{model}/#{model.to_s.singularize}").each do |item|
|
120
120
|
c.instance_eval do
|
121
121
|
attr_accessor :id
|
122
122
|
attr_accessor :uri
|
@@ -129,7 +129,7 @@ module DeltaCloud
|
|
129
129
|
# Add default attributes [id and href] to class
|
130
130
|
def base_object(c, model, response)
|
131
131
|
obj = nil
|
132
|
-
|
132
|
+
Nokogiri::XML(response).xpath("#{model.to_s.singularize}").each do |item|
|
133
133
|
c.instance_eval do
|
134
134
|
attr_accessor :id
|
135
135
|
attr_accessor :uri
|
@@ -155,7 +155,7 @@ module DeltaCloud
|
|
155
155
|
logger << "[DC] Creating class #{obj.class.name}\n"
|
156
156
|
obj.instance_eval do
|
157
157
|
# Declare methods for all attributes in object
|
158
|
-
item.
|
158
|
+
item.xpath('./*').each do |attribute|
|
159
159
|
# If attribute is a link to another object then
|
160
160
|
# create a method which request this object from API
|
161
161
|
if api.entry_points.keys.include?(:"#{attribute.name}s")
|
@@ -174,7 +174,7 @@ module DeltaCloud
|
|
174
174
|
# to dynamicaly create .stop!, .start! methods
|
175
175
|
when "actions":
|
176
176
|
actions = []
|
177
|
-
attribute.
|
177
|
+
attribute.xpath('link').each do |link|
|
178
178
|
actions << [link['rel'], link[:href]]
|
179
179
|
define_method :"#{link['rel'].sanitize}!" do
|
180
180
|
client.request(:"#{link['method']}", link['href'], {}, {})
|
@@ -191,18 +191,19 @@ module DeltaCloud
|
|
191
191
|
end
|
192
192
|
# Property attribute is handled differently
|
193
193
|
when "property":
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
194
|
+
attr_accessor :"#{attribute['name'].sanitize}"
|
195
|
+
if attribute['value'] =~ /^(\d+)$/
|
196
|
+
obj.send(:"#{attribute['name'].sanitize}=",
|
197
|
+
DeltaCloud::HWP::FloatProperty.new(attribute, attribute['name']))
|
198
|
+
else
|
199
|
+
obj.send(:"#{attribute['name'].sanitize}=",
|
200
|
+
DeltaCloud::HWP::Property.new(attribute, attribute['name']))
|
200
201
|
end
|
201
202
|
# Public and private addresses are returned as Array
|
202
203
|
when "public_addresses", "private_addresses":
|
203
204
|
attr_accessor :"#{attribute.name.sanitize}"
|
204
205
|
obj.send(:"#{attribute.name.sanitize}=",
|
205
|
-
attribute.
|
206
|
+
attribute.xpath('address').collect { |address| address.text })
|
206
207
|
# Value for other attributes are just returned using
|
207
208
|
# method with same name as attribute (eg. .owner_id, .state)
|
208
209
|
else
|
@@ -221,16 +222,16 @@ module DeltaCloud
|
|
221
222
|
def discover_entry_points
|
222
223
|
return if discovered?
|
223
224
|
request(:get, @api_uri.to_s) do |response|
|
224
|
-
api_xml =
|
225
|
-
@driver_name = api_xml.
|
226
|
-
@api_version = api_xml.
|
225
|
+
api_xml = Nokogiri::XML(response)
|
226
|
+
@driver_name = api_xml.xpath('/api').first['driver']
|
227
|
+
@api_version = api_xml.xpath('/api').first['version']
|
227
228
|
logger << "[API] Version #{@api_version}\n"
|
228
229
|
logger << "[API] Driver #{@driver_name}\n"
|
229
|
-
api_xml.
|
230
|
+
api_xml.css("api > link").each do |entry_point|
|
230
231
|
rel, href = entry_point['rel'].to_sym, entry_point['href']
|
231
232
|
@entry_points.store(rel, href)
|
232
233
|
logger << "[API] Entry point '#{rel}' added\n"
|
233
|
-
entry_point.
|
234
|
+
entry_point.css("feature").each do |feature|
|
234
235
|
@features[rel] ||= []
|
235
236
|
@features[rel] << feature['name'].to_sym
|
236
237
|
logger << "[API] Feature #{feature['name']} added to #{rel}\n"
|
@@ -239,6 +240,17 @@ module DeltaCloud
|
|
239
240
|
end
|
240
241
|
declare_entry_points_methods(@entry_points)
|
241
242
|
end
|
243
|
+
|
244
|
+
def create_instance_credential(opts={}, &block)
|
245
|
+
params = { :name => opts[:name] }
|
246
|
+
instance_credential = nil
|
247
|
+
request(:post, entry_points[:instance_credentials], {}, params) do |response|
|
248
|
+
c = DeltaCloud.define_class("InstanceCredential")
|
249
|
+
instance_credential = base_object(c, :instance_credential, response)
|
250
|
+
yield instance_credential if block_given?
|
251
|
+
end
|
252
|
+
return instance_credential
|
253
|
+
end
|
242
254
|
|
243
255
|
# Create a new instance, using image +image_id+. Possible optiosn are
|
244
256
|
#
|
@@ -254,11 +266,13 @@ module DeltaCloud
|
|
254
266
|
name = opts[:name]
|
255
267
|
realm_id = opts[:realm]
|
256
268
|
user_data = opts[:user_data]
|
269
|
+
key_name = opts[:key_name]
|
257
270
|
|
258
271
|
params = opts.dup
|
259
272
|
( params[:realm_id] = realm_id ) if realm_id
|
260
273
|
( params[:name] = name ) if name
|
261
274
|
( params[:user_data] = user_data ) if user_data
|
275
|
+
( params[:keyname] = user_data ) if key_name
|
262
276
|
|
263
277
|
if opts[:hardware_profile].is_a?(String)
|
264
278
|
params[:hwp_id] = opts[:hardware_profile]
|
@@ -315,16 +329,16 @@ module DeltaCloud
|
|
315
329
|
|
316
330
|
# Check if specified collection have wanted feature
|
317
331
|
def feature?(collection, name)
|
318
|
-
@
|
332
|
+
@features.has_key?(collection) && @features[collection].include?(name)
|
319
333
|
end
|
320
334
|
|
321
335
|
# List available instance states and transitions between them
|
322
336
|
def instance_states
|
323
337
|
states = []
|
324
338
|
request(:get, entry_points[:instance_states]) do |response|
|
325
|
-
|
339
|
+
Nokogiri::XML(response).xpath('states/state').each do |state_el|
|
326
340
|
state = DeltaCloud::InstanceState::State.new(state_el['name'])
|
327
|
-
state_el.
|
341
|
+
state_el.xpath('transition').each do |transition_el|
|
328
342
|
state.transitions << DeltaCloud::InstanceState::Transition.new(
|
329
343
|
transition_el['to'],
|
330
344
|
transition_el['action']
|
@@ -349,9 +363,10 @@ module DeltaCloud
|
|
349
363
|
def documentation(collection, operation=nil)
|
350
364
|
data = {}
|
351
365
|
request(:get, "/docs/#{collection}") do |body|
|
352
|
-
document =
|
366
|
+
document = Nokogiri::XML(body)
|
353
367
|
if operation
|
354
|
-
data[:
|
368
|
+
data[:operation] = operation
|
369
|
+
data[:description] = document.xpath('/docs/collection/operations/operation[@name = "'+operation+'"]/description').first.text.strip
|
355
370
|
return false unless data[:description]
|
356
371
|
data[:params] = []
|
357
372
|
(document/"/docs/collection/operations/operation[@name='#{operation}']/parameter").each do |param|
|
@@ -363,9 +378,11 @@ module DeltaCloud
|
|
363
378
|
end
|
364
379
|
else
|
365
380
|
data[:description] = (document/'/docs/collection/description').text
|
381
|
+
data[:collection] = collection
|
382
|
+
data[:operations] = (document/"/docs/collection/operations/operation").collect{ |o| o['name'] }
|
366
383
|
end
|
367
384
|
end
|
368
|
-
return Documentation.new(data)
|
385
|
+
return Documentation.new(self, data)
|
369
386
|
end
|
370
387
|
|
371
388
|
private
|
@@ -384,15 +401,23 @@ module DeltaCloud
|
|
384
401
|
end
|
385
402
|
|
386
403
|
class Documentation
|
387
|
-
attr_reader :description
|
388
|
-
attr_reader :params
|
389
404
|
|
390
|
-
|
391
|
-
|
405
|
+
attr_reader :api, :description, :params, :collection_operations
|
406
|
+
attr_reader :collection, :operation
|
407
|
+
|
408
|
+
def initialize(api, opts={})
|
409
|
+
@description, @api = opts[:description], api
|
392
410
|
@params = parse_parameters(opts[:params]) if opts[:params]
|
411
|
+
@collection_operations = opts[:operations] if opts[:operations]
|
412
|
+
@collection = opts[:collection]
|
413
|
+
@operation = opts[:operation]
|
393
414
|
self
|
394
415
|
end
|
395
416
|
|
417
|
+
def operations
|
418
|
+
@collection_operations.collect { |o| api.documentation(@collection, o) }
|
419
|
+
end
|
420
|
+
|
396
421
|
class OperationParameter
|
397
422
|
attr_reader :name
|
398
423
|
attr_reader :type
|
@@ -466,12 +491,12 @@ module DeltaCloud
|
|
466
491
|
self.class.instance_eval do
|
467
492
|
attr_reader :range
|
468
493
|
end
|
469
|
-
@range = { :from => xml.
|
494
|
+
@range = { :from => xml.xpath('range').first['first'], :to => xml.xpath('range').first['last'] }
|
470
495
|
when 'enum':
|
471
496
|
self.class.instance_eval do
|
472
497
|
attr_reader :options
|
473
498
|
end
|
474
|
-
@options = xml.
|
499
|
+
@options = xml.xpath('enum/entry').collect { |e| e['value'] }
|
475
500
|
end
|
476
501
|
end
|
477
502
|
|
data/lib/documentation.rb
CHANGED
@@ -12,7 +12,8 @@ end
|
|
12
12
|
@dc.entry_points.keys.each do |ep|
|
13
13
|
@dc.send(ep)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
|
+
class_list = DeltaCloud::classes.collect { |c| DeltaCloud::module_eval("::DeltaCloud::API::#{c}")}
|
16
17
|
|
17
18
|
def read_method_description(c, method)
|
18
19
|
if method =~ /es$/
|
@@ -57,13 +58,13 @@ class_list.each do |c|
|
|
57
58
|
@dc.entry_points.keys.each do |ep|
|
58
59
|
out << "# Return #{ep.to_s.classify} object with given id\n"
|
59
60
|
out << "# "
|
60
|
-
out << "#
|
61
|
+
out << "# #{@dc.documentation(ep.to_s).description.split("\n").join("\n# ")}"
|
61
62
|
out << "# @return [#{ep.to_s.classify}]"
|
62
63
|
out << "def #{ep.to_s.gsub(/s$/, '')}"
|
63
64
|
out << "end"
|
64
65
|
out << "# Return collection of #{ep.to_s.classify} objects"
|
65
66
|
out << "# "
|
66
|
-
out << "#
|
67
|
+
out << "# #{@dc.documentation(ep.to_s).description.split("\n").join("\n# ")}"
|
67
68
|
@dc.documentation(ep.to_s, 'index').params.each do |p|
|
68
69
|
out << p.to_comment
|
69
70
|
end
|
data/lib/plain_formatter.rb
CHANGED
@@ -77,7 +77,7 @@ module DeltaCloud
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def format(obj)
|
80
|
-
object_name = obj.class.name.classify.gsub(/^DeltaCloud::/, '')
|
80
|
+
object_name = obj.class.name.classify.gsub(/^DeltaCloud::API::/, '')
|
81
81
|
format_class = DeltaCloud::PlainFormatter::FormatObject.const_get(object_name)
|
82
82
|
format_class.new(obj).format
|
83
83
|
end
|
@@ -37,9 +37,7 @@ describe "hardware_profiles" do
|
|
37
37
|
hardware_profiles.each do |hwp|
|
38
38
|
hwp.uri.should_not be_nil
|
39
39
|
hwp.uri.should be_a(String)
|
40
|
-
prop_check(hwp.architecture, String)
|
41
|
-
prop_check(hwp.storage, Float)
|
42
|
-
prop_check(hwp.memory, Float)
|
40
|
+
prop_check(hwp.architecture, String) if hwp.architecture
|
43
41
|
end
|
44
42
|
end
|
45
43
|
end
|
@@ -61,6 +59,14 @@ describe "hardware_profiles" do
|
|
61
59
|
end
|
62
60
|
end
|
63
61
|
|
62
|
+
it "should allow fetching different hardware_profiles" do
|
63
|
+
client = DeltaCloud.new( API_NAME, API_PASSWORD, API_URL )
|
64
|
+
hwp1 = client.hardware_profile( 'm1-small' )
|
65
|
+
hwp2 = client.hardware_profile( 'm1-xlarge' )
|
66
|
+
hwp1.storage.value.should_not eql(hwp2.storage.value)
|
67
|
+
hwp1.memory.value.should_not eql(hwp2.memory.value)
|
68
|
+
end
|
69
|
+
|
64
70
|
it "should allow fetching a hardware_profile by URI" do
|
65
71
|
DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
|
66
72
|
hwp = client.fetch_hardware_profile( API_URL + '/hardware_profiles/m1-small' )
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bbrowning-deltacloud-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 99
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
-
|
11
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
- 4
|
11
|
+
version: 0.0.9.4
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Red Hat, Inc.
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-
|
19
|
+
date: 2010-08-23 00:00:00 -04:00
|
20
20
|
default_executable: deltacloudc
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -36,19 +36,21 @@ dependencies:
|
|
36
36
|
type: :runtime
|
37
37
|
version_requirements: *id001
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
|
-
name:
|
39
|
+
name: nokogiri
|
40
40
|
prerelease: false
|
41
41
|
requirement: &id002 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
hash:
|
46
|
+
hash: 62196439
|
47
47
|
segments:
|
48
|
+
- 1
|
49
|
+
- 5
|
48
50
|
- 0
|
49
|
-
-
|
51
|
+
- beta
|
50
52
|
- 2
|
51
|
-
version: 0.
|
53
|
+
version: 1.5.0.beta.2
|
52
54
|
type: :runtime
|
53
55
|
version_requirements: *id002
|
54
56
|
- !ruby/object:Gem::Dependency
|
@@ -77,34 +79,34 @@ extra_rdoc_files:
|
|
77
79
|
- COPYING
|
78
80
|
files:
|
79
81
|
- Rakefile
|
82
|
+
- lib/deltacloud.rb
|
80
83
|
- lib/documentation.rb
|
81
84
|
- lib/plain_formatter.rb
|
82
|
-
- lib/deltacloud.rb
|
83
85
|
- init.rb
|
84
86
|
- bin/deltacloudc
|
85
87
|
- COPYING
|
86
|
-
- specs/
|
87
|
-
- specs/
|
88
|
-
- specs/
|
89
|
-
- specs/
|
90
|
-
- specs/spec_helper.rb
|
91
|
-
- specs/instance_states_spec.rb
|
92
|
-
- specs/hardware_profiles_spec.rb
|
93
|
-
- specs/storage_volume_spec.rb
|
88
|
+
- specs/fixtures/images/img1.yml
|
89
|
+
- specs/fixtures/images/img2.yml
|
90
|
+
- specs/fixtures/images/img3.yml
|
91
|
+
- specs/fixtures/instances/inst0.yml
|
94
92
|
- specs/fixtures/instances/inst1.yml
|
95
93
|
- specs/fixtures/instances/inst2.yml
|
96
|
-
- specs/fixtures/instances/inst0.yml
|
97
|
-
- specs/fixtures/storage_volumes/vol1.yml
|
98
|
-
- specs/fixtures/storage_volumes/vol3.yml
|
99
|
-
- specs/fixtures/storage_volumes/vol2.yml
|
100
|
-
- specs/fixtures/storage_snapshots/snap3.yml
|
101
94
|
- specs/fixtures/storage_snapshots/snap1.yml
|
102
95
|
- specs/fixtures/storage_snapshots/snap2.yml
|
103
|
-
- specs/fixtures/
|
104
|
-
- specs/fixtures/
|
105
|
-
- specs/fixtures/
|
106
|
-
- specs/
|
96
|
+
- specs/fixtures/storage_snapshots/snap3.yml
|
97
|
+
- specs/fixtures/storage_volumes/vol1.yml
|
98
|
+
- specs/fixtures/storage_volumes/vol2.yml
|
99
|
+
- specs/fixtures/storage_volumes/vol3.yml
|
100
|
+
- specs/hardware_profiles_spec.rb
|
101
|
+
- specs/images_spec.rb
|
107
102
|
- specs/initialization_spec.rb
|
103
|
+
- specs/instance_states_spec.rb
|
104
|
+
- specs/instances_spec.rb
|
105
|
+
- specs/realms_spec.rb
|
106
|
+
- specs/shared/resources.rb
|
107
|
+
- specs/spec_helper.rb
|
108
|
+
- specs/storage_snapshot_spec.rb
|
109
|
+
- specs/storage_volume_spec.rb
|
108
110
|
has_rdoc: true
|
109
111
|
homepage: http://www.deltacloud.org
|
110
112
|
licenses: []
|
@@ -140,25 +142,25 @@ signing_key:
|
|
140
142
|
specification_version: 3
|
141
143
|
summary: Deltacloud REST Client
|
142
144
|
test_files:
|
143
|
-
- specs/
|
144
|
-
- specs/
|
145
|
-
- specs/
|
146
|
-
- specs/
|
147
|
-
- specs/spec_helper.rb
|
148
|
-
- specs/instance_states_spec.rb
|
149
|
-
- specs/hardware_profiles_spec.rb
|
150
|
-
- specs/storage_volume_spec.rb
|
145
|
+
- specs/fixtures/images/img1.yml
|
146
|
+
- specs/fixtures/images/img2.yml
|
147
|
+
- specs/fixtures/images/img3.yml
|
148
|
+
- specs/fixtures/instances/inst0.yml
|
151
149
|
- specs/fixtures/instances/inst1.yml
|
152
150
|
- specs/fixtures/instances/inst2.yml
|
153
|
-
- specs/fixtures/instances/inst0.yml
|
154
|
-
- specs/fixtures/storage_volumes/vol1.yml
|
155
|
-
- specs/fixtures/storage_volumes/vol3.yml
|
156
|
-
- specs/fixtures/storage_volumes/vol2.yml
|
157
|
-
- specs/fixtures/storage_snapshots/snap3.yml
|
158
151
|
- specs/fixtures/storage_snapshots/snap1.yml
|
159
152
|
- specs/fixtures/storage_snapshots/snap2.yml
|
160
|
-
- specs/fixtures/
|
161
|
-
- specs/fixtures/
|
162
|
-
- specs/fixtures/
|
163
|
-
- specs/
|
153
|
+
- specs/fixtures/storage_snapshots/snap3.yml
|
154
|
+
- specs/fixtures/storage_volumes/vol1.yml
|
155
|
+
- specs/fixtures/storage_volumes/vol2.yml
|
156
|
+
- specs/fixtures/storage_volumes/vol3.yml
|
157
|
+
- specs/hardware_profiles_spec.rb
|
158
|
+
- specs/images_spec.rb
|
164
159
|
- specs/initialization_spec.rb
|
160
|
+
- specs/instance_states_spec.rb
|
161
|
+
- specs/instances_spec.rb
|
162
|
+
- specs/realms_spec.rb
|
163
|
+
- specs/shared/resources.rb
|
164
|
+
- specs/spec_helper.rb
|
165
|
+
- specs/storage_snapshot_spec.rb
|
166
|
+
- specs/storage_volume_spec.rb
|