Skip to content

Commit 8a25c56

Browse files
author
Kunal Singh
committed
Libraries and Samples for VMC M9 release
Signed-off-by: Kunal Singh<[email protected]>
1 parent f87594f commit 8a25c56

27 files changed

+1374
-10
lines changed

lib/vapi-client-bindings/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<a href='vapi_client_bindings-3.1.0-py2.py3-none-any.whl'>vapi_client_bindings-3.1.0-py2.py3-none-any.whl</a><br />
1+
<a href='vapi_client_bindings-3.2.0-py2.py3-none-any.whl'>vapi_client_bindings-3.2.0-py2.py3-none-any.whl</a><br />
Binary file not shown.
Binary file not shown.

lib/vapi-common-client/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<a href='vapi_common_client-2.12.0-py2.py3-none-any.whl'>vapi_common_client-2.12.0-py2.py3-none-any.whl</a><br />
1+
<a href='vapi_common_client-2.14.0-py2.py3-none-any.whl'>vapi_common_client-2.14.0-py2.py3-none-any.whl</a><br />
Binary file not shown.
Binary file not shown.

lib/vapi-runtime/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<a href='vapi_runtime-2.12.0-py2.py3-none-any.whl'>vapi_runtime-2.12.0-py2.py3-none-any.whl</a><br />
1+
<a href='vapi_runtime-2.14.0-py2.py3-none-any.whl'>vapi_runtime-2.14.0-py2.py3-none-any.whl</a><br />

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ lxml >= 4.3.0
22
pyVmomi >= 6.7
33
suds ; python_version < '3'
44
suds-jurko ; python_version >= '3.0'
5-
vapi-client-bindings == 3.1.0
5+
vapi-client-bindings == 3.2.0
66
vmc-client-bindings
77
nsx-python-sdk
88
nsx-policy-python-sdk
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
* *******************************************************
3+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
4+
* SPDX-License-Identifier: MIT
5+
* *******************************************************
6+
*
7+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
8+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
9+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
10+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
11+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
12+
"""
13+
14+
__author__ = 'VMware, Inc.'
15+
__vcenter_version__ = '6.7+'
16+
17+
from vmware.vapi.vsphere.client import create_vsphere_client
18+
19+
from samples.vsphere.common import (sample_cli, sample_util)
20+
from samples.vsphere.common.ssl_helper import get_unverified_session
21+
22+
23+
"""
24+
Demonstrates setting and getting TimeZone.Accepted values are
25+
26+
valid Timezone values for appliance
27+
28+
"""
29+
30+
parser = sample_cli.build_arg_parser()
31+
32+
parser.add_argument(
33+
'--time_sync',
34+
required=True,
35+
action='store',
36+
choices=['DISABLED', 'HOST'],
37+
help='DISABLED,time synchronization is disabled and HOST,Host time synchronization ')
38+
39+
args = sample_util.process_cli_args(parser.parse_args())
40+
time_sync = args.time_sync
41+
42+
# Connect to vAPI services
43+
session = get_unverified_session() if args.skipverification else None
44+
client = create_vsphere_client(server=args.server,
45+
username=args.username,
46+
password=args.password,
47+
session=session)
48+
timesync_mode = client.appliance.Timesync.TimeSyncMode(time_sync)
49+
print("Setting the appliance time syncronization as : " + time_sync)
50+
client.appliance.Timesync.set(timesync_mode)
51+
print("Timesync as : " + client.appliance.Timesync.get())

samples/vsphere/contentlibrary/lib/cls_api_client.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
LocalLibrary,
1919
SubscribedLibrary)
2020
from com.vmware.content.library_client import Item, SubscribedItem, Subscriptions
21+
from com.vmware.content.library.item_client import Changes
2122
from com.vmware.content.library.item_client import DownloadSession
2223
from com.vmware.content.library.item_client import UpdateSession
2324
from com.vmware.content.library.item.downloadsession_client import File as DownloadSessionFile
@@ -26,6 +27,7 @@
2627
from com.vmware.vcenter.iso_client import Image
2728
from com.vmware.vcenter.ovf_client import LibraryItem
2829
from com.vmware.vcenter.vm_template_client import LibraryItems as VmtxLibraryItem
30+
from com.vmware.vcenter.vm_template.library_items_client import CheckOuts, Versions
2931

3032

3133
class ClsApiClient(object):
@@ -77,9 +79,23 @@ def __init__(self, service_manager):
7779
# machine templates
7880
self.vmtx_service = VmtxLibraryItem(self.service_manager.stub_config)
7981

80-
# ####
82+
# Returns the service for managing subscription information of
83+
# the subscribers of a published library.
8184
self.subscriptions = Subscriptions(self.service_manager.stub_config)
8285

8386
# Creates the service that communicates with virtual machines
8487
self.vm_service = VM(self.service_manager.stub_config)
88+
89+
# Returns the service for managing checkouts of a library item containing
90+
# a virtual machine template
91+
self.check_outs_service = CheckOuts(self.service_manager.stub_config)
92+
93+
# Returns the service for managing the live versions of the virtual machine
94+
# templates contained in a library item
95+
self.versions_service = Versions(self.service_manager.stub_config)
96+
97+
# Returns the service for managing the history of content changes made
98+
# to a library item
99+
self.changes_service = Changes(self.service_manager.stub_config)
100+
85101
# TODO: Add the other CLS services, eg. storage, config, type
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
17+
__author__ = 'VMware, Inc.'
18+
__vcenter_version__ = '7.0.0+'
19+
20+
from pyVmomi import vim
21+
from com.vmware.vcenter.vm_template.library_items_client import CheckOuts
22+
from com.vmware.vcenter.vm_template.library_items_client import Versions
23+
24+
from vmware.vapi.vsphere.client import create_vsphere_client
25+
26+
from samples.vsphere.common.id_generator import rand
27+
from samples.vsphere.common.sample_base import SampleBase
28+
from samples.vsphere.common.ssl_helper import get_unverified_session
29+
from samples.vsphere.common.vim.helpers.vim_utils import get_obj_by_moId
30+
from samples.vsphere.contentlibrary.lib.cls_api_client import ClsApiClient
31+
from samples.vsphere.contentlibrary.lib.cls_api_helper import ClsApiHelper
32+
from samples.vsphere.vcenter.helper.resource_pool_helper import (
33+
get_resource_pool)
34+
35+
36+
class CheckOutVmTemplateWorkflow(SampleBase):
37+
"""
38+
Demonstrates how to check out a VM from a library item containing a virtual
39+
machine template, check in the VM checked out from the item, and rollback
40+
the item to a previous version.
41+
42+
Prerequisites:
43+
- A library item containing a virtual machine template
44+
- A resource pool
45+
- A datacenter
46+
"""
47+
48+
def __init__(self):
49+
SampleBase.__init__(self, self.__doc__)
50+
self.servicemanager = None
51+
self.client = None
52+
self.helper = None
53+
self.item_name = None
54+
self.vm_name = None
55+
self.datacenter_name = None
56+
self.resource_pool_name = None
57+
58+
def _options(self):
59+
self.argparser.add_argument('-itemname', '--itemname',
60+
required=True,
61+
help='The name of the library item '
62+
'containing the VM template '
63+
'to be checked out')
64+
self.argparser.add_argument('-datacentername', '--datacentername',
65+
required=True,
66+
help='The name of the datacenter in which '
67+
'to check out the VM')
68+
self.argparser.add_argument('-resourcepoolname', '--resourcepoolname',
69+
required=True,
70+
help='The name of the resource pool in '
71+
'the datacenter in which to place '
72+
'the VM')
73+
self.argparser.add_argument('-vmname', '--vmname',
74+
help='The name of the VM to check out of '
75+
'the library item')
76+
77+
def _setup(self):
78+
# Required arguments
79+
self.datacenter_name = self.args.datacentername
80+
self.resource_pool_name = self.args.resourcepoolname
81+
self.item_name = self.args.itemname
82+
83+
# Optional arguments
84+
self.vm_name = (self.args.vmname if self.args.vmname
85+
else rand('checked-out-vm-'))
86+
87+
self.servicemanager = self.get_service_manager()
88+
self.client = ClsApiClient(self.servicemanager)
89+
self.helper = ClsApiHelper(self.client, self.skip_verification)
90+
91+
session = get_unverified_session() if self.skip_verification else None
92+
self.vsphere_client = create_vsphere_client(server=self.server,
93+
username=self.username,
94+
password=self.password,
95+
session=session)
96+
97+
def _execute(self):
98+
# Get the identifiers
99+
item_id = self.helper.get_item_id_by_name(self.item_name)
100+
assert item_id
101+
102+
resource_pool_id = get_resource_pool(self.vsphere_client,
103+
self.datacenter_name,
104+
self.resource_pool_name)
105+
assert resource_pool_id
106+
107+
version_before_check_out = self.client.library_item_service.get(
108+
item_id).content_version
109+
110+
self.print_live_versions(item_id)
111+
112+
# Build the check out spec
113+
check_out_spec = CheckOuts.CheckOutSpec()
114+
placement_spec = CheckOuts.PlacementSpec()
115+
placement_spec.resource_pool = resource_pool_id
116+
check_out_spec.placement = placement_spec
117+
check_out_spec.name = self.vm_name
118+
119+
# Check out VM from item
120+
vm_id = self.client.check_outs_service.check_out(item_id,
121+
check_out_spec)
122+
print("VM (ID: {}) checked out from item".format(vm_id))
123+
124+
# Get library id associated with checked out VM
125+
info = self.vsphere_client.vcenter.vm.LibraryItem.get(
126+
vm_id)
127+
assert info.check_out
128+
print("Library item associated with checked out VM is {}".format(
129+
info.check_out.library_item))
130+
131+
# Check in VM into the library item
132+
check_in_spec = CheckOuts.CheckInSpec()
133+
check_in_spec.message = "Check in message"
134+
version_after_check_in = self.client.check_outs_service.check_in(
135+
item_id, vm_id, check_in_spec)
136+
print("VM (ID: {}) checked into item {}".format(vm_id, item_id))
137+
self.print_live_versions(item_id)
138+
139+
# Rollback to previous version
140+
rollback_message = "Rollback to v{}".format(version_before_check_out)
141+
rollback_spec = Versions.RollbackSpec(rollback_message)
142+
version_after_rollback = self.client.versions_service.rollback(
143+
item_id, version_before_check_out, rollback_spec)
144+
print("Item rolled back to version {}. New item version is {}".format(
145+
version_before_check_out, version_after_rollback))
146+
self.print_live_versions(item_id)
147+
148+
# Delete previous version
149+
self.client.versions_service.delete(item_id, version_after_check_in)
150+
print("Deleted version {} of item".format(version_after_check_in))
151+
self.print_live_versions(item_id)
152+
self.print_change_history(item_id)
153+
154+
def print_live_versions(self, item_id):
155+
# Get and print live versions of the VM template item
156+
versions_info = self.client.versions_service.list(item_id)
157+
print("Live versions of VM template item:")
158+
for version_info in versions_info:
159+
vm_template = get_obj_by_moId(self.servicemanager.content,
160+
[vim.VirtualMachine],
161+
version_info.vm_template)
162+
print("Version: {}, VM template name: {}".format(
163+
version_info.version, vm_template.name))
164+
165+
def print_change_history(self, item_id):
166+
# Get and print change history of the VM template item
167+
changes_summary = self.client.changes_service.list(item_id)
168+
print("Change history of VM template item:")
169+
for change_summary in changes_summary:
170+
print("Change version: {}, Time: {}, User: {}, Message: {}".format(
171+
change_summary.version, change_summary.time,
172+
change_summary.user, change_summary.short_message))
173+
174+
175+
def main():
176+
check_out_workflow_sample = CheckOutVmTemplateWorkflow()
177+
check_out_workflow_sample.main()
178+
179+
180+
if __name__ == '__main__':
181+
main()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
* *******************************************************
3+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
4+
* SPDX-License-Identifier: MIT
5+
* *******************************************************
6+
*
7+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
8+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
9+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
10+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
11+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
12+
"""
13+
14+
__author__ = 'VMware, Inc.'
15+
__vcenter_version__ = '6.7+'
16+
17+
import time
18+
import logging
19+
from com.vmware.vcenter.vm.guest_client import Power
20+
from com.vmware.vcenter.vm.guest_client import Identity
21+
from com.vmware.vapi.std.errors_client import (NotFound, ServiceUnavailable)
22+
23+
24+
def wait_for_guest_info_ready(vsphere_client, vmId, timeout):
25+
"""
26+
Waits for the Tools info to be ready, or times out.
27+
"""
28+
print('Waiting for guest info to be ready.')
29+
start = time.time()
30+
timeout = start + timeout
31+
while timeout > time.time():
32+
logging.info('Waiting for guest info to be ready')
33+
time.sleep(1)
34+
try:
35+
result = vsphere_client.vcenter.vm.guest.Identity.get(vmId)
36+
break
37+
except ServiceUnavailable as e:
38+
logging.debug('Got ServiceUnavailable waiting for guest info')
39+
pass
40+
except Exception as e:
41+
print('Unexpected exception %s waiting for guest info' % e)
42+
raise e
43+
if time.time() >= timeout:
44+
raise Exception('Timed out waiting for guest info to be available.\n'
45+
'Be sure the VM has VMware Tools.')
46+
else:
47+
logging.info('Took %d seconds for guest info to be available'
48+
% (time.time() - start))
49+
50+
51+
def wait_for_guest_power_state(vsphere_client, vmId, desiredState, timeout):
52+
"""
53+
Waits for the guest to reach the desired power state, or times out.
54+
"""
55+
print("Waiting for guest power state {}".format(desiredState))
56+
start = time.time()
57+
timeout = start + timeout
58+
while timeout > time.time():
59+
time.sleep(1)
60+
curState = vsphere_client.vcenter.vm.guest.Power.get(vmId).state
61+
logging.debug('Current guest power state is %s, looking for %s'
62+
% (curState, desiredState))
63+
if desiredState == curState:
64+
break
65+
if desiredState != curState:
66+
raise Exception('Timed out waiting for guest to reach desired power state')
67+
else:
68+
logging.info('Took %s seconds for guest power state to change to %s'
69+
% (time.time() - start, desiredState))
70+
71+
72+
def wait_for_power_operations_state(vsphere_client, vmId, desiredState, timeout):
73+
"""
74+
Waits for the desired soft power operations state, or times out.
75+
"""
76+
print('Waiting for guest power operations to be {}'.format(desiredState))
77+
start = time.time()
78+
timeout = start + timeout
79+
while timeout > time.time():
80+
time.sleep(1)
81+
curState = vsphere_client.vcenter.vm.guest.Power.get(vmId).operations_ready
82+
logging.debug('Current guest operations ready state is %s,'
83+
' looking for %s' % (curState, desiredState))
84+
if desiredState == curState:
85+
break
86+
if desiredState != curState:
87+
raise Exception('Timed out waiting for guest to reach desired '
88+
' operations ready state')
89+
else:
90+
logging.info('Took %s seconds for guest operations ready state\
91+
to change to %s' % (time.time() - start, desiredState))

0 commit comments

Comments
 (0)