deltacloud-client 0.1.0 → 0.1.1
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/Rakefile +3 -9
- data/bin/deltacloudc +1 -0
- data/lib/base_object.rb +23 -15
- data/lib/deltacloud.rb +43 -15
- data/specs/instances_spec.rb +2 -2
- data/specs/storage_volume_spec.rb +0 -2
- metadata +18 -18
data/Rakefile
CHANGED
@@ -25,15 +25,9 @@ task 'documentation' do
|
|
25
25
|
load 'lib/documentation.rb'
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
@specs.values.each do |spec|
|
34
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
35
|
-
pkg.need_tar = true
|
36
|
-
end
|
28
|
+
spec = Gem::Specification.load('deltacloud-client.gemspec')
|
29
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
30
|
+
pkg.need_tar = true
|
37
31
|
end
|
38
32
|
|
39
33
|
if Gem.available?('rspec')
|
data/bin/deltacloudc
CHANGED
@@ -124,6 +124,7 @@ if options[:collection] and options[:operation]
|
|
124
124
|
# If collection is set and requested operation is 'show' just 'singularize'
|
125
125
|
# collection name and print item with specified id (-i parameter)
|
126
126
|
if options[:operation].eql?('show')
|
127
|
+
invalid_usage("Missing ID, must be provided with --id") unless options[:id]
|
127
128
|
puts format(client.send(options[:collection].gsub(/s$/, ''), options[:id]))
|
128
129
|
exit(0)
|
129
130
|
end
|
data/lib/base_object.rb
CHANGED
@@ -172,7 +172,7 @@ module DeltaCloud
|
|
172
172
|
base_method_handler(m, args)
|
173
173
|
rescue NoHandlerForMethod
|
174
174
|
case m[:type]
|
175
|
-
when :action_link then do_action(m)
|
175
|
+
when :action_link then do_action(m, args)
|
176
176
|
else raise NoHandlerForMethod
|
177
177
|
end
|
178
178
|
end
|
@@ -180,14 +180,19 @@ module DeltaCloud
|
|
180
180
|
|
181
181
|
private
|
182
182
|
|
183
|
-
def do_action(m)
|
184
|
-
|
183
|
+
def do_action(m, args)
|
184
|
+
args = args.first || {}
|
185
|
+
method = m[:method].to_sym
|
186
|
+
@client.request(method,
|
187
|
+
m[:href],
|
188
|
+
method == :get ? args : {},
|
189
|
+
method == :get ? {} : args)
|
185
190
|
action_trigger(m[:rel])
|
186
191
|
end
|
187
192
|
|
188
193
|
end
|
189
194
|
|
190
|
-
class
|
195
|
+
class StatefulObject < ActionObject
|
191
196
|
attr_reader :state
|
192
197
|
|
193
198
|
def initialize(opts={}, &block)
|
@@ -258,25 +263,28 @@ module DeltaCloud
|
|
258
263
|
end
|
259
264
|
|
260
265
|
def self.add_class(name, parent=:base)
|
261
|
-
|
262
|
-
|
263
|
-
when :action then 'ActionObject'
|
264
|
-
when :state then 'StateFullObject'
|
265
|
-
end
|
266
|
+
parent = parent.to_s
|
267
|
+
parent_class = "#{parent.classify}Object"
|
266
268
|
@defined_classes ||= []
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
DeltaCloud::API.const_get("#{name.classify}")
|
269
|
+
class_name = "#{parent.classify}::#{name.classify}"
|
270
|
+
unless @defined_classes.include?(class_name)
|
271
|
+
DeltaCloud::API.class_eval("class #{class_name} < DeltaCloud::#{parent_class}; end")
|
272
|
+
@defined_classes << class_name
|
272
273
|
end
|
274
|
+
|
275
|
+
DeltaCloud::API.const_get(parent.classify).const_get(name.classify)
|
273
276
|
end
|
274
277
|
|
275
278
|
def self.guess_model_type(response)
|
276
279
|
response = Nokogiri::XML(response.to_s)
|
277
280
|
return :action if ((response/'//actions').length == 1) and ((response/'//state').length == 0)
|
278
|
-
return :
|
281
|
+
return :stateful if ((response/'//actions').length == 1) and ((response/'//state').length == 1)
|
279
282
|
return :base
|
280
283
|
end
|
281
284
|
|
285
|
+
class API
|
286
|
+
class Action; end
|
287
|
+
class Base; end
|
288
|
+
class Stateful; end
|
289
|
+
end
|
282
290
|
end
|
data/lib/deltacloud.rb
CHANGED
@@ -34,8 +34,9 @@ module DeltaCloud
|
|
34
34
|
# @param [String, password] API password
|
35
35
|
# @param [String, user_name] API URL (eg. http://localhost:3001/api)
|
36
36
|
# @return [DeltaCloud::API]
|
37
|
-
def self.new(user_name, password, api_url, &block)
|
38
|
-
|
37
|
+
def self.new(user_name, password, api_url, opts={}, &block)
|
38
|
+
opts ||= {}
|
39
|
+
API.new(user_name, password, api_url, opts, &block)
|
39
40
|
end
|
40
41
|
|
41
42
|
# Check given credentials if their are valid against
|
@@ -62,11 +63,13 @@ module DeltaCloud
|
|
62
63
|
end
|
63
64
|
|
64
65
|
class API
|
65
|
-
attr_reader
|
66
|
+
attr_reader :api_uri, :driver_name, :api_version, :features, :entry_points
|
67
|
+
attr_reader :api_driver, :api_provider
|
66
68
|
|
67
69
|
def initialize(user_name, password, api_url, opts={}, &block)
|
68
70
|
opts[:version] = true
|
69
|
-
@
|
71
|
+
@api_driver, @api_provider = opts[:driver], opts[:provider]
|
72
|
+
@username, @password = opts[:username] || user_name, opts[:password] || password
|
70
73
|
@api_uri = URI.parse(api_url)
|
71
74
|
@features, @entry_points = {}, {}
|
72
75
|
@verbose = opts[:verbose] || false
|
@@ -90,7 +93,7 @@ module DeltaCloud
|
|
90
93
|
# Define methods based on 'rel' attribute in entry point
|
91
94
|
# Two methods are declared: 'images' and 'image'
|
92
95
|
def declare_entry_points_methods(entry_points)
|
93
|
-
|
96
|
+
|
94
97
|
API.instance_eval do
|
95
98
|
entry_points.keys.select {|k| [:instance_states].include?(k)==false }.each do |model|
|
96
99
|
|
@@ -99,13 +102,13 @@ module DeltaCloud
|
|
99
102
|
base_object_collection(model, response)
|
100
103
|
end
|
101
104
|
end
|
102
|
-
|
105
|
+
|
103
106
|
define_method :"#{model.to_s.singularize}" do |*args|
|
104
107
|
request(:get, "#{entry_points[model]}/#{args[0]}") do |response|
|
105
108
|
base_object(model, response)
|
106
109
|
end
|
107
110
|
end
|
108
|
-
|
111
|
+
|
109
112
|
define_method :"fetch_#{model.to_s.singularize}" do |url|
|
110
113
|
id = url.grep(/\/#{model}\/(.*)$/)
|
111
114
|
self.send(model.to_s.singularize.to_sym, $1)
|
@@ -129,7 +132,7 @@ module DeltaCloud
|
|
129
132
|
|
130
133
|
# Convert XML response to defined Ruby Class
|
131
134
|
def xml_to_class(base_object, item)
|
132
|
-
|
135
|
+
|
133
136
|
return nil unless item
|
134
137
|
|
135
138
|
params = {
|
@@ -166,11 +169,17 @@ module DeltaCloud
|
|
166
169
|
end && next
|
167
170
|
end
|
168
171
|
|
172
|
+
if attribute.name == 'mount'
|
173
|
+
obj.add_link!("instance", (attribute/"./instance/@id").first)
|
174
|
+
obj.add_text!("device", (attribute/"./device/@name").first.value)
|
175
|
+
next
|
176
|
+
end
|
177
|
+
|
169
178
|
# Deal with collections like public-addresses, private-addresses
|
170
179
|
if (attribute/'./*').length > 0
|
171
180
|
obj.add_collection!(attribute.name, (attribute/'*').collect { |value| value.text }) && next
|
172
181
|
end
|
173
|
-
|
182
|
+
|
174
183
|
# Anything else is treaten as text object
|
175
184
|
obj.add_text!(attribute.name, attribute.text.convert)
|
176
185
|
end
|
@@ -185,15 +194,15 @@ module DeltaCloud
|
|
185
194
|
api_xml = Nokogiri::XML(response)
|
186
195
|
@driver_name = api_xml.xpath('/api').first['driver']
|
187
196
|
@api_version = api_xml.xpath('/api').first['version']
|
188
|
-
|
197
|
+
|
189
198
|
api_xml.css("api > link").each do |entry_point|
|
190
199
|
rel, href = entry_point['rel'].to_sym, entry_point['href']
|
191
200
|
@entry_points.store(rel, href)
|
192
|
-
|
201
|
+
|
193
202
|
entry_point.css("feature").each do |feature|
|
194
203
|
@features[rel] ||= []
|
195
204
|
@features[rel] << feature['name'].to_sym
|
196
|
-
|
205
|
+
|
197
206
|
end
|
198
207
|
end
|
199
208
|
end
|
@@ -235,6 +244,25 @@ module DeltaCloud
|
|
235
244
|
raise NoMethodError
|
236
245
|
end
|
237
246
|
|
247
|
+
def use_driver(driver, opts={})
|
248
|
+
if opts[:driver]
|
249
|
+
@api_driver = driver
|
250
|
+
@driver_name = driver
|
251
|
+
discover_entry_points
|
252
|
+
end
|
253
|
+
@username = opts[:username] if opts[:username]
|
254
|
+
@password = opts[:password] if opts[:password]
|
255
|
+
@api_provider = opts[:provider] if opts[:provider]
|
256
|
+
return self
|
257
|
+
end
|
258
|
+
|
259
|
+
def extended_headers
|
260
|
+
headers = {}
|
261
|
+
headers["X-Deltacloud-Driver"] = @api_driver.to_s if @api_driver
|
262
|
+
headers["X-Deltacloud-Provider"] = @api_provider.to_s if @api_provider
|
263
|
+
headers
|
264
|
+
end
|
265
|
+
|
238
266
|
# Basic request method
|
239
267
|
#
|
240
268
|
def request(*args, &block)
|
@@ -247,9 +275,9 @@ module DeltaCloud
|
|
247
275
|
if conf[:query_args] != {}
|
248
276
|
conf[:path] += '?' + URI.escape(conf[:query_args].collect{ |key, value| "#{key}=#{value}" }.join('&')).to_s
|
249
277
|
end
|
250
|
-
|
278
|
+
|
251
279
|
if conf[:method].eql?(:post)
|
252
|
-
RestClient.send(:post, conf[:path], conf[:form_data], default_headers) do |response, request, block|
|
280
|
+
RestClient.send(:post, conf[:path], conf[:form_data], default_headers.merge(extended_headers)) do |response, request, block|
|
253
281
|
handle_backend_error(response) if response.code.eql?(500)
|
254
282
|
if response.respond_to?('body')
|
255
283
|
yield response.body if block_given?
|
@@ -258,7 +286,7 @@ module DeltaCloud
|
|
258
286
|
end
|
259
287
|
end
|
260
288
|
else
|
261
|
-
RestClient.send(conf[:method], conf[:path], default_headers) do |response, request, block|
|
289
|
+
RestClient.send(conf[:method], conf[:path], default_headers.merge(extended_headers)) do |response, request, block|
|
262
290
|
handle_backend_error(response) if response.code.eql?(500)
|
263
291
|
if conf[:method].eql?(:get) and [301, 302, 307].include? response.code
|
264
292
|
response.follow_redirection(request) do |response, request, block|
|
data/specs/instances_spec.rb
CHANGED
@@ -34,9 +34,9 @@ describe "instances" do
|
|
34
34
|
instance.owner_id.should_not be_nil
|
35
35
|
instance.owner_id.should be_a( String )
|
36
36
|
instance.image.should_not be_nil
|
37
|
-
instance.image.should be_a( DeltaCloud::API::Image )
|
37
|
+
instance.image.should be_a( DeltaCloud::API::Base::Image )
|
38
38
|
instance.hardware_profile.should_not be_nil
|
39
|
-
instance.hardware_profile.should be_a( DeltaCloud::API::HardwareProfile )
|
39
|
+
instance.hardware_profile.should be_a( DeltaCloud::API::Base::HardwareProfile )
|
40
40
|
instance.state.should_not be_nil
|
41
41
|
instance.state.should be_a( String )
|
42
42
|
instance.public_addresses.should_not be_nil
|
@@ -44,7 +44,6 @@ describe "storage volumes" do
|
|
44
44
|
storage_volume = client.storage_volume( 'vol3' )
|
45
45
|
storage_volume.id.should eql( 'vol3' )
|
46
46
|
storage_volume.uri.should eql( API_URL + '/storage_volumes/vol3' )
|
47
|
-
storage_volume.state.should eql( 'IN-USE' )
|
48
47
|
storage_volume.capacity.should eql( 1.0 )
|
49
48
|
storage_volume.device.should eql( '/dev/sda1' )
|
50
49
|
storage_volume.instance.should_not be_nil
|
@@ -61,7 +60,6 @@ describe "storage volumes" do
|
|
61
60
|
storage_volume.should_not be_nil
|
62
61
|
storage_volume.id.should eql( 'vol3' )
|
63
62
|
storage_volume.uri.should eql( API_URL + '/storage_volumes/vol3' )
|
64
|
-
storage_volume.state.should eql( 'IN-USE' )
|
65
63
|
storage_volume.capacity.should eql( 1.0 )
|
66
64
|
storage_volume.device.should eql( '/dev/sda1' )
|
67
65
|
storage_volume.instance.should_not be_nil
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deltacloud-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Red Hat, Inc.
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-12-09 00:00:00 +01:00
|
19
19
|
default_executable: deltacloudc
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -26,12 +26,12 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 13
|
30
30
|
segments:
|
31
31
|
- 1
|
32
|
-
-
|
33
|
-
-
|
34
|
-
version: 1.
|
32
|
+
- 6
|
33
|
+
- 1
|
34
|
+
version: 1.6.1
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
37
|
- !ruby/object:Gem::Dependency
|
@@ -42,12 +42,12 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - ">="
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
hash:
|
45
|
+
hash: 1
|
46
46
|
segments:
|
47
47
|
- 1
|
48
48
|
- 4
|
49
|
-
-
|
50
|
-
version: 1.4.
|
49
|
+
- 3
|
50
|
+
version: 1.4.3
|
51
51
|
type: :runtime
|
52
52
|
version_requirements: *id002
|
53
53
|
- !ruby/object:Gem::Dependency
|
@@ -81,8 +81,8 @@ files:
|
|
81
81
|
- lib/hwp_properties.rb
|
82
82
|
- lib/instance_state.rb
|
83
83
|
- lib/string.rb
|
84
|
-
- lib/deltacloud.rb
|
85
84
|
- lib/base_object.rb
|
85
|
+
- lib/deltacloud.rb
|
86
86
|
- init.rb
|
87
87
|
- bin/deltacloudc
|
88
88
|
- COPYING
|
@@ -103,9 +103,11 @@ files:
|
|
103
103
|
- specs/initialization_spec.rb
|
104
104
|
- specs/instance_states_spec.rb
|
105
105
|
- specs/realms_spec.rb
|
106
|
-
- specs/spec_helper.rb
|
107
106
|
- specs/storage_snapshot_spec.rb
|
107
|
+
- specs/hardware_profiles_spec.rb
|
108
108
|
- specs/storage_volume_spec.rb
|
109
|
+
- specs/spec_helper.rb
|
110
|
+
- specs/instances_spec.rb
|
109
111
|
- specs/data/storage_volumes/vol1.yml
|
110
112
|
- specs/data/storage_volumes/vol3.yml
|
111
113
|
- specs/data/storage_volumes/vol2.yml
|
@@ -118,8 +120,6 @@ files:
|
|
118
120
|
- specs/data/instances/inst1.yml
|
119
121
|
- specs/data/instances/inst2.yml
|
120
122
|
- specs/data/instances/inst0.yml
|
121
|
-
- specs/hardware_profiles_spec.rb
|
122
|
-
- specs/instances_spec.rb
|
123
123
|
has_rdoc: true
|
124
124
|
homepage: http://www.deltacloud.org
|
125
125
|
licenses: []
|
@@ -172,9 +172,11 @@ test_files:
|
|
172
172
|
- specs/initialization_spec.rb
|
173
173
|
- specs/instance_states_spec.rb
|
174
174
|
- specs/realms_spec.rb
|
175
|
-
- specs/spec_helper.rb
|
176
175
|
- specs/storage_snapshot_spec.rb
|
176
|
+
- specs/hardware_profiles_spec.rb
|
177
177
|
- specs/storage_volume_spec.rb
|
178
|
+
- specs/spec_helper.rb
|
179
|
+
- specs/instances_spec.rb
|
178
180
|
- specs/data/storage_volumes/vol1.yml
|
179
181
|
- specs/data/storage_volumes/vol3.yml
|
180
182
|
- specs/data/storage_volumes/vol2.yml
|
@@ -187,5 +189,3 @@ test_files:
|
|
187
189
|
- specs/data/instances/inst1.yml
|
188
190
|
- specs/data/instances/inst2.yml
|
189
191
|
- specs/data/instances/inst0.yml
|
190
|
-
- specs/hardware_profiles_spec.rb
|
191
|
-
- specs/instances_spec.rb
|