Skip to content

Initial sample drop #3

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
Mar 3, 2017
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.py[cod]
__pycache__/
.DS_Store
.idea/*
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: python
python:
- "2.7.13"
- "3.6"
# command to install dependencies
install:
- pip install -r requirements.txt
- pip install -r test-requirements.txt
- pip install pycodestyle
# command to run tests
script: pycodestyle samples/*
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License

Copyright 2017 VMware Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
195 changes: 193 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,193 @@
# vsphere-automation-sdk-python-samples
Python Samples for the vSphere Automation SDK
# VMware vSphere Automation SDK for Python
[![Build Status](https://travis-ci.com/tianhao64/vsphere-automation-sdk-python-samples.svg?token=v9mEJjcpDiQ9DrYbzyaQ&branch=master)](https://travis-ci.com/tianhao64/vsphere-automation-sdk-python-samples)
## Table of Contents
- [Abstract](#abstract)
- [Quick Start Guide](#quick-start-guide)
- [Installing the required Python Packages](#installing-the-required-python-packages)
- [Setting up a vSphere Test Environment](#setting-up-a-vsphere-test-environment)
- [Running the SDK Sample Setup Script](#running-the-sdk-sample-setup-script)
- [Running a complex sample](#running-a-complex-sample)
- [API Documentation](#api-documentation)
- [Submitting samples](#submitting-samples)
- [Required Information](#required-information)
- [Suggested Information](#suggested-information)
- [Contribution Process](#contribution-process)
- [Code Style](#code-style)
- [Resource Maintenance](#resource-maintenance)
- [Maintenance Ownership](#maintenance-ownership)
- [Filing Issues](#filing-issues)
- [Resolving Issues](#resolving-issues)
- [VMware Sample Exchange](#vmware-sample-exchange)
- [Repository Administrator Resources](#repository-administrator-resources)
- [Board Members](#board-members)
- [Approval of Additions](#approval-of-additions)

## Abstract
This document describes the vSphere Automation Python SDK samples that use the vSphere Automation
python client library. Additionally, some of the samples demonstrate the combined use of the
vSphere Automation and vSphere APIs. To support this combined use, the vSphere Automation Python SDK
samples require the vSphere Management SDK packages (pyVmomi) to be installed on the client.
The samples have been developed to work with python 2.7.x and 3.3+

## Supported vCenter Releases:
vCenter 6.0 and 6.5.
Certain APIs and samples that are introduced in 6.5 release, such as vCenter, Virtual Machine and Appliance Management. Please refer to the notes in each sample for detailed compatibility information.

## Quick Start Guide
This document will walk you through getting up and running with the Python SDK Samples.
Prior to running the samples you will need to setup a vCenter test environment and
install local Python packages, the following steps will take you through this process.

Before you can run the SDK samples we'll need to walk you through the following steps:

1. Installing the required Python packages
2. Installing SDK provided packages
3. Setting up a vSphere test environment
4. Running SDK Samples setup script

### Installing the required Python Packages
**Note:** The SDK requires Python v2.7+ (preferably v3.6) to run the setup/samples,
please make sure you have the appropriate version installed before continuing.
If you are on macOS/OSX/Linux, please note that the system installed version of
Python may be outdated and/or not be intended for development and we recommended you [install Python](http://docs.python-guide.org/en/latest/starting/installation/) yourself before installing the required packages. [Virtualenv](https://virtualenv.pypa.io/en/stable/) is also highly recommended.

The required packages are listed in the requirements.txt file and installed using "pip install"; For more details on how to install python packages using pip please refer to the [pip user guide](http://pip.readthedocs.io/en/latest/user_guide/).

```cmd
pip install -r requirements.txt
```

### Setting up a vSphere Test Environment
**NOTE:** The samples are intended to be run against a freshly installed **non-Production** vSphere setup as the scripts may make changes to the test environment and in some cases can destroy items when needed.

To run the samples a vSphere test environment is required with the following configuration
* 1 vCenter Server
* 2 ESX hosts
* 1 NFS Datastore with at least 3GB of free capacity

Please have the details of these available but do not have any configuration pre-created on vCenter server or ESXi Hosts, for example there should be no existing datacenters, clusters or attached hosts on the vCenter server.

### Running the SDK Sample Setup Script
Before executing the samples we'll need to setup the vSphere test environment using one of the sample scripts. Before we run the script we'll need to edit one of the files and provide IP addresses for the various machine instances.

First, from the command line change to the SDK ./bin folder.

```cmd
$ cd /path/to/vsphere-automation-sdk-python-samples/bin
```

Next, using a text editor open ../samples/vsphere/vcenter/setup/testbed.py and edit the following settings replace everything in < > brackets with your environment information. Leave the rest of the settings in this file at their default values.

```python
config["SERVER"] = "<vcenter_hostname_or_ip>"
config["USERNAME"] = "<vsphere_username>"
config["PASSWORD"] = "<vsphere_password>"

config["ESX_HOST1"] = "<ESX_host1_ipaddress>"
config["ESX_HOST2"] = "<ESX_host2_ipaddress>"
config["ESX_USER"] = "<esx_username>"
config["ESX_PASS"] = "<esx_password>"

config["USE_NFS"] = True
config["NFS_HOST"] = "<nfs_ipaddress>"
config["NFS_REMOTE_PATH"] = "/store1"
```

Save and close the file.

At this point, we're ready to run the setup script.

This script will perform the following:
* Create 2 test Datacenters
* Create a test Cluster
* Create Test Folders for VM Storage
* Attach the hosts
* Create a Distributed Switch
* Create a Distributed Portgroup
* Attach the NFS datastore (if Selected) to the hosts
* Copy the [Photon OS](https://vmware.github.io/photon/) ISO image downloaded from [VMware's bintray server](https://dl.bintray.com/vmware/photon) to the datastore
* Create directories to add sample ports


**Note:** The setup script may take several minutes to complete.

**To view the available command-line options:**

```cmd
$ ./run_sample.sh ../samples/vsphere/vcenter/setup/main.py -h
```

**To run the setup script:**

```cmd
$ ./run_sample.sh ../samples/vsphere/vcenter/setup/main.py -sv
```

After completion you will see from the output and also the vSphere Webclient that the environment has now been fully setup and is ready to easily run further samples.

### Running a complex sample
This SDK includes a sample script which can be used to perform a number of actions and give you an indication of how to perform multiple vCenter actions, this script is located in the /samples/vsphere/vcenter/setup/ directory, use the following instructions to run this sample:

**Run the vAPI vCenter sample suite:**

```cmd
$ ./run_sample.sh ../samples/vsphere/vcenter/setup/main.py -riv
```

## API Documentation
The API documentation can be found [here] (doc/client.zip)

## Submitting samples
### Required Information
The following information must be included in the README.md or in the sample docstring in case README already exists in same folder.
* Author Name
* This can include full name, email address or other identifiable piece of information that would allow interested parties to contact author with questions.
* Date
* Date the sample was originally written
* Minimal/High Level Description
* What does the sample do ?
* Any KNOWN limitations or dependencies

### Suggested Information
The following information should be included when possible. Inclusion of information provides valuable information to consumers of the resource.
* vSphere version against which the sample was developed/tested
* SDK version against which the sample was developed/tested
* Python version against which the sample was developed/tested

### Contribution Process

* Follow the [GitHub process](https://help.github.com/articles/fork-a-repo)
* Please use one branch per sample or change-set
* Please use one commit and pull request per sample
* Please post the sample output along with the pull request
* If you include a license with your sample, use the project license

### Code Style

Please conform to pep8 standards. Check your code by running the pep8 tool.
https://pypi.python.org/pypi/pep8

## Resource Maintenance
### Maintenance Ownership
Ownership of any and all submitted samples are maintained by the submitter.
### Filing Issues
Any bugs or other issues should be filed within GitHub by way of the repository’s Issue Tracker.
### Resolving Issues
Any community member can resolve issues within the repository, however only the board member can approve the update. Once approved, assuming the resolution involves a pull request, only a board member will be able to merge and close the request.

### VMware Sample Exchange
It is highly recommended to add any and all submitted samples to the VMware Sample Exchange: <https://code.vmware.com/samples>

Sample Exchange can be allowed to access your GitHub resources, by way of a linking process, where they can be indexed and searched by the community. There are VMware social media accounts which will advertise resources posted to the site and there's no additional accounts needed, as the VMware Sample Exchange uses MyVMware credentials.

## Repository Administrator Resources
### Board Members

Board members are volunteers from the SDK community and VMware staff members, board members are not held responsible for any issues which may occur from running of samples from this repository.

Members:
* Tianhao He (VMware)
* Steve Trefethen (VMware)

### Approval of Additions
Items added to the repository, including items from the Board members, require 2 votes from the board members before being added to the repository. The approving members will have ideally downloaded and tested the item. When two “Approved for Merge” comments are added from board members, the pull can then be committed to the repository.
11 changes: 11 additions & 0 deletions bin/run_sample.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off
setlocal ENABLEDELAYEDEXPANSION
:: Clear the command-prompt screen
cls
set SRCDIR=%cd%\..\
set LIBDIR=%cd%\..\lib
set PYTHONPATH=%PYTHONPATH%;%SRCDIR%
setlocal DisableDelayedExpansion
:: Run the sample
python %*
endlocal
13 changes: 13 additions & 0 deletions bin/run_sample.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
SCRIPTDIR=`dirname $0`
cd $SCRIPTDIR
PATHDIR=`pwd`
PROJECT_ROOT=$PATHDIR/..
SRCDIR=$PROJECT_ROOT/
LIBDIR=$PROJECT_ROOT/lib

# add the src directory to the python path
export PYTHONPATH=$PYTHONPATH:$SRCDIR

# run the sample
python $@
Binary file added doc/client.zip
Binary file not shown.
9 changes: 9 additions & 0 deletions lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SDK libraries

For python developers, client libraries are supplied for testing and development purposes. All the supplied libraries are located under lib directory.

Name | Description
------------------------------------| -------------
vapi_runtime-\<version>.zip | vAPI runtime responsible for serialization/de-serialization of objects and wire protocol
vapi_common_client-\<version>.zip | vAPI common client code
vapi_client_bindings-\<version>.zip | client stubs for vSphere Automation APIs
Binary file added lib/vapi_client_bindings-2.5.0.zip
Binary file not shown.
Binary file added lib/vapi_common_client-2.5.0.zip
Binary file not shown.
Binary file added lib/vapi_runtime-2.5.0.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pyOpenSSL >= 0.14
pyVmomi >= 6.5
lxml
suds ; python_version < '3'
suds-jurko ; python_version >= '3.0'
lib/vapi_runtime-2.5.0.zip
lib/vapi_common_client-2.5.0.zip
lib/vapi_client_bindings-2.5.0.zip
24 changes: 24 additions & 0 deletions samples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
* *******************************************************
* Copyright 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.
"""

__author__ = 'VMware, Inc.'
__copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.'


# Required to distribute different parts of this
# package as multiple distribution
try:
import pkg_resources
pkg_resources.declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__) # @ReservedAssignment
11 changes: 11 additions & 0 deletions samples/vsphere/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#Client Samples

The following table shows the sample sub-directories and their contents.

Directory | Description
----------------| -------------
common | Samples common helper classes and abstractions; This package does NOT contain any sample
contentlibrary | Samples for Content Library APIs
tagging | Samples for Tagging APIs
vcenter | Samples for managing vSphere infrastructure and virtual machines
sso | Samples for Platform Service Controller(PSC), SSO and Lookup Service APIs
24 changes: 24 additions & 0 deletions samples/vsphere/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
* *******************************************************
* Copyright 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.
"""

__author__ = 'VMware, Inc.'
__copyright__ = 'Copyright 2016 VMware, Inc. All rights reserved.'


# Required to distribute different parts of this
# package as multiple distribution
try:
import pkg_resources
pkg_resources.declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__) # @ReservedAssignment
10 changes: 10 additions & 0 deletions samples/vsphere/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Required to distribute different parts of this
# package as multiple distribution
try:
import pkg_resources

pkg_resources.declare_namespace(__name__)
except ImportError:
from pkgutil import extend_path

__path__ = extend_path(__path__, __name__) # @ReservedAssignment
42 changes: 42 additions & 0 deletions samples/vsphere/common/id_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
* *******************************************************
* Copyright (c) VMware, Inc. 2013. 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.
"""

__author__ = 'VMware, Inc.'
__copyright__ = 'Copyright 2013 VMware, Inc. All rights reserved.'


import uuid
import string
import random


def generate_random_uuid():
return str(uuid.uuid4())


def rand(value):
return value + generate_random_string(5)


def generate_random_string(length):
return ''.join(random.choice(string.ascii_uppercase) for _i in range(length))


def main():
print(generate_random_uuid())
print(generate_random_string(5))
print(rand('Simple VM-'))


# Start program
if __name__ == "__main__":
main()
Loading