Sha256: db69ab01572aadca4a3c7c7d6ff2e2ac983a2b16e70444bc912edfd4d845092c

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'yao'
require 'yaml'

module Kaname
  class Config
    @@username = String.new

    def self.setup
      load_config unless envs_exist?
      setup_yao
    end

    def self.username
      @@username
    end

    private

    def self.envs_exist?
      %w[OS_AUTH_URL OS_TENANT_NAME OS_USERNAME OS_PASSWORD OS_CERT OS_KEY OS_REGION_NAME].any?{|k|ENV[k]}
    end

    def self.load_config
      config_file = File.join(Dir.home, '.kaname')
      raise '~/.kaname is missing' unless File.exists?(config_file)

      config = YAML.load_file(config_file)

      %w[auth_url tenant username password].each do |conf_item|
        raise "Configuration '#{conf_item}' is missing. Check your ~/.kaname" unless config[conf_item]
      end

      @@auth_url       = config['auth_url']
      @@tenant         = config['tenant']
      @@username       = config['username']
      @@password       = config['password']
      @@client_cert    = config['client_cert']
      @@client_key     = config['client_key']
      @@region_name    = config['region_name']
      true
    end

    def self.setup_yao
      Yao.configure do
        auth_url    (ENV['OS_AUTH_URL']    || @@auth_url)
        tenant_name (ENV['OS_TENANT_NAME'] || @@tenant)
        username    (ENV['OS_USERNAME']    || @@username)
        password    (ENV['OS_PASSWORD']    || @@password)
        client_cert (ENV['OS_CERT']        || @@client_cert)
        client_key  (ENV['OS_KEY']         || @@client_key)
        region_name (ENV['OS_REGION_NAME'] || @@region_name)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kaname-0.6.3 lib/kaname/config.rb