1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-23 18:10:00 -05:00
vsphere-automation-sdk-python/samples/vsphere/vcenter/helper/vm_helper.py

49 lines
1.4 KiB
Python
Raw Normal View History

2016-10-26 19:08:23 -04:00
"""
* *******************************************************
* Copyright (c) VMware, Inc. 2016. All Rights Reserved.
* *******************************************************
*
* 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.'
__copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.'
__vcenter_version__ = '6.5+'
2016-10-26 19:08:23 -04:00
from com.vmware.vcenter_client import VM
def get_vm(stub_config, vm_name):
"""
Return the identifier of a vm
Note: The method assumes that there is only one vm with the mentioned name.
"""
vm_svc = VM(stub_config)
names = set([vm_name])
vms = vm_svc.list(VM.FilterSpec(names=names))
if len(vms) == 0:
print("VM with name ({}) not found".format(vm_name))
return None
vm = vms[0].vm
print("Found VM '{}' ({})".format(vm_name, vm))
return vm
def get_vms(stub_config, vm_names):
"""Return identifiers of a list of vms"""
vm_svc = VM(stub_config)
vms = vm_svc.list(VM.FilterSpec(names=vm_names))
if len(vms) == 0:
print('No vm found')
return None
print("Found VMs '{}' ({})".format(vm_names, vms))
return vms