diff --git a/doc/README.md b/doc/README.md deleted file mode 100644 index 1345a16e..00000000 --- a/doc/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# SDK API Documentation - -API references for each product can be found in below zip packages: - -Name | Online Version ------------| ----------------------------------------- -client.zip | [vSphere Automation Python API Reference](https://vdc-repo.vmware.com/vmwb-repository/dcr-public/3f9d46be-a5f4-452c-8b1c-ae2bb05bc0a6/58d6917d-4da9-45b1-869a-f68550834f8e/vsphere/py-modindex.html) -vmc.zip | VMware Cloud on AWS Python API Reference diff --git a/doc/client.zip b/doc/client.zip deleted file mode 100644 index b170410d..00000000 Binary files a/doc/client.zip and /dev/null differ diff --git a/doc/nsx_policy_python_sdk.zip b/doc/nsx_policy_python_sdk.zip deleted file mode 100644 index d9127a0b..00000000 Binary files a/doc/nsx_policy_python_sdk.zip and /dev/null differ diff --git a/doc/vmc.zip b/doc/vmc.zip deleted file mode 100644 index 4b00216d..00000000 Binary files a/doc/vmc.zip and /dev/null differ diff --git a/doc/vmc_app_python_sdk.zip b/doc/vmc_app_python_sdk.zip deleted file mode 100644 index 28769f13..00000000 Binary files a/doc/vmc_app_python_sdk.zip and /dev/null differ diff --git a/lib/vapi-client-bindings/index.html b/lib/vapi-client-bindings/index.html index 46cb60f5..f9d370ac 100644 --- a/lib/vapi-client-bindings/index.html +++ b/lib/vapi-client-bindings/index.html @@ -1 +1 @@ -vapi_client_bindings-1.3.1-py2.py3-none-any.whl
+vapi_client_bindings-1.4.0-py2.py3-none-any.whl
diff --git a/lib/vapi-client-bindings/vapi_client_bindings-1.3.1-py2.py3-none-any.whl b/lib/vapi-client-bindings/vapi_client_bindings-1.4.0-py2.py3-none-any.whl similarity index 64% rename from lib/vapi-client-bindings/vapi_client_bindings-1.3.1-py2.py3-none-any.whl rename to lib/vapi-client-bindings/vapi_client_bindings-1.4.0-py2.py3-none-any.whl index f0003cb6..be4bdc61 100644 Binary files a/lib/vapi-client-bindings/vapi_client_bindings-1.3.1-py2.py3-none-any.whl and b/lib/vapi-client-bindings/vapi_client_bindings-1.4.0-py2.py3-none-any.whl differ diff --git a/lib/vmc-client-bindings/index.html b/lib/vmc-client-bindings/index.html index 331028fe..337fb6ee 100644 --- a/lib/vmc-client-bindings/index.html +++ b/lib/vmc-client-bindings/index.html @@ -1 +1 @@ -vmc_client_bindings-1.2.0-py2.py3-none-any.whl
\ No newline at end of file +vmc_client_bindings-1.3.0-py2.py3-none-any.whl
\ No newline at end of file diff --git a/lib/vmc-client-bindings/vmc_client_bindings-1.2.0-py2.py3-none-any.whl b/lib/vmc-client-bindings/vmc_client_bindings-1.2.0-py2.py3-none-any.whl deleted file mode 100644 index e7e81e3f..00000000 Binary files a/lib/vmc-client-bindings/vmc_client_bindings-1.2.0-py2.py3-none-any.whl and /dev/null differ diff --git a/lib/vmc-client-bindings/vmc_client_bindings-1.3.0-py2.py3-none-any.whl b/lib/vmc-client-bindings/vmc_client_bindings-1.3.0-py2.py3-none-any.whl new file mode 100644 index 00000000..e9c90e1c Binary files /dev/null and b/lib/vmc-client-bindings/vmc_client_bindings-1.3.0-py2.py3-none-any.whl differ diff --git a/requirements.txt b/requirements.txt index aa7b2251..ebf5fee9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,8 +2,8 @@ pyVmomi >= 6.7 suds ; python_version < '3' suds-jurko ; python_version >= '3.0' tabulate -vapi-client-bindings == 1.3.1 -vmc-client-bindings == 1.2.0 +vapi-client-bindings == 1.4.0 +vmc-client-bindings vapi-vmc-client nsx-policy-python-sdk vmc-app-python-sdk diff --git a/samples/vsphere/compute_policy/README.md b/samples/vsphere/compute_policy/README.md new file mode 100644 index 00000000..220e2332 --- /dev/null +++ b/samples/vsphere/compute_policy/README.md @@ -0,0 +1,39 @@ +# Compute Policy API samples + +This directory contains samples for the Compute Policy APIs. + +## Compute Policy workflow +`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. + +## Testbed requirements +- 1 vCenter server. +- 1 cluster on the vCenter server with DRS enabled. +- At least 2 hosts and 1 virtual machine in the cluster. +- 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. + +## Running the sample +``` +python3 samples/vsphere/compute_policy/compute_policy_workflow.py \ +-s \ +-u \ +-p \ +-v -c \ +-n \ +-vn \ +-vt \ +-hn \ +-ht \ +``` + +##### Sample output +``` +vcenter server = 10.192.174.79 +vc username = Administrator@vsphere.local +Found VM 'vm_1' (vm-31) +Creating a VM-Host affinity policy +Policy created with id: 46e6c0a6-135c-4bfe-82ee-b1938128b5b9 +Powering on vm_1 +The compliance status of policy 46e6c0a6-135c-4bfe-82ee-b1938128b5b9 for VM vm-31 is COMPLIANT +Deleting the policy 46e6c0a6-135c-4bfe-82ee-b1938128b5b9 +Powering off vm_1 +``` diff --git a/samples/vsphere/compute_policy/compute_policy_workflow.py b/samples/vsphere/compute_policy/compute_policy_workflow.py new file mode 100644 index 00000000..ab966b26 --- /dev/null +++ b/samples/vsphere/compute_policy/compute_policy_workflow.py @@ -0,0 +1,171 @@ +""" +* ******************************************************* +* Copyright VMware, Inc. 2018. All Rights Reserved. +* SPDX-License-Identifier: MIT +* ******************************************************* +* +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. +""" + +__author__ = 'VMware, Inc.' + +# To create a policy of a different type, import the CreateSpec of the +# corresponding capability. +from com.vmware.vcenter.compute.policies.capabilities.vm_host_affinity_client \ + import CreateSpec +from com.vmware.vapi.std_client import DynamicID +from com.vmware.vcenter.vm_client import Power +from com.vmware.vcenter_client import Host +from samples.vsphere.common import sample_cli +from samples.vsphere.common import sample_util +from samples.vsphere.common.ssl_helper import get_unverified_session +from samples.vsphere.vcenter.helper.vm_helper import get_vm +from vmware.vapi.vsphere.client import create_vsphere_client + + +def attach_tag(client, inv_obj, inv_type, tag): + dyn_id = DynamicID(type=inv_type, id=inv_obj) + try: + client.tagging.TagAssociation.attach(tag.id, dyn_id) + except Exception as e: + print("Check that the tag is associable to {}".format(inv_type)) + raise e + + +class ComputePolicyWorkflow(object): + """ + Demonstrates usage of 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. + """ + def __init__(self): + self.policy_id = None + self.vm_id = None + self.vm_info = None + + # Create argument parser for standard inputs: + # server, username, password, cleanup and skipverification. + parser = sample_cli.build_arg_parser() + + parser.add_argument('-n', '--name', required=True, + help='Name of the policy') + parser.add_argument('-d', '--description', required=False, + help='Description for the policy', + default='Sample policy description') + parser.add_argument('-vn', '--vmname', required=True, + help='Name of the virtual machine') + parser.add_argument('-hn', '--hostname', required=True, + help='Name of the host') + parser.add_argument('-vt', '--vmtag', required=True, + help='Tag name to attach to the virtual machine') + parser.add_argument('-ht', '--hosttag', required=True, + help='Tag name to attach to the host') + + # Parse the arguments. + args = sample_util.process_cli_args(parser.parse_args()) + self.vm_name = args.vmname + self.vm_tag_name = args.vmtag + self.host_name = args.hostname + self.host_tag_name = args.hosttag + self.policy_name = args.name + self.policy_desc = args.description + self.cleardata = args.cleardata + + # Skip server cert verification if needed. + # This is not recommended in production code. + session = get_unverified_session() if args.skipverification else None + + # Connect to vSphere client. + self.client = create_vsphere_client(server=args.server, + username=args.username, + password=args.password, + session=session) + + def run(self): + # Get the virtual machine and power it off. + self.vm_id = get_vm(self.client, self.vm_name) + self.vm_info = self.client.vcenter.VM.get(self.vm_id) + if not self.vm_info: + raise ValueError("Virtual machine {} not found".format( + self.vm_name)) + else: + if self.vm_info.power_state == Power.State.POWERED_ON: + self.client.vcenter.vm.Power.stop(self.vm_id) + elif self.vm_info.power_state == Power.State.SUSPENDED: + self.client.vcenter.vm.Power.start(self.vm_id) + self.client.vcenter.vm.Power.stop(self.vm_id) + + # Get the tags. + tags = self.client.tagging.Tag.list() + for tag in tags: + info = self.client.tagging.Tag.get(tag) + if info.name == self.vm_tag_name: + vm_tag = info + if info.name == self.host_tag_name: + host_tag = info + + if not vm_tag or not host_tag: + raise ValueError("Provided tag(s) not found") + + # Tag the virtual machine and the host. + attach_tag(self.client, self.vm_id, "VirtualMachine", vm_tag) + + filter_spec = Host.FilterSpec(names=set([self.host_name])) + all_hosts = self.client.vcenter.Host.list(filter_spec) + if not len(all_hosts) > 0: + raise ValueError("Provided host not found") + host_id = all_hosts[0].host + + attach_tag(self.client, host_id, "HostSystem", host_tag) + + # Create a vm-host affinity policy. + create_spec = CreateSpec(vm_tag=vm_tag.id, host_tag=host_tag.id, + name=self.policy_name, + description=self.policy_desc) + print("Creating a VM-Host affinity policy") + try: + self.policy_id = self.client.vcenter.compute.\ + Policies.create(create_spec) + except Exception as e: + print("Policy creation failed") + raise e + print("Policy created with id: {}".format(self.policy_id)) + + # Power-on the virtual machine. + print("Powering on {}".format(self.vm_name)) + self.client.vcenter.vm.Power.start(self.vm_id) + self.vm_info = self.client.vcenter.VM.get(self.vm_id) + assert self.vm_info.power_state == Power.State.POWERED_ON + + # Check the compliance status of the policy on this virtual machine. + status = self.client.vcenter.vm.compute.Policies.get(self.vm_id, + self.policy_id) + print("The compliance status of policy {} for virtual machine " + "{} is {}".format(self.policy_id, self.vm_id, status.status)) + + def cleanup(self): + ''' + Delete the policy and power off the virtual machine. + ''' + if self.policy_id is not None: + print("Deleting the policy {}".format(self.policy_id)) + self.client.vcenter.compute.Policies.delete(self.policy_id) + + if self.vm_info.power_state == Power.State.POWERED_ON: + print("Powering off {}".format(self.vm_name)) + self.client.vcenter.vm.Power.stop(self.vm_id) + + +def main(): + cp_workflow = ComputePolicyWorkflow() + cp_workflow.run() + if cp_workflow.cleardata: + cp_workflow.cleanup() + + +if __name__ == '__main__': + main()