From ead9df5c42163e2d198e67356c15f9615b3d236a Mon Sep 17 00:00:00 2001 From: pgbidkar Date: Tue, 31 Jan 2017 21:48:28 -0800 Subject: [PATCH 1/6] New sample to List the VMs present in Specified vCenter Server --- samples/vsphere/vcenter/vm/list_vms.py | 111 +++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 samples/vsphere/vcenter/vm/list_vms.py diff --git a/samples/vsphere/vcenter/vm/list_vms.py b/samples/vsphere/vcenter/vm/list_vms.py new file mode 100644 index 00000000..c76438f3 --- /dev/null +++ b/samples/vsphere/vcenter/vm/list_vms.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python + +""" +* ******************************************************* +* 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. +""" +import atexit + +from com.vmware.vcenter.vm_client import Power +from samples.vsphere.common import vapiconnect +from samples.vsphere.common.sample_util import parse_cli_args_vm +from samples.vsphere.common.sample_util import pp +from samples.vsphere.vcenter.setup import testbed + +from samples.vsphere.vcenter.helper.vm_helper import get_vm + +""" +Demonstrates getting list of VMs present in vCenter + +Sample Prerequisites: +vCenter/ESX +""" + +#!/usr/bin/env python + +""" +* ******************************************************* +* 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. +""" + +from com.vmware.vcenter.vm.hardware.boot_client import Device as BootDevice +from com.vmware.vcenter.vm.hardware_client import ( + Disk, Ethernet) +from com.vmware.vcenter.vm.hardware_client import ScsiAddressSpec +from com.vmware.vcenter.vm_client import (Power) +from com.vmware.vcenter_client import VM +from samples.vsphere.common import vapiconnect +from samples.vsphere.common.sample_util import parse_cli_args +from samples.vsphere.vcenter.setup import testbed + +from samples.vsphere.vcenter.helper.vm_helper import get_vm + +""" +Demonstrates how to List the VMs present in a vCenter server: + +Sample Prerequisites: + - VC + - ESX + - datacenter +""" + +stub_config = None + + +def setup(context=None): + global stub_config, cleardata + server, username, password, cleardata, skip_verification = \ + parse_cli_args() + stub_config = vapiconnect.connect(server, + username, + password, + skip_verification) + + +def run(): + list_vms(stub_config) + + +def list_vms(stub_config): + """ + List VMs present in server + + """ + vm_svc = VM(stub_config) + list_of_vms = vm_svc.list() + for vm in list_of_vms: + print ('{}'.format(vm)) + + +def cleanup(): + pass + + +def main(): + try: + setup() + cleanup() + run() + if cleardata: + cleanup() + finally: + if stub_config: + vapiconnect.logout(stub_config) + + +if __name__ == '__main__': + main() \ No newline at end of file From be3b51e4b79fa6fdb71d7ddb400469e9070fc6ce Mon Sep 17 00:00:00 2001 From: pgbidkar Date: Thu, 2 Feb 2017 00:50:31 -0800 Subject: [PATCH 2/6] Modified script to incorporate Review comments --- samples/vsphere/vcenter/vm/list_vms.py | 62 +++++--------------------- 1 file changed, 11 insertions(+), 51 deletions(-) diff --git a/samples/vsphere/vcenter/vm/list_vms.py b/samples/vsphere/vcenter/vm/list_vms.py index c76438f3..481e13a6 100644 --- a/samples/vsphere/vcenter/vm/list_vms.py +++ b/samples/vsphere/vcenter/vm/list_vms.py @@ -11,69 +11,30 @@ * WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, * NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. """ -import atexit -from com.vmware.vcenter.vm_client import Power from samples.vsphere.common import vapiconnect -from samples.vsphere.common.sample_util import parse_cli_args_vm +from samples.vsphere.common.sample_util import parse_cli_args from samples.vsphere.common.sample_util import pp -from samples.vsphere.vcenter.setup import testbed - -from samples.vsphere.vcenter.helper.vm_helper import get_vm +from com.vmware.vcenter_client import VM """ Demonstrates getting list of VMs present in vCenter - Sample Prerequisites: vCenter/ESX """ -#!/usr/bin/env python - -""" -* ******************************************************* -* 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. -""" - -from com.vmware.vcenter.vm.hardware.boot_client import Device as BootDevice -from com.vmware.vcenter.vm.hardware_client import ( - Disk, Ethernet) -from com.vmware.vcenter.vm.hardware_client import ScsiAddressSpec -from com.vmware.vcenter.vm_client import (Power) -from com.vmware.vcenter_client import VM -from samples.vsphere.common import vapiconnect -from samples.vsphere.common.sample_util import parse_cli_args -from samples.vsphere.vcenter.setup import testbed - -from samples.vsphere.vcenter.helper.vm_helper import get_vm - -""" -Demonstrates how to List the VMs present in a vCenter server: - -Sample Prerequisites: - - VC - - ESX - - datacenter -""" - stub_config = None - +cleardata = False def setup(context=None): - global stub_config, cleardata - server, username, password, cleardata, skip_verification = \ - parse_cli_args() - stub_config = vapiconnect.connect(server, - username, - password, - skip_verification) + global stub_config, cleardata + server, username, password, cleardata, skip_verification = \ + parse_cli_args() + stub_config = vapiconnect.connect(server, + username, + password, + skip_verification) + #atexit.register(vapiconnect.logout, stub_config) def run(): @@ -83,7 +44,6 @@ def run(): def list_vms(stub_config): """ List VMs present in server - """ vm_svc = VM(stub_config) list_of_vms = vm_svc.list() From 2ecbca4718e964b4a5f6b16d6a2bcf05e8892601 Mon Sep 17 00:00:00 2001 From: pgbidkar Date: Mon, 6 Feb 2017 03:36:00 -0800 Subject: [PATCH 3/6] Modified to incorporate review comments --- samples/vsphere/vcenter/vm/list_vms.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/samples/vsphere/vcenter/vm/list_vms.py b/samples/vsphere/vcenter/vm/list_vms.py index 481e13a6..7b691b58 100644 --- a/samples/vsphere/vcenter/vm/list_vms.py +++ b/samples/vsphere/vcenter/vm/list_vms.py @@ -12,6 +12,7 @@ * NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. """ +import atexit from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import parse_cli_args from samples.vsphere.common.sample_util import pp @@ -34,37 +35,30 @@ def setup(context=None): username, password, skip_verification) - #atexit.register(vapiconnect.logout, stub_config) + atexit.register(vapiconnect.logout, stub_config) def run(): - list_vms(stub_config) - - -def list_vms(stub_config): """ List VMs present in server """ vm_svc = VM(stub_config) list_of_vms = vm_svc.list() + print "----------------------------" + print "List Of VMs" + print "----------------------------" for vm in list_of_vms: print ('{}'.format(vm)) - - -def cleanup(): - pass - + print "----------------------------" + def main(): try: setup() - cleanup() run() - if cleardata: - cleanup() finally: if stub_config: - vapiconnect.logout(stub_config) + pass if __name__ == '__main__': From 9a4e62b90af2348974d1f068ec518520baa900d2 Mon Sep 17 00:00:00 2001 From: pgbidkar Date: Mon, 20 Feb 2017 00:28:13 -0800 Subject: [PATCH 4/6] Modified according to review comments. --- samples/vsphere/vcenter/vm/list_vms.py | 32 +++++++++++--------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/samples/vsphere/vcenter/vm/list_vms.py b/samples/vsphere/vcenter/vm/list_vms.py index 7b691b58..b4ab96b4 100644 --- a/samples/vsphere/vcenter/vm/list_vms.py +++ b/samples/vsphere/vcenter/vm/list_vms.py @@ -13,6 +13,7 @@ """ import atexit +import json from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import parse_cli_args from samples.vsphere.common.sample_util import pp @@ -39,27 +40,22 @@ def setup(context=None): def run(): - """ - List VMs present in server - """ - vm_svc = VM(stub_config) - list_of_vms = vm_svc.list() - print "----------------------------" - print "List Of VMs" - print "----------------------------" - for vm in list_of_vms: - print ('{}'.format(vm)) - print "----------------------------" + """ + List VMs present in server + """ + vm_svc = VM(stub_config) + list_of_vms = vm_svc.list() + print ("----------------------------") + print ("List Of VMs") + print ("----------------------------") + for vm in list_of_vms: + print ('{}'.format(vm)) + print ("----------------------------") def main(): - try: - setup() - run() - finally: - if stub_config: - pass - + setup() + run() if __name__ == '__main__': main() \ No newline at end of file From e4309fae2396954ede6a843124ece74fe6c78534 Mon Sep 17 00:00:00 2001 From: pgbidkar Date: Mon, 20 Feb 2017 00:31:01 -0800 Subject: [PATCH 5/6] Removed unused import json --- samples/vsphere/vcenter/vm/list_vms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/vsphere/vcenter/vm/list_vms.py b/samples/vsphere/vcenter/vm/list_vms.py index b4ab96b4..7d912342 100644 --- a/samples/vsphere/vcenter/vm/list_vms.py +++ b/samples/vsphere/vcenter/vm/list_vms.py @@ -13,7 +13,6 @@ """ import atexit -import json from samples.vsphere.common import vapiconnect from samples.vsphere.common.sample_util import parse_cli_args from samples.vsphere.common.sample_util import pp From 3a3ec038347184a4b5cefd4f4c51f2d39f52289c Mon Sep 17 00:00:00 2001 From: pgbidkar Date: Wed, 22 Feb 2017 14:05:01 +0530 Subject: [PATCH 6/6] Fixed indentation space and Tab format issues --- samples/vsphere/vcenter/vm/list_vms.py | 43 ++++++++++++-------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/samples/vsphere/vcenter/vm/list_vms.py b/samples/vsphere/vcenter/vm/list_vms.py index 7d912342..4d79de26 100644 --- a/samples/vsphere/vcenter/vm/list_vms.py +++ b/samples/vsphere/vcenter/vm/list_vms.py @@ -27,34 +27,31 @@ vCenter/ESX stub_config = None cleardata = False + def setup(context=None): - global stub_config, cleardata - server, username, password, cleardata, skip_verification = \ - parse_cli_args() - stub_config = vapiconnect.connect(server, - username, - password, - skip_verification) - atexit.register(vapiconnect.logout, stub_config) + global stub_config, cleardata + server, username, password, cleardata, skip_verification = parse_cli_args() + stub_config = vapiconnect.connect(server, username, password, skip_verification) + atexit.register(vapiconnect.logout, stub_config) def run(): - """ - List VMs present in server - """ - vm_svc = VM(stub_config) - list_of_vms = vm_svc.list() - print ("----------------------------") - print ("List Of VMs") - print ("----------------------------") - for vm in list_of_vms: - print ('{}'.format(vm)) - print ("----------------------------") - + """ + List VMs present in server + """ + vm_svc = VM(stub_config) + list_of_vms = vm_svc.list() + print ("----------------------------") + print ("List Of VMs") + print ("----------------------------") + for vm in list_of_vms: + print ('{}'.format(vm)) + print ("----------------------------") + def main(): - setup() - run() + setup() + run() if __name__ == '__main__': - main() \ No newline at end of file + main()