Class: HpcloudController

Inherits:
Object
  • Object
show all
Defined in:
lib/process/cloud/providers/hpcloud/hpcloud.rb

Overview

Following class describe how FORJ should handle HP Cloud objects. Except Cloud connection, all HPCloud objects management are described/called in HP* modules.

Instance Method Summary collapse

Instance Method Details

#_get_instance_attr(oControlerObject, key) ⇒ Object



415
416
417
418
419
420
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 415

def _get_instance_attr(oControlerObject, key)
  return (oControlerObject) if key[0] == :metadata
  return nil if oControlerObject.send(key[0]).nil?
  return oControlerObject.send(key[0]) if key.length == 1
  oControlerObject.send(key[0]).rh_get(key[1..-1])
end

#_server_metadata_get(oControlerObject) ⇒ Object



405
406
407
408
409
410
411
412
413
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 405

def (oControlerObject)
  ret = {}
  oControlerObject..each do |m|
    k = m.attributes[:key]
    v = m.attributes[:value]
    ret[k] = v
  end
  ret
end

#connect(sObjectType, hParams) ⇒ Object

rubocop: disable Metrics/ClassLength



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 186

def connect(sObjectType, hParams)
  case sObjectType
  when :services
    Fog::HP.authenticate_v2(hParams[:hdata], hParams[:excon_opts])
  when :compute_connection
    Fog::Compute.new hParams[:hdata].merge(:provider => :hp, :version => 'v2')
  when :network_connection
    Fog::HP::Network.new(hParams[:hdata])
  else
    controller_error "'%s' is not a valid object for 'connect'", sObjectType
  end
end

#create(sObjectType, hParams) ⇒ Object

Create controller for Hpcloud



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 202

def create(sObjectType, hParams)
  case sObjectType
  when :public_ip
    required?(hParams, :compute_connection)
    required?(hParams, :server)
    HPCompute.server_assign_address(hParams[:compute_connection],
                                    hParams[:server])
  when :server
    required?(hParams, :compute_connection)
    required?(hParams, :image)
    required?(hParams, :network)
    required?(hParams, :flavor)
    required?(hParams, :keypairs)
    required?(hParams, :security_groups)
    required?(hParams, :server_name)

    options = {
      :name => hParams[:server_name],
      :flavor_id => hParams[:flavor].id,
      :image_id => hParams[:image].id,
      :key_name => hParams[:keypairs].name,
      :security_groups => [hParams[:security_groups].name],
      :networks => [hParams[:network].id]
    }

    HPCompute.create_server(hParams[:compute_connection], options,
                            hParams[:user_data], hParams[:meta_data])
  when :image
    required?(hParams, :compute_connection)
    required?(hParams, 'server#image_name')

    HPCompute.get_image(hParams[:compute_connection],
                        hParams['server#image_name'])
  when :network
    required?(hParams, :network_connection)
    required?(hParams, :network_name)

    HPNetwork.create_network(hParams[:network_connection],
                             hParams[:network_name])
  when :subnetwork
    required?(hParams, :network_connection)
    required?(hParams, :network)
    required?(hParams, :subnetwork_name)

    HPNetwork.create_subnetwork(hParams[:network_connection],
                                hParams[:network],
                                hParams[:subnetwork_name])
  when :security_groups
    required?(hParams, :network_connection)
    required?(hParams, :security_group)

    HPSecurityGroups.create_sg(hParams[:network_connection],
                               hParams[:security_group], hParams[:sg_desc])
  when :keypairs
    required?(hParams, :compute_connection)
    required?(hParams, 'credentials#keypair_name')
    required?(hParams, :public_key)

    HPKeyPairs.create_keypair(hParams[:compute_connection],
                              hParams['credentials#keypair_name'],
                              hParams[:public_key])
  when :router
    required?(hParams, :network_connection)
    required?(hParams, :router_name)

    # Forcelly used admin_status_up to true.
    hParams[:hdata] = hParams[:hdata].merge(:admin_state_up => true)

    HPNetwork.create_router(hParams[:network_connection], hParams[:hdata])
  when :rule
    required?(hParams, :network_connection)
    required?(hParams, :security_groups)
    HPSecurityGroups.create_rule(hParams[:network_connection],
                                 hParams[:hdata])
  when :router_interface
    required?(hParams, :router)
    required?(hParams, :subnetwork)
    HPNetwork.add_interface(hParams[:router], hParams[:subnetwork])
  else
    controller_error "'%s' is not a valid object for 'create'", sObjectType
  end
end

#delete(sObjectType, hParams) ⇒ Object

rubocop: enable CyclomaticComplexity, MethodLength



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 331

def delete(sObjectType, hParams)
  case sObjectType
  when :network
    HPNetwork.delete_network(hParams[:network_connection],
                             hParams[:network])
  when :rule
    HPSecurityGroups.delete_rule(hParams[:network_connection],
                                 hParams[:id])
    obj = hParams[:network_connection]
    obj.security_group_rules.get(hParams[:id]).destroy
  when :server
    required?(hParams, :compute_connection)
    required?(hParams, :server)
    HPCompute.delete_server(hParams[:compute_connection],
                            hParams[:server])
  end
end

#get(sObjectType, sUniqId, hParams) ⇒ Object

rubocop: disable CyclomaticComplexity,



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 349

