|
| 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