Skip to content

Add a basic sample template for simple samples #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions sample_template/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
Boilerplate code for new samples
================================

sample_template.py contains boilerplate code that can be used for most of
the new samples. Please fill out the TODOs with your sample information.
The steps included in the template code are:

* Cmd line argument parser.
* Login to vAPI services using vSphere Automation API.
* Cleanup after sample execution.
* For complex samples require setup and cleanup please use sample_template_complex.py
* For simple samples which just demo basic API usage please use sample_template_basic.py
60 changes: 60 additions & 0 deletions sample_template/sample_template_basic.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self):
parser = sample_cli.build_arg_parser()

# Add your custom input arguments
parser.add_argument('-n', '--vm_name',
parser.add_argument('--vm_name',
action='store',
default='Sample_Default_VM_for_Simple_Testbed',
help='Name of the testing vm')
Expand Down