This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
/
client.py
1480 lines (1265 loc) · 53.5 KB
/
client.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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.
from datetime import datetime
from datetime import timedelta
from distutils.version import StrictVersion
import json
import logging
import sys
import time
import urllib
from flufl.enum import Enum
from lxml import etree
from lxml import objectify
import requests
from pyvcloud.vcd.exceptions import AccessForbiddenException, \
BadRequestException, ClientException, ConflictException, \
EntityNotFoundException, InternalServerException, \
InvalidContentLengthException, MethodNotAllowedException, \
MissingLinkException, MissingRecordException, MultipleLinksException, \
MultipleRecordsException, NotAcceptableException, NotFoundException, \
OperationNotSupportedException, RequestTimeoutException,\
TaskTimeoutException, UnauthorizedException, UnknownApiException, \
UnsupportedMediaTypeException, VcdException, VcdTaskException # NOQA
SIZE_1MB = 1024 * 1024
NSMAP = {
'ovf':
'http://schemas.dmtf.org/ovf/envelope/1',
'ovfenv':
'http://schemas.dmtf.org/ovf/environment/1',
'rasd':
'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/'
'2/CIM_ResourceAllocationSettingData',
'vcloud':
'http://www.vmware.com/vcloud/v1.5',
've':
'http://www.vmware.com/schema/ovfenv',
'vmw':
'http://www.vmware.com/schema/ovf',
'vmext':
'http://www.vmware.com/vcloud/extension/v1.5',
'xs':
'http://www.w3.org/2001/XMLSchema',
'xsi':
'http://www.w3.org/2001/XMLSchema-instance'
}
# Convenience objects for building vCloud API XML objects
E = objectify.ElementMaker(
annotate=False,
namespace=NSMAP['vcloud'],
nsmap={
None: NSMAP['vcloud'],
'xsi': NSMAP['xsi'],
'xs': NSMAP['xs'],
'ovf': NSMAP['ovf']
})
E_VMEXT = objectify.ElementMaker(
annotate=False,
namespace=NSMAP['vmext'],
nsmap={
None: NSMAP['vcloud'],
'vmext': NSMAP['vmext']
})
E_OVF = objectify.ElementMaker(
annotate=False, namespace=NSMAP['ovf'], nsmap={
None: NSMAP['ovf']
})
E_RASD = objectify.ElementMaker(
annotate=False,
namespace=NSMAP['rasd'],
nsmap={
None: NSMAP['rasd'],
'vcloud': NSMAP['vcloud']
})
# Important! Values must be listed in ascending order.
API_CURRENT_VERSIONS = [
'27.0', '28.0', '29.0', '30.0'
]
VCLOUD_STATUS_MAP = {
-1: "Could not be created",
0: "Unresolved",
1: "Resolved",
2: "Deployed",
3: "Suspended",
4: "Powered on",
5: "Waiting for user input",
6: "Unknown state",
7: "Unrecognized state",
8: "Powered off",
9: "Inconsistent state",
10: "Children do not all have the same status",
11: "Upload initiated, OVF descriptor pending",
12: "Upload initiated, copying contents",
13: "Upload initiated , disk contents pending",
14: "Upload has been quarantined",
15: "Upload quarantine period has expired"
}
class BasicLoginCredentials(object):
def __init__(self, user, org, password):
self.user = user
self.org = org
self.password = password
class RelationType(Enum):
ADD = 'add'
ALTERNATE = 'alternate'
CONTROL_ACCESS = 'controlAccess'
DEPLOY = 'deploy'
DISABLE = 'disable'
DISK_ATTACH = 'disk:attach'
DISK_DETACH = 'disk:detach'
DOWN = 'down'
DOWN_EXTENSIBILITY = 'down:extensibility'
DOWNLOAD_DEFAULT = 'download:default'
EDGE_GATEWAYS = 'edgeGateways'
EDIT = 'edit'
ENABLE = 'enable'
LINK_TO_TEMPLATE = 'linkToTemplate'
NEXT_PAGE = 'nextPage'
ORG_VDC_NETWORKS = 'orgVdcNetworks'
POWER_OFF = 'power:powerOff'
POWER_ON = 'power:powerOn'
POWER_REBOOT = 'power:reboot'
POWER_RESET = 'power:reset'
POWER_SHUTDOWN = 'power:shutdown'
POWER_SUSPEND = 'power:suspend'
PUBLISH = 'publish'
RECOMPOSE = 'recompose'
REMOVE = 'remove'
SNAPSHOT_CREATE = 'snapshot:create'
SNAPSHOT_REVERT_TO_CURRENT = 'snapshot:revertToCurrent'
SNAPSHOT_REMOVE_ALL = 'snapshot:removeAll'
TASK_CANCEL = 'task:cancel'
UNDEPLOY = 'undeploy'
UNLINK_FROM_TEMPLATE = 'unlinkFromTemplate'
UNREGISTER = 'unregister'
UP = 'up'
class ResourceType(Enum):
"""Contains resource type names."""
ACL_RULE = 'aclRule'
ADMIN_API_DEFINITION = 'adminApiDefinition'
ADMIN_ALLOCATED_EXTERNAL_ADDRESS = 'adminAllocatedExternalAddress'
ADMIN_CATALOG = 'adminCatalog'
ADMIN_CATALOG_ITEM = 'adminCatalogItem'
ADMIN_DISK = 'adminDisk'
ADMIN_EVENT = 'adminEvent'
ADMIN_FILE_DESCRIPTOR = 'adminFileDescriptor'
ADMIN_GROUP = 'adminGroup'
ADMIN_MEDIA = 'adminMedia'
ADMIN_ORG_NETWORK = 'adminOrgNetwork'
ADMIN_ORG_VDC = 'adminOrgVdc'
ADMIN_ORG_VDC_STORAGE_PROFILE = 'adminOrgVdcStorageProfile'
ADMIN_ROLE = 'adminRole'
ADMIN_SERVICE = 'adminService'
ADMIN_SHADOW_VM = 'adminShadowVM'
ADMIN_TASK = 'adminTask'
ADMIN_USER = 'adminUser'
ADMIN_VAPP = 'adminVApp'
ADMIN_VAPP_NETWORK = 'adminVAppNetwork'
ADMIN_VAPP_TEMPLATE = 'adminVAppTemplate'
ADMIN_VM = 'adminVM'
ADMIN_VM_DISK_RELATION = 'adminVMDiskRelation'
ALLOCATED_EXTERNAL_ADDRESS = 'allocatedExternalAddress'
API_DEFINITION = 'apiDefinition'
API_FILTER = 'apiFilter'
BLOCKING_TASK = 'blockingTask'
CATALOG = 'catalog'
CATALOG_ITEM = 'catalogItem'
CELL = 'cell'
CONDITION = 'condition'
DATASTORE = 'datastore'
DATASTORE_PROVIDER_VDC_RELATION = 'datastoreProviderVdcRelation'
DISK = 'disk'
DV_SWITCH = 'dvSwitch'
EDGE_GATEWAY = 'edgeGateway'
EVENT = 'event'
EXTERNAL_LOCALIZATION = 'externalLocalization'
EXTERNAL_NETWORK = 'externalNetwork'
FILE_DESCRIPTOR = 'fileDescriptor'
FROM_CLOUD_TUNNEL = 'fromCloudTunnel'
GROUP = 'group'
HOST = 'host'
MEDIA = 'media'
NETWORK_POOL = 'networkPool'
NSXT_MANAGER = 'nsxTManager'
ORGANIZATION = 'organization'
ORG_NETWORK = 'orgNetwork'
ORG_VDC = 'orgVdc'
ORG_VDC_NETWORK = 'orgVdcNetwork'
ORG_VDC_RESOURCE_POOL_RELATION = 'orgVdcResourcePoolRelation'
ORG_VDC_STORAGE_PROFILE = 'orgVdcStorageProfile'
PORT_GROUP = 'portGroup'
PROVIDER_VDC = 'providerVdc'
PROVIDER_VDC_RESOURCE_POOL_RELATION = 'providerVdcResourcePoolRelation'
PROVIDER_VDC_STORAGE_PROFILE = 'providerVdcStorageProfile'
RESOURCE_POOL = 'resourcePool'
RESOURCE_POOL_VM_LIST = 'resourcePoolVmList'
RIGHT = 'right'
RESOURCE_CLASS = 'resourceClass'
RESOURCE_CLASS_ACTION = 'resourceClassAction'
ROLE = 'role'
SERVICE = 'service'
SERVICE_LINK = 'serviceLink'
SERVICE_RESOURCE = 'serviceResource'
STRANDED_ITEM = 'strandedItem'
STRANDED_USER = 'strandedUser'
TASK = 'task'
TO_CLOUD_TUNNEL = 'toCloudTunnel'
USER = 'user'
VAPP = 'vApp'
VAPP_NETWORK = 'vAppNetwork'
VAPP_ORG_VDC_NETWORK_RELATION = 'vAppOrgVdcNetworkRelation'
VAPP_TEMPLATE = 'vAppTemplate'
VIRTUAL_CENTER = 'virtualCenter'
VM = 'vm'
VM_DISK_RELATION = 'vmDiskRelation'
VM_GROUPS = 'vmGroups'
VM_GROUP_VMS = 'vmGroupVms'
RESOURCE_TYPES = [r.value for r in ResourceType]
class EntityType(Enum):
ADMIN = 'application/vnd.vmware.admin.vcloud+xml'
ADMIN_CATALOG = 'application/vnd.vmware.admin.catalog+xml'
ADMIN_ORG = 'application/vnd.vmware.admin.organization+xml'
ADMIN_SERVICE = 'application/vnd.vmware.admin.service+xml'
API_EXTENSIBILITY = 'application/vnd.vmware.vcloud.apiextensibility+xml'
AMQP_SETTINGS = 'application/vnd.vmware.admin.amqpSettings+xml'
CATALOG = 'application/vnd.vmware.vcloud.catalog+xml'
CAPTURE_VAPP_PARAMS = \
'application/vnd.vmware.vcloud.captureVAppParams+xml'
COMPOSE_VAPP_PARAMS = \
'application/vnd.vmware.vcloud.composeVAppParams+xml'
CONTROL_ACCESS_PARAMS = 'application/vnd.vmware.vcloud.controlAccess+xml'
DEPLOY = 'application/vnd.vmware.vcloud.deployVAppParams+xml'
DISK = 'application/vnd.vmware.vcloud.disk+xml'
DISK_ATTACH_DETACH_PARAMS = \
'application/vnd.vmware.vcloud.diskAttachOrDetachParams+xml'
DISK_CREATE_PARMS = 'application/vnd.vmware.vcloud.diskCreateParams+xml'
EDGE_GATEWAY = 'application/vnd.vmware.admin.edgeGateway+xml'
EDGE_GATEWAY_SERVICE_CONFIGURATION = \
'application/vnd.vmware.admin.edgeGatewayServiceConfiguration+xml'
EXTENSION = 'application/vnd.vmware.admin.vmwExtension+xml'
EXTENSION_SERVICES = 'application/vnd.vmware.admin.extensionServices+xml'
EXTERNAL_NETWORK = 'application/vnd.vmware.admin.vmwexternalnet+xml'
EXTERNAL_NETWORK_REFS = \
'application/vnd.vmware.admin.vmwExternalNetworkReferences+xml'
INSTANTIATE_VAPP_TEMPLATE_PARAMS = \
'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'
LEASE_SETTINGS = 'application/vnd.vmware.vcloud.leaseSettingsSection+xml'
MEDIA = 'application/vnd.vmware.vcloud.media+xml'
METADATA = 'application/vnd.vmware.vcloud.metadata+xml'
NETWORK_CONFIG_SECTION = \
'application/vnd.vmware.vcloud.networkConfigSection+xml'
NETWORK_CONNECTION_SECTION = \
'application/vnd.vmware.vcloud.networkConnectionSection+xml'
NETWORK_MANAGERS = 'application/vnd.vmware.admin.networkManagers+xml'
NETWORK_POOL_REFERENCES = \
'application/vnd.vmware.admin.vmwNetworkPoolReferences+xml'
NSXT_MANAGER = 'application/vnd.vmware.admin.nsxTmanager+xml'
ORG = 'application/vnd.vmware.vcloud.org+xml'
ORG_NETWORK = 'application/vnd.vmware.vcloud.orgNetwork+xml'
ORG_LIST = 'application/vnd.vmware.vcloud.orgList+xml'
ORG_RIGHTS = 'application/vnd.vmware.admin.org.rights+xml'
ORG_VDC_NETWORK = 'application/vnd.vmware.vcloud.orgVdcNetwork+xml'
OWNER = 'application/vnd.vmware.vcloud.owner+xml'
PROVIDER_VDC = 'application/vnd.vmware.admin.providervdc+xml'
PROVIDER_VDC_PARAMS = \
'application/vnd.vmware.admin.createProviderVdcParams+xml'
PUBLISH_CATALOG_PARAMS = \
'application/vnd.vmware.admin.publishCatalogParams+xml'
QUERY_LIST = 'application/vnd.vmware.vcloud.query.queryList+xml'
RASD_ITEM = 'application/vnd.vmware.vcloud.rasdItem+xml'
RASD_ITEMS_LIST = 'application/vnd.vmware.vcloud.rasdItemsList+xml'
RECOMPOSE_VAPP_PARAMS = \
'application/vnd.vmware.vcloud.recomposeVAppParams+xml'
RECORDS = 'application/vnd.vmware.vcloud.query.records+xml'
REGISTER_VC_SERVER_PARAMS = \
'application/vnd.vmware.admin.registerVimServerParams+xml'
ROLE = 'application/vnd.vmware.admin.role+xml'
RIGHT = 'application/vnd.vmware.admin.right+xml'
SNAPSHOT_CREATE = 'application/vnd.vmware.vcloud.createSnapshotParams+xml'
SYSTEM_SETTINGS = 'application/vnd.vmware.admin.systemSettings+xml'
TASK = 'application/vnd.vmware.vcloud.task+xml'
TASKS_LIST = 'application/vnd.vmware.vcloud.tasksList+xml'
TEXT_XML = 'text/xml'
UNDEPLOY = 'application/vnd.vmware.vcloud.undeployVAppParams+xml'
UPLOAD_VAPP_TEMPLATE_PARAMS = \
'application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml'
USER = 'application/vnd.vmware.admin.user+xml'
VAPP = 'application/vnd.vmware.vcloud.vApp+xml'
VAPP_TEMPLATE = 'application/vnd.vmware.vcloud.vAppTemplate+xml'
VDC = 'application/vnd.vmware.vcloud.vdc+xml'
VDC_REFERENCES = 'application/vnd.vmware.admin.vdcReferences+xml'
VDCS_PARAMS = 'application/vnd.vmware.admin.createVdcParams+xml'
VIM_SERVER_REFS = 'application/vnd.vmware.admin.vmwVimServerReferences+xml'
VIRTUAL_CENTER = 'application/vnd.vmware.admin.vmwvirtualcenter+xml'
VM = 'application/vnd.vmware.vcloud.vm+xml'
VMS = 'application/vnd.vmware.vcloud.vms+xml'
class QueryResultFormat(Enum):
RECORDS = ('application/vnd.vmware.vcloud.query.records+xml', 'records')
ID_RECORDS = ('application/vnd.vmware.vcloud.query.idrecords+xml',
'idrecords')
REFERENCES = ('application/vnd.vmware.vcloud.query.references+xml',
'references')
class _WellKnownEndpoint(Enum):
LOGGED_IN_ORG = (RelationType.DOWN, EntityType.ORG.value)
ORG_VDC = (RelationType.DOWN, EntityType.VDC.value)
ORG_NETWORK = (RelationType.DOWN, EntityType.ORG_NETWORK.value)
ORG_CATALOG = (RelationType.DOWN, EntityType.CATALOG.value)
QUERY_LIST = (RelationType.DOWN, EntityType.QUERY_LIST.value)
ADMIN = (RelationType.DOWN, EntityType.ADMIN.value)
API_EXTENSIBILITY = (RelationType.DOWN_EXTENSIBILITY,
EntityType.API_EXTENSIBILITY.value)
EXTENSION = (RelationType.DOWN, EntityType.EXTENSION.value)
ORG_LIST = (RelationType.DOWN, EntityType.ORG_LIST.value)
SNAPSHOT_CREATE = (RelationType.SNAPSHOT_CREATE,
EntityType.SNAPSHOT_CREATE.value)
class FenceMode(Enum):
ISOLATED = 'isolated'
DIRECT = 'direct'
BRIDGED = 'bridged'
NAT_ROUTED = 'natRouted'
class NetworkAdapterType(Enum):
VMXNET = 'VMXNET'
VMXNET2 = 'VMXNET2'
VMXNET3 = 'VMXNET3'
E1000 = 'E1000'
E1000E = 'E1000E'
VLANCE = 'PCNet32'
def _get_session_endpoints(session):
"""Return a map of well known endpoints.
Build and return a map keyed by well-known endpoints, yielding hrefs,
from a <Session> XML element.
:param lxml.objectify.ObjectifiedElement session: session object.
:return: session endpoint hrefs.
:rtype: dict
"""
smap = {}
for endpoint in _WellKnownEndpoint:
(rel, media_type) = endpoint.value
link = find_link(session, rel, media_type, False)
if link is not None:
smap[endpoint] = link.href
return smap
def _response_has_content(response):
return response.content is not None and len(response.content) > 0
def _objectify_response(response, as_object=True):
"""Convert XML response content to an lxml object.
:param str response: an XML response as a string.
:param boolean as_object: If True convert to an
lxml.objectify.ObjectifiedElement where XML properties look like
python object attributes.
:return: lxml.objectify.ObjectifiedElement or xml.etree.ElementTree object.
:rtype: lxml.objectify.ObjectifiedElement
"""
if _response_has_content(response):
if as_object:
return objectify.fromstring(response.content)
else:
return etree.fromstring(response.content)
else:
return None
class TaskStatus(Enum):
QUEUED = 'queued'
PRE_RUNNING = 'preRunning'
RUNNING = 'running'
SUCCESS = 'success'
ERROR = 'error'
CANCELED = 'canceled'
ABORTED = 'aborted'
class _TaskMonitor(object):
_DEFAULT_POLL_SEC = 5
_DEFAULT_TIMEOUT_SEC = 600
def __init__(self, client):
self._client = client
def wait_for_success(self,
task,
timeout=_DEFAULT_TIMEOUT_SEC,
poll_frequency=_DEFAULT_POLL_SEC,
callback=None):
return self.wait_for_status(
task,
timeout,
poll_frequency, [TaskStatus.ERROR], [TaskStatus.SUCCESS],
callback=callback)
def wait_for_status(self,
task,
timeout=_DEFAULT_TIMEOUT_SEC,
poll_frequency=_DEFAULT_POLL_SEC,
fail_on_statuses=[
TaskStatus.ABORTED, TaskStatus.CANCELED,
TaskStatus.ERROR
],
expected_target_statuses=[TaskStatus.SUCCESS],
callback=None):
"""Waits for task to reach expected status.
:param Task task: Task returned by post or put calls.
:param float timeout: Time (in seconds, floating point, fractional)
to wait for task to finish.
:param float poll_frequency: time (in seconds, as above) with which
task will be polled.
:param list fail_on_statuses: method will raise an exception if any
of the TaskStatus in this list is reached. If this parameter is
None then either task will achieve expected target status or throw
TimeOutException.
:param list expected_target_statuses: list of expected target
status.
:return: Task we were waiting for
:rtype Task:
:raises TimeoutException: If task is not finished within given time.
:raises VcdException: If task enters a status in fail_on_statuses list
"""
if fail_on_statuses is None:
_fail_on_statuses = []
elif isinstance(fail_on_statuses, TaskStatus):
_fail_on_statuses[fail_on_statuses]
else:
_fail_on_statuses = fail_on_statuses
task_href = task.get('href')
start_time = datetime.now()
while True:
task = self._get_task_status(task_href)
if callback is not None:
callback(task)
task_status = task.get('status').lower()
for status in expected_target_statuses:
if task_status == status.value.lower():
return task
for status in _fail_on_statuses:
if task_status == status.value.lower():
raise VcdTaskException(task_status, task.Error)
if start_time - datetime.now() > timedelta(seconds=timeout):
break
time.sleep(poll_frequency)
raise TaskTimeoutException("Task timeout")
def _get_task_status(self, task_href):
return self._client.get_resource(task_href)
def get_status(self, task):
return self._get_task_status(task.get('href')).get('status').lower()
class Client(object):
"""A low-level interface to the vCloud Director REST API.
Clients default to the production vCD API version as of the pyvcloud
module release and will try to negotiate down to a lower API version
that pyvcloud certifies if the vCD server is older. You can also set
the version explicitly using the api_version parameter or by calling
the set_highest_supported_version() method.
The log_file is set by the first client instantiated and will be
ignored in later clients.
:param str uri: vCD server host name or connection URI.
:param str api_version: vCD API version to use.
:param boolean verify_ssl_certs: If True validate server certificate;
False allows self-signed certificates.
:param str log_file: log file name or None, which suppresses logging.
:param boolean log_request: if True log HTTP requests.
:param boolean log_headers: if True log HTTP headers.
:param boolean log_bodies: if True log HTTP bodies.
"""
_REQUEST_ID_HDR_NAME = 'X-VMWARE-VCLOUD-REQUEST-ID'
def __init__(self,
uri,
api_version=None,
verify_ssl_certs=True,
log_file=None,
log_requests=False,
log_headers=False,
log_bodies=False):
self._uri = uri
if len(self._uri) > 0:
if self._uri[-1] == '/':
self._uri += 'api'
else:
self._uri += '/api'
if self._uri.startswith('https://') or self._uri.startswith(
'http://'):
pass
else:
self._uri = 'https://' + self._uri
# If user provides API version we accept it, otherwise use default
# and set negotiation flag.
if api_version is None:
self._api_version = API_CURRENT_VERSIONS[-1]
self._negotiate_api_version = True
else:
self._api_version = api_version
self._negotiate_api_version = False
self._session_endpoints = None
self._session = None
self._query_list_map = None
self._task_monitor = None
self._verify_ssl_certs = verify_ssl_certs
self._logger = logging.getLogger(__name__)
self._logger.setLevel(logging.DEBUG)
# This makes sure that we don't append a new handler to the logger
# every time we create a new client.
if not self._logger.handlers:
if log_file is not None:
handler = logging.FileHandler(log_file)
else:
handler = logging.NullHandler()
formatter = logging.Formatter('%(asctime)-23.23s | '
'%(levelname)-5.5s | '
'%(name)-15.15s | '
'%(module)-15.15s | '
'%(funcName)-12.12s | '
'%(message)s')
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._log_requests = log_requests
self._log_headers = log_headers
self._log_bodies = log_bodies
self.fsencoding = sys.getfilesystemencoding()
self._is_sysadmin = False
def _get_response_request_id(self, response):
"""Extract request id of a request to vCD from the response.
:param requests.Response response: response from vCD.
:return: the request id.
:rtype: str
"""
return response.headers[self._REQUEST_ID_HDR_NAME]
def get_api_version(self):
"""Return vCD API version client is using.
:return: api version of the client.
:rtype: str
"""
return self._api_version
def get_supported_versions_list(self):
"""Return non-deprecated server API versions as iterable list.
:return: versions (str) sorted in numerical order.
:rtype: list
"""
versions = self.get_supported_versions()
active_versions = []
for version in versions.VersionInfo:
if not hasattr(version, 'deprecated') or \
version.get('deprecated') == 'false':
active_versions.append(str(version.Version))
active_versions.sort(key=StrictVersion)
return active_versions
def get_supported_versions(self):
"""Return non-deprecated API versions on vCD server.
:return: an object containing SupportedVersions XML element which
represents versions supported by vCD.
:rtype: lxml.objectify.ObjectifiedElement
"""
with requests.Session() as new_session:
# Use with block to avoid leaking socket connections.
response = self._do_request_prim(
'GET', self._uri + '/versions', new_session, accept_type='')
sc = response.status_code
if sc != 200:
raise VcdException('Unable to get supported API versions.')
return objectify.fromstring(response.content)
def set_highest_supported_version(self):
"""Set the client API version to the highest server API version.
This call is intended to make it easy to work with new vCD
features before they are officially supported in pyvcloud.
Production applications should either use the default pyvcloud API
version or set the API version explicitly to freeze compatibility.
:return: selected api version.
:rtype: str
"""
active_versions = self.get_supported_versions_list()
self._api_version = active_versions[-1]
self._negotiate_api_version = False
self._logger.debug('API versions supported: %s' % active_versions)
self._logger.debug(
'API version set to: %s' % self._api_version)
return self._api_version
def set_credentials(self, creds):
"""Set credentials and authenticate to create a new session.
This call will automatically negotiate the server API version if
it was not set previously. Note that the method may generate
exceptions from the underlying socket connection, which we pass
up unchanged to the client.
:param BasicLoginCredentials creds: Credentials containing org,
user, and password.
:raises: VcdException: if automatic API negotiation fails to arrive
at a supported client version
"""
# If we need to negotiate the server API level find the highest
# server version that pyvcloud supports.
if self._negotiate_api_version:
self._logger.debug("Negotiating API version")
active_versions = self.get_supported_versions_list()
self._logger.debug('API versions supported: %s' % active_versions)
# Versions are strings sorted in ascending order, so we can work
# backwards to find a match.
for version in reversed(active_versions):
if version in API_CURRENT_VERSIONS:
self._api_version = version
self._negotiate_api_version = False
self._logger.debug(
'API version negotiated to: %s' % self._api_version)
break
# Still need to negotiate? That means we didn't find a
# suitable version.
if self._negotiate_api_version:
raise VcdException(
"Unable to find a supported API version in " +
" available server versions: {0}".format(active_versions))
# We can now proceed to login. Ensure we close session if
# any exception is thrown to avoid leaking a socket connection.
self._logger.debug('API version in use: %s' % self._api_version)
new_session = requests.Session()
try:
response = self._do_request_prim(
'POST',
self._uri + '/sessions',
new_session,
auth=('%s@%s' % (creds.user, creds.org), creds.password))
sc = response.status_code
if sc != 200:
r = None
try:
r = _objectify_response(response)
except Exception:
pass
if r is not None:
self._response_code_to_exception(
sc,
self._get_response_request_id(response), r)
else:
raise VcdException('Login failed.')
session = objectify.fromstring(response.content)
self._session_endpoints = _get_session_endpoints(session)
self._session = new_session
self._session.headers['x-vcloud-authorization'] = \
response.headers['x-vcloud-authorization']
self._is_sysadmin = self._is_sys_admin(session.get('org'))
except Exception:
new_session.close()
raise
def rehydrate(self, state):
self._session = requests.Session()
self._session.headers['x-vcloud-authorization'] = state.get('token')
self._is_sysadmin = self._is_sys_admin(state.get('org'))
wkep = state.get('wkep')
self._session_endpoints = {}
for endpoint in _WellKnownEndpoint:
if endpoint.name in wkep:
self._session_endpoints[endpoint] = wkep[endpoint.name]
def rehydrate_from_token(self, token):
new_session = requests.Session()
new_session.headers['x-vcloud-authorization'] = token
new_session.headers['Accept'] = 'application/*+xml;version=%s' % \
self._api_version
response = self._do_request_prim('GET', self._uri + "/session",
new_session)
sc = response.status_code
if sc != 200:
self._response_code_to_exception(sc, self._get_response_request_id(
response), _objectify_response(response))
session = objectify.fromstring(response.content)
self._is_sysadmin = self._is_sys_admin(session.get('org'))
self._session_endpoints = _get_session_endpoints(session)
self._session = new_session
self._session.headers['x-vcloud-authorization'] = \
response.headers['x-vcloud-authorization']
return session
def logout(self):
"""Destroy the server session and deallocate local resources.
Logout is idempotent. Reusing a client after logout will result
in undefined behavior.
"""
if self._session is not None:
uri = self._uri + '/session'
result = self._do_request('DELETE', uri)
self._session.close()
self._session = None
return result
def _is_sys_admin(self, logged_in_org):
if logged_in_org.lower() == 'system':
return True
return False
def is_sysadmin(self):
return self._is_sysadmin
def get_api_uri(self):
return self._uri
def get_task_monitor(self):
if self._task_monitor is None:
self._task_monitor = _TaskMonitor(self)
return self._task_monitor
def _do_request(self,
method,
uri,
contents=None,
media_type=None,
objectify_results=True):
response = self._do_request_prim(
method,
uri,
self._session,
contents=contents,
media_type=media_type)
sc = response.status_code
if 200 <= sc <= 299:
return _objectify_response(response, objectify_results)
self._response_code_to_exception(sc, self._get_response_request_id(
response), _objectify_response(response, objectify_results))
@staticmethod
def _response_code_to_exception(sc, request_id, objectify_response):
if sc == 400:
raise BadRequestException(sc, request_id, objectify_response)
if sc == 401:
raise UnauthorizedException(sc, request_id, objectify_response)
if sc == 403:
raise AccessForbiddenException(sc, request_id, objectify_response)
if sc == 404:
raise NotFoundException(sc, request_id, objectify_response)
if sc == 405:
raise MethodNotAllowedException(sc, request_id, objectify_response)
if sc == 406:
raise NotAcceptableException(sc, request_id, objectify_response)
if sc == 408:
raise RequestTimeoutException(sc, request_id, objectify_response)
if sc == 409:
raise ConflictException(sc, request_id, objectify_response)
if sc == 415:
raise UnsupportedMediaTypeException(sc, request_id,
objectify_response)
if sc == 416:
raise InvalidContentLengthException(sc, request_id,
objectify_response)
if sc == 500:
raise InternalServerException(sc, request_id, objectify_response)
raise UnknownApiException(sc, request_id, objectify_response)
def _do_request_prim(self,
method,
uri,
session,
contents=None,
media_type=None,
accept_type=None,
auth=None):
headers = {}
if media_type is not None:
headers['Content-Type'] = media_type
headers['Accept'] = '%s;version=%s' % \
('application/*+xml' if accept_type is None else accept_type,
self._api_version)
if contents is None:
data = None
else:
if isinstance(contents, dict):
data = json.dumps(contents)
else:
data = etree.tostring(contents)
response = session.request(
method,
uri,
data=data,
headers=headers,
auth=auth,
verify=self._verify_ssl_certs)
if self._log_requests or self._log_headers or self._log_bodies:
self._logger.debug('Request uri (%s): %s' % (method, uri))
if self._log_headers:
self._logger.debug('Request headers: %s, %s' % (session.headers,
headers))
if self._log_bodies and data is not None:
if sys.version_info[0] < 3:
d = data
else:
if isinstance(data, str):
d = data
else:
d = data.decode(self.fsencoding)
self._logger.debug('Request body: %s' % d)
if self._log_requests or self._log_headers or self._log_bodies:
self._logger.debug(
'Response status code: %s' % response.status_code)
if self._log_headers:
self._logger.debug('Response headers: %s' % response.headers)
if self._log_bodies and _response_has_content(response):
if sys.version_info[0] < 3:
d = response.content
else:
if isinstance(response.content, str):
d = response.content
else:
d = response.content.decode(self.fsencoding)
self._logger.debug('Response body: %s' % d)
return response
def upload_fragment(self, uri, contents, range_str):
headers = {}
headers['Content-Range'] = range_str
headers['Content-Length'] = str(len(contents))
data = contents
response = self._session.request(
'PUT',
uri,
data=data,
headers=headers,
verify=self._verify_ssl_certs)
if self._log_headers or self._log_bodies:
self._logger.debug('Request uri: %s' % uri)
if self._log_headers:
self._logger.debug('Request headers: %s, %s' %
(self._session.headers, headers))
if self._log_headers:
self._logger.debug(
'Response status code: %s' % response.status_code)
self._logger.debug('Response headers: %s' % response.headers)
if self._log_bodies and _response_has_content(response):
self._logger.debug('Response body: %s' % response.content)
return response
def download_from_uri(self,
uri,
file_name,
chunk_size=SIZE_1MB,
size=0,
callback=None):
response = self._session.request(
'GET', uri, stream=True, verify=self._verify_ssl_certs)
sc = response.status_code
if sc is not 200:
self._response_code_to_exception(sc, None, response)
bytes_written = 0
with open(file_name, 'wb') as f:
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
bytes_written += len(chunk)
if callback is not None:
callback(bytes_written, size)
if self._log_headers or self._log_bodies:
self._logger.debug('Request uri: %s' % uri)
if self._log_headers:
self._logger.debug(
'Response status code: %s' % response.status_code)
self._logger.debug(
'Response headers: %s' % response.headers)
return bytes_written
def put_resource(self, uri, contents, media_type, objectify_results=True):
"""Puts the specified contents to the specified resource.
This method does an HTTP PUT.
"""
return self._do_request(
'PUT',
uri,
contents=contents,
media_type=media_type,
objectify_results=objectify_results)
def put_linked_resource(self, resource, rel, media_type, contents):
"""Puts to a resource link.
Puts the contents of the resource referenced by the link with the
specified rel and mediaType in the specified resource.
:return: the result of the PUT operation.
:raises: OperationNotSupportedException: if the operation fails due to
the link being not visible to the logged in user of the client.
"""
try:
return self.put_resource(
find_link(resource, rel, media_type).href, contents,
media_type)
except MissingLinkException as e:
raise OperationNotSupportedException from e