lib/connection.rb in forj-0.0.36 vs lib/connection.rb in forj-0.0.37

- old
+ new

@@ -30,22 +30,34 @@ attr_accessor :oCompute attr_accessor :oNetwork attr_accessor :sAccountName - def initialize(oConfig) + def initialize(oConfig, bAutoConnect = true) - @sAccountName = oConfig.get('account_name') + Logging.fatal(1, 'Internal Error: Missing global $HPC_ACCOUNTS') if not $HPC_ACCOUNTS + + @oConfig = oConfig + @sAccountName = @oConfig.get('account_name') @provider='HP' # TODO: Support multiple provider. (Generic Provider object required) - @sAccountName = oConfig.get('provider') if not @sAccountName + @sAccountName = @oConfig.get('provider') if not @sAccountName @sAccountName = 'hpcloud' if not @sAccountName - @credentials = get_credentials(@sAccountName) - oSSLError=SSLErrorMgt.new + @credentials = get_credentials() # Trying to get Compute object + compute_connect if bAutoConnect + # Trying to get Network object + network_connect if bAutoConnect + + end + + def compute_connect + + oSSLError=SSLErrorMgt.new # Retry object + Logging.debug("compute: Connecting to '%s' - Project '%s'" % [@provider, @credentials['tenant_id']]) begin @oCompute=Fog::Compute.new({ :provider => @provider, :hp_access_key => @credentials['access_key'], @@ -57,14 +69,17 @@ }) rescue => e if not oSSLError.ErrorDetected(e.message,e.backtrace) retry end - Logging.fatal(1, 'Unable to connect.') + Logging.fatal(1, 'Compute: Unable to connect.') end - + end + + def network_connect # Trying to get Network object + oSSLError=SSLErrorMgt.new # Retry object Logging.debug("HP network: Connecting to '%s' - Project '%s'" % [@provider, @credentials['tenant_id']]) begin @oNetwork=Fog::HP::Network.new({ :hp_access_key => @credentials['access_key'], :hp_secret_key => @credentials['secret_key'], @@ -74,36 +89,35 @@ }) rescue => e if not oSSLError.ErrorDetected(e.message,e.backtrace) retry end - Logging.fatal(1, 'Unable to connect.') + Logging.fatal(1, 'Network: Unable to connect.') end end -end + def get_credentials() + # TODO: Should support forj credentials. not hpcloud credentials. -def get_credentials(sAccountName) - # TODO: Should support forj credentials. not hpcloud credentials. + creds = File.join($HPC_ACCOUNTS, @sAccountName) + if not File.exists?(creds) + Logging.fatal(1, "'%s' was not configured. Did you executed 'forj setup %s'? Please do it and retry." % [@sAccountName, @sAccountName]) + end + @oConfig.ExtraLoad(creds, :hpc_accounts, @sAccountName) - Logging.fatal(1, 'Internal Error: Missing sAccountName') if not sAccountName + template = @oConfig.ExtraGet(:hpc_accounts, @sAccountName) + credentials = {} + begin + credentials['access_key'] = template[:credentials][:account_id] + credentials['secret_key'] = template[:credentials][:secret_key] + credentials['auth_uri'] = template[:credentials][:auth_uri] + credentials['tenant_id'] = template[:credentials][:tenant_id] + credentials['availability_zone'] = template[:regions][:compute] + rescue => e + Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")]) + puts 'your credentials are not configured, delete the file %s and run forj setup again' % [creds] + end + credentials + end - creds = File.expand_path('~/.hpcloud/accounts/%s' % [sAccountName]) - if not File.exists?(creds) - Logging.fatal(1, "'%s' was not configured. Did you executed 'forj setup %s'? Please do it and retry." % [sAccountName, sAccountName]) - end - template = YAML.load_file(creds) - credentials = Hash.new - - begin - credentials['access_key'] = template[:credentials][:account_id] - credentials['secret_key'] = template[:credentials][:secret_key] - credentials['auth_uri'] = template[:credentials][:auth_uri] - credentials['tenant_id'] = template[:credentials][:tenant_id] - credentials['availability_zone'] = template[:regions][:compute] - rescue => e - Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")]) - puts 'your credentials are not configured, delete the file %s and run forj setup again' % [creds] - end - credentials end