This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
/
vcd_vapp.py
337 lines (312 loc) · 14.4 KB
/
vcd_vapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# VMware vCloud Director Python SDK
# Copyright (c) 2017-2018 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from pyvcloud.vcd.client import NSMAP
from pyvcloud.vcd.client import TaskStatus
from pyvcloud.vcd.org import Org
from pyvcloud.vcd.test import TestCase
from pyvcloud.vcd.vapp import VApp
from pyvcloud.vcd.vdc import VDC
class TestVApp(TestCase):
def test_001_instantiate_vapp(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')
result = vdc.instantiate_vapp(
self.config['vcd']['vapp'],
self.config['vcd']['catalog'],
self.config['vcd']['template'],
network='net2',
fence_mode='natRouted',
deploy=False,
power_on=False)
task = self.client.get_task_monitor().wait_for_status(
task=result.Tasks.Task[0],
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
@unittest.skip("reconfigure_vapp_network is not a valid method")
def test_011_update_vapp_network(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')
vapp = vdc.get_vapp(self.config['vcd']['vapp'])
assert self.config['vcd']['vapp'] == vapp.get('name')
result = vdc.reconfigure_vapp_network(self.config['vcd']['vapp'],
'net2',
self.config['vcd']['fence_mode'])
task = self.client.get_task_monitor().wait_for_status(
task=result,
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
def test_012_add_disk(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
assert self.config['vcd']['vapp'] == vapp_resource.get('name')
vm_name = self.config['vcd']['vm']
vapp = VApp(self.client, resource=vapp_resource)
disk_size = 1024 # 1GB
result = vapp.add_disk_to_vm(vm_name, disk_size)
task = self.client.get_task_monitor().wait_for_status(
task=result,
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
@unittest.skip("param identical is invalid")
def test_100_instantiate_vapp_identical(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')
result = vdc.instantiate_vapp(
self.config['vcd']['vapp'],
self.config['vcd']['catalog'],
self.config['vcd']['template'],
network=self.config['vcd']['network'],
fence_mode='bridged',
deploy=True,
power_on=False,
identical=True)
task = self.client.get_task_monitor().wait_for_status(
task=result.Tasks.Task[0],
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
vdc.reload()
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vm = vapp_resource.xpath(
'//vcloud:VApp/vcloud:Children/vcloud:Vm', namespaces=NSMAP)
assert len(vm) > 0
assert vm[0].get('name') == self.config['vcd']['vm']
def test_101_connect_orgvdc_network(self):
org_in_use = self.config['vcd']['org_in_use']
org = Org(self.client,
href=self.client.get_org_by_name(org_in_use).get('href'))
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
result = vapp.connect_org_vdc_network(self.config['vcd'][
'orgvdc_network'])
task = self.client.get_task_monitor().wait_for_status(
task=result,
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
def test_102_disconnect_orgvdc_network(self):
org_in_use = self.config['vcd']['org_in_use']
org = Org(self.client,
href=self.client.get_org_by_name(org_in_use).get('href'))
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
result = vapp.disconnect_org_vdc_network(self.config['vcd'][
'orgvdc_network'])
task = self.client.get_task_monitor().wait_for_status(
task=result,
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
def test_11_change_owner(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
user_resource = org.get_user(self.config['vcd']['new_vapp_user'])
vdc = VDC(self.client, href=vdc_resource.get('href'))
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
vapp.change_owner(user_resource.get('href'))
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
assert self.config['vcd']['new_vapp_user'] == \
vapp_resource.Owner.User.get('name')
def test_110_instantiate_vapp_custom_disk_size(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')
result = vdc.instantiate_vapp(
self.config['vcd']['vapp'],
self.config['vcd']['catalog'],
self.config['vcd']['template'],
network=self.config['vcd']['network'],
fence_mode='bridged',
deploy=True,
power_on=False,
disk_size=self.config['vcd']['disk_size_new'])
task = self.client.get_task_monitor().wait_for_status(
task=result.Tasks.Task[0],
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
vdc.resource = vdc.client.get_resource(vdc.href)
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vms = vapp_resource.xpath(
'//vcloud:VApp/vcloud:Children/vcloud:Vm', namespaces=NSMAP)
assert len(vms) > 0
items = vms[0].xpath(
'//ovf:VirtualHardwareSection/ovf:Item',
namespaces={
'ovf': NSMAP['ovf']
})
assert len(items) > 0
found_disk = False
for item in items:
if item['{' + NSMAP['rasd'] + '}ResourceType'] == 17: # NOQA
found_disk = True
assert item['{' + NSMAP['rasd'] + '}VirtualQuantity'] == \
(self.config['vcd']['disk_size_new'] * 1024 * 1024)
break
# this check makes sure that the vm isn't disk-less
assert found_disk
# cleanup
self.test_100_delete_vapp()
def test_1000_delete_vapp(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
v = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, href=v.get('href'))
assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')
result = vdc.delete_vapp(self.config['vcd']['vapp'], force=True)
task = self.client.get_task_monitor().wait_for_status(
task=result,
timeout=60,
poll_frequency=2,
fail_on_statuses=None,
expected_target_statuses=[
TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
TaskStatus.CANCELED
],
callback=None)
assert task.get('status') == TaskStatus.SUCCESS.value
def test_1001_remove_all_vapp_access(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, resource=vdc_resource)
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
control_access = vapp.remove_access_settings(remove_all=True)
self.assertFalse(hasattr(control_access, 'AccessSettings'))
def test_1002_add_vapp_access(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, resource=vdc_resource)
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
control_access = vapp.add_access_settings(
access_settings_list=[
{'name': self.config['vcd']['access_user'], 'type': 'user'},
{'name': self.config['vcd']['access_user1'], 'type': 'user',
'access_level': 'Change'}
])
assert len(control_access.AccessSettings.AccessSetting) == 2
def test_1003_get_vapp_access(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, resource=vdc_resource)
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
control_access = vapp.get_access_settings()
assert len(control_access.AccessSettings.AccessSetting) == 2
def test_1004_remove_vapp_access(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, resource=vdc_resource)
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
control_access = vapp.remove_access_settings(
access_settings_list=[
{'name': self.config['vcd']['access_user'], 'type': 'user'}
])
assert len(control_access.AccessSettings.AccessSetting) == 1
def test_1005_share_vapp_access(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, resource=vdc_resource)
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
control_access = vapp.share_with_org_members(
everyone_access_level='ReadOnly')
assert control_access.IsSharedToEveryone.text == 'true'
assert control_access.EveryoneAccessLevel.text == 'ReadOnly'
def test_1006_unshare_vapp_access(self):
logged_in_org = self.client.get_org()
org = Org(self.client, resource=logged_in_org)
vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
vdc = VDC(self.client, resource=vdc_resource)
vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
vapp = VApp(self.client, resource=vapp_resource)
control_access = vapp.unshare_from_org_members()
assert control_access.IsSharedToEveryone.text == 'false'
if __name__ == '__main__':
unittest.main()