vcloud-rest 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGELOG.md +22 -0
  2. data/LICENSE +202 -0
  3. data/README.md +88 -0
  4. data/lib/vcloud-rest/connection.rb +419 -0
  5. metadata +82 -0
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ Changes
2
+ ==
3
+ 2012-12-19 (0.1.1)
4
+ --
5
+
6
+ * Fix gemspec URL
7
+
8
+ 2012-12-19 (0.1.0)
9
+ --
10
+
11
+ * Add support for main operations:
12
+ * login/logout
13
+ * organization _list/show_
14
+ * vdc _show_
15
+ * catalog _show_
16
+ * catalog item _show_
17
+ * vapp _create/delete/startup/shutdown_
18
+
19
+ 2012-12-14 (0.0.1)
20
+ --
21
+
22
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ vcloud-rest [![Build Status](https://secure.travis-ci.org/astratto/vcloud-rest.png?branch=master)](http://travis-ci.org/astratto/vcloud-rest) [![Dependency Status](https://gemnasium.com/astratto/vcloud-rest.png)](https://gemnasium.com/astratto/vcloud-rest)
2
+ ===========
3
+
4
+ DESCRIPTION
5
+ --
6
+ Unofficial ruby bindings for VMWare® vCloud Director's rest APIs.
7
+
8
+ Note: at this stage both _v.1.5_ and _v.5.1_ are supported. It defaults to _v.5.1_ but it's possible to specify _api\_version="1.5"_.
9
+
10
+ See [vCloud API](http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.doc_51/GUID-86CA32C2-3753-49B2-A471-1CE460109ADB.html) for details.
11
+
12
+ This code is BETA QUALITY.
13
+
14
+ INSTALLATION
15
+ --
16
+ This plugin is distributed as a Ruby Gem. To install it, run:
17
+
18
+ gem install vcloud-rest
19
+
20
+ Depending on your system's configuration, you may need to run this command with root privileges.
21
+
22
+ vcloud-rest is tested against ruby 1.9.x and 1.8.7+.
23
+
24
+ FEATURES
25
+ --
26
+ - login/logout
27
+ - list/show Organizations
28
+ - show VDCs
29
+ - show Catalogs
30
+ - show Catalog Items
31
+ - create/start/stop/delete vApps
32
+
33
+ TODO
34
+ --
35
+ - extend test coverage
36
+ - a lot more...
37
+
38
+ PREREQUISITES
39
+ --
40
+ - nokogiri ~> 1.5.5
41
+ - rest-client ~> 1.6.7
42
+
43
+ For testing purpose:
44
+ - minitest (included in ruby 1.9)
45
+ - webmock
46
+
47
+ USAGE
48
+ --
49
+
50
+ require 'vcloud-rest/connection'
51
+ conn = VCloudClient::Connection.new(HOST, USER, PASSWORD, ORG_NAME)
52
+ conn.login
53
+ conn.list_organizations
54
+
55
+ TESTING
56
+ --
57
+ Simply run:
58
+
59
+ rake
60
+ Or:
61
+
62
+ ruby spec/connection_spec.rb
63
+
64
+ Note: in order to run tests with ruby 1.8.7+ you need to export RUBYOPT="rubygems"
65
+
66
+ LICENSE
67
+ --
68
+
69
+ Author:: Stefano Tortarolo <stefano.tortarolo@gmail.com>
70
+
71
+ Copyright:: Copyright (c) 2012
72
+ License:: Apache License, Version 2.0
73
+
74
+ Licensed under the Apache License, Version 2.0 (the "License");
75
+ you may not use this file except in compliance with the License.
76
+ You may obtain a copy of the License at
77
+
78
+ http://www.apache.org/licenses/LICENSE-2.0
79
+
80
+ Unless required by applicable law or agreed to in writing, software
81
+ distributed under the License is distributed on an "AS IS" BASIS,
82
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
83
+ See the License for the specific language governing permissions and
84
+ limitations under the License.
85
+
86
+ CREDITS
87
+ --
88
+ This code was inspired by [knife-cloudstack](https://github.com/CloudStack-extras/knife-cloudstack).
@@ -0,0 +1,419 @@
1
+ #
2
+ # Author:: Stefano Tortarolo (<stefano.tortarolo@gmail.com>)
3
+ # Copyright:: Copyright (c) 2012
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'rest-client'
20
+ require 'nokogiri'
21
+
22
+ module VCloudClient
23
+ class UnauthorizedAccess < StandardError; end
24
+ class WrongAPIVersion < StandardError; end
25
+ class WrongItemIDError < StandardError; end
26
+ class InvalidStateError < StandardError; end
27
+ class UnhandledError < StandardError; end
28
+
29
+ # Main class to access vCloud rest APIs
30
+ class Connection
31
+ attr_reader :api_url, :auth_key
32
+
33
+ def initialize(host, username, password, org_name, api_version)
34
+ @host = host
35
+ @api_url = "#{host}/api"
36
+ @username = username
37
+ @password = password
38
+ @org_name = org_name
39
+ @api_version = (api_version || "5.1")
40
+ end
41
+
42
+ ##
43
+ # Authenticate against the specified server
44
+ def login
45
+ params = {
46
+ 'method' => :post,
47
+ 'command' => '/sessions'
48
+ }
49
+
50
+ response, headers = send_request(params)
51
+
52
+ if !headers.has_key?(:x_vcloud_authorization)
53
+ raise "Unable to authenticate: missing x_vcloud_authorization header"
54
+ end
55
+
56
+ @auth_key = headers[:x_vcloud_authorization]
57
+ end
58
+
59
+ ##
60
+ # Destroy the current session
61
+ def logout
62
+ params = {
63
+ 'method' => :delete,
64
+ 'command' => '/session'
65
+ }
66
+
67
+ response, headers = send_request(params)
68
+ end
69
+
70
+ ##
71
+ # List existing organizations and their IDs
72
+ def list_organizations
73
+ params = {
74
+ 'method' => :get,
75
+ 'command' => '/org'
76
+ }
77
+
78
+ response, headers = send_request(params)
79
+ orgs = response.css('OrgList Org')
80
+
81
+ results = {}
82
+ orgs.each do |org|
83
+ results[org['name']] = org['href'].gsub("#{@api_url}/org/", "")
84
+ end
85
+ results
86
+ end
87
+
88
+ ##
89
+ # Show details about an organization:
90
+ # - catalogs
91
+ # - vdcs
92
+ # - networks
93
+ def show_organization(orgId)
94
+ params = {
95
+ 'method' => :get,
96
+ 'command' => "/org/#{orgId}"
97
+ }
98
+
99
+ response, headers = send_request(params)
100
+ catalogs = {}
101
+ response.css("Link[type='application/vnd.vmware.vcloud.catalog+xml']").each do |item|
102
+ catalogs[item['name']] = item['href'].gsub("#{@api_url}/catalog/", "")
103
+ end
104
+
105
+ vdcs = {}
106
+ response.css("Link[type='application/vnd.vmware.vcloud.vdc+xml']").each do |item|
107
+ vdcs[item['name']] = item['href'].gsub("#{@api_url}/vdc/", "")
108
+ end
109
+
110
+ networks = {}
111
+ response.css("Link[type='application/vnd.vmware.vcloud.orgNetwork+xml']").each do |item|
112
+ networks[item['name']] = item['href'].gsub("#{@api_url}/network/", "")
113
+ end
114
+
115
+ tasklists = {}
116
+ response.css("Link[type='application/vnd.vmware.vcloud.tasksList+xml']").each do |item|
117
+ tasklists[item['name']] = item['href'].gsub("#{@api_url}/tasksList/", "")
118
+ end
119
+
120
+ [catalogs, vdcs, networks, tasklists]
121
+ end
122
+
123
+ ##
124
+ # Show details about a given catalog
125
+ def show_catalog(catalogId)
126
+ params = {
127
+ 'method' => :get,
128
+ 'command' => "/catalog/#{catalogId}"
129
+ }
130
+
131
+ response, headers = send_request(params)
132
+ description = response.css("Description").first
133
+ description = description.text unless description.nil?
134
+
135
+ items = {}
136
+ response.css("CatalogItem[type='application/vnd.vmware.vcloud.catalogItem+xml']").each do |item|
137
+ items[item['name']] = item['href'].gsub("#{@api_url}/catalogItem/", "")
138
+ end
139
+
140
+ [description, items]
141
+ end
142
+
143
+ ##
144
+ # Show details about a given vdc:
145
+ # - description
146
+ # - vapps
147
+ # - networks
148
+ def show_vdc(vdcId)
149
+ params = {
150
+ 'method' => :get,
151
+ 'command' => "/vdc/#{vdcId}"
152
+ }
153
+
154
+ response, headers = send_request(params)
155
+ description = response.css("Description").first
156
+ description = description.text unless description.nil?
157
+
158
+ vapps = {}
159
+ response.css("ResourceEntity[type='application/vnd.vmware.vcloud.vApp+xml']").each do |item|
160
+ vapps[item['name']] = item['href'].gsub("#{@api_url}/vApp/vapp-", "")
161
+ end
162
+
163
+ networks = {}
164
+ response.css("Network[type='application/vnd.vmware.vcloud.network+xml']").each do |item|
165
+ networks[item['name']] = item['href'].gsub("#{@api_url}/network/", "")
166
+ end
167
+
168
+ [description, vapps, networks]
169
+ end
170
+
171
+ ##
172
+ # Show details about a given catalog item:
173
+ # - description
174
+ # - vApp templates
175
+ def show_catalog_item(catalogItemId)
176
+ params = {
177
+ 'method' => :get,
178
+ 'command' => "/catalogItem/#{catalogItemId}"
179
+ }
180
+
181
+ response, headers = send_request(params)
182
+ description = response.css("Description").first
183
+ description = description.text unless description.nil?
184
+
185
+ items = {}
186
+ response.css("Entity[type='application/vnd.vmware.vcloud.vAppTemplate+xml']").each do |item|
187
+ items[item['name']] = item['href'].gsub("#{@api_url}/vAppTemplate/", "")
188
+ end
189
+
190
+ [description, items]
191
+ end
192
+
193
+ ##
194
+ # Show details about a given vapp:
195
+ # - name
196
+ # - description
197
+ # - status
198
+ # - IP
199
+ # - Children VMs:
200
+ # -- IP addresses
201
+ # -- status
202
+ # -- ID
203
+ def show_vapp(vAppId)
204
+ params = {
205
+ 'method' => :get,
206
+ 'command' => "/vApp/vapp-#{vAppId}"
207
+ }
208
+
209
+ response, headers = send_request(params)
210
+
211
+ vapp_node = response.css('VApp').first
212
+ if vapp_node
213
+ name = vapp_node['name']
214
+ status = convert_status(vapp_node['status'])
215
+ end
216
+
217
+ description = response.css("Description").first
218
+ description = description.text unless description.nil?
219
+
220
+ ip = response.css('IpAddress').first
221
+ ip = ip.text unless ip.nil?
222
+
223
+ vms = response.css('Children Vm')
224
+ vms_hash = {}
225
+ vms.each do |vm|
226
+ addresses = vm.css('rasd|Connection').collect{|n| n['ipAddress']}
227
+ vms_hash[vm['name']] = {:addresses => addresses,
228
+ :status => convert_status(vm['status']),
229
+ :id => vm['href'].gsub("#{@api_url}/vApp/vm-", '')
230
+ }
231
+ end
232
+
233
+ # TODO: EXPAND INFO FROM RESPONSE
234
+ [name, description, status, ip, vms_hash]
235
+ end
236
+
237
+ ##
238
+ # Delete a given vapp
239
+ # NOTE: It doesn't verify that the vapp is shutdown
240
+ def delete_vapp(vAppId)
241
+ params = {
242
+ 'method' => :delete,
243
+ 'command' => "/vApp/vapp-#{vAppId}"
244
+ }
245
+
246
+ response, headers = send_request(params)
247
+ task_id = headers[:location].gsub("#{@api_url}/task/", "")
248
+ task_id
249
+ end
250
+
251
+ ##
252
+ # Shutdown a given vapp
253
+ def poweroff_vapp(vAppId)
254
+ builder = Nokogiri::XML::Builder.new do |xml|
255
+ xml.UndeployVAppParams(
256
+ "xmlns" => "http://www.vmware.com/vcloud/v1.5") {
257
+ xml.UndeployPowerAction 'powerOff'
258
+ }
259
+ end
260
+
261
+ params = {
262
+ 'method' => :post,
263
+ 'command' => "/vApp/vapp-#{vAppId}/action/undeploy"
264
+ }
265
+
266
+ response, headers = send_request(params, builder.to_xml,
267
+ "application/vnd.vmware.vcloud.undeployVAppParams+xml")
268
+
269
+ response
270
+ end
271
+
272
+ ##
273
+ # Boot a given vapp
274
+ def poweron_vapp(vAppId)
275
+ params = {
276
+ 'method' => :post,
277
+ 'command' => "/vApp/vapp-#{vAppId}/power/action/powerOn"
278
+ }
279
+
280
+ response, headers = send_request(params)
281
+ # TODO: track Task using headers[:location]
282
+ [response, headers]
283
+ end
284
+
285
+ ##
286
+ # Create a vapp starting from a template
287
+ #
288
+ # Params:
289
+ # - vdc: the associated VDC
290
+ # - vapp_name: name of the target vapp
291
+ # - vapp_description: description of the target vapp
292
+ # - vapp_templateid: ID of the vapp template
293
+ def create_vapp_from_template(vdc, vapp_name, vapp_description, vapp_templateid)
294
+ builder = Nokogiri::XML::Builder.new do |xml|
295
+ xml.InstantiateVAppTemplateParams(
296
+ "xmlns" => "http://www.vmware.com/vcloud/v1.5",
297
+ "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
298
+ "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1",
299
+ "name" => vapp_name,
300
+ "deploy" => "true",
301
+ "powerOn" => "true") {
302
+ xml.Description vapp_description
303
+ xml.Source("href" => "#{@api_url}/vAppTemplate/#{vapp_templateid}")
304
+ }
305
+ end
306
+
307
+ params = {
308
+ "method" => :post,
309
+ "command" => "/vdc/#{vdc}/action/instantiateVAppTemplate"
310
+ }
311
+
312
+ response, headers = send_request(params, builder.to_xml, "application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml")
313
+
314
+ vapp_id = headers[:location].gsub("#{@api_url}/vApp/vapp-", "")
315
+
316
+ task = response.css("VApp Task[operationName='vdcInstantiateVapp']").first
317
+ task_id = task["href"].gsub("#{@api_url}/task/", "")
318
+
319
+ [vapp_id, task_id]
320
+ end
321
+
322
+ ##
323
+ # Show a given task
324
+ def show_task(taskid)
325
+ params = {
326
+ 'method' => :get,
327
+ 'command' => "/task/#{taskid}"
328
+ }
329
+
330
+ response, headers = send_request(params)
331
+
332
+ task = response.css('Task').first
333
+ status = task['status']
334
+ start_time = task['startTime']
335
+ end_time = task['endTime']
336
+
337
+ [status, start_time, end_time, response]
338
+ end
339
+
340
+ ##
341
+ # Poll a given task until completion
342
+ def wait_task_completion(taskid)
343
+ status, errormsg, start_time, end_time, response = nil
344
+ loop do
345
+ status, start_time, end_time, response = show_task(taskid)
346
+ break if status != 'running'
347
+ sleep 1
348
+ end
349
+
350
+ if status == 'error'
351
+ errormsg = response.css("Error").first
352
+ errormsg = "Error code #{errormsg['majorErrorCode']} - #{errormsg['message']}"
353
+ end
354
+
355
+ [status, errormsg, start_time, end_time]
356
+ end
357
+
358
+ private
359
+ ##
360
+ # Sends a synchronous request to the vCloud API and returns the response as parsed XML + headers.
361
+ def send_request(params, payload=nil, content_type=nil)
362
+ headers = {:accept => "application/*+xml;version=#{@api_version}"}
363
+ if @auth_key
364
+ headers.merge!({:x_vcloud_authorization => @auth_key})
365
+ end
366
+
367
+ if content_type
368
+ headers.merge!({:content_type => content_type})
369
+ end
370
+
371
+ request = RestClient::Request.new(:method => params['method'],
372
+ :user => "#{@username}@#{@org_name}",
373
+ :password => @password,
374
+ :headers => headers,
375
+ :url => "#{@api_url}#{params['command']}",
376
+ :payload => payload)
377
+ begin
378
+ response = request.execute
379
+ if ![200, 201, 202, 204].include?(response.code)
380
+ puts "Warning: unattended code #{response.code}"
381
+ end
382
+
383
+ # TODO: handle asynch properly, see TasksList
384
+ [Nokogiri.parse(response), response.headers]
385
+ rescue RestClient::Unauthorized => e
386
+ raise UnauthorizedAccess, "Client not authorized. Please check your credentials."
387
+ rescue RestClient::BadRequest => e
388
+ body = Nokogiri.parse(e.http_body)
389
+ message = body.css("Error").first["message"]
390
+
391
+ case message
392
+ when /The request has invalid accept header/
393
+ raise WrongAPIVersion, "Invalid accept header. Please verify that the server supports v.#{@api_version} or specify a different API Version."
394
+ when /validation error on field 'id': String value has invalid format or length/
395
+ raise WrongItemIDError, "Invalid ID specified. Please verify that the item exists and correctly typed."
396
+ when /The requested operation could not be executed on vApp "(.*)". Stop the vApp and try again/
397
+ raise InvalidStateError, "Invalid request. Stop vApp '#{$1}' and try again."
398
+ else
399
+ raise UnhandledError, "BadRequest - unhandled error: #{message}.\nPlease report this issue."
400
+ end
401
+ end
402
+ end
403
+
404
+ ##
405
+ # Convert status codes into human readable description
406
+ def convert_status(status_code)
407
+ case status_code.to_i
408
+ when 3
409
+ 'suspended'
410
+ when 4
411
+ 'running'
412
+ when 8
413
+ 'stopped'
414
+ else
415
+ "Unknown #{status_code}"
416
+ end
417
+ end
418
+ end # class
419
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vcloud-rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefano Tortarolo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
46
+ description: Ruby bindings to create, list and manage vCloud servers
47
+ email:
48
+ - stefano.tortarolo@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - CHANGELOG.md
54
+ - README.md
55
+ - LICENSE
56
+ - lib/vcloud-rest/connection.rb
57
+ homepage: https://github.com/astratto/vcloud-rest
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.24
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Unofficial ruby bindings for VMWare vCloud's API
81
+ test_files: []
82
+ has_rdoc: