From 1e631f7061d010dff736224677cf49b582036c5d Mon Sep 17 00:00:00 2001 From: Jobin George Date: Mon, 25 Jun 2018 17:15:00 +0530 Subject: [PATCH 1/2] A sample to list the Services in the vCenter Server. Signed-off-by:jobingeo jgeorge@vmware.com --- samples/vsphere/services/services_list.py | 75 +++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 samples/vsphere/services/services_list.py diff --git a/samples/vsphere/services/services_list.py b/samples/vsphere/services/services_list.py new file mode 100644 index 00000000..e45f354c --- /dev/null +++ b/samples/vsphere/services/services_list.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +""" +* ******************************************************* +* Copyright (c) VMware, Inc. 2016. 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.' +__copyright__ = 'Copyright 2018 VMware, Inc. All rights reserved.' +__vcenter_version__ = '6.7+' + +from tabulate import tabulate + +from com.vmware.vcenter.services_client import Service +from samples.vsphere.common import vapiconnect +from samples.vsphere.common import sample_cli +from samples.vsphere.common import sample_util + +class ListServices(object): + """ + Demonstrates the details of vCenter Services + + Retrieves the vCenter Services, its Health Status and Service Startup Type. + + Prerequisites: + - vCenter Server + """ + + + def __init__(self): + self.stub_config = None + + def setup(self): + # Create argument parser for standard inputs: + # server, username, password, cleanup and skipverification + parser = sample_cli.build_arg_parser() + args = sample_util.process_cli_args(parser.parse_args()) + + # Connect to vSphere client + self.stub_config = vapiconnect.connect(host=args.server, + user=args.username, + pwd=args.password, + skip_verification=args.skipverification) + def run(self): + services_client = Service(self.stub_config) + services_list = services_client.list_details() + + table = [] + for key,value in services_list.items(): + row = [key, + value.name_key, + value.health, + value.state, + value.startup_type] + table.append(row) + headers = ["Service Name", "Service Name Key", "Service Health", "Service Status", "Service Startup Type"] + print(tabulate(table,headers)) + + + +def main(): + list_services = ListServices() + list_services.setup() + list_services.run() + +if __name__ == '__main__': + main() \ No newline at end of file From 911c3e3bc9fa54e9a073abf3fdf0aba5e7d85ad5 Mon Sep 17 00:00:00 2001 From: Jobin George Date: Mon, 25 Jun 2018 21:13:31 +0530 Subject: [PATCH 2/2] Addressed the Review comments. Signed-off-by:jobingeo jgeorge@vmware.com --- samples/vsphere/services/services_list.py | 31 ++++++++++------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/samples/vsphere/services/services_list.py b/samples/vsphere/services/services_list.py index e45f354c..1be3cac5 100644 --- a/samples/vsphere/services/services_list.py +++ b/samples/vsphere/services/services_list.py @@ -19,8 +19,9 @@ __vcenter_version__ = '6.7+' from tabulate import tabulate -from com.vmware.vcenter.services_client import Service -from samples.vsphere.common import vapiconnect +from vmware.vapi.vsphere.client import create_vsphere_client +from samples.vsphere.common.ssl_helper import get_unverified_session + from samples.vsphere.common import sample_cli from samples.vsphere.common import sample_util @@ -36,23 +37,22 @@ class ListServices(object): def __init__(self): - self.stub_config = None - - def setup(self): # Create argument parser for standard inputs: - # server, username, password, cleanup and skipverification + # server, username, password and skipverification parser = sample_cli.build_arg_parser() args = sample_util.process_cli_args(parser.parse_args()) - # Connect to vSphere client - self.stub_config = vapiconnect.connect(host=args.server, - user=args.username, - pwd=args.password, - skip_verification=args.skipverification) - def run(self): - services_client = Service(self.stub_config) - services_list = services_client.list_details() + # 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): + services_list = self.client.vcenter.services.Service.list_details() table = [] for key,value in services_list.items(): row = [key, @@ -64,11 +64,8 @@ class ListServices(object): headers = ["Service Name", "Service Name Key", "Service Health", "Service Status", "Service Startup Type"] print(tabulate(table,headers)) - - def main(): list_services = ListServices() - list_services.setup() list_services.run() if __name__ == '__main__':