vagrant-softlayer 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,329 +1,339 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'csv'
4
- require 'getoptlong'
5
- require 'json'
6
- require 'pp'
7
-
8
- require 'rubygems'
9
- require 'softlayer_api'
10
-
11
- class SoftLayerVlans
12
- def initialize(sl_api_key, sl_username, sl_endpoint_url = SoftLayer::API_PUBLIC_ENDPOINT)
13
- if ! [ SoftLayer::API_PUBLIC_ENDPOINT, SoftLayer::API_PRIVATE_ENDPOINT ].include?(sl_endpoint_url)
14
- raise ArgumentError, "Invalid SoftLayer Endpoint URL provided, expected one of #{ [ SoftLayer::API_PUBLIC_ENDPOINT, SoftLayer::API_PRIVATE_ENDPOINT ].inspect }: #{ sl_endpoint_url.inspect }"
15
- end
16
-
17
- @sl_credentials = {
18
- :api_key => sl_api_key,
19
- :endpoint_url => sl_endpoint_url,
20
- :username => sl_username
21
- }
22
-
23
- begin
24
- @sl_services = {
25
- :account => SoftLayer::Service.new("SoftLayer_Account", @sl_credentials)
26
- }
27
- rescue Exception => e
28
- raise Exception, "Failed to initialize SoftLayer Account service for retrieving network vlan details: #{ e.message }"
29
- end
30
- end
31
-
32
- def [](vlan_name)
33
- vlan_details = vlans(:vlan_name => vlan_name)
34
-
35
- raise KeyError, "Invalid vlan name provided as a SoftLayerVlans key: #{ vlan_name.inspect }" if vlan_details.empty?
36
-
37
- return vlan_details[0]
38
- end
39
-
40
- def has_vlan?(vlan_name)
41
- vlan_details = vlans(:vlan_name => vlan_name)
42
-
43
- return ! vlan_details.empty?
44
- end
45
-
46
- def vlans(filter = { :datacenter_name => nil, :vlan_name => nil, :vlan_space => nil })
47
- vlan_details = []
48
-
49
- if filter.class != Hash
50
- raise TypeError, "Invalid value specified for filter, expected hash of filter values #{ { :datacenter_name => nil, :vlan_name => nil, :vlan_space => nil }.inspect }: #{ filter.inspect }"
51
- end
52
-
53
- if ! filter.keys.select{|vlan_property| ! [ :datacenter_name, :vlan_name, :vlan_space ].include?(vlan_property)}.empty?
54
- raise KeyError, "Invalid vlan property used as filter key, expected one of #{ [ :datacenter_name, :vlan_name, :vlan_space ].inspect }: #{ filter.keys.inspect }"
55
- end
56
-
57
- if filter.has_key?(:datacenter_name) && ! filter[:datacenter_name].nil? && ! [Array, String].include?(filter[:datacenter_name].class)
58
- raise TypeError, "Invalid type for filter key :datacenter_name, must be a Array or String of datacenter name(s): #{ filter[:datacenter_name].class }"
59
- end
60
-
61
- if filter.has_key?(:vlan_name) && ! filter[:vlan_name].nil? && ! [ Array, String ].include?(filter[:vlan_name].class)
62
- raise TypeError, "Invalid type for filter key :vlan_name, must be a Array or String of vlan name(s): #{ filter[:vlan_name].class }"
63
- end
64
-
65
- if filter.has_key?(:vlan_space) && ! filter[:vlan_space].nil? && ! [ nil, :public, :private ].include?(filter[:vlan_space])
66
- raise ArgumentError, "Invalid value for filter key :vlan_space, expected one of #{ [ nil, :public, :private ].inspect }: #{ filter[:vlan_space].inspect }"
67
- end
68
-
69
- routers = @sl_services[:account].object_mask("mask[routers,routers.datacenter,routers.networkVlans,routers.networkVlans.networkSpace,routers.networkVlans.type]").getObject["routers"]
70
-
71
- routers.each do |router|
72
- router["networkVlans"].delete_if {|vlan| ! vlan.has_key?("name") || vlan["type"]["keyName"] != "STANDARD"}
73
-
74
- router["networkVlans"].each do |vlan|
75
- vlan_details.push({
76
- :datacenter => router["datacenter"]["name"],
77
- :id => vlan["id"],
78
- :name => vlan["name"],
79
- :qualified_name => [ router["hostname"].split('.').reverse.join('.'), vlan["vlanNumber"] ].join('.'),
80
- :space => vlan["networkSpace"].to_s.downcase
81
- })
82
- end
83
- end
84
-
85
- if filter.has_key?(:datacenter_name) && ! filter[:datacenter_name].nil?
86
- vlan_details.delete_if {|vlan| (filter[:datacenter_name].class == Array ?
87
- (! filter[:datacenter_name].include?(vlan[:datacenter])) :
88
- (filter[:datacenter_name] != vlan[:datacenter])
89
- )}
90
- end
91
-
92
- if filter.has_key?(:vlan_name) && ! filter[:vlan_name].nil?
93
- vlan_details.delete_if {|vlan| (filter[:vlan_name].class == Array ?
94
- (! (filter[:vlan_name].include?(vlan[:name]) || filter[:vlan_name].include?(vlan[:qualified_name]))) :
95
- (filter[:vlan_name] != vlan[:name] && filter[:vlan_name] != vlan[:qualified_name])
96
- )}
97
- end
98
-
99
- vlan_details.delete_if {|vlan| filter[:vlan_space].to_s != vlan[:space]} if filter.has_key?(:vlan_space) && ! filter[:vlan_space].nil?
100
-
101
- return vlan_details
102
- end
103
- end
104
-
105
- class VagrantSoftLayerVlans
106
- def initialize()
107
- @cli_opts = [
108
- [ '--datacenter_name', '-d', GetoptLong::REQUIRED_ARGUMENT ],
109
- [ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ],
110
- [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
111
- [ '--sl_api_key', '-k', GetoptLong::REQUIRED_ARGUMENT ],
112
- [ '--sl_endpoint_url', '-e', GetoptLong::REQUIRED_ARGUMENT ],
113
- [ '--sl_username', '-u', GetoptLong::REQUIRED_ARGUMENT ],
114
- [ '--vlan_name', '-v', GetoptLong::REQUIRED_ARGUMENT ],
115
- [ '--vlan_space', '-s', GetoptLong::REQUIRED_ARGUMENT ]
116
- ]
117
- @config = {
118
- :columns => [ :id, :name, :qualified_name, :space, :datacenter ],
119
- :column_labels => {
120
- :datacenter => 'datacenter',
121
- :id => 'id',
122
- :name => 'name',
123
- :qualified_name => 'qualified name',
124
- :space => 'space'
125
- },
126
- :datacenter_name => nil,
127
- :format => :pretty,
128
- :formats => [ :csv, :json, :perty, :pretty, :raw ],
129
- :sl_credentials => {
130
- :api_key => nil,
131
- :endpoint_url => SoftLayer::API_PUBLIC_ENDPOINT,
132
- :username => nil
133
- },
134
- :vlan_name => nil,
135
- :vlan_space => nil
136
- }
137
- @help = <<-EOF
138
- vagrant-softlayer-vlans [OPTION]
139
-
140
- --datacenter_name, -d DCNAME,...:
141
- Comma separated list of datacenter names to filter vlan results on.
142
-
143
- --format, -f FORMAT:
144
- Sets the output format of the vlan results. Acceptable values are 'csv', 'json', 'perty', 'pretty', or 'raw'.
145
- Defaults to 'pretty'.
146
-
147
- --help, -h:
148
- Print this help.
149
-
150
- --sl_username USERNAME, -u USERNAME:
151
- Sets the SoftLayer account user name. If not specified, it is assumed SL_API_USERNAME environment variable is set.
152
-
153
- --sl_api_key SL_API_KEY, -k SL_API_KEY:
154
- Sets the SoftLayer API key. If not specified, it is assumed SL_API_KEY environment variable is set.
155
-
156
- --sl_endpoint_url SL_API_BASE_URL, -e SL_API_BASE_URL:
157
- Sets the SoftLayer endpoint URL. If not specified, it assumed SL_API_BASE_URL environment variable is set to API_PUBLIC_ENDPOINT or API_PRIVATE_ENDPOINT.
158
- Defaults to API_PUBLIC_ENDPOINT.
159
-
160
- --vlan_name, -v VLAN_NAME,...:
161
- Comma separated list of vlan names to filter vlan results on. A vlan name can be the vlan name or qualified name.
162
-
163
- --vlan_space, -s VLAN_SPACE:
164
- Sets the vlan space to filter vlan results on. Acceptable spaces are 'public' or 'private'.
165
-
166
- EOF
167
- end
168
-
169
- def run()
170
- proc_cli_options
171
-
172
- begin
173
- sl_vlans = SoftLayerVlans.new(@config[:sl_credentials][:api_key], @config[:sl_credentials][:username], @config[:sl_credentials][:endpoint_url])
174
- rescue Exception => e
175
- $stderr.puts "ERROR: Failed to instantiate SoftLayerVlans instance: #{ e.message }"
176
- exit 1
177
- end
178
-
179
- begin
180
- vlan_details = sl_vlans.vlans(:datacenter_name => @config[:datacenter_name], :vlan_name => @config[:vlan_name], :vlan_space => @config[:vlan_space])
181
- rescue Exception => e
182
- $stderr.puts "ERROR: Failed to retrieve SoftLayer account vlans: #{ e.message }"
183
- exit 1
184
- end
185
-
186
- print_vlans(vlan_details) if ! vlan_details.empty?
187
- end
188
-
189
- private
190
-
191
- def proc_cli_options()
192
- begin
193
- opts = GetoptLong.new(*@cli_opts)
194
- opts.quiet = true
195
-
196
- opts.each do |opt, optval|
197
- case opt
198
- when '--datacenter_name'
199
- @config[:datacenter_name] = optval.to_s.split(',')
200
-
201
- when '--format'
202
- if ! @config[:formats].include?(optval.to_s.downcase.to_sym)
203
- $stderr.puts "ERROR: Invalid format value, expected one of #{ @config[:formats].map{|format| format.to_s}.inspect }: #{ optval.inspect }"
204
- exit 2
205
- end
206
-
207
- @config[:format] = optval.to_s.downcase.to_sym
208
-
209
- when '--help'
210
- puts @help
211
- exit 0
212
-
213
- when '--sl_api_key'
214
- @config[:sl_credentials][:api_key] = optval.to_s
215
-
216
- when '--sl_endpoint_url'
217
- if ! [ "API_PUBLIC_ENDPOINT", "API_PRIVATE_ENDPOINT" ].include?(optval.to_s.upcase)
218
- $stderr.puts "ERROR: Invalid endpoint_url value: " + optval.to_s.upcase
219
- exit 2
220
- end
221
-
222
- @config[:sl_credentials][:endpoint_url] = (optval.to_s.upcase == 'API_PUBLIC_ENDPOINT' ? SoftLayer::API_PUBLIC_ENDPOINT : SoftLayer::API_PRIVATE_ENDPOINT)
223
-
224
- when '--sl_username'
225
- @config[:sl_credentials][:username] = optval.to_s
226
-
227
- when '--vlan_name'
228
- @config[:vlan_name] = optval.to_s.split(',')
229
-
230
- when '--vlan_space'
231
- if ! [ :private, :public ].include?(optval.to_s.downcase.to_sym)
232
- $stderr.puts "ERROR: Invalid vlan space value, expected one of #{ [ 'private', 'public' ].inspect }: #{ optval.inspect }"
233
- exit 2
234
- end
235
-
236
- @config[:vlan_space] = optval.to_s.downcase.to_sym
237
-
238
- end
239
- end
240
- rescue GetoptLong::Error => e
241
- $stderr.puts "vagrant-softlayer-vlans failed to process cli options: #{ e.message }"
242
- exit 1
243
- end
244
-
245
- @config[:sl_credentials][:username] = ENV["SL_API_USERNAME"] if @config[:sl_credentials][:username].nil? && ENV.include?("SL_API_USERNAME")
246
- @config[:sl_credentials][:api_key] = ENV["SL_API_KEY"] if @config[:sl_credentials][:api_key].nil? && ENV.include?("SL_API_KEY")
247
-
248
- if @config[:sl_credentials][:endpoint_url].nil? && ENV.include?("SL_API_BASE_URL")
249
- if ! [ 'API_PRIVATE_ENDPOINT', 'API_PUBLIC_ENDPOINT' ].include?(ENV["SL_API_BASE_URL"])
250
- $stderr.puts "ERROR: Invalid SoftLayer endpoint URL specified in environment variable SL_API_BASE_URL, expected one of #{ [ 'API_PRIVATE_ENDPOINT', 'API_PUBLIC_ENDPOINT' ].inspect }: #{ ENV["SL_API_BASE_URL"].inspect }"
251
- exit 2
252
- end
253
-
254
- @config[:sl_credentials][:endpoint_url] = (ENV["SL_API_BASE_URL"] == "API_PUBLIC_ENDPOINT" ? SoftLayer::API_PUBLIC_ENDPOINT : SoftLayer::API_PRIVATE_ENDPOINT)
255
- end
256
-
257
- if @config[:sl_credentials][:username].nil?
258
- $stderr.puts "ERROR: No SoftLayer username specified"
259
- exit 2
260
- end
261
-
262
- if @config[:sl_credentials][:username].nil?
263
- $stderr.puts "ERROR: No SoftLayer user name specified"
264
- exit 2
265
- end
266
-
267
- if @config[:sl_credentials][:api_key].nil?
268
- $stderr.puts "ERROR: No SoftLayer API key specified"
269
- exit 2
270
- end
271
- end
272
-
273
- def print_vlans(vlan_details)
274
- case @config[:format]
275
- when :csv
276
- puts @config[:columns].map{|col| @config[:column_labels][col]}.to_csv(:force_quotes => true)
277
-
278
- vlan_details.each do |vlan|
279
- csv_row = []
280
-
281
- @config[:columns].each {|col| csv_row.push(vlan[col])}
282
-
283
- puts csv_row.to_csv(:force_quotes => true)
284
- end
285
-
286
- when :json
287
- puts JSON.pretty_generate(vlan_details)
288
-
289
- when :perty, :pretty
290
- table = []
291
-
292
- table.push(@config[:columns].map{|col| @config[:column_labels][col]})
293
-
294
- vlan_details.each {|vlan| table.push(@config[:columns].map {|col| vlan[col].to_s})}
295
-
296
- max_col_widths = table.transpose.map{|col| col.group_by(&:size).max.first + 2}
297
-
298
- puts ':' + max_col_widths.map{|col_width| '.' * col_width}.join(':') + ':'
299
-
300
- print ':'
301
-
302
- table[0].each_index {|col| print table[0][col].center(max_col_widths[col]) + ':'}
303
-
304
- puts
305
-
306
- puts ':' + max_col_widths.map{|colWidth| '.' * colWidth}.join(':') + ':'
307
-
308
- table.shift
309
-
310
- table.each do |row|
311
- print ':'
312
-
313
- row.each_index {|col| print row[col].center(max_col_widths[col]) + ':'}
314
-
315
- puts
316
- end
317
-
318
- puts ':' + max_col_widths.map{|col_width| '.' * col_width}.join(':') + ':'
319
-
320
- when :raw
321
- pp vlan_details
322
-
323
- end
324
- end
325
- end
326
-
327
- if __FILE__ == $0
328
- VagrantSoftLayerVlans.new.run()
329
- end
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv'
4
+ require 'getoptlong'
5
+ require 'json'
6
+ require 'pp'
7
+
8
+ require 'rubygems'
9
+ require 'softlayer_api'
10
+
11
+ class SoftLayerVlans
12
+ def initialize(sl_api_key, sl_username, sl_endpoint_url = SoftLayer::API_PUBLIC_ENDPOINT, sl_timeout = 60)
13
+ if ! [ SoftLayer::API_PUBLIC_ENDPOINT, SoftLayer::API_PRIVATE_ENDPOINT ].include?(sl_endpoint_url)
14
+ raise ArgumentError, "Invalid SoftLayer Endpoint URL provided, expected one of #{ [ SoftLayer::API_PUBLIC_ENDPOINT, SoftLayer::API_PRIVATE_ENDPOINT ].inspect }: #{ sl_endpoint_url.inspect }"
15
+ end
16
+
17
+ @sl_credentials = {
18
+ :api_key => sl_api_key,
19
+ :endpoint_url => sl_endpoint_url,
20
+ :timeout => sl_timeout,
21
+ :user_agent => "vagrant-softlayer",
22
+ :username => sl_username
23
+ }
24
+
25
+ begin
26
+ @sl_services = {
27
+ :account => SoftLayer::Client.new(@sl_credentials)["SoftLayer_Account"]
28
+ }
29
+ rescue Exception => e
30
+ raise Exception, "Failed to initialize SoftLayer Account service for retrieving network vlan details: #{ e.message }"
31
+ end
32
+ end
33
+
34
+ def [](vlan_name)
35
+ vlan_details = vlans(:vlan_name => vlan_name)
36
+
37
+ raise KeyError, "Invalid vlan name provided as a SoftLayerVlans key: #{ vlan_name.inspect }" if vlan_details.empty?
38
+
39
+ return vlan_details[0]
40
+ end
41
+
42
+ def has_vlan?(vlan_name)
43
+ vlan_details = vlans(:vlan_name => vlan_name)
44
+
45
+ return ! vlan_details.empty?
46
+ end
47
+
48
+ def vlans(filter = { :datacenter_name => nil, :vlan_name => nil, :vlan_space => nil })
49
+ vlan_details = []
50
+
51
+ if filter.class != Hash
52
+ raise TypeError, "Invalid value specified for filter, expected hash of filter values #{ { :datacenter_name => nil, :vlan_name => nil, :vlan_space => nil }.inspect }: #{ filter.inspect }"
53
+ end
54
+
55
+ if ! filter.keys.select{|vlan_property| ! [ :datacenter_name, :vlan_name, :vlan_space ].include?(vlan_property)}.empty?
56
+ raise KeyError, "Invalid vlan property used as filter key, expected one of #{ [ :datacenter_name, :vlan_name, :vlan_space ].inspect }: #{ filter.keys.inspect }"
57
+ end
58
+
59
+ if filter.has_key?(:datacenter_name) && ! filter[:datacenter_name].nil? && ! [Array, String].include?(filter[:datacenter_name].class)
60
+ raise TypeError, "Invalid type for filter key :datacenter_name, must be a Array or String of datacenter name(s): #{ filter[:datacenter_name].class }"
61
+ end
62
+
63
+ if filter.has_key?(:vlan_name) && ! filter[:vlan_name].nil? && ! [ Array, String ].include?(filter[:vlan_name].class)
64
+ raise TypeError, "Invalid type for filter key :vlan_name, must be a Array or String of vlan name(s): #{ filter[:vlan_name].class }"
65
+ end
66
+
67
+ if filter.has_key?(:vlan_space) && ! filter[:vlan_space].nil? && ! [ nil, :public, :private ].include?(filter[:vlan_space])
68
+ raise ArgumentError, "Invalid value for filter key :vlan_space, expected one of #{ [ nil, :public, :private ].inspect }: #{ filter[:vlan_space].inspect }"
69
+ end
70
+
71
+ routers = @sl_services[:account].object_mask("mask[routers,routers.datacenter,routers.networkVlans,routers.networkVlans.networkSpace,routers.networkVlans.type]").getObject["routers"]
72
+
73
+ routers.each do |router|
74
+ router["networkVlans"].delete_if {|vlan| ! vlan.has_key?("name") || vlan["type"]["keyName"] != "STANDARD"}
75
+
76
+ router["networkVlans"].each do |vlan|
77
+ vlan_details.push({
78
+ :datacenter => router["datacenter"]["name"],
79
+ :id => vlan["id"],
80
+ :name => vlan["name"],
81
+ :qualified_name => [ router["hostname"].split('.').reverse.join('.'), vlan["vlanNumber"] ].join('.'),
82
+ :space => vlan["networkSpace"].to_s.downcase
83
+ })
84
+ end
85
+ end
86
+
87
+ if filter.has_key?(:datacenter_name) && ! filter[:datacenter_name].nil?
88
+ vlan_details.delete_if {|vlan| (filter[:datacenter_name].class == Array ?
89
+ (! filter[:datacenter_name].include?(vlan[:datacenter])) :
90
+ (filter[:datacenter_name] != vlan[:datacenter])
91
+ )}
92
+ end
93
+
94
+ if filter.has_key?(:vlan_name) && ! filter[:vlan_name].nil?
95
+ vlan_details.delete_if {|vlan| (filter[:vlan_name].class == Array ?
96
+ (! (filter[:vlan_name].include?(vlan[:name]) || filter[:vlan_name].include?(vlan[:qualified_name]))) :
97
+ (filter[:vlan_name] != vlan[:name] && filter[:vlan_name] != vlan[:qualified_name])
98
+ )}
99
+ end
100
+
101
+ vlan_details.delete_if {|vlan| filter[:vlan_space].to_s != vlan[:space]} if filter.has_key?(:vlan_space) && ! filter[:vlan_space].nil?
102
+
103
+ return vlan_details
104
+ end
105
+ end
106
+
107
+ class VagrantSoftLayerVlans
108
+ def initialize()
109
+ @cli_opts = [
110
+ [ '--datacenter_name', '-d', GetoptLong::REQUIRED_ARGUMENT ],
111
+ [ '--format', '-f', GetoptLong::REQUIRED_ARGUMENT ],
112
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
113
+ [ '--sl_api_key', '-k', GetoptLong::REQUIRED_ARGUMENT ],
114
+ [ '--sl_endpoint_url', '-e', GetoptLong::REQUIRED_ARGUMENT ],
115
+ [ '--sl_timeout', '-t', GetoptLong::REQUIRED_ARGUMENT ],
116
+ [ '--sl_username', '-u', GetoptLong::REQUIRED_ARGUMENT ],
117
+ [ '--vlan_name', '-v', GetoptLong::REQUIRED_ARGUMENT ],
118
+ [ '--vlan_space', '-s', GetoptLong::REQUIRED_ARGUMENT ]
119
+ ]
120
+ @config = {
121
+ :columns => [ :id, :name, :qualified_name, :space, :datacenter ],
122
+ :column_labels => {
123
+ :datacenter => 'datacenter',
124
+ :id => 'id',
125
+ :name => 'name',
126
+ :qualified_name => 'qualified name',
127
+ :space => 'space'
128
+ },
129
+ :datacenter_name => nil,
130
+ :format => :pretty,
131
+ :formats => [ :csv, :json, :perty, :pretty, :raw ],
132
+ :sl_credentials => {
133
+ :api_key => nil,
134
+ :endpoint_url => SoftLayer::API_PUBLIC_ENDPOINT,
135
+ :timeout => 60,
136
+ :username => nil
137
+ },
138
+ :vlan_name => nil,
139
+ :vlan_space => nil
140
+ }
141
+ @help = <<-EOF
142
+ vagrant-softlayer-vlans [OPTION]
143
+
144
+ --datacenter_name|-d DCNAME,...:
145
+ Comma separated list of datacenter names to filter vlan results on.
146
+
147
+ --format|-f FORMAT:
148
+ Sets the output format of the vlan results. Acceptable values are 'csv', 'json', 'perty', 'pretty', or 'raw'.
149
+ Defaults to 'pretty'.
150
+
151
+ --help|-h:
152
+ Print this help.
153
+
154
+ --sl_api_key|-k SL_API_KEY
155
+ Sets the SoftLayer API key. If not specified, it is assumed SL_API_KEY environment variable is set.
156
+
157
+ --sl_endpoint_url|-e SL_API_BASE_URL
158
+ Sets the SoftLayer endpoint URL. If not specified, it assumed SL_API_BASE_URL environment variable is set to API_PUBLIC_ENDPOINT or API_PRIVATE_ENDPOINT.
159
+ Defaults to API_PUBLIC_ENDPOINT.
160
+
161
+ --sl_timeout|-t SL_TIMEOUT
162
+ Sets the SoftLayer API call timeout value in seconds.
163
+
164
+ --sl_username|-u USERNAME
165
+ Sets the SoftLayer account user name. If not specified, it is assumed SL_API_USERNAME environment variable is set.
166
+
167
+ --vlan_name|-v VLAN_NAME,...
168
+ Comma separated list of vlan names to filter vlan results on. A vlan name can be the vlan name or qualified name.
169
+
170
+ --vlan_space|-s VLAN_SPACE
171
+ Sets the vlan space to filter vlan results on. Acceptable spaces are 'public' or 'private'.
172
+
173
+ EOF
174
+ end
175
+
176
+ def run()
177
+ proc_cli_options
178
+
179
+ begin
180
+ sl_vlans = SoftLayerVlans.new(@config[:sl_credentials][:api_key], @config[:sl_credentials][:username], @config[:sl_credentials][:endpoint_url], @config[:sl_credentials][:timeout])
181
+ rescue Exception => e
182
+ $stderr.puts "ERROR: Failed to instantiate SoftLayerVlans instance: #{ e.message }"
183
+ exit 1
184
+ end
185
+
186
+ begin
187
+ vlan_details = sl_vlans.vlans(:datacenter_name => @config[:datacenter_name], :vlan_name => @config[:vlan_name], :vlan_space => @config[:vlan_space])
188
+ rescue Exception => e
189
+ $stderr.puts "ERROR: Failed to retrieve SoftLayer account vlans: #{ e.message }"
190
+ exit 1
191
+ end
192
+
193
+ print_vlans(vlan_details) if ! vlan_details.empty?
194
+ end
195
+
196
+ private
197
+
198
+ def proc_cli_options()
199
+ begin
200
+ opts = GetoptLong.new(*@cli_opts)
201
+ opts.quiet = true
202
+
203
+ opts.each do |opt, optval|
204
+ case opt
205
+ when '--datacenter_name'
206
+ @config[:datacenter_name] = optval.to_s.split(',')
207
+
208
+ when '--format'
209
+ if ! @config[:formats].include?(optval.to_s.downcase.to_sym)
210
+ $stderr.puts "ERROR: Invalid format value, expected one of #{ @config[:formats].map{|format| format.to_s}.inspect }: #{ optval.inspect }"
211
+ exit 2
212
+ end
213
+
214
+ @config[:format] = optval.to_s.downcase.to_sym
215
+
216
+ when '--help'
217
+ puts @help
218
+ exit 0
219
+
220
+ when '--sl_api_key'
221
+ @config[:sl_credentials][:api_key] = optval.to_s
222
+
223
+ when '--sl_endpoint_url'
224
+ if ! [ "API_PUBLIC_ENDPOINT", "API_PRIVATE_ENDPOINT" ].include?(optval.to_s.upcase)
225
+ $stderr.puts "ERROR: Invalid endpoint_url value: " + optval.to_s.upcase
226
+ exit 2
227
+ end
228
+
229
+ @config[:sl_credentials][:endpoint_url] = (optval.to_s.upcase == 'API_PUBLIC_ENDPOINT' ? SoftLayer::API_PUBLIC_ENDPOINT : SoftLayer::API_PRIVATE_ENDPOINT)
230
+
231
+ when '--sl_timeoout'
232
+ @config[:sl_credentials][:timeout] = optval.to_i
233
+
234
+ when '--sl_username'
235
+ @config[:sl_credentials][:username] = optval.to_s
236
+
237
+ when '--vlan_name'
238
+ @config[:vlan_name] = optval.to_s.split(',')
239
+
240
+ when '--vlan_space'
241
+ if ! [ :private, :public ].include?(optval.to_s.downcase.to_sym)
242
+ $stderr.puts "ERROR: Invalid vlan space value, expected one of #{ [ 'private', 'public' ].inspect }: #{ optval.inspect }"
243
+ exit 2
244
+ end
245
+
246
+ @config[:vlan_space] = optval.to_s.downcase.to_sym
247
+
248
+ end
249
+ end
250
+ rescue GetoptLong::Error => e
251
+ $stderr.puts "vagrant-softlayer-vlans failed to process cli options: #{ e.message }"
252
+ exit 1
253
+ end
254
+
255
+ @config[:sl_credentials][:username] = ENV["SL_API_USERNAME"] if @config[:sl_credentials][:username].nil? && ENV.include?("SL_API_USERNAME")
256
+ @config[:sl_credentials][:api_key] = ENV["SL_API_KEY"] if @config[:sl_credentials][:api_key].nil? && ENV.include?("SL_API_KEY")
257
+
258
+ if @config[:sl_credentials][:endpoint_url].nil? && ENV.include?("SL_API_BASE_URL")
259
+ if ! [ 'API_PRIVATE_ENDPOINT', 'API_PUBLIC_ENDPOINT' ].include?(ENV["SL_API_BASE_URL"])
260
+ $stderr.puts "ERROR: Invalid SoftLayer endpoint URL specified in environment variable SL_API_BASE_URL, expected one of #{ [ 'API_PRIVATE_ENDPOINT', 'API_PUBLIC_ENDPOINT' ].inspect }: #{ ENV["SL_API_BASE_URL"].inspect }"
261
+ exit 2
262
+ end
263
+
264
+ @config[:sl_credentials][:endpoint_url] = (ENV["SL_API_BASE_URL"] == "API_PUBLIC_ENDPOINT" ? SoftLayer::API_PUBLIC_ENDPOINT : SoftLayer::API_PRIVATE_ENDPOINT)
265
+ end
266
+
267
+ if @config[:sl_credentials][:username].nil?
268
+ $stderr.puts "ERROR: No SoftLayer username specified"
269
+ exit 2
270
+ end
271
+
272
+ if @config[:sl_credentials][:username].nil?
273
+ $stderr.puts "ERROR: No SoftLayer user name specified"
274
+ exit 2
275
+ end
276
+
277
+ if @config[:sl_credentials][:api_key].nil?
278
+ $stderr.puts "ERROR: No SoftLayer API key specified"
279
+ exit 2
280
+ end
281
+ end
282
+
283
+ def print_vlans(vlan_details)
284
+ case @config[:format]
285
+ when :csv
286
+ puts @config[:columns].map{|col| @config[:column_labels][col]}.to_csv(:force_quotes => true)
287
+
288
+ vlan_details.each do |vlan|
289
+ csv_row = []
290
+
291
+ @config[:columns].each {|col| csv_row.push(vlan[col])}
292
+
293
+ puts csv_row.to_csv(:force_quotes => true)
294
+ end
295
+
296
+ when :json
297
+ puts JSON.pretty_generate(vlan_details)
298
+
299
+ when :perty, :pretty
300
+ table = []
301
+
302
+ table.push(@config[:columns].map{|col| @config[:column_labels][col]})
303
+
304
+ vlan_details.each {|vlan| table.push(@config[:columns].map {|col| vlan[col].to_s})}
305
+
306
+ max_col_widths = table.transpose.map{|col| col.group_by(&:size).max.first + 2}
307
+
308
+ puts ':' + max_col_widths.map{|col_width| '.' * col_width}.join(':') + ':'
309
+
310
+ print ':'
311
+
312
+ table[0].each_index {|col| print table[0][col].center(max_col_widths[col]) + ':'}
313
+
314
+ puts
315
+
316
+ puts ':' + max_col_widths.map{|colWidth| '.' * colWidth}.join(':') + ':'
317
+
318
+ table.shift
319
+
320
+ table.each do |row|
321
+ print ':'
322
+
323
+ row.each_index {|col| print row[col].center(max_col_widths[col]) + ':'}
324
+
325
+ puts
326
+ end
327
+
328
+ puts ':' + max_col_widths.map{|col_width| '.' * col_width}.join(':') + ':'
329
+
330
+ when :raw
331
+ pp vlan_details
332
+
333
+ end
334
+ end
335
+ end
336
+
337
+ if __FILE__ == $0
338
+ VagrantSoftLayerVlans.new.run()
339
+ end