Class: QingStor::SDK::Config
- Inherits:
-
Hash
- Object
- Hash
- QingStor::SDK::Config
- Defined in:
- lib/qingstor/sdk/general/config.rb
Constant Summary collapse
- DEFAULT_AS_HASH =
{ host: 'qingstor.com'.freeze, port: 443, protocol: 'https'.freeze, connection_retries: 3, log_level: 'warn'.freeze, enable_virtual_host_style: false }
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Class Method Summary collapse
Instance Method Summary collapse
- #check ⇒ Object
-
#initialize(initial_config = {}) ⇒ Config
constructor
A new instance of Config.
- #load_config_from_file(path) ⇒ Object
- #load_default_config ⇒ Object
- #load_env_config ⇒ Object
- #load_user_config ⇒ Object
- #update(another_config = {}) ⇒ Object
Constructor Details
#initialize(initial_config = {}) ⇒ Config
Returns a new instance of Config.
49 50 51 52 53 54 55 56 57 58 59 |
# 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 |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
30 31 32 |
# File 'lib/qingstor/sdk/general/config.rb', line 30 def connection @connection end |
Class Method Details
Instance Method Details
#check ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# 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) ⇒ Object
152 153 154 155 |
# 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 ⇒ Object
119 120 121 |
# File 'lib/qingstor/sdk/general/config.rb', line 119 def load_default_config update DEFAULT_AS_HASH end |
#load_env_config ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# 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 ⇒ Object
123 124 125 126 127 128 129 |
# 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 |