mirror of
https://github.com/vmware/vsphere-automation-sdk-python.git
synced 2024-11-22 01:39:58 -05:00
Merge pull request #145 from tianhao64/master
Add a basic sample template for simple samples
This commit is contained in:
commit
90d5c09943
@ -1,10 +1,5 @@
|
|||||||
Boilerplate code for new samples
|
Boilerplate code for new samples
|
||||||
================================
|
================================
|
||||||
|
|
||||||
sample_template.py contains boilerplate code that can be used for most of
|
* For complex samples require setup and cleanup please use sample_template_complex.py
|
||||||
the new samples. Please fill out the TODOs with your sample information.
|
* For simple samples which just demo basic API usage please use sample_template_basic.py
|
||||||
The steps included in the template code are:
|
|
||||||
|
|
||||||
* Cmd line argument parser.
|
|
||||||
* Login to vAPI services using vSphere Automation API.
|
|
||||||
* Cleanup after sample execution.
|
|
||||||
|
60
sample_template/sample_template_basic.py
Normal file
60
sample_template/sample_template_basic.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
"""
|
||||||
|
* *******************************************************
|
||||||
|
* Copyright (c) VMware, Inc. 2019. 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__ = 'TODO: <your name and email>'
|
||||||
|
__vcenter_version__ = 'TODO: <compatible vcenter versions>'
|
||||||
|
|
||||||
|
from com.vmware.vcenter_client import VM
|
||||||
|
from vmware.vapi.vsphere.client import create_vsphere_client
|
||||||
|
|
||||||
|
from samples.vsphere.common import sample_cli
|
||||||
|
from samples.vsphere.common import sample_util
|
||||||
|
from samples.vsphere.common.ssl_helper import get_unverified_session
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
TODO: Sample description and prerequisites.
|
||||||
|
e.g. Demonstrates getting list of VMs present in vCenter
|
||||||
|
|
||||||
|
Sample Prerequisites:
|
||||||
|
- vCenter
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Create argument parser for standard inputs:
|
||||||
|
# server, username, password, cleanup and skipverification
|
||||||
|
parser = sample_cli.build_arg_parser()
|
||||||
|
|
||||||
|
# Add your custom input arguments
|
||||||
|
parser.add_argument('--vm_name',
|
||||||
|
action='store',
|
||||||
|
default='Sample_Default_VM_for_Simple_Testbed',
|
||||||
|
help='Name of the testing vm')
|
||||||
|
|
||||||
|
args = sample_util.process_cli_args(parser.parse_args())
|
||||||
|
|
||||||
|
# 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
|
||||||
|
client = create_vsphere_client(server=args.server,
|
||||||
|
username=args.username,
|
||||||
|
password=args.password,
|
||||||
|
session=session)
|
||||||
|
|
||||||
|
# List VMs
|
||||||
|
filter_spec = VM.FilterSpec(names=set([args.vm_name]))
|
||||||
|
vms = client.vcenter.VM.list(filter_spec)
|
||||||
|
print(vms)
|
@ -38,7 +38,7 @@ class Sample(object):
|
|||||||
parser = sample_cli.build_arg_parser()
|
parser = sample_cli.build_arg_parser()
|
||||||
|
|
||||||
# Add your custom input arguments
|
# Add your custom input arguments
|
||||||
parser.add_argument('-n', '--vm_name',
|
parser.add_argument('--vm_name',
|
||||||
action='store',
|
action='store',
|
||||||
default='Sample_Default_VM_for_Simple_Testbed',
|
default='Sample_Default_VM_for_Simple_Testbed',
|
||||||
help='Name of the testing vm')
|
help='Name of the testing vm')
|
Loading…
Reference in New Issue
Block a user