From 0f1107ce53e861cb35b784c2c5b606240187e3fc Mon Sep 17 00:00:00 2001 From: Tianhao He Date: Sun, 27 Aug 2017 17:32:52 -0700 Subject: [PATCH] add lab for vmworld 2017 --- vmworld2017/README.md | 37 ++++++++ vmworld2017/_config.yml | 1 + vmworld2017/lab.md | 182 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 vmworld2017/README.md create mode 100644 vmworld2017/_config.yml create mode 100644 vmworld2017/lab.md diff --git a/vmworld2017/README.md b/vmworld2017/README.md new file mode 100644 index 00000000..aa39b774 --- /dev/null +++ b/vmworld2017/README.md @@ -0,0 +1,37 @@ +# VMTN6720U - Hackathon Training: Getting started with the vSphere Automation SDK for Python +**Date:** 08/28/17 6:30PM-8:00PM + +# Agenda +* Introduction to vSphere REST API +* vSphere Automation SDKs +* Getting Started Using the Python SDK +* Lab + +# Introduction to vSphere REST API +* Consume from SDKs, CLI’s +* NOTE: Need bullets from Alan + +# vSphere Automation SDKs +* Supported Languages + * Python, Java, Ruby, Perl, .NET, REST (JavaScript/Postman) +* Open Source on Github + * SDK Includes: + * Runtime + * Serialization + * Language Binding + * Python Wrapper around REST API + * API Reference Documentation (for offline usage) + +# [Labs](lab.md) + +**Wifi**: VMworld-Hackathon-Training-4
+**Wifi Password**: Hackathon2017 + +Connect to VC matching first letter of your last name (poorman’s load balance): + +**VC1**: [192.168.104.11](https://192.168.104.11) (A-H)
+**VC2**: [192.168.104.13](https://192.168.104.13) (I-Q)
+**VC3**: [192.168.104.13](https://192.168.104.13) (R-Z)
+ +**VC Username**: administrator@vsphere.local
+**VC Password**: VMware1! diff --git a/vmworld2017/_config.yml b/vmworld2017/_config.yml new file mode 100644 index 00000000..259a24e4 --- /dev/null +++ b/vmworld2017/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-tactile \ No newline at end of file diff --git a/vmworld2017/lab.md b/vmworld2017/lab.md new file mode 100644 index 00000000..7f971826 --- /dev/null +++ b/vmworld2017/lab.md @@ -0,0 +1,182 @@ +# VMworld 2017 Python SDK Hackathon + +## Install Python +Go to: https://www.python.org/downloads/ and download and install Python for your Platform. +The SDK supports both python2 and python3, but we recommend to install the latest version of python3. + +## Setup Python SDK + +Create and activate a virtual env: + +#### Python2: + + virtualenv + +#### Python3: + +| Windows | OSX | +|---------|-----| +|```py -m venv ```|```python3 -m venv ```| +|```\Scripts\activate```|```source /bin/activate```| + +## Clone the github repo +The python github repo is located [here](https://github.com/vmware/vsphere-automation-sdk-python) + +```bash +git clone https://github.com/vmware/vsphere-automation-sdk-python +``` +Or optionally [download](https://github.com/vmware/vsphere-automation-sdk-python/archive/master.zip) the SDK from Github as a .zip file. + +## Install dependencies + +```bash +cd vsphere-automation-sdk-python +pip install -r requirements.txt +``` + +## Set PYTHONPATH to use SDK helper methods + +#### Linux/Mac: + + export PYTHONPATH=${PWD}:$PYTHONPATH + +#### Windows: + + set PYTHONPATH=%cd%;%PYTHONPATH% + +## OPTIONAL: Install Visual Studio Code IDE +Microsoft's Visual Studio Code is a great (free) IDE that can be used for Python via a plugin providing a rich development environment. + +[Visual Studio Code](https://code.visualstudio.com/)
+[Python Plugin](https://marketplace.visualstudio.com/items?itemName=donjayamanne.python) + +## vSphere Python SDK Examples +Start the Python command interpreter: + +#### Linux/Mac: + + python3 + +#### Windows: + + py + +## List VC inventory in interactive mode + +### Connect to the vSphere REST API endpoint + +```python +from samples.vsphere.common import vapiconnect +stub_config = vapiconnect.connect("", "administrator@vsphere.local", "VMware1!", True) +``` + +### List VMs + +```python +from com.vmware.vcenter_client import VM +vm_svc = VM(stub_config) +vm_svc.list() +``` + +### List Datacenters + +```python +from com.vmware.vcenter_client import * +Datacenter(stub_config).list() +``` + +### List Clusters + +```python +Cluster(stub_config).list() +``` + +### List Hosts + +```python +Host(stub_config).list() +``` + +### List Folders + +```python +Folder(stub_config).list() +``` + +### List Datastore + +```python +Datastore(stub_config).list() +``` + +### Find a specific host using a filter spec + +```python +filter_spec = Host.FilterSpec(names=set([""])) +Host(stub_config).list(filter_spec) +``` + +Note: To exit interactive Python simply type quit() and press Return: + +```python +quit() +``` + +## Run samples on github +For this portion of the lab we recommend using your favorite code editor or as mentioned above download and install [Visual Studio Code](https://code.visualstudio.com/). We will be using Visual Studio code to demo these examples. + +Open the sample property file testbed.py using your favourite text editor, such as: + +#### Linux/OSX: + +```bash +vi samples/vsphere/vcenter/setup/testbed.py +``` + +#### Windows: + +```shell +notepad samples\vsphere\vcenter\setup\testbed.py +``` + +And then change the following two settings: + +```python +config["SERVER"] = "" +config["VM_DATASTORE_NAME"] = "vsanDatastore" +``` + +### Run create basic VM sample + +**NOTE:** Be sure to name the VM using your First and Last name so it's unique!! + +```bash +python samples/vsphere/vcenter/vm/create/create_basic_vm.py -v -n "vm_" +``` + +### Verify the new VM is created successfully + +```bash +python samples/vsphere/vcenter/vm/list_vms.py -v +``` + +### Run deploy ovf template sample + +**NOTE:** Use single quote for password in Mac 'VMware1!' + +```bash +python samples/vsphere/contentlibrary/ovfdeploy/deploy_ovf_template.py -v -clustername "Cluster1" -libitemname "Sample_OVF" +``` + +### Verify the new VM is deployed successfully + +```bash +python samples/vsphere/vcenter/vm/list_vms.py -v +``` + +## Exercises +Try to use the sample_template to create three scripts and then run them: + +* Power on/off the VM you created +* Edit VM cpu/memory +* Add a CD-ROM to your VM