Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
#vmworld
CODE2219U
Python 101 for the vAdmin
Kyle Ruddy, VMware, Inc.Cody De Arkland, VMware, Inc.
#CODE2219U
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc.
Disclaimer
This presentation may contain product features or functionality that are currently under development.
This overview of new technology represents no commitment from VMware to deliver these features in any generally available product.
Features are subject to change, and must not be included in contracts, purchase orders, or sales agreements of any kind.
Technical feasibility and market demand will affect final delivery.
Pricing and packaging for any new features/functionality/technology discussed or presented, have not been determined.
2
The information in this presentation is for informational purposes only and may not be incorporated into any contract. There is no commitment or obligation to deliver any items presented herein. VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 3
Speaker Overview
Writer
GitHub
Podcast
@ thehumblelab.com
@ github.com/codyde
@ vBrownBag.com
@ codydearkland
Cody De ArklandSenior Technical Marketing Architect
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 4
Speaker Overview
Writer
GitHub
Podcast
MS MVP
@ kmruddy.com
@ github.com/kmruddy
@ vBrownBag.com
@kmruddy
Cloud/Datacenter Management
Kyle RuddySenior Architect, Technical Marketing
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc.
Agenda
5
Why Python?
Basic Usage
Software Development Kits
VM Lifecyle Tasks
VMworld 2019 Content: Not for publication or distribution
6©2019 VMware, Inc.
Why Python?
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 7
Why Python? Aside from it making us all think of Monty Python
Object Oriented Language – Very Readable, easy to understand
Swiss army knife of languages; from infrastructure management to web development. Simple scripting to machine learning
Vast resources for learning at all skill levels (communities, training sites, blog content, simulator training)
Widely accepted as a key Developer Operations (DevOps) skill/language to know
It’s just Awesome (Fact, not preference) https://stackoverflow.blog/2017/09/06/incredible-growth-python/The Incredible Growth of PythonVMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 8
Staging Our EnvironmentTools of the Trade
Python 3.6
Visual Studio: Code – Free Integrated Developer Environment (IDE; reads –Place to write your code with lots of tools to help you out along the way)
GitHub.com – Popular git repository, you might have heard of it
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 9
Exploring the APIVMware Code API Explorer
https://code.vmware.com/apis
Central API reference for many products
Constantly updated
Includes links to associated SDKs and tooling
VMworld 2019 Content: Not for publication or distribution
10©2019 VMware, Inc.
Scenario 1 Using built-in tools to access vCenter objects
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 11
Concepts you’ll learn
• Interacting with the REQUESTS Module
• Did you know Python has over 125,000 modules that can be used to simplify interactions?
• Setting variables
• Get into the habit of creating parameterized code. It’ll save you in the long run!
• POST’s and GET’s against the vCenter API
• Parsing/Returning/Printing responses
• Building Functions and “If” statements
Interacting with the vCenter REST APIs
Scenario 1 – Overview
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 12
Modules are a collection of classes, methods, and or variables that extend and enhance the functionality of Python
Our focus is on the “Requests” module, a common module for interacting with HTTP endpoints via REST
In our initial examples we import a package that disables certificate verification, not best practice, but Homelab
We leverage basic variables for the initial example; but our code could be modified to use environment variables, static file values, or prompt the user for input
Scenario 1 – Variables
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 13
vCenter API requires a POST method to “send” our authentication data
Using f’{variable}’ is called an “F String”; allows you to directly call a variable in a set of data
Leverage “auth” in our requests.post call to initiate basic authentication with username and password
Simple post will just return the response object; create a new object to interact with the returned data
Once you have your object, use tab to “autocomplete” and see what values are available
When we access the “.json()” method we are able to interact directly with the JSON objects
We store our authentication token to a variable to call in future actions
Scenario 1 – Posting Stuff’s and Getting Objects!
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 14
GET method is used to “get” calls via a REST API
Header needs to include the authentication token.
The /vcenter/vms REST endpoint returns a JSON object of all VMs in a vCenter environment
If we assign the dataset to an object, we can perform operations against it
We can parse the object individually by calling the index in the array (obj.json()[0], obj.json()[1], etc…) or parse the whole object leveraging a “for” loop.
Scenario 1 – Getting Data and Returning JSON
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 15
Using a “for” loop we can perform actions against each object in a JSON array
We loop through and create a “print” statement, concatenating the name and power state to the object
Using a for loop we can also “search” for a specific item inside the object
Working with JSON objects should become second nature when working with various APIs
Scenario 1 – Parsing Our Data
VMworld 2019 Content: Not for publication or distribution
16©2019 VMware, Inc.
Scenario 1 – Demo
VMworld 2019 Content: Not for publication or distribution
17©2019 VMware, Inc.
Scenario 2Introducing an SDK
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 18
Simplifies the development process
Extends the capabilities of the existing language set
Platform specific
Usually includes built-in debugging capabilities
May include sample code
What is a Software Development Kit?
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 19
Interacts with vSphere Web Services API
Open Source and available on GitHubhttps://github.com/vmware/pyvmomi
Supports Python 2.7 and 3.3+
Interacts with:
• vSphere Automation API
• VMware Cloud on AWS
• VMware NSX-T
Open Source and available on GitHubhttps://github.com/vmware/vsphere-automation-sdk-python
Supports Python 2.7 and 3.3+
pyVmomi vSphere Automation SDK for Python
VMware SDKs for Python
VMworld 2019 Content: Not for publication or distribution
20©2019 VMware, Inc.
Scenario 2 – DemovSphere Automation SDK for Python – Install
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 21
VMworld 2019 Content: Not for publication or distribution
22©2019 VMware, Inc.
Scenario 3Controlling the VM Lifecycle
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 23
Create
Power Status
Modification
Removal
Common VM Lifecycle Tasks
VMworld 2019 Content: Not for publication or distribution
24©2019 VMware, Inc.
Scenario 3 – DemoControlling the VM Lifecycle
VMworld 2019 Content: Not for publication or distribution
©2019 VMware, Inc. 26
So What’s Next?Continuing Your Training
Great Resources To Continue Learning
• Stack Overflow Forums
• Learn Python The Hard way (Book)
• CodeAcademy
• Pluralsight
VMworld 2019 Content: Not for publication or distribution
VMworld 2019 Content: Not for publication or distribution
VMworld 2019 Content: Not for publication or distribution