|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +* ******************************************************* |
| 5 | +* Copyright (c) VMware, Inc. 2017. 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 | +import atexit |
| 17 | +from samples.vsphere.common import vapiconnect |
| 18 | +from samples.vsphere.common.sample_util import parse_cli_args_vm |
| 19 | +from com.vmware.vcenter_client import VM |
| 20 | +from com.vmware.vcenter.vm_client import Power |
| 21 | +from samples.vsphere.vcenter.helper.vm_helper import get_vm |
| 22 | + |
| 23 | +""" |
| 24 | +Demonstrates Deleting User specified Virtual Machine (VM) |
| 25 | +Sample Prerequisites: |
| 26 | +vCenter/ESX |
| 27 | +""" |
| 28 | + |
| 29 | +stub_config = None |
| 30 | +vm_name = None |
| 31 | +vm = None |
| 32 | + |
| 33 | + |
| 34 | +def setup(context=None): |
| 35 | + global stub_config, vm_name |
| 36 | + server, username, password, cleardata, skip_verification, vm_name = \ |
| 37 | + parse_cli_args_vm(vm_name) |
| 38 | + stub_config = vapiconnect.connect(server, username, password, |
| 39 | + skip_verification) |
| 40 | + atexit.register(vapiconnect.logout, stub_config) |
| 41 | + |
| 42 | + |
| 43 | +def run(): |
| 44 | + """ |
| 45 | + Delete User specified VM from Server |
| 46 | + """ |
| 47 | + global vm |
| 48 | + vm_svc = VM(stub_config) |
| 49 | + power_svc = Power(stub_config) |
| 50 | + vm = get_vm(stub_config, vm_name) |
| 51 | + if not vm: |
| 52 | + raise Exception('Sample requires an existing vm with name ({}).' |
| 53 | + 'Please create the vm first.'.format(vm_name)) |
| 54 | + print("Deleting VM -- '{}-({})')".format(vm_name, vm)) |
| 55 | + state = power_svc.get(vm) |
| 56 | + if state == Power.Info(state=Power.State.POWERED_ON): |
| 57 | + power_svc.stop(vm) |
| 58 | + elif state == Power.Info(state=Power.State.SUSPENDED): |
| 59 | + power_svc.start(vm) |
| 60 | + power_svc.stop(vm) |
| 61 | + vm_svc.delete(vm) |
| 62 | + print("Deleted VM -- '{}-({})".format(vm_name, vm)) |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + setup() |
| 67 | + run() |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + main() |
0 commit comments