vcautils 0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/vca.rb ADDED
@@ -0,0 +1,514 @@
1
+
2
+
3
+ #################################################################################
4
+ #### Massimo Re Ferre' ####
5
+ #### www.it20.info ####
6
+ #### vcautils, a set of utilities for vCloud Air Consumers ####
7
+ #################################################################################
8
+
9
+
10
+
11
+
12
+ #################################################################################
13
+ #### vcaondemand.rb is the front end program that presents a CLI interface ####
14
+ #### It leverages the vcautils library ####
15
+ #################################################################################
16
+
17
+
18
+ #################################################################################
19
+ #### IMPORTANT !! ####
20
+ #### The program reads a file called vcautils.yml in the working directory ####
21
+ #### If the file does not exist the program will abort ####
22
+ #### The file is used to provide the program with connectivity parameters ####
23
+ #################################################################################
24
+
25
+ # This is the format of the vcautils.yml file:
26
+ # :username: email@domain
27
+ # :password: password
28
+ # :serviceroot: https://vca.vmware.com
29
+ # :mode: admin | developer
30
+
31
+ # These are the additional modules/gems required to run the program
32
+
33
+ require 'httparty'
34
+ require 'yaml'
35
+ require 'xml-fu'
36
+ require 'pp'
37
+ require 'json'
38
+ require 'awesome_print' #optional - useful for debugging
39
+
40
+ require 'modules/vca-be'
41
+
42
+
43
+ # We stole this piece of code (silence_warnings) from the Internet.
44
+ # We are using it to silence the warnings of the certificates settings (below)
45
+
46
+
47
+ def silence_warnings(&block)
48
+ warn_level = $VERBOSE
49
+ $VERBOSE = nil
50
+ result = block.call
51
+ $VERBOSE = warn_level
52
+ result
53
+ end
54
+
55
+
56
+
57
+
58
+
59
+ #=begin
60
+ class String
61
+ def black; "\033[30m#{self}\033[0m" end
62
+ def red; "\033[31m#{self}\033[0m" end
63
+ def green; "\033[32m#{self}\033[0m" end
64
+ def brown; "\033[33m#{self}\033[0m" end
65
+ def blue; "\033[34m#{self}\033[0m" end
66
+ def magenta; "\033[35m#{self}\033[0m" end
67
+ def cyan; "\033[36m#{self}\033[0m" end
68
+ def gray; "\033[37m#{self}\033[0m" end
69
+ def bg_black; "\033[40m#{self}\033[0m" end
70
+ def bg_red; "\033[41m#{self}\033[0m" end
71
+ def bg_green; "\033[42m#{self}\033[0m" end
72
+ def bg_brown; "\033[43m#{self}\033[0m" end
73
+ def bg_blue; "\033[44m#{self}\033[0m" end
74
+ def bg_magenta; "\033[45m#{self}\033[0m" end
75
+ def bg_cyan; "\033[46m#{self}\033[0m" end
76
+ def bg_gray; "\033[47m#{self}\033[0m" end
77
+ def bold; "\033[1m#{self}\033[22m" end
78
+ def reverse_color; "\033[7m#{self}\033[27m" end
79
+ end #String
80
+ #=end
81
+
82
+
83
+
84
+
85
+
86
+ # This bypass certification checks... NOT a great idea for production but ok for test / dev
87
+
88
+ silence_warnings do
89
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
90
+ end
91
+
92
+
93
+ # This is what the program accepts as input
94
+
95
+ def usage
96
+ puts "\te.g. vca token".green
97
+ puts "\te.g. vca plans".green
98
+ puts "\te.g. vca instances".green
99
+ puts "\te.g. vca instance".green + " <instance id>".magenta
100
+ puts "\te.g. vca users".green
101
+ puts "\te.g. vca servicegroups".green
102
+ puts "\te.g. vca billedcosts".green
103
+ puts "\te.g. vca billablecosts".green
104
+ puts "\te.g. vca customquery".green + " <REST GET query>".magenta + " <ContentType>".magenta
105
+ puts "\n"
106
+
107
+ end #usage
108
+
109
+
110
+ # These are the variables the program accept as inputs (see the usage section for more info)
111
+
112
+
113
+
114
+ $input0 = ARGV[0]
115
+
116
+ $input1 = ARGV[1]
117
+
118
+ $input2 = ARGV[2]
119
+
120
+ $input3 = ARGV[3]
121
+
122
+ $input4 = ARGV[4]
123
+
124
+
125
+ # The if checks if the user called an operation. If not the case, we print the text on how to use the CLI
126
+
127
+
128
+ if $input0
129
+
130
+ # We login (the login method is in vcautilscore)
131
+
132
+ file_path = "vcautils.yml"
133
+ raise "no file #{file_path}" unless File.exists? file_path
134
+ configuration = YAML.load_file(file_path)
135
+ username = configuration[:username]
136
+ password = configuration[:password]
137
+ serviceroot = configuration[:serviceroot]
138
+ mode = configuration[:mode]
139
+ loginserviceroot = serviceroot
140
+
141
+ puts
142
+ puts "Logging in ...\n\n"
143
+
144
+ iam = Iam.new()
145
+
146
+ sc = Sc.new()
147
+
148
+ billing = Billing.new()
149
+
150
+ metering = Metering.new()
151
+
152
+ token = iam.login(username, password, loginserviceroot)
153
+
154
+ misc = Misc.new()
155
+
156
+
157
+
158
+ case $input0.chomp
159
+
160
+
161
+ when 'token'
162
+ puts "OAuth token:\n".green + "Bearer " + token
163
+
164
+
165
+
166
+ when 'users'
167
+ usersarray = iam.users(token, serviceroot)
168
+ if mode == "developer" then puts JSON.pretty_generate(usersarray) end
169
+ usersarray['Users']['User'].length.times do |i|
170
+ puts "id : ".green + usersarray['Users']['User'][i]["id"].blue
171
+ puts "state : ".green + usersarray['Users']['User'][i]["state"].blue
172
+ puts "email : ".green + usersarray['Users']['User'][i]["email"].blue
173
+ puts "metacreated : ".green + usersarray['Users']['User'][i]["meta"]["created"].blue
174
+ puts "metamodified : ".green + usersarray['Users']['User'][i]["meta"]["modified"].blue
175
+ puts
176
+ end #do
177
+
178
+
179
+ when 'plans'
180
+ plansarray = sc.plans(token, serviceroot)
181
+ if mode == "developer" then puts JSON.pretty_generate(plansarray) end
182
+ plansarray['PlanList']['plans'].length.times do |i|
183
+ puts "id : ".green + plansarray['PlanList']['plans'][i]["id"].blue
184
+ puts "name : ".green + plansarray['PlanList']['plans'][i]["name"].blue
185
+ puts "serviceName : ".green + plansarray['PlanList']['plans'][i]["serviceName"].blue
186
+ puts "region : ".green + plansarray['PlanList']['plans'][i]["region"].blue
187
+ puts
188
+ end #do
189
+
190
+
191
+
192
+ when 'instances'
193
+ instancesarray = sc.instances(token, serviceroot)
194
+ if mode == "developer" then puts JSON.pretty_generate(instancesarray) end
195
+ instancesarray["InstanceList"]["instances"].length.times do |i|
196
+ puts "name : ".green + instancesarray["InstanceList"]["instances"][i]["name"].blue
197
+ puts "id : ".green + instancesarray["InstanceList"]["instances"][i]["id"].blue
198
+ puts "serviceGroupId : ".green + instancesarray["InstanceList"]["instances"][i]["serviceGroupId"].blue
199
+ puts "region : ".green + instancesarray["InstanceList"]["instances"][i]["region"].blue
200
+ puts "planId : ".green + instancesarray["InstanceList"]["instances"][i]["planId"].blue
201
+ puts "apiUrl : ".green + instancesarray["InstanceList"]["instances"][i]["apiUrl"].blue
202
+ puts "dashboardUrl : ".green + instancesarray["InstanceList"]["instances"][i]["dashboardUrl"].blue
203
+ puts "instanceAttributes : ".green + instancesarray["InstanceList"]["instances"][i]["instanceAttributes"].blue
204
+ puts
205
+ end #do
206
+
207
+
208
+
209
+ when 'instance'
210
+ instancesarray = sc.instances(token, serviceroot)
211
+ plansarray = sc.plans(token, serviceroot)
212
+ if $input1
213
+ instanceexists = false
214
+ instancesarray["InstanceList"]["instances"].length.times do |i|
215
+ if instancesarray["InstanceList"]["instances"][i]["id"] == $input1
216
+ plan = instancesarray["InstanceList"]["instances"][i]["planId"]
217
+ plansarray['PlanList']['plans'].length.times do |x|
218
+ if plansarray['PlanList']['plans'][x]['id'] == plan
219
+ case plan.chomp
220
+
221
+ #Subscription Service
222
+ when "region:PMP:planID:be817b3c-c762-45c4-9915-f956dc67ba82"
223
+ puts "Sorry the instance ".red + $input1 + " refers to a Subscription instance. This tool doesn't support Subscription instances".red
224
+ puts
225
+
226
+ #OnDemand (and DR on OnDemand)
227
+ when "region:us-virginia-1-12.vchs.vmware.com:planID:67538ff0-f4c3-48cb-8a6f-b0a3ac5aa324",
228
+ "region:au-south-1-15.vchs.vmware.com:planID:c65d5821-aa97-4141-915a-7d7eab0a9d51",
229
+ "region:de-germany-1-16.vchs.vmware.com:planID:c65d5821-aa97-4141-915a-7d7eab0a9d51",
230
+ "region:uk-slough-1-6.vchs.vmware.com:planID:c65d5821-aa97-4141-915a-7d7eab0a9d51",
231
+ "region:us-california-1-3.vchs.vmware.com:planID:c65d5821-aa97-4141-915a-7d7eab0a9d51",
232
+ "region:us-virginia-1-4.vchs.vmware.com:planID:c65d5821-aa97-4141-915a-7d7eab0a9d51"
233
+ orgname, sessionuri = misc.extractendpoint(instancesarray["InstanceList"]["instances"][i]["instanceAttributes"])
234
+ if mode == "developer" then puts instancesarray["InstanceList"]["instances"][i]["instanceAttributes"] end
235
+ if mode == "developer" then puts "orgname, sessionuri = " + orgname.to_s + ", " + sessionuri.to_s end
236
+ #right now I am not using the apiUrl
237
+ #ideally that should be the starting point
238
+ #however to speed up things I am using sessionuri + the specific (packaged) query I need directly
239
+ apiUrl = instancesarray["InstanceList"]["instances"][i]["apiUrl"]
240
+ #puts "The API URL for this service is : ".bold.green + apiUrl.bold.blue
241
+ puts "The session URL for this instance is : ".bold.green + sessionuri.bold.blue
242
+ puts "The friendly <vCD Org name> for this instance is : ".bold.green + orgname.bold.blue
243
+ puts
244
+
245
+ #Object Storage powered by EMC ViPR
246
+ when "region:us-california-1-3.vchs.vmware.com:planID:os.vca.vmware.4562.6481"
247
+ puts "Oooohhhh ".red + $input1 + " is an Object Storage Instance powered by ViPR.... <under construction>".red
248
+ puts
249
+
250
+ #when
251
+ #########
252
+ # New service types will have to be inserted here (e.g. DBaaS)
253
+ #########
254
+
255
+
256
+ end #case plan.chomp
257
+ end #if plansarray['PlanList']['plans'][x]['id'] == plan
258
+ end #plansarray['PlanList']['plans'].length.times do |x|
259
+
260
+
261
+ instanceexists = true
262
+ end #if instancesarray["InstanceList"]["instances"][i]["id"] == $input1
263
+ end #instancesarray["InstanceList"]["instances"].length.times do
264
+ if instanceexists == false
265
+ puts "Sorry the instance ".red + $input1 + " doesn't exist".red
266
+ puts
267
+ end #if instance exists
268
+ else #if $input1
269
+ puts "You need to specify an instance".red
270
+ puts
271
+ end #if $input1
272
+
273
+
274
+
275
+
276
+ ##### This is done ########
277
+ when 'servicegroups'
278
+ servicegroupsarray = billing.servicegroups(token, serviceroot)
279
+ if mode == "developer" then puts JSON.pretty_generate(servicegroupsarray) end
280
+ servicegroupsarray["serviceGroupList"]["serviceGroup"].length.times do |i|
281
+ puts
282
+ puts "id : ".green + servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["id"].blue
283
+ puts "displayName : ".green + servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["displayName"].blue
284
+ puts "billingCurrency : ".green + servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["billingCurrency"].blue
285
+ puts "availableBills : ".green
286
+ puts " - bill:".green
287
+ servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["availableBills"]["bill"].length.times do |e|
288
+ puts " - billingPeriod:".green
289
+ puts " - month :".green + servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["availableBills"]["bill"][e]["billingPeriod"]["month"].to_s.blue
290
+ puts " - year :".green + servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["availableBills"]["bill"][e]["billingPeriod"]["year"].to_s.blue
291
+ puts " - sartDate :".green + servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["availableBills"]["bill"][e]["billingPeriod"]["startDate"].blue
292
+ puts " - endDate :".green + servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["availableBills"]["bill"][e]["billingPeriod"]["endDate"].blue
293
+ puts
294
+ end #do e
295
+ puts
296
+ puts
297
+ end #do i
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+ ##### This is done ########
306
+ when 'billedcosts'
307
+ if $input1
308
+ servicegroupexists = false
309
+ servicegroupsarray.length.times do |i|
310
+ if servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["id"] == $input1
311
+ billedcostsarray = billing.billedcosts(token, serviceroot, servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["id"])
312
+ if billedcostsarray["status"]
313
+ puts "Sorry, there is no billing yet for this servicegroup".red
314
+ puts
315
+ else
316
+ #I need to convert cost to string as if it's nil it will complain
317
+ puts "cost : " + billedcostsarray["cost"].to_s
318
+ puts "currency : " + billedcostsarray["currency"]
319
+ puts "month : " + billedcostsarray["month"].to_s
320
+ puts "year : " + billedcostsarray["year"].to_s
321
+ puts "startTime : " + billedcostsarray["startTime"]
322
+ puts "endTime : " + billedcostsarray["endTime"]
323
+ #puts JSON.pretty_generate(billedcostsarray)
324
+ #servicegrouparray.length.times do |i|
325
+ #end #do
326
+ end
327
+ servicegroupexists = true
328
+ end #servicegroup check
329
+ end #do
330
+ if servicegroupexists == false
331
+ puts "Sorry the servicegroup ".red + $input1 + " doesn't exist".red
332
+ puts
333
+ end #if servicegroup exists
334
+ end #main if
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+ ##### vCA RESPONDS WITH "NOT IMPLEMENTED" ########
343
+ when 'billedusage'
344
+ if $input1
345
+ servicegroupexists = false
346
+ servicegroupsarray["serviceGroupList"]["serviceGroup"].length.times do |i|
347
+ if servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["id"] == $input1
348
+ billedusagearray = billing.billedusage(token, serviceroot, servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["id"])
349
+ #servicegrouparray.length.times do |i|
350
+ #end #do
351
+ servicegroupexists = true
352
+ end #servicegroup check
353
+ end #do
354
+ if servicegroupexists == false
355
+ puts "Sorry the servicegroup ".red + $input1 + " doesn't exist".red
356
+ puts
357
+ end #if servicegroup exists
358
+ end #main if
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
+ ##### This is done ########
367
+ when 'billablecosts'
368
+ if $input1
369
+ servicegroupexists = false
370
+ servicegroupsarray["serviceGroupList"]["serviceGroup"].length.times do |i|
371
+ if servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["id"] == $input1
372
+ billablecostsarray = metering.billablecosts(token, serviceroot, servicegroupsarray["serviceGroupList"]["serviceGroup"][i]["id"])
373
+ puts "currency : " + billablecostsarray["currency"]
374
+ puts "lastUpdateTime : " + billablecostsarray["lastUpdateTime"]
375
+ billablecostsarray["cost"].length.times do |e|
376
+ puts " - type : " + billablecostsarray["cost"][e]["type"]
377
+ puts " - costItem : "
378
+ billablecostsarray["cost"][e]["costItem"].length.times do |n|
379
+ puts " - properties : "
380
+ puts " - property : "
381
+ billablecostsarray["cost"][e]["costItem"][n]["properties"]["property"].length.times do |m|
382
+ puts " - value : " + billablecostsarray["cost"][e]["costItem"][n]["properties"]["property"][m]["value"]
383
+ puts " - name : " + billablecostsarray["cost"][e]["costItem"][n]["properties"]["property"][m]["name"]
384
+ end #do
385
+ end #do
386
+ puts
387
+ puts
388
+ end #do
389
+ servicegroupexists = true
390
+ end #servicegroup check
391
+ end #do
392
+ if servicegroupexists == false
393
+ puts "Sorry the servicegroup ".red + $input1 + " doesn't exist".red
394
+ puts
395
+ end #if servicegroup exists
396
+ end #main if
397
+
398
+
399
+
400
+
401
+
402
+ ##### doesn't work ########
403
+ when 'billableusage'
404
+ if $input1
405
+ instanceexists = false
406
+ instancesarray.length.times do |i|
407
+ if instancesarray[i]["Id"] == $input1
408
+ billableusagearray = metering.billableusage(token, serviceroot, instancesarray[i]["Id"])
409
+ puts "currency : " + billablecostsarray["currency"]
410
+ puts "lastUpdateTime : " + billablecostsarray["lastUpdateTime"]
411
+ billablecostsarray["cost"].length.times do |e|
412
+ puts " - type : " + billablecostsarray["cost"][e]["type"]
413
+ puts " - costItem : "
414
+ billablecostsarray["cost"][e]["costItem"].length.times do |n|
415
+ puts " - properties : "
416
+ puts " - property : "
417
+ billablecostsarray["cost"][e]["costItem"][n]["properties"]["property"].length.times do |m|
418
+ puts " - value : " + billablecostsarray["cost"][e]["costItem"][n]["properties"]["property"][m]["value"]
419
+ puts " - name : " + billablecostsarray["cost"][e]["costItem"][n]["properties"]["property"][m]["name"]
420
+ end #do
421
+ end #do
422
+ puts
423
+ puts
424
+ end #do
425
+ instanceexists = true
426
+ end #instance check
427
+ end #do
428
+ if instanceexists == false
429
+ puts "Sorry the instance ".red + $input1 + " doesn't exist".red
430
+ puts
431
+ end #if servicegroup exists
432
+ end #main if
433
+
434
+
435
+
436
+
437
+
438
+ when 'custom'
439
+ if $input1
440
+ customresult = misc.customquery(token, serviceroot, $input1, $input2)
441
+ puts JSON.pretty_generate(customresult)
442
+ else
443
+ puts "Please provide a valid API call path (with proper Content Type) to run a GET against https://vca.vmware.com".green
444
+ puts "Example: /api/iam/Users".green
445
+ puts
446
+ end #main if
447
+
448
+
449
+
450
+
451
+ else #case $input0
452
+ puts "\n"
453
+ puts "Noooooope!".red
454
+ puts "\n"
455
+ usage()
456
+ puts "\n"
457
+
458
+ end
459
+
460
+
461
+ # If the user did not specify an operation at all we suggest how to properly use the CLI
462
+
463
+ else #if $input0
464
+ puts "\n"
465
+ puts "Use any of the following operations".red
466
+ puts "\n"
467
+ usage()
468
+ puts "\n"
469
+
470
+
471
+
472
+ end #main if
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+
488
+
489
+
490
+
491
+
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+
502
+
503
+
504
+
505
+
506
+
507
+
508
+
509
+
510
+
511
+
512
+
513
+
514
+