Skip to content

Commit 90d5c09

Browse files
authored
Merge pull request #145 from tianhao64/master
Add a basic sample template for simple samples
2 parents 65ee81b + 757d3cc commit 90d5c09

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

sample_template/README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
Boilerplate code for new samples
22
================================
33

4-
sample_template.py contains boilerplate code that can be used for most of
5-
the new samples. Please fill out the TODOs with your sample information.
6-
The steps included in the template code are:
7-
8-
* Cmd line argument parser.
9-
* Login to vAPI services using vSphere Automation API.
10-
* Cleanup after sample execution.
4+
* For complex samples require setup and cleanup please use sample_template_complex.py
5+
* For simple samples which just demo basic API usage please use sample_template_basic.py
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'TODO: <your name and email>'
17+
__vcenter_version__ = 'TODO: <compatible vcenter versions>'
18+
19+
from com.vmware.vcenter_client import VM
20+
from vmware.vapi.vsphere.client import create_vsphere_client
21+
22+
from samples.vsphere.common import sample_cli
23+
from samples.vsphere.common import sample_util
24+
from samples.vsphere.common.ssl_helper import get_unverified_session
25+
26+
27+
"""
28+
TODO: Sample description and prerequisites.
29+
e.g. Demonstrates getting list of VMs present in vCenter
30+
31+
Sample Prerequisites:
32+
- vCenter
33+
"""
34+
35+
# Create argument parser for standard inputs:
36+
# server, username, password, cleanup and skipverification
37+
parser = sample_cli.build_arg_parser()
38+
39+
# Add your custom input arguments
40+
parser.add_argument('--vm_name',
41+
action='store',
42+
default='Sample_Default_VM_for_Simple_Testbed',
43+
help='Name of the testing vm')
44+
45+
args = sample_util.process_cli_args(parser.parse_args())
46+
47+
# Skip server cert verification if needed.
48+
# This is not recommended in production code.
49+
session = get_unverified_session() if args.skipverification else None
50+
51+
# Connect to vSphere client
52+
client = create_vsphere_client(server=args.server,
53+
username=args.username,
54+
password=args.password,
55+
session=session)
56+
57+
# List VMs
58+
filter_spec = VM.FilterSpec(names=set([args.vm_name]))
59+
vms = client.vcenter.VM.list(filter_spec)
60+
print(vms)

sample_template/sample_template.py renamed to sample_template/sample_template_complex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self):
3838
parser = sample_cli.build_arg_parser()
3939

4040
# Add your custom input arguments
41-
parser.add_argument('-n', '--vm_name',
41+
parser.add_argument('--vm_name',
4242
action='store',
4343
default='Sample_Default_VM_for_Simple_Testbed',
4444
help='Name of the testing vm')

0 commit comments

Comments
 (0)