Skip to content

Commit 5ba4450

Browse files
hetstrefethen
het
authored andcommitted
VMware Cloud on AWS 1.5
Updated VMware Cloud on AWS Console API bindings Updated VMware Cloud on AWS SDDC API bindings Added Compute Policy API bindings Add compute Policy API samples Remove doc folder. The API specs are hosted on gh-pages branch now. The API doc links can be found on the main README. Signed-off-by: het <[email protected]>
1 parent 285a06b commit 5ba4450

13 files changed

+214
-12
lines changed

doc/README.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

doc/client.zip

-4.13 MB
Binary file not shown.

doc/nsx_policy_python_sdk.zip

-3.62 MB
Binary file not shown.

doc/vmc.zip

-2.78 MB
Binary file not shown.

doc/vmc_app_python_sdk.zip

-2.35 MB
Binary file not shown.

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-1.3.1-py2.py3-none-any.whl'>vapi_client_bindings-1.3.1-py2.py3-none-any.whl</a><br />
1+
<a href='vapi_client_bindings-1.4.0-py2.py3-none-any.whl'>vapi_client_bindings-1.4.0-py2.py3-none-any.whl</a><br />

lib/vmc-client-bindings/index.html

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

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ pyVmomi >= 6.7
22
suds ; python_version < '3'
33
suds-jurko ; python_version >= '3.0'
44
tabulate
5-
vapi-client-bindings == 1.3.1
6-
vmc-client-bindings == 1.2.0
5+
vapi-client-bindings == 1.4.0
6+
vmc-client-bindings
77
vapi-vmc-client
88
nsx-policy-python-sdk
99
vmc-app-python-sdk
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Compute Policy API samples
2+
3+
This directory contains samples for the Compute Policy APIs.
4+
5+
## Compute Policy workflow
6+
`compute_policy_workflow.py` uses the compute policy APIs to create a policy of VM-Host affinity capability and checks the compliance status of the policy for a particular virtual machine after the virtual machine is powered on.
7+
8+
## Testbed requirements
9+
- 1 vCenter server.
10+
- 1 cluster on the vCenter server with DRS enabled.
11+
- At least 2 hosts and 1 virtual machine in the cluster.
12+
- A tag that can be associated with virtual machines and a tag that can be associated with hosts. Please refer to the [tagging samples](https://gitlab.eng.vmware.com/vapi-sdk/vsphere-automation-sdk-python/tree/cloud/samples/vsphere/tagging) for more information on creating categories, tags and tag associations.
13+
14+
## Running the sample
15+
```
16+
python3 samples/vsphere/compute_policy/compute_policy_workflow.py \
17+
-s <vcenter-ip> \
18+
-u <username> \
19+
-p <password> \
20+
-v -c \
21+
-n <name-for-the-policy> \
22+
-vn <name-of-the-vm> \
23+
-vt <name-of-the-tag-that-can-be-associated-with-vms> \
24+
-hn <host-name> \
25+
-ht <name-of-the-tag-that-can-be-associated-with-hosts> \
26+
```
27+
28+
##### Sample output
29+
```
30+
vcenter server = 10.192.174.79
31+
vc username = [email protected]
32+
Found VM 'vm_1' (vm-31)
33+
Creating a VM-Host affinity policy
34+
Policy created with id: 46e6c0a6-135c-4bfe-82ee-b1938128b5b9
35+
Powering on vm_1
36+
The compliance status of policy 46e6c0a6-135c-4bfe-82ee-b1938128b5b9 for VM vm-31 is COMPLIANT
37+
Deleting the policy 46e6c0a6-135c-4bfe-82ee-b1938128b5b9
38+
Powering off vm_1
39+
```
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""
2+
* *******************************************************
3+
* Copyright VMware, Inc. 2018. 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+
16+
# To create a policy of a different type, import the CreateSpec of the
17+
# corresponding capability.
18+
from com.vmware.vcenter.compute.policies.capabilities.vm_host_affinity_client \
19+
import CreateSpec
20+
from com.vmware.vapi.std_client import DynamicID
21+
from com.vmware.vcenter.vm_client import Power
22+
from com.vmware.vcenter_client import Host
23+
from samples.vsphere.common import sample_cli
24+
from samples.vsphere.common import sample_util
25+
from samples.vsphere.common.ssl_helper import get_unverified_session
26+
from samples.vsphere.vcenter.helper.vm_helper import get_vm
27+
from vmware.vapi.vsphere.client import create_vsphere_client
28+
29+
30+
def attach_tag(client, inv_obj, inv_type, tag):
31+
dyn_id = DynamicID(type=inv_type, id=inv_obj)
32+
try:
33+
client.tagging.TagAssociation.attach(tag.id, dyn_id)
34+
except Exception as e:
35+
print("Check that the tag is associable to {}".format(inv_type))
36+
raise e
37+
38+
39+
class ComputePolicyWorkflow(object):
40+
"""
41+
Demonstrates usage of the compute policy APIs to create a policy of
42+
VM-Host affinity capability and checks the compliance status of the policy
43+
for a particular virtual machine after the virtual machine is powered on.
44+
"""
45+
def __init__(self):
46+
self.policy_id = None
47+
self.vm_id = None
48+
self.vm_info = None
49+
50+
# Create argument parser for standard inputs:
51+
# server, username, password, cleanup and skipverification.
52+
parser = sample_cli.build_arg_parser()
53+
54+
parser.add_argument('-n', '--name', required=True,
55+
help='Name of the policy')
56+
parser.add_argument('-d', '--description', required=False,
57+
help='Description for the policy',
58+
default='Sample policy description')
59+
parser.add_argument('-vn', '--vmname', required=True,
60+
help='Name of the virtual machine')
61+
parser.add_argument('-hn', '--hostname', required=True,
62+
help='Name of the host')
63+
parser.add_argument('-vt', '--vmtag', required=True,
64+
help='Tag name to attach to the virtual machine')
65+
parser.add_argument('-ht', '--hosttag', required=True,
66+
help='Tag name to attach to the host')
67+
68+
# Parse the arguments.
69+
args = sample_util.process_cli_args(parser.parse_args())
70+
self.vm_name = args.vmname
71+
self.vm_tag_name = args.vmtag
72+
self.host_name = args.hostname
73+
self.host_tag_name = args.hosttag
74+
self.policy_name = args.name
75+
self.policy_desc = args.description
76+
self.cleardata = args.cleardata
77+
78+
# Skip server cert verification if needed.
79+
# This is not recommended in production code.
80+
session = get_unverified_session() if args.skipverification else None
81+
82+
# Connect to vSphere client.
83+
self.client = create_vsphere_client(server=args.server,
84+
username=args.username,
85+
password=args.password,
86+
session=session)
87+
88+
def run(self):
89+
# Get the virtual machine and power it off.
90+
self.vm_id = get_vm(self.client, self.vm_name)
91+
self.vm_info = self.client.vcenter.VM.get(self.vm_id)
92+
if not self.vm_info:
93+
raise ValueError("Virtual machine {} not found".format(
94+
self.vm_name))
95+
else:
96+
if self.vm_info.power_state == Power.State.POWERED_ON:
97+
self.client.vcenter.vm.Power.stop(self.vm_id)
98+
elif self.vm_info.power_state == Power.State.SUSPENDED:
99+
self.client.vcenter.vm.Power.start(self.vm_id)
100+
self.client.vcenter.vm.Power.stop(self.vm_id)
101+
102+
# Get the tags.
103+
tags = self.client.tagging.Tag.list()
104+
for tag in tags:
105+
info = self.client.tagging.Tag.get(tag)
106+
if info.name == self.vm_tag_name:
107+
vm_tag = info
108+
if info.name == self.host_tag_name:
109+
host_tag = info
110+
111+
if not vm_tag or not host_tag:
112+
raise ValueError("Provided tag(s) not found")
113+
114+
# Tag the virtual machine and the host.
115+
attach_tag(self.client, self.vm_id, "VirtualMachine", vm_tag)
116+
117+
filter_spec = Host.FilterSpec(names=set([self.host_name]))
118+
all_hosts = self.client.vcenter.Host.list(filter_spec)
119+
if not len(all_hosts) > 0:
120+
raise ValueError("Provided host not found")
121+
host_id = all_hosts[0].host
122+
123+
attach_tag(self.client, host_id, "HostSystem", host_tag)
124+
125+
# Create a vm-host affinity policy.
126+
create_spec = CreateSpec(vm_tag=vm_tag.id, host_tag=host_tag.id,
127+
name=self.policy_name,
128+
description=self.policy_desc)
129+
print("Creating a VM-Host affinity policy")
130+
try:
131+
self.policy_id = self.client.vcenter.compute.\
132+
Policies.create(create_spec)
133+
except Exception as e:
134+
print("Policy creation failed")
135+
raise e
136+
print("Policy created with id: {}".format(self.policy_id))
137+
138+
# Power-on the virtual machine.
139+
print("Powering on {}".format(self.vm_name))
140+
self.client.vcenter.vm.Power.start(self.vm_id)
141+
self.vm_info = self.client.vcenter.VM.get(self.vm_id)
142+
assert self.vm_info.power_state == Power.State.POWERED_ON
143+
144+
# Check the compliance status of the policy on this virtual machine.
145+
status = self.client.vcenter.vm.compute.Policies.get(self.vm_id,
146+
self.policy_id)
147+
print("The compliance status of policy {} for virtual machine "
148+
"{} is {}".format(self.policy_id, self.vm_id, status.status))
149+
150+
def cleanup(self):
151+
'''
152+
Delete the policy and power off the virtual machine.
153+
'''
154+
if self.policy_id is not None:
155+
print("Deleting the policy {}".format(self.policy_id))
156+
self.client.vcenter.compute.Policies.delete(self.policy_id)
157+
158+
if self.vm_info.power_state == Power.State.POWERED_ON:
159+
print("Powering off {}".format(self.vm_name))
160+
self.client.vcenter.vm.Power.stop(self.vm_id)
161+
162+
163+
def main():
164+
cp_workflow = ComputePolicyWorkflow()
165+
cp_workflow.run()
166+
if cp_workflow.cleardata:
167+
cp_workflow.cleanup()
168+
169+
170+
if __name__ == '__main__':
171+
main()

0 commit comments

Comments
 (0)