#!/usr/bin/env ruby # encoding: UTF-8 # (c) Copyright 2014 Hewlett-Packard Development Company, L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require 'rubygems' require 'fog' require 'require_relative' require_relative 'yaml_parse.rb' include YamlParse # # Connection module # class ForjConnection attr_accessor :oCompute attr_accessor :oNetwork def initialize(oConfig) sAccountName = oConfig.get('account_name') @provider='HP' # TODO: Support multiple provider. (Generic Provider object required) sAccountName = oConfig.get('provider') if not sAccountName sAccountName = 'hpcloud' if not sAccountName @credentials = get_credentials(sAccountName) oSSLError=SSLErrorMgt.new # Trying to get Compute 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'], :hp_secret_key => @credentials['secret_key'], :hp_auth_uri => @credentials['auth_uri'], :hp_tenant_id => @credentials['tenant_id'], :hp_avl_zone => @credentials['availability_zone'], :version => 'v2' }) rescue => e if not oSSLError.ErrorDetected(e.message,e.backtrace) retry end Logging.fatal(1, 'Unable to connect.') end # Trying to get Network 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'], :hp_auth_uri => @credentials['auth_uri'], :hp_tenant_id => @credentials['tenant_id'], :hp_avl_zone => @credentials['availability_zone'] }) rescue => e if not oSSLError.ErrorDetected(e.message,e.backtrace) retry end Logging.fatal(1, 'Unable to connect.') end end end def get_credentials(sAccountName) # TODO: Should support forj credentials. not hpcloud credentials. raise 'Internal Error: Missing sAccountName' if not sAccountName 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