class QingStor::SDK::Config

Constants

DEFAULT_AS_HASH

Attributes

connection[RW]

Public Class Methods

init(access_key_id, secret_access_key) click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 41
def self.init(access_key_id, secret_access_key)
  initial_config = {
    access_key_id:     access_key_id,
    secret_access_key: secret_access_key
  }
  Config.new(initial_config)
end
new(initial_config = {}) click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 49
def initialize(initial_config = {})
  self.connection = Net::HTTP::Persistent.new
  # load default config as basic
  load_default_config
  # load from config, env path superior to ~/.qingstor/config.yaml
  load_user_config_path_exist
  # load envs, cover corresponding config if env exists
  load_env_config
  # cover by user's param
  update initial_config
end

Private Class Methods

is_default?(key, value) click to toggle source

@return boolean

# File lib/qingstor/sdk/general/config.rb, line 181
def self.is_default?(key, value)
  DEFAULT_AS_HASH[key.to_sym].present? && DEFAULT_AS_HASH[key.to_sym] == value
end

Public Instance Methods

check() click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 68
def check
  if self[:access_key_id].blank? && self[:secret_access_key].present? ||
     self[:access_key_id].present? && self[:secret_access_key].blank?
    raise ConfigurationError, 'ak and sk should be both both empty or not empty'
  end

  if self[:access_key_id].blank? && self[:secret_access_key].blank?
    Logger.warn 'Both ak and sk not configured, will call api as anonymous user'
  end

  # check endpoint and host/port/protocol
  if self[:endpoint].blank?
    # host/port/protocol must set if endpoint not set
    %i[host port protocol].each do |x|
      if self[x].blank?
        raise ConfigurationError, "#{x.to_sym} not specified"
      end
    end
  else
    # if endpoint set, host/port/protocol ignore, and warn
    %i[host port protocol].each do |x|
      if self[x].present? && !Config.is_default?(x, self[x])
        Logger.warn "Endpoint configured, #{x.to_sym} will be ignored"
      end
    end
  end

  # add ip check for vhost enabled
  if self[:enable_virtual_host_style]
    if self[:endpoint].present?
      uri = Preprocessor.parse_endpoint self[:endpoint]
      ip = uri.host
    else
      ip = self[:host]
    end
    if is_valid_ip? ip
      raise ConfigurationError, 'ip host not allowed if vhost enabled'
    end
  end

  if self[:additional_user_agent].present?
    self[:additional_user_agent].each_byte do |x|
      # Allow space(32) to ~(126) in ASCII Table, exclude "(34).
      if x < 32 || x > 126 || x == 32 || x == 34
        raise ConfigurationError, 'additional User-Agent contains characters that not allowed'
      end
    end
  end
  self
end
load_config_from_file(path) click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 152
def load_config_from_file(path)
  path = path.sub '~', Dir.home if path.start_with? '~/'
  update YAML.load_file File.absolute_path path
end
load_default_config() click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 119
def load_default_config
  update DEFAULT_AS_HASH
end
load_env_config() click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 131
def load_env_config
  another_config = {}
  unless ENV[Contract::ENV_ACCESS_KEY_ID].nil?
    another_config[:access_key_id] =
      ENV[Contract::ENV_ACCESS_KEY_ID]
  end
  unless ENV[Contract::ENV_SECRET_ACCESS_KEY].nil?
    another_config[:secret_access_key] =
      ENV[Contract::ENV_SECRET_ACCESS_KEY]
  end
  unless ENV[Contract::ENV_ENABLE_VIRTUAL_HOST_STYLE].nil?
    another_config[:enable_virtual_host_style] =
      ENV[Contract::ENV_ENABLE_VIRTUAL_HOST_STYLE]
  end
  unless ENV[Contract::ENV_ENDPOINT].nil?
    another_config[:endpoint] =
      ENV[Contract::ENV_ENDPOINT]
  end
  update another_config
end
load_user_config() click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 123
def load_user_config
  if !ENV[Contract::ENV_CONFIG_PATH].nil?
    load_config_from_file ENV[Contract::ENV_CONFIG_PATH]
  else
    load_config_from_file Contract::USER_CONFIG_FILEPATH
  end
end
update(another_config = {}) click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 61
def update(another_config = {})
  deep_merge! another_config.deep_symbolize_keys!
  parse_boolean(:enable_virtual_host_style)
  Logger.set_level self[:log_level]
  self
end

Private Instance Methods

is_valid_ip?(ip) click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 173
def is_valid_ip?(ip)
  IPAddr.new ip
  true
rescue
  false
end
load_user_config_path_exist() click to toggle source

load user config if path exist, and skip check

# File lib/qingstor/sdk/general/config.rb, line 160
def load_user_config_path_exist
  # if env path configured, update from env; otherwise, if ~/.qingstor/config.yaml exists, update from this
  if !ENV[Contract::ENV_CONFIG_PATH].nil? && File.exist?(ENV[Contract::ENV_CONFIG_PATH])
    load_config_from_file ENV[Contract::ENV_CONFIG_PATH]
  elsif File.exist? Contract::USER_CONFIG_FILEPATH
    load_config_from_file Contract::USER_CONFIG_FILEPATH
  end
end
parse_boolean(key) click to toggle source
# File lib/qingstor/sdk/general/config.rb, line 169
def parse_boolean(key)
  self[key.to_sym] = self[key.to_sym].to_s.downcase == 'true'
end