6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/cloudstack_client/configuration.rb', line 6
def self.load(configuration)
file = configuration[:config_file] || Configuration.locate_config_file
unless File.exist?(file)
raise ConfigurationError, "Configuration file '#{file}' not found."
end
begin
config = YAML.safe_load(IO.read(file), permitted_classes: [Symbol])
rescue => e
message = "Can't load configuration from file '#{file}'."
if configuration[:debug]
message += "\nMessage: #{e.message}"
message += "\nBacktrace:\n\t#{e.backtrace.join("\n\t")}"
end
raise ConfigurationError, message
end
unless config.is_a?(Hash)
raise ConfigurationError, "Configuration file '#{file}' must contain a hash."
end
if env = configuration[:env] || config[:default]
unless config = config[env]
raise ConfigurationError, "Can't find environment #{env}."
end
end
unless config.is_a?(Hash)
raise ConfigurationError, "Environment #{env} must contain a hash."
end
required_keys = %i[url api_key secret_key]
missing_keys = required_keys.reject do |key|
config.key?(key) && !config[key].to_s.strip.empty?
end
unless missing_keys.empty?
message = "The environment #{env || '\'-\''} does not contain all required keys."
raise ConfigurationError, message
end
config.merge(environment: env)
end
|