Class: DeltaCloud

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, password, api_uri, opts = {}, &block) ⇒ DeltaCloud

Returns a new instance of DeltaCloud.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/deltacloud.rb', line 46

def initialize(name, password, api_uri, opts={}, &block)
  @logger       = Logger.new( STDERR )
  @name         = name
  @password     = password
  @api_uri      = URI.parse( api_uri )
  @entry_points = {}
  @verbose      = opts[:verbose]
  @features = {}
  discover_entry_points
  connect( &block )
  self
end

Instance Attribute Details

#api_uriObject (readonly)

Returns the value of attribute api_uri.



34
35
36
# File 'lib/deltacloud.rb', line 34

def api_uri
  @api_uri
end

#driver_nameObject (readonly)

Returns the value of attribute driver_name.



36
37
38
# File 'lib/deltacloud.rb', line 36

def driver_name
  @driver_name
end

#entry_pointsObject (readonly)

Returns the value of attribute entry_points.



35
36
37
# File 'lib/deltacloud.rb', line 35

def entry_points
  @entry_points
end

#featuresObject (readonly)

Returns the value of attribute features.



38
39
40
# File 'lib/deltacloud.rb', line 38

def features
  @features
end

#last_request_xmlObject (readonly)

Returns the value of attribute last_request_xml.



37
38
39
# File 'lib/deltacloud.rb', line 37

def last_request_xml
  @last_request_xml
end

#loggerObject

Returns the value of attribute logger.



33
34
35
# File 'lib/deltacloud.rb', line 33

def logger
  @logger
end

Class Method Details

.driver_name(url) ⇒ Object



40
41
42
43
44
# File 'lib/deltacloud.rb', line 40

def self.driver_name(url)
  DeltaCloud.new( nil, nil, url) do |client|
    return client.driver_name
  end
end

Instance Method Details

#api_hostObject



67
68
69
# File 'lib/deltacloud.rb', line 67

def api_host
  @api_uri.host
end

#api_pathObject



75
76
77
# File 'lib/deltacloud.rb', line 75

def api_path
  @api_uri.path
end

#api_portObject



71
72
73
# File 'lib/deltacloud.rb', line 71

def api_port
  @api_uri.port
end

#connect(&block) ⇒ Object



60
61
62
63
64
65
# File 'lib/deltacloud.rb', line 60

def connect(&block)
  @http = RestClient::Resource.new( api_uri.to_s , :accept => 'application/xml' )
  discover_entry_points
  block.call( self ) if block
  self
end

#create_instance(image_id, opts = {}) ⇒ Object

Create a new instance, using image image_id. Possible optiosn are

name  - a user-defined name for the instance
realm - a specific realm for placement of the instance
hardware_profile - either a string giving the name of the
                   hardware profile or a hash. The hash must have an
                   entry +id+, giving the id of the hardware profile,
                   and may contain additional names of properties,
                   e.g. 'storage', to override entries in the
                   hardware profile


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/deltacloud.rb', line 275

def create_instance(image_id, opts={})
  name = opts[:name]
  realm_id = opts[:realm]

  params = opts.dup
  ( params[:realm_id] = realm_id ) if realm_id
  ( params[:name] = name ) if name

  if opts[:hardware_profile].is_a?(String)
    params[:hwp_id] = opts[:hardware_profile]
  elsif opts[:hardware_profile].is_a?(Hash)
    params.delete(:hardware_profile)
    opts[:hardware_profile].each do |k,v|
      params[:"hwp_#{k}"] = v
    end
  end

  params[:image_id] = image_id
  request( entry_points[:instances], :post, {}, params ) do |response|
    doc = REXML::Document.new( response )
    instance = doc.root
    uri = instance.attributes['href']
    return DCloud::Instance.new( self, uri, instance )
  end
end

#feature?(collection, name) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/deltacloud.rb', line 79

def feature?(collection, name)
  @features.has_key?(collection) && @features[collection].include?(name)
end

#fetch_documentation(collection, operation = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/deltacloud.rb', line 121

