The vSphere Automation SDK for Python has recently received some exciting updates. These updates add the ability to easily develop against and automate VMware Cloud on AWS! There are even samples to list organization information, manage SDDCs, and lots of networking functionality!
Let’s take a look at how to easily get started with this SDK on a MacOS system. Before diving in though, there are a couple prerequisites we’ll need to download and install.
The first thing we’ll want to do before getting started with this SDK is to clone the repository to the local system. We can do that with the following command:
git clone https://github.com/vmware/vsphere-automation-sdk-python.git
Next, we’ll need to install the package manager, Homebrew. Using Homebrew will allow us to easily download and update additional programs. It can be easily downloaded and installed with the following command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now, we’ll install Python in the recommended version of 3.6. It’s worth noting that MacOS comes with a version of Python already installed. However, it is generally an older version (normally, Python 2.7) and is used for some system related tasks. Due to that, it is best not to modify that version or the defaults.
Utilizing our package manager, we’ll install Python 3.6 with the following command:
brew install python3
It’s also recommended to isolate your development environments from each other so, we’ll setup a virtual environment and jump into it. We can do that with the following commands:
python3 -m venv VMWonAWSDemo source VMWonAWSDemo/bin/activate
Then, we’ll install the SDK requirements. They’re listed within the root of the SDK folder in the file labeled: requirements.txt. After checking out which requirements are necessary, we’ll install those requirements by way of the following command while in the root of the SDK folder:
pip install --upgrade --force-reinstall -r requirements.txt --extra-index-url file:///Users/kyleruddy/GitHub/vsphere-automation-sdk-python/lib/
Lastly, we need to update the PYTHONPATH variable. This allows Python to be able to use the SDK helper methods. We can do this with the following command:
One other item worth mentioning is an IDE. This will make editing, running and debugging the code a lot easier to deal with. I generally use Visual Studio Code, which is available here.
Let’s get started using some of the samples that have been included with this SDK.
Obtaining Refresh Token
The VMware Cloud on AWS APIs use an OAuth refresh token for authentication. These have to be created through the Cloud Console. Once logged into the Cloud Console, click the drop-down on the top right-hand corner that’s by your name. From there, click on the ‘OAuth Refresh Token’ button. If you don’t already have a refresh token available, there will be a blue link to create a new token. Afterwards, you’ll have a refresh token and be ready to authenticate into the VMware Cloud on AWS APIs.
Obtaining Organization Information
The most important thing you can do when getting started is making your Organization (Org) ID easily accessible. This ID is referenced in almost every API method VMware Cloud on AWS has. If we browse through the SDK folder structure, we’ll see there’s an example we can use to retrieve information about the Orgs. There is a Python package named ‘organization_operations.py’ in the ‘samples/vmc/orgs’ directory.
export refreshtoken='0073c5b2-xxxx-xxxx-xxxx-606592f7f070' python3 ./samples/vmc/orgs/organization_operations.py -r $refreshtoken
We can see in the results above this example performs a couple tasks. First, we receive a list of all the Orgs this refresh token is associated with. Second, we get some additional information about the first Org that was returned.
Obtaining SDDC Information
The next most important thing we can obtain is information about the SDDC/s this Org has. However, at the time of this blog release, that functionality wasn’t explicitly available. Let’s see how to do this in the REPL, or Python’s interactive interpreter:
import requests from vmware.vapi.vmc.client import create_vmc_client refreshtoken = '0073c5b2-xxxx-xxxx-xxxx-606592f7f070' orgid = 'eb8f2d0a-xxxx-xxxx-xxxx-a4d6282c5555' vmc_client = create_vmc_client(refreshtoken) sddcs = vmc_client.orgs.Sddcs.list(orgid) sddcs sddcs.name
I think this ability is something that will be quite helpful as part of the example SDDC package. Adding it is as easy as adding the following to the ‘create_delete_sddc.py’ file:
def list_sddc(self): sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id) if not sddcs: raise ValueError('The sample requires at least one SDDC associated' 'with the calling user') print("\n# Example: List SDDCs") table =  for sddc in sddcs: table.append([sddc.id, sddc.name, sddc.resource_config.region]) print(tabulate(table, ['ID', 'Name', 'AWS Region']))
However, you won’t need to do that. This SDK is open-sourced and I was able to submit a pull request to get it added in already! As part of the PR, we updated the file name to be ‘sddc_crud.py’ and it is available in the ‘samples/vmc/sddc’ directory.
Logical Network Tasks
Last example, let’s run through some tasks for logical network administration. There’s a sample for this as well. The ‘logical_network_crud.py’ package will create a new logical network, list all of the logical networks, list only the newly created logical network, and then delete the newly created logical network. This file is available in the ‘samples/vmc/networks/’ directory.
export orgid='eb8f2d0a-xxxx-xxxx-xxxx-a4d6282c5555' export sddcid='584e8cc8-xxxx-xxxx-xxxx-733871f9f0a6' python3 ./samples/vmc/networks/logical_network_crud.py -r $refreshtoken -o $orgid -s $sddcid --network-name 'Python-SDK-LogicalNetwork' -c
This post shows you how to easily get started with the vSphere Automation SDK for Python with VMware Cloud on AWS. It takes you through the setup of your local development environment, as well as running some of the individual samples. You can then take whichever is most relevant to you and apply to your environment or, pull parts of this code out and use it to automate or integrate as needed.
With our SDKs now being open sourced, we are intent on making sure these samples are of a high quality. If you notice areas of improvement, or come up with some new samples, please feel free to raise issues or pull requests on our GitHub repository.
If you’ve missed any of the prior posts, here are the others in this ‘Getting Started’ series with our open-sourced SDKs: