Skip to content

Commit d5e6b4c

Browse files
committed
Merge pull request #96 from ju2wheels/sl_config
Cleanup Config formatting, add more client properties and normalize thei...
2 parents cc6dbef + d50fde7 commit d5e6b4c

2 files changed

Lines changed: 105 additions & 48 deletions

File tree

lib/softlayer/Config.rb

Lines changed: 103 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ module SoftLayer
3636
# SoftLayer-Python language bindings). A simple config file looks something like this:
3737
#
3838
# [softlayer]
39-
# username = joeusername
4039
# api_key = DEADBEEFBADF00D
40+
# endpoint_url = 'API_PUBLIC_ENDPOINT'
4141
# timeout = 60
42+
# user_agent = "softlayer-ruby x.x.x"
43+
# username = joeusername
4244
#
4345
# = Environment Variables
4446
#
@@ -52,70 +54,124 @@ module SoftLayer
5254
#
5355
# - +$SL_API_USERNAME+
5456
# - +$SL_API_KEY+
55-
# - +$SL_API_BASE_URL+
57+
# - +$SL_API_BASE_URL+ (or alias +$SL_API_ENDPOINT_URL+)
58+
#
59+
# = XML RPC Variables
60+
#
61+
# The config allows for two variables that are passed on to the underlying XML RPC agent
62+
# for interacting with the SoftLayer API (as with other settings these can be loaded from
63+
# config file, environment variables, globals or provided values):
64+
#
65+
# - +SL_API_TIMEOUT+
66+
# - +SL_API_USER_AGENT+
5667
#
5768
# = Direct parameters
5869
#
5970
# Finally, the Config.client_settings routine accepts a hash of arguments. If any
6071
# of the key information is provided in that hash, that information will override
6172
# any discovered through the techniques above.
6273
#
74+
class Config
75+
ENDPOINT_URL_ALIAS = [ 'API_PRIVATE_ENDPOINT', 'API_PUBLIC_ENDPOINT' ]
76+
FILE_LOCATIONS = [ '/etc/softlayer.conf', '~/.softlayer', './.softlayer' ]
77+
78+
def Config.client_settings(provided_settings = {})
79+
settings = { :endpoint_url => API_PUBLIC_ENDPOINT }
80+
81+
settings.merge! file_settings
82+
settings.merge! environment_settings
83+
settings.merge! globals_settings
84+
settings.merge! provided_settings
85+
86+
settings
87+
end
6388

64-
class Config
65-
def Config.globals_settings
66-
result = {}
67-
result[:username] = $SL_API_USERNAME if $SL_API_USERNAME
68-
result[:api_key] = $SL_API_KEY if $SL_API_KEY
69-
result[:endpoint_url] = $SL_API_BASE_URL || API_PUBLIC_ENDPOINT
70-
result
71-
end
89+
def Config.environment_settings
90+
result = {}
7291

73-
def Config.environment_settings
74-
result = {}
75-
result[:username] = ENV['SL_USERNAME'] if ENV['SL_USERNAME']
76-
result[:api_key] = ENV['SL_API_KEY'] if ENV['SL_API_KEY']
77-
result
78-
end
92+
result[:api_key] = ENV['SL_API_KEY'] if ENV['SL_API_KEY']
93+
result[:user_agent] = ENV['SL_API_USER_AGENT'] || "softlayer_api gem/#{SoftLayer::VERSION} (Ruby #{RUBY_PLATFORM}/#{RUBY_VERSION})"
94+
result[:username] = ENV['SL_USERNAME'] if ENV['SL_USERNAME']
7995

80-
FILE_LOCATIONS = ['/etc/softlayer.conf', '~/.softlayer', './.softlayer']
96+
if ENV['SL_API_BASE_URL'] && ENDPOINT_URL_ALIAS.include?(ENV['SL_API_BASE_URL'])
97+
result[:endpoint_url] = (ENV["SL_API_BASE_URL"] == "API_PUBLIC_ENDPOINT" ? API_PUBLIC_ENDPOINT : API_PRIVATE_ENDPOINT)
98+
elsif ENV['SL_API_ENDPOINT_URL'] && ENDPOINT_URL_ALIAS.include?(ENV['SL_API_ENDPOINT_URL'])
99+
result[:endpoint_url] = (ENV["SL_API_ENDPOINT_URL"] == "API_PUBLIC_ENDPOINT" ? API_PUBLIC_ENDPOINT : API_PRIVATE_ENDPOINT)
100+
elsif (ENV['SL_API_BASE_URL'] && ! ENDPOINT_URL_ALIAS.include?(ENV['SL_API_BASE_URL'])) ||
101+
(ENV['SL_API_ENDPOINT_URL'] && ! ENDPOINT_URL_ALIAS.include?(ENV['SL_API_ENDPOINT_URL']))
102+
result[:endpoint_url] = ENV['SL_API_BASE_URL'] || ENV['SL_API_ENDPOINT_URL']
103+
end
81104

82-
def Config.file_settings(*additional_files)
83-
result = {}
105+
begin
106+
result[:timeout] = Integer(ENV['SL_API_TIMEOUT']) if ENV['SL_API_TIMEOUT']
107+
rescue => integer_parse_exception
108+
raise "Expected the value of the timeout configuration property, '#{ENV['SL_API_TIMEOUT']}', to be parseable as an integer"
109+
end
84110

85-
search_path = FILE_LOCATIONS
86-
search_path = search_path + additional_files if additional_files
87-
search_path = search_path.map { |file_path| File.expand_path(file_path) }
111+
result
112+
end
88113

89-
search_path.each do |file_path|
90-
if File.readable? file_path
91-
config = ConfigParser.new file_path
92-
softlayer_section = config['softlayer']
114+
def Config.file_settings(*additional_files)
115+
result = {}
93116

94-
if softlayer_section
95-
result[:username] = softlayer_section['username'] if softlayer_section['username']
96-
result[:endpoint_url] = softlayer_section['endpoint_url'] if softlayer_section['endpoint_url']
97-
result[:api_key] = softlayer_section['api_key'] if softlayer_section['api_key']
117+
search_path = FILE_LOCATIONS
118+
search_path = search_path + additional_files if additional_files
119+
search_path = search_path.map { |file_path| File.expand_path(file_path) }
120+
121+
search_path.each do |file_path|
122+
if File.readable? file_path
123+
config = ConfigParser.new file_path
124+
softlayer_section = config['softlayer']
125+
126+
if softlayer_section
127+
result[:api_key] = softlayer_section['api_key'] if softlayer_section['api_key']
128+
result[:user_agent] = softlayer_section['user_agent'] || "softlayer_api gem/#{SoftLayer::VERSION} (Ruby #{RUBY_PLATFORM}/#{RUBY_VERSION})"
129+
result[:username] = softlayer_section['username'] if softlayer_section['username']
130+
131+
if softlayer_section['base_url'] && ENDPOINT_URL_ALIAS.include?(softlayer_section['base_url'])
132+
result[:endpoint_url] = (softlayer_section['base_url'] == "API_PUBLIC_ENDPOINT" ? API_PUBLIC_ENDPOINT : API_PRIVATE_ENDPOINT)
133+
elsif softlayer_section['endpoint_url'] && ENDPOINT_URL_ALIAS.include?(softlayer_section['endpoint_url'])
134+
result[:endpoint_url] = (softlayer_section['endpoint_url'] == "API_PUBLIC_ENDPOINT" ? API_PUBLIC_ENDPOINT : API_PRIVATE_ENDPOINT)
135+
elsif (softlayer_section['base_url'] && ! ENDPOINT_URL_ALIAS.include?(softlayer_section['base_url'])) ||
136+
(softlayer_section['endpoint_url'] && ! ENDPOINT_URL_ALIAS.include?(softlayer_section['endpoint_url']))
137+
result[:endpoint_url] = softlayer_section['base_url'] || softlayer_section['endpoint_url']
138+
end
98139

99140
begin
100-
result[:timeout] = Integer(softlayer_section['timeout']) if softlayer_section['timeout']
141+
result[:timeout] = Integer(softlayer_section['timeout']) if softlayer_section['timeout']
101142
rescue => integer_parse_exception
102-
$stderr.puts "Expected the value of the timeout configuration property, '#{result[:timeout]}', to be parseable as an integer"
143+
raise "Expected the value of the timeout configuration property, '#{softlayer_section['timeout']}', to be parseable as an integer"
103144
end
104-
end
105-
end
106-
end
145+
end
146+
end
147+
end
107148

108-
result
109-
end
149+
result
150+
end
110151

111-
def Config.client_settings(provided_settings = {})
112-
settings = { :endpoint_url => API_PUBLIC_ENDPOINT }
113-
settings.merge! file_settings
114-
settings.merge! environment_settings
115-
settings.merge! globals_settings
116-
settings.merge! provided_settings
117-
118-
settings
119-
end
120-
end
152+
def Config.globals_settings
153+
result = {}
154+
155+
result[:api_key] = $SL_API_KEY if $SL_API_KEY
156+
result[:user_agent] = $SL_API_USER_AGENT || "softlayer_api gem/#{SoftLayer::VERSION} (Ruby #{RUBY_PLATFORM}/#{RUBY_VERSION})"
157+
result[:username] = $SL_API_USERNAME if $SL_API_USERNAME
158+
159+
if $SL_API_ENDPOINT_URL && ENDPOINT_URL_ALIAS.include?($SL_API_ENDPOINT_URL)
160+
result[:endpoint_url] = ($SL_API_ENDPOINT_URL == "API_PUBLIC_ENDPOINT" ? API_PUBLIC_ENDPOINT : API_PRIVATE_ENDPOINT)
161+
elsif $SL_API_BASE_URL && ENDPOINT_URL_ALIAS.include?($SL_API_BASE_URL)
162+
result[:endpoint_url] = ($SL_API_BASE_URL == "API_PUBLIC_ENDPOINT" ? API_PUBLIC_ENDPOINT : API_PRIVATE_ENDPOINT)
163+
elsif ($SL_API_BASE_URL && ! ENDPOINT_URL_ALIAS.include?($SL_API_BASE_URL)) ||
164+
($SL_API_ENDPOINT_URL && ! ENDPOINT_URL_ALIAS.include?($SL_API_ENDPOINT_URL))
165+
result[:endpoint_url] = $SL_API_ENDPOINT_URL || $SL_API_BASE_URL
166+
end
167+
168+
begin
169+
result[:timeout] = Integer($SL_API_TIMEOUT) if $SL_API_TIMEOUT
170+
rescue => integer_parse_exception
171+
raise "Expected the value of the timeout configuration property, '#{$SL_API_TIMEOUT}', to be parseable as an integer"
172+
end
173+
174+
result
175+
end
176+
end
121177
end

spec/Config_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
it "retrieves config information from environment variables" do
1717
ENV.store("SL_USERNAME", "PoohBear")
1818
ENV.store("SL_API_KEY", "DEADBEEFBADF00D")
19+
ENV.store("SL_API_USER_AGENT", "de trackerz")
1920

20-
expect(SoftLayer::Config.environment_settings).to eq({ :username => "PoohBear", :api_key => "DEADBEEFBADF00D" })
21+
expect(SoftLayer::Config.environment_settings).to eq({ :user_agent => "de trackerz", :username => "PoohBear", :api_key => "DEADBEEFBADF00D" })
2122
end
2223

2324
it "retrieves the properties from a custom file" do

0 commit comments

Comments
 (0)