def get(sObjectType, sUniqId, hParams)
  case sObjectType
  when :server_log
    required?(hParams, :server)

    hParams[:server].console_output(sUniqId)
  when :server
    required?(hParams, :compute_connection)
    HPCompute.get_server(hParams[:compute_connection], sUniqId)
  when :image
    required?(hParams, :compute_connection)
    HPCompute.get_image(hParams[:compute_connection], sUniqId)
  when :network
    required?(hParams, :network_connection)
    HPNetwork.get_network(hParams[:network_connection], sUniqId)
  when :keypairs
    required?(hParams, :compute_connection)
    HPKeyPairs.get_keypair(hParams[:compute_connection], sUniqId)
  when :public_ip
    required?(hParams, :compute_connection)
    required?(hParams, :server)
    HPCompute.get_server_assigned_address(hParams[:compute_connection],
                                          sUniqId)
  else
    forjError "'%s' is not a valid object for 'get'", sObjectType
  end
end

#get_attr(oControlerObject, key) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 388

def get_attr(oControlerObject, key)
  if oControlerObject.is_a?(Excon::Response)
    oControlerObject.data.rh_get(:body, key)
  else
    attributes = oControlerObject.attributes
    def_attributes = oControlerObject.class.attributes
    controller_error "attribute '%s' is unknown in '%s'. "\
                     "Valid one are : '%s'",
                     key[0], oControlerObject.class,
                     def_attributes unless def_attributes.include?(key[0])
    return attributes.rh_get(key) if attributes.rh_exist?(key)
    _get_instance_attr(oControlerObject, key)
  end
rescue => e
  controller_error "Unable to map '%s'. %s", key, e.message
end

#get_services(sObjectType, oParams) ⇒ Object

This function requires to return an Array of values or nil.



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 452

def get_services(sObjectType, oParams)
  case sObjectType
  when :services
    # oParams[sObjectType] will provide the controller object.
    # This one can be interpreted only by controller code,
    # except if controller declares how to map with this object.
    # Processes can deal only process mapped data.
    # Currently there is no services process function. No need to map.
    services = oParams[:services]
    if !oParams[:list_services].is_a?(Array)
      service_to_find = [oParams[:list_services]]
    else
      service_to_find = oParams[:list_services]
    end
    # Search for service. Ex: Can be :Networking or network. I currently do
    # not know why...
    search_services = services.rh_get(:service_catalog)
    service = nil
    service_to_find.each do |sServiceElem|
      if search_services.key?(sServiceElem)
        service = sServiceElem
        break
      end
    end

    controller_error 'Unable to find services %s',
                     service_to_find if service.nil?
    result = services.rh_get(:service_catalog, service).keys
    result.delete('name')
    result.each_index do |iIndex|
      result[iIndex] = result[iIndex].to_s if result[iIndex].is_a?(Symbol)
    end
    return result
  else
    controller_error "'%s' is not a valid object for 'get_services'",
                     sObjectType
  end
end

#query(sObjectType, sQuery, hParams) ⇒ Object

This function return a collection which have to provide: functions: [], length, each Used by network process.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 288

def query(sObjectType, sQuery, hParams)
  case sObjectType
  when :public_ip
    required?(hParams, :compute_connection)
    required?(hParams, :server)
    HPCompute.query_server_assigned_addresses(hParams[:compute_connection],
                                              hParams[:server], sQuery)
  when :server
    required?(hParams, :compute_connection)
    HPCompute.query_server(hParams[:compute_connection], sQuery)
  when :image
    required?(hParams, :compute_connection)
    HPCompute.query_image(hParams[:compute_connection], sQuery)
  when :network
    required?(hParams, :network_connection)
    HPNetwork.query_network(hParams[:network_connection], sQuery)
  when :subnetwork
    required?(hParams, :network_connection)
    HPNetwork.query_subnetwork(hParams[:network_connection], sQuery)
  when :router
    required?(hParams, :network_connection)
    HPNetwork.query_router(hParams[:network_connection], sQuery)
  when :port
    required?(hParams, :network_connection)
    HPNetwork.query_port(hParams[:network_connection], sQuery)
  when :security_groups
    required?(hParams, :network_connection)
    HPSecurityGroups.query_sg(hParams[:network_connection], sQuery)
  when :rule
    required?(hParams, :network_connection)
    HPSecurityGroups.query_rule(hParams[:network_connection], sQuery)
  when :keypairs
    required?(hParams, :compute_connection)
    HPKeyPairs.query_keypair(hParams[:compute_connection], sQuery)
  when :flavor
    required?(hParams, :compute_connection)
    HPCompute.query_flavor(hParams[:compute_connection], sQuery)
  else
    controller_error "'%s' is not a valid object for 'query'", sObjectType
  end
end

#query_each(oFogObject) ⇒ Object

rubocop: enable CyclomaticComplexity



378
379
380
381
382
383
384
385
386
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 378

def query_each(oFogObject)
  case oFogObject.class.to_s
  when 'Fog::HP::Network::Networks'
    oFogObject.each { |value| yield(value) }
  else
    controller_error "'%s' is not a valid list for 'each'",
                     oFogObject.class
  end
end

#set_attr(oControlerObject, key, value) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 422

def set_attr(oControlerObject, key, value)
  controller_class = oControlerObject.class

  controller_error "No set feature for '%s'",
                   controller_class if oControlerObject.is_a?(Excon::Response)

  attributes = oControlerObject.attributes
  def_attributes = oControlerObject.class.attributes
  controller_error "attribute '%s' is unknown in '%s'. Valid one are : '%s'",
                   key[0], oControlerObject.class,
                   def_attributes unless def_attributes.include?(key[0])
  attributes.rh_set(value, key)
rescue => e
  controller_error "Unable to map '%s' on '%s'\n%s",
                   key, sObjectType, e.message
end

#update(sObjectType, oObject, _hParams) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 439

def update(sObjectType, oObject, _hParams)
  case sObjectType
  when :router
    controller_error 'Object to update is nil' if oObject.nil?

    HPNetwork.update_router(oObject[:object])
  else
    controller_error "'%s' is not a valid list for 'update'",
                     oFogObject.class
  end
end