def fetch_documentation(collection, operation=nil)
  response = @http["docs/#{collection}#{operation ? "/#{operation}" : ''}"].get(:accept => "application/xml")
  doc = REXML::Document.new( response.to_s )
  if operation.nil?
    docs = {
      :name => doc.get_elements('docs/collection').first.attributes['name'],
      :description => doc.get_elements('docs/collection/description').first.text,
      :operations => []
    }
    doc.get_elements('docs/collection/operations/operation').each do |operation|
      p = {}
      p[:name] = operation.attributes['name']
      p[:description] = operation.get_elements('description').first.text
      p[:parameters] = []
      operation.get_elements('parameter').each do |param|
        p[:parameters] << param.attributes['name']
      end
      docs[:operations] << p
    end
  else
    docs = {
      :name => doc.get_elements('docs/operation').attributes['name'],
      :description => doc.get_elements('docs/operation/description').first.text,
      :parameters => []
    }
    doc.get_elements('docs/operation/parameter').each do |param|
      docs[:parameters] << param.attributes['name']
    end
  end
  docs
end

#fetch_hardware_profile(uri) ⇒ Object



105
106
107
108
109
# File 'lib/deltacloud.rb', line 105

def fetch_hardware_profile(uri)
  xml = fetch_resource( :hardware_profile, uri )
  return DCloud::HardwareProfile.new( self, uri, xml ) if xml
  nil
end

#fetch_image(uri) ⇒ Object



359
360
361
362
363
# File 'lib/deltacloud.rb', line 359

def fetch_image(uri)
  xml = fetch_resource( :image, uri )
  return DCloud::Image.new( self, uri, xml ) if xml
  nil
end

#fetch_instance(uri) ⇒ Object



259
260
261
262
263
# File 'lib/deltacloud.rb', line 259

def fetch_instance(uri)
  xml = fetch_resource( :instance, uri )
  return DCloud::Instance.new( self, uri, xml ) if xml
  nil
end

#fetch_realm(uri) ⇒ Object



199
200
201
202
203
# File 'lib/deltacloud.rb', line 199

def fetch_realm(uri)
  xml = fetch_resource( :realm, uri )
  return DCloud::Realm.new( self, uri, xml ) if xml
  nil
end

#fetch_resource(type, uri) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/deltacloud.rb', line 111

def fetch_resource(type, uri)
  request( uri ) do |response|
    doc = REXML::Document.new( response )
    if ( doc.root && ( doc.root.name == type.to_s.gsub( /_/, '-' ) ) )
      return doc.root
    end
  end
  nil
end

#fetch_storage_snapshot(uri) ⇒ Object



353
354
355
356
357
# File 'lib/deltacloud.rb', line 353

def fetch_storage_snapshot(uri)
  xml = fetch_resource( :storage_snapshot, uri )
  return DCloud::StorageSnapshot.new( self, uri, xml ) if xml
  nil
end

#fetch_storage_volume(uri) ⇒ Object



324
325
326
327
328
# File 'lib/deltacloud.rb', line 324

def fetch_storage_volume(uri)
  xml = fetch_resource( :storage_volume, uri )
  return DCloud::StorageVolume.new( self, uri, xml ) if xml
  nil
end

#hardware_profile(id) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/deltacloud.rb', line 95

def hardware_profile(id)
  request( entry_points[:hardware_profiles], :get, {:id=>id } ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( '/hardware-profile' ).each do |hwp|
      uri = hwp.attributes['href']
      return DCloud::HardwareProfile.new( self, uri, hwp )
    end
  end
end

#hardware_profiles(opts = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/deltacloud.rb', line 83

def hardware_profiles(opts={})
  hardware_profiles = []
  request(entry_points[:hardware_profiles], :get, opts) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'hardware-profiles/hardware-profile' ).each do |hwp|
      uri = hwp.attributes['href']
      hardware_profiles << DCloud::HardwareProfile.new( self, uri, hwp )
    end
  end
  hardware_profiles
end

#image(id) ⇒ Object



218
219
220
221
222
223
224
225
226
227
# File 'lib/deltacloud.rb', line 218

def image(id)
  request( entry_points[:images], :get, {:id=>id } ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'image' ).each do |instance|
      uri = instance.attributes['href']
      return DCloud::Image.new( self, uri, instance )
    end
  end
  nil
end

#images(opts = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/deltacloud.rb', line 205

def images(opts={})
  images = []
  request_path = entry_points[:images]
  request( request_path, :get, opts ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'images/image' ).each do |image|
      uri = image.attributes['href']
      images << DCloud::Image.new( self, uri, image )
    end
  end
  images
end

#instance(id) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/deltacloud.rb', line 241

def instance(id)
  request( entry_points[:instances], :get, {:id=>id } ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'instance' ).each do |instance|
      uri = instance.attributes['href']
      return DCloud::Instance.new( self, uri, instance )
    end
  end
  nil
end

#instance_state(name) ⇒ Object



171
172
173
174
# File 'lib/deltacloud.rb', line 171

def instance_state(name)
  found = instance_states.find{|e| e.name.to_s == name.to_s}
  found
end

#instance_statesObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/deltacloud.rb', line 153

def instance_states
  states = []
  request( entry_points[:instance_states] ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'states/state' ).each do |state_elem|
      state = DCloud::State.new( state_elem.attributes['name'] )
      state_elem.get_elements( 'transition' ).each do |transition_elem|
        state.transitions << DCloud::Transition.new(
                               transition_elem.attributes['to'],
                               transition_elem.attributes['action']
                             )
      end
      states << state
    end
  end
  states
end

#instances(opts = {}) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/deltacloud.rb', line 229

def instances(opts={})
  instances = []
  request( entry_points[:instances], :get, opts ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'instances/instance' ).each do |instance|
      uri = instance.attributes['href']
      instances << DCloud::Instance.new( self, uri, instance )
    end
  end
  instances
end

#post_instance(uri) ⇒ Object



252
253
254
255
256
257
# File 'lib/deltacloud.rb', line 252

def post_instance(uri)
  request( uri, :post ) do |response|
    return true
  end
  return false
end

#realm(id) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/deltacloud.rb', line 188

def realm(id)
  request( entry_points[:realms], :get, {:id=>id } ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'realm' ).each do |realm|
      uri = realm.attributes['href']
      return DCloud::Realm.new( self, uri, realm )
    end
  end
  nil
end

#realms(opts = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/deltacloud.rb', line 176

def realms(opts={})
  realms = []
  request( entry_points[:realms], :get, opts ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'realms/realm' ).each do |realm|
      uri = realm.attributes['href']
      realms << DCloud::Realm.new( self, uri, realm )
    end
  end
  realms
end

#storage_snapshot(id) ⇒ Object



342
343
344
345
346
347
348
349
350
351
# File 'lib/deltacloud.rb', line 342

def storage_snapshot(id)
  request( entry_points[:storage_snapshots], :get, {:id=>id } ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'storage-snapshot' ).each do |storage_snapshot|
      uri = storage_snapshot.attributes['href']
      return DCloud::StorageSnapshot.new( self, uri, storage_snapshot )
    end
  end
  nil
end

#storage_snapshots(opts = {}) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/deltacloud.rb', line 330

def storage_snapshots(opts={})
  storage_snapshots = []
  request( entry_points[:storage_snapshots] ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'storage-snapshots/storage-snapshot' ).each do |instance|
      uri = instance.attributes['href']
      storage_snapshots << DCloud::StorageSnapshot.new( self, uri, instance )
    end
  end
  storage_snapshots
end

#storage_volume(id) ⇒ Object



313
314
315
316
317
318
319
320
321
322
# File 'lib/deltacloud.rb', line 313

def storage_volume(id)
  request( entry_points[:storage_volumes], :get, {:id=>id } ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'storage-volume' ).each do |storage_volume|
      uri = storage_volume.attributes['href']
      return DCloud::StorageVolume.new( self, uri, storage_volume )
    end
  end
  nil
end

#storage_volumes(opts = {}) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
# File 'lib/deltacloud.rb', line 301

def storage_volumes(opts={})
  storage_volumes = []
  request( entry_points[:storage_volumes] ) do |response|
    doc = REXML::Document.new( response )
    doc.get_elements( 'storage-volumes/storage-volume' ).each do |instance|
      uri = instance.attributes['href']
      storage_volumes << DCloud::StorageVolume.new( self, uri, instance )
    end
  end
  storage_volumes
end