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.
@@ -0,0 +1,215 @@
1
+ #################################################################################
2
+ #### Massimo Re Ferre' ####
3
+ #### www.it20.info ####
4
+ #### vcautils, a set of utilities for vCloud Air Consumers ####
5
+ #################################################################################
6
+
7
+
8
+ class Iam
9
+ include HTTParty
10
+ format :xml
11
+ #debug_output $stderr
12
+
13
+
14
+
15
+ def hashtoarray(structure)
16
+ #this function work arounds an issue of httparty (and other REST clients apparently) that do not comply to the XML Schema correctly
17
+ #in some circumstances the httparty response contains a hash whereas it should be an array of hash with one item
18
+ #this function takes input a JSON structure and check if it's a hash. If it is, it will turn it into an array of hash with one element
19
+ #if the input is already an Array of hash it will do nothing
20
+ #for further reference: http://stackoverflow.com/questions/28282125/httparty-response-interpretation-in-ruby/
21
+ structure = [structure] unless structure.is_a? Array
22
+ return structure
23
+ end #hashtoarray
24
+
25
+
26
+
27
+ def login(username, password, serviceroot)
28
+ self.class.base_uri serviceroot
29
+ #Avoid setting the basic_auth as (due to an httparty problem) it propagates and gets retained for other calls inside the class
30
+ #self.class.basic_auth username, password
31
+ auth_token = Base64.encode64(username + ":" + password)
32
+ self.class.default_options[:headers] = {"Accept" => "application/xml;version=5.7", "Authorization" => "Basic " + auth_token}
33
+ response = self.class.post('/api/iam/login')
34
+ token = response.headers['vchs-authorization']
35
+ return token
36
+ end #login
37
+
38
+
39
+
40
+ def users(token, serviceroot)
41
+ self.class.base_uri serviceroot
42
+ #legacy type
43
+ #self.class.default_options[:headers] = { "Accept" => "application/xml;class=com.vmware.vchs.iam.api.schema.v2.classes.user.Users;version=5.7", "Authorization" => "Bearer " + token }
44
+ self.class.default_options[:headers] = { "Accept" => "application/xml;version=*", "Authorization" => "Bearer " + token }
45
+ usersarray = self.class.get('/api/iam/Users')
46
+ usersarray['Users']['User'] = hashtoarray(usersarray['Users']['User'])
47
+ usersarray['Users']['User'].length.times do |e|
48
+ usersarray['Users']['User'][e]["roles"]["role"] = hashtoarray(usersarray['Users']['User'][e]["roles"]["role"])
49
+ end
50
+ return usersarray
51
+ end #users
52
+
53
+
54
+ def logout
55
+ self.class.delete('/api/session')
56
+ end
57
+
58
+ def links
59
+ response = self.class.get('/api/session')
60
+ response['Session']['Link'].each do |link|
61
+ puts link['href']
62
+ end
63
+ end
64
+
65
+
66
+ end #Iam
67
+
68
+
69
+
70
+
71
+ class Sc
72
+ include HTTParty
73
+ format :xml
74
+ #debug_output $stderr
75
+
76
+ def hashtoarray(structure)
77
+ #this function work arounds an issue of httparty (and other REST clients apparently) that do not comply to the XML Schema correctly
78
+ #in some circumstances the httparty response contains a hash whereas it should be an array of hash with one item
79
+ #this function takes input a JSON structure and check if it's a hash. If it is, it will turn it into an array of hash with one element
80
+ #if the input is already an Array of hash it will do nothing
81
+ #for further reference: http://stackoverflow.com/questions/28282125/httparty-response-interpretation-in-ruby/
82
+ structure = [structure] unless structure.is_a? Array
83
+ return structure
84
+ end #hashtoarray
85
+
86
+
87
+ def plans(token, serviceroot)
88
+ self.class.base_uri serviceroot
89
+ self.class.default_options[:headers] = { "Accept" => "application/xml;version=5.7", "Authorization" => "Bearer " + token }
90
+ plansarray = self.class.get('/api/sc/plans')
91
+ return plansarray
92
+ end #plans
93
+
94
+
95
+ def instances(token, serviceroot)
96
+ self.class.base_uri serviceroot
97
+ self.class.default_options[:headers] = { "Accept" => "application/xml;version=5.7", "Authorization" => "Bearer " + token }
98
+ instancesarray = self.class.get('/api/sc/instances')
99
+ instancesarray["InstanceList"]["instances"] = hashtoarray(instancesarray["InstanceList"]["instances"])
100
+ return instancesarray
101
+ end #instances
102
+
103
+ end #Sc
104
+
105
+
106
+
107
+
108
+ class Billing
109
+ include HTTParty
110
+ format :json
111
+ #debug_output $stderr
112
+
113
+ def servicegroups(token, serviceroot)
114
+ self.class.base_uri serviceroot
115
+ self.class.default_options[:headers] = { "Accept" => "application/json;version=5.7", "Authorization" => "Bearer " + token }
116
+ servicegroupsarray = self.class.get('/api/billing/service-groups')
117
+ return servicegroupsarray
118
+ end #servicegroups
119
+
120
+
121
+
122
+ def billedcosts(token, serviceroot, servicegroupid)
123
+ self.class.base_uri serviceroot
124
+ self.class.default_options[:headers] = { "Accept" => "application/json;version=5.7", "Authorization" => "Bearer " + token }
125
+ billedcostsarray = self.class.get('/api/billing/service-group/' + servicegroupid + '/billed-costs')
126
+ return billedcostsarray
127
+ end #billedcosts
128
+
129
+
130
+
131
+
132
+ def billedusage(token, serviceroot, servicegroupid)
133
+ self.class.base_uri serviceroot
134
+ self.class.default_options[:headers] = { "Accept" => "application/json;version=5.7", "Authorization" => "Bearer " + token }
135
+ billedusagearray = self.class.get('/api/billing/service-group/' + servicegroupid + '/billed-usage')
136
+ end #billedusage
137
+
138
+
139
+
140
+
141
+ end #Billing
142
+
143
+
144
+
145
+
146
+
147
+
148
+ class Metering
149
+ include HTTParty
150
+ format :json
151
+ #debug_output $stderr
152
+
153
+
154
+ def billablecosts(token, serviceroot, servicegroupid)
155
+ self.class.base_uri serviceroot
156
+ self.class.default_options[:headers] = { "Accept" => "application/json;version=5.7", "Authorization" => "Bearer " + token }
157
+ billablecostsarray = self.class.get('/api/metering/service-group/' + servicegroupid + '/billable-costs')
158
+ return billablecostsarray
159
+ end #billablecosts
160
+
161
+
162
+
163
+ def billableusage(token, serviceroot, instanceid)
164
+ self.class.base_uri serviceroot
165
+ self.class.default_options[:headers] = { "Accept" => "application/json;version=5.7", "Authorization" => "Bearer " + token }
166
+ billableusagearray = self.class.get('/api/metering/service-instance/' + instanceid + '/billable-usage')
167
+ return billableusagearray
168
+ end #billableusage
169
+
170
+
171
+
172
+
173
+ end #Metering
174
+
175
+
176
+
177
+
178
+ class Misc
179
+ include HTTParty
180
+ format :xml
181
+ #debug_output $stderr
182
+
183
+ def customquery(token, serviceroot, customapicall, acceptcontentspecific)
184
+ if acceptcontentspecific != nil
185
+ acceptcontent = "application/xml" + ";class=" + acceptcontentspecific + ";version=5.7"
186
+ end
187
+ self.class.base_uri serviceroot
188
+ puts acceptcontent
189
+ self.class.default_options[:headers] = { "Accept" => acceptcontent, "Authorization" => "Bearer " + token }
190
+ customresult = self.class.get(customapicall)
191
+ return customresult
192
+ end #customquery
193
+
194
+
195
+ def extractendpoint(instanceattributes)
196
+ #I turn the string into a hash
197
+ attributes = JSON.parse(instanceattributes)
198
+ #I return the orgname and sessionuri values in the hash (note that I clean the uri to only provide the FQDN)
199
+ return attributes["orgName"], attributes["sessionUri"][0..-14]
200
+ end #extractendpoint
201
+
202
+
203
+
204
+
205
+ end #Misc
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
@@ -0,0 +1,63 @@
1
+ #################################################################################
2
+ #### Massimo Re Ferre' ####
3
+ #### www.it20.info ####
4
+ #### vcautils, a set of utilities for vCloud Air Consumers ####
5
+ #################################################################################
6
+
7
+ class Vchs
8
+ include HTTParty
9
+ format :xml
10
+ #debug_output $stderr
11
+
12
+
13
+ def hashtoarray(structure)
14
+ #this function work arounds an issue of httparty (and other REST clients apparently) that do not comply to the XML Schema correctly
15
+ #in some circumstances the httparty response contains a hash whereas it should be an array of hash with one item
16
+ #this function takes input a JSON structure and check if it's a hash. If it is, it will turn it into an array of hash with one element
17
+ #if the input is already an Array of hash it will do nothing
18
+ #for further reference: http://stackoverflow.com/questions/28282125/httparty-response-interpretation-in-ruby/
19
+ structure = [structure] unless structure.is_a? Array
20
+ return structure
21
+ end #hashtoarray
22
+
23
+
24
+ def login(username, password, subscriptionserviceroot)
25
+ self.class.base_uri subscriptionserviceroot
26
+ #Avoid setting the basic_auth as (due to an httparty problem) it propagates and gets retained for other calls inside the class
27
+ #self.class.basic_auth username, password
28
+ auth_token = Base64.encode64(username + ":" + password)
29
+ self.class.default_options[:headers] = {"Accept" => "application/xml;version=5.6", "Authorization" => "Basic " + auth_token}
30
+ response = self.class.post('/api/vchs/sessions')
31
+ token = response.headers['x-vchs-authorization']
32
+ return token
33
+ end #login
34
+
35
+
36
+ def services(token, subscriptionserviceroot)
37
+ self.class.base_uri subscriptionserviceroot
38
+ self.class.default_options[:headers] = { "Accept" => "application/xml;version=5.6", "x-vchs-authorization" => token }
39
+ servicesarray = self.class.get('/api/vchs/services')
40
+ servicesarray["Services"]["Service"] = hashtoarray(servicesarray["Services"]["Service"])
41
+ return servicesarray
42
+ end #services
43
+
44
+
45
+ def vdcs(token,href)
46
+ self.class.base_uri href
47
+ self.class.default_options[:headers] = { "Accept" => "application/xml;version=5.6", "x-vchs-authorization" => token }
48
+ vdcssarray = self.class.get('')
49
+ vdcssarray["Compute"]["VdcRef"] = hashtoarray(vdcssarray["Compute"]["VdcRef"])
50
+ return vdcssarray
51
+ end #vdcs
52
+
53
+
54
+ def computeattributes(token,href)
55
+ self.class.base_uri href
56
+ self.class.default_options[:headers] = { "Accept" => "application/xml;version=5.6", "x-vchs-authorization" => token }
57
+ computeattributes = self.class.post('')
58
+ return computeattributes
59
+ end #computeattributes
60
+
61
+
62
+ end #Subscription
63
+