lib/onering/api.rb in onering-client-0.0.77 vs lib/onering/api.rb in onering-client-0.0.78

- old
+ new

@@ -14,10 +14,11 @@ require 'yaml' require 'hashlib' require 'deep_merge' require 'addressable/uri' require 'httparty' +require 'onering/config' module Onering class API module Actions class Retry < ::Exception; end @@ -41,26 +42,23 @@ include ::HTTParty attr_accessor :url format :json - DEFAULT_CONFIG={} DEFAULT_BASE="https://onering" DEFAULT_PATH="/api" - DEFAULT_OPTIONS_FILE=["~/.onering/cli.yml", "/etc/onering/cli.yml"] DEFAULT_CLIENT_PEM=["~/.onering/client.pem", "/etc/onering/client.pem"] DEFAULT_CLIENT_KEY=["~/.onering/client.key", "/etc/onering/client.key"] DEFAULT_VALIDATION_PEM="/etc/onering/validation.pem" def initialize(options={}) - @_config = {} @_plugins = {} @_connection_options = options # load and merge all config file sources - _load_config(@_connection_options[:configfile], @_connection_options.get(:config, {})) + Onering::Config.load(@_connection_options[:configfile], @_connection_options.get(:config, {})) # source interface specified # !! HAX !! HAX !! HAX !! HAX !! HAX !! HAX !! HAX !! HAX !! HAX !! HAX !! # Due to certain versions of Ruby's Net::HTTP not allowing you explicitly # specify the source IP/interface to use, this horrific monkey patch is @@ -89,11 +87,11 @@ raise "Invalid source IP address #{options.get('config.source')}" end end # set API connectivity details - Onering::API.base_uri @_config.get(:url, DEFAULT_BASE) + Onering::API.base_uri Onering::Config.get(:url, DEFAULT_BASE) # add default parameters options.get('config.params',{}).each do |k,v| _default_param(k,v) end @@ -109,11 +107,11 @@ return self end def request(method, endpoint, options={}) - endpoint = [@_config.get(:path, DEFAULT_PATH).strip, endpoint.sub(/^\//,'')].join('/') + endpoint = [Onering::Config.get(:path, DEFAULT_PATH).strip, endpoint.sub(/^\//,'')].join('/') case (method.to_sym rescue method) when :post rv = Onering::API.post(endpoint, options) when :put @@ -189,51 +187,17 @@ else super end end - def opt(name, default=nil) - @_config.get(name, default) - end - - def status() Onering::API.get("/").parsed_response end - - - private # ----------------------------------------------------------------------------- - def _load_config(configfile, config={}) - if configfile.nil? - configfile = [] - else - # recursively grab all .yml files if directory is specified - configfile = (File.directory?(configfile) ? Dir.glob(File.join(configfile, "**", "*.yml")).sort : [configfile]) - end - - # list all existing config files from least specific to most - @_configfiles = (configfile + DEFAULT_OPTIONS_FILE).compact.select{|i| - (File.exists?(File.expand_path(i)) rescue false) - }.reverse - - # merge all config files with more-specific values overriding less-specific ones - @_config = DEFAULT_CONFIG - @_configfiles.each do |i| - c = YAML.load(File.read(File.expand_path(i))) rescue {} - @_config.deep_merge!(c) - end - - # settings specified in the library override everything - @_config.deep_merge!(config.compact) unless config.empty? - end - - -# ----------------------------------------------------------------------------- def _setup_auth() - type = @_config.get('authentication.type', :auto) + type = Onering::Config.get('authentication.type', :auto) case type.to_sym when :token _setup_auth_token() @@ -251,18 +215,18 @@ # ----------------------------------------------------------------------------- def _setup_auth_ssl() begin # get first keyfile found - key = (([@_config.get('authentication.keyfile')] + DEFAULT_CLIENT_PEM).compact.select{|i| + key = (([Onering::Config.get('authentication.keyfile')] + DEFAULT_CLIENT_PEM).compact.select{|i| (File.exists?(File.expand_path(i)) rescue false) }).first # SSL client key not found, attempt autoregistration... if key.nil? - if @_config.get('authentication.autoregister', true) - validation_key = @_config.get('authentication.validation_keyfile', DEFAULT_VALIDATION_PEM) + if Onering::Config.get('authentication.autoregister', true) + validation_key = Onering::Config.get('authentication.validation_keyfile', DEFAULT_VALIDATION_PEM) validation_key = (File.expand_path(validation_key) rescue validation_key) # if validation key exists, autoregister if File.size?(validation_key) # set the authentication PEM to validation.pem @@ -326,10 +290,10 @@ # ----------------------------------------------------------------------------- def _setup_auth_token() # get first keyfile found - key = @_config.get('authentication.key') + key = Onering::Config.get('authentication.key') raise Errors::AuthenticationMissing.new("Cannot find an API token") if key.nil? # set auth mechanism Onering::API.headers({ 'X-Auth-Mechanism' => 'token' \ No newline at end of file