lib/process/cloud/providers/hpcloud/hpcloud.rb in lorj_cloud-0.1.3 vs lib/process/cloud/providers/hpcloud/hpcloud.rb in lorj_cloud-0.1.4
- old
+ new
@@ -19,168 +19,18 @@
require 'fog' # We use fog to access HPCloud
hpcloud_path = File.expand_path(File.dirname(__FILE__))
+load File.join(hpcloud_path, 'hpcloud_declare.rb')
+load File.join(hpcloud_path, 'hpcloud_generic.rb')
+load File.join(hpcloud_path, 'hpcloud_query.rb')
+
load File.join(hpcloud_path, 'compute.rb')
load File.join(hpcloud_path, 'network.rb')
load File.join(hpcloud_path, 'security_groups.rb')
-# Defines Meta HPCloud object
-class Hpcloud
- define_obj :services
- obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
- obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
- obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
- obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
- obj_needs :data, ':excon_opts/:connect_timeout', :default_value => 30
- obj_needs :data, ':excon_opts/:read_timeout', :default_value => 240
- obj_needs :data, ':excon_opts/:write_timeout', :default_value => 240
-
- # Defines Object structure and function stored on the Hpcloud class object.
- # Compute Object
- define_obj :compute_connection
- # Defines Data used by compute.
-
- obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
- obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
- obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
- obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
- obj_needs :data, 'services#compute', :mapping => :hp_avl_zone
-
- define_obj :network_connection
- obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
- obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
- obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
- obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
- obj_needs :data, 'services#network', :mapping => :hp_avl_zone
-
- # Forj predefine following query mapping, used by ForjProcess
- # id => id, name => name
- # If we need to add another mapping, add
- # query_mapping :id => :MyID
- # If the query is not push through and Hash object, the Provider
- # will needs to create his own mapping function.
- define_obj :network
- def_attr_mapping :external, :router_external
-
- define_obj :rule
- obj_needs :data, :dir, :mapping => :direction
- attr_value_mapping :IN, 'ingress'
- attr_value_mapping :OUT, 'egress'
-
- obj_needs :data, :proto, :mapping => :protocol
- obj_needs :data, :port_min, :mapping => :port_range_min
- obj_needs :data, :port_max, :mapping => :port_range_max
- obj_needs :data, :addr_map, :mapping => :remote_ip_prefix
- obj_needs :data, :sg_id, :mapping => :security_group_id
-
- def_attr_mapping :dir, :direction
- def_attr_mapping :proto, :protocol
- def_attr_mapping :port_min, :port_range_min
- def_attr_mapping :port_max, :port_range_max
- def_attr_mapping :addr_map, :remote_ip_prefix
- def_attr_mapping :sg_id, :security_group_id
-
- define_obj :keypairs
-
- undefine_attribute :id # Do not return any predefined ID
-
- # ************************************ Router Object
- define_obj :router
-
- obj_needs_optional
- obj_needs :data, :router_name, :mapping => :name
- # The FORJ gateway_network_id is extracted from
- # Fog::HP::Network::Router[:external_gateway_info][:network_id]
- obj_needs :data, :external_gateway_id, :mapping => [:external_gateway_info,
- 'network_id']
-
- def_attr_mapping :gateway_network_id, [:external_gateway_info, 'network_id']
-
- # ************************************ SERVER Object
- define_obj :server
- def_attr_mapping :private_ip_addresses, [:addresses, '{key}', [:addr]]
- def_attr_mapping :meta_data, :metadata
- def_attr_mapping :status, :state
- attr_value_mapping :create, 'BUILD'
- attr_value_mapping :boot, :boot
- attr_value_mapping :active, 'ACTIVE'
- attr_value_mapping :error, 'ERROR'
- attr_value_mapping :shutdown, 'SHUTOFF'
-
- # ************************************ SERVER log Object
- define_obj :server_log
-
- # Excon::Response object type
- def_attr_mapping :output, 'output'
-
- # ************************************* Public IP Object
- define_obj :public_ip
- def_attr_mapping :server_id, :instance_id
- def_attr_mapping :public_ip, :ip
-
- # defines setup Cloud data (:account => true for setup)
- define_data('credentials#account_id',
- :account => true,
- :desc => 'HPCloud Access Key (From horizon, user drop down, '\
- 'manage keys)',
- :validate => /^[A-Z0-9]*$/
- )
- define_data('credentials#account_key',
- :account => true,
- :desc => 'HPCloud secret Key (From horizon, user drop down, '\
- 'manage keys)',
- :encrypted => false,
- :validate => /^.+/
- )
- define_data('credentials#auth_uri',
- :account => true,
- :desc => 'HPCloud Authentication service URL (default is HP '\
- 'Public cloud)',
- :validate => %r{^http(s)?://.*$},
- :default_value => 'https://region-a.geo-1.identity.hpcloudsvc'\
- '.com:35357/v2.0/'
- )
- define_data('credentials#tenant',
- :account => true,
- :desc => 'HPCloud Tenant ID (from horizon, identity, projecs,'\
- ' Project ID)',
- :validate => /^[0-9]+$/
- )
-
- define_data('services#compute',
- :account => true,
- :desc => 'HPCloud Compute service zone (Ex: region-a.geo-1)',
- :list_values => {
- :query_type => :controller_call,
- :object => :services,
- :query_call => :get_services,
- :query_params => { :list_services => [:Compute, :compute] },
- :validate => :list_strict
- }
- )
-
- define_data('services#network',
- :account => true,
- :desc => 'HPCloud Network service zone (Ex: region-a.geo-1)',
- :list_values => {
- :query_type => :controller_call,
- :object => :services,
- :query_call => :get_services,
- :query_params => { :list_services => [:Networking, :network] },
- :validate => :list_strict
- }
- )
-
- data_value_mapping 'xsmall', 'standard.xsmall'
- data_value_mapping 'small', 'standard.small'
- data_value_mapping 'medium', 'standard.medium'
- data_value_mapping 'large', 'standard.large'
- data_value_mapping 'xlarge', 'standard.xlarge'
-end
-
# Following class describe how FORJ should handle HP Cloud objects.
# Except Cloud connection, all HPCloud objects management are described/called
# in HP* modules.
class HpcloudController # rubocop: disable Metrics/ClassLength
def connect(sObjectType, hParams)
@@ -280,54 +130,10 @@
else
controller_error "'%s' is not a valid object for 'create'", sObjectType
end
end
- # This function return a collection which have to provide:
- # functions: [], length, each
- # Used by network process.
- 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
# rubocop: enable CyclomaticComplexity, MethodLength
def delete(sObjectType, hParams)
case sObjectType
when :network
@@ -387,37 +193,38 @@
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
- def _server_metadata_get(oControlerObject)
+ def _server_metadata_get(oControlerObject, key)
+ return [false, nil] unless key == :metadata
ret = {}
oControlerObject.metadata.each do |m|
k = m.attributes[:key]
v = m.attributes[:value]
ret[k] = v
end
- ret
+ [true, ret]
end
def _get_instance_attr(oControlerObject, key)
- return _server_metadata_get(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])
+ found, ret = _server_metadata_get(oControlerObject, key[0])
+ found, ret = _get_from(oControlerObject, key[0]) unless found
+
+ unless found
+ Lorj.debug(4, "Unable to get '%s' from '%s'. Attribute inexistent.",
+ key[0], oControlerObject.class)
+ return nil
+ end
+
+ return ret if key.length == 1 || !ret.is_a?(Hash)
+ ret.rh_get(key[1..-1])
end
def set_attr(oControlerObject, key, value)
controller_class = oControlerObject.class