Home > Blogs > VMware {code} > Tag Archives: python

Tag Archives: python

VMware Code Developer Center Microsite – VMware Cloud on AWS

VMware Code has a terrific set of Developer Center microsites already available for vRealize Automation, NSX, Workspace ONE, and Apteligent. Each of these Developer Centers give easy access to documentation, developer and automation tools, code samples, and more. As of today, we are happy to announce the brand-new microsite dedicated to VMware Cloud on AWS!

VMware Code Developer Center Microsite for VMware Cloud on AWS

The VMware Code Developer Center microsite for VMware Cloud on AWS gives direct access to those resources developers and automation specialists desire most! There’s an overview featuring what’s available through the VMware Cloud on AWS service as well as some videos that dive right into the available APIs and tooling available. This is followed up by a more detailed overview of the available core capabilities. Then, the key use cases are outlined along with the ability to find out more information about each of them and what kind of development efforts are estimated.

The Developer Resources section has a ton of great information available. This section gives direct links to the SDKs and CLIs that support VMware Cloud on AWS. There is also a detailed list of documentation and blog posts to help better understand each aspect of the service as well as how to quickly and easily get started using each of those SDKs and CLIs. Lastly, this section features code that’s been contributed by both VMware and the community to the VMware Code Sample Exchange in a variety of formats. From PowerCLI to Python, CloudFormation to Terraform, there’s some great resources already available and we more than welcome additional contributions!

The last section is dedicated to Community! Community involvement is an important aspect of the automation and developer resources and this section includes links to get involved directly. Joining VMware Code allows for access to an active Slack group with over 5,000 members, receive regular newsletters with the latest VMware information, and access to the developer forums!

Check out the VMware Code Developer Center microsite for VMware Cloud on AWS today!
Let us know in the comments what you think and/or if there’s anything missing that you may be looking for.

Datacenter CLI – Installation Walkthrough

Datacenter CLI (DCLI) 2.9.1 has been released and the new features are fantastic! One of the key new updates is how we install this new version of DCLI. In the past, DCLI was installed as part of vSphere CLI (vCLI). In this current version, DCLI is installed through the Python Package Index (PyPI) using pip. PyPI is the official, third-party, online repository for Python packages. Pip is a package management system which handles the installation process of Python packages from repositories like PyPI.

If you already have Python installed, version 2.7 or later, we can install DCLI with the following command:

DCLI Installation Process

Let’s cover a couple scenarios where the above might not work.

Command Not Found – MacOS

MacOS comes with Python installed by default. However, this version of Python is generally used for system related tasks, isn’t up to date, and doesn’t always include pip. That means that there is a chance you could be greeted with the following:
DCLI Install - pip command not found

Installing a new, updated, version of Python is quite simple and can be done in multiple ways. My preferred way of installing Python on MacOS is with Homebrew, a widely used command line driven package manager. It can be easily downloaded and installed with the following command:

Example: Installing Homebrew

Then, we can install Python with the following command:

Afterwards, pip will now be available and we can now easily install DCLI with the command above!

Command Not Found – Windows

Windows does not come natively with Python installed. The first step will be to download and install a supported version of Python (2.7 or better). This can be obtained from the Python Downloads page. When performing the installation, ensure the ‘Add Python x.x to PATH’ option is checked.

DCLI Installation - Python Install

Once the installation is complete, open up your favorite terminal window (I used PowerShell), and run the following command:

DCLI Installation - pip install dcli

Summary

Datacenter CLI 2.9.1 greatly expands the available features and changed the installation process. This blog took a detailed look at the installation process and how to the more well-known issues people have ran into.

For more information about the DCLI 2.9.1 release, see the following vSphere blog post: New Release: VMware Datacenter CLI 2.9.1 For more specific information about, see the VMware Datacenter CLI (DCLI) User’s Guide.

Getting Started Using the vSphere Automation SDK for Python with VMware Cloud on AWS

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.

Prerequisites

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:

Example: Cloning the SDK Repository Locally

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:

Example: Installing Homebrew

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:

Example: Installing Python 3.6 Using Homebrew

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:

Example: Create and Enter a Virtual Environment

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:

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.

Example: Obtaining the VMware Cloud on AWS OAuth Token

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.

Example:

Example: Obtaining Org listings and detailed information about an Org

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:

Example: Using the REPL to obtain SDDC Information

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:

Example: Obtaining a list of SDDCs in an Org

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.

Example:

Example: Logical Network Workflow to create, list, and delete an object

Summary

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:

vSphere Automation SDKs Updated for VMware Cloud on AWS

The vSphere Automation SDKs have recently received an exciting update! These open-sourced SDKs can now also interact with the VMware Cloud on AWS service. The first two SDK languages updated to include this feature set is Python and Java. The remaining SDK languages will follow soon.

The VMware Cloud on AWS service offers a couple different RESTful APIs to interact with. The updated SDKs will focus on the VMware Cloud on AWS (VMC) API, as well as provide the authentication method through the Cloud Services Platform (CSP) API. To find out more about the APIs available as part of the VMware Cloud on AWS service, see this blog post: An Overview of VMware Cloud on AWS APIs

On this initial release, both the Python and Java SDKs include a handful of sample frameworks. These include the following functions:

  • Listing all Organizations associated with a user
  • Creation and removal of an SDDC
  • Managing the host count for a deployed SDDC

Let’s take a look at a couple of these in action.

vSphere Automation SDK for Python

One of the vSphere Automation SDK for Python samples has the functionality to perform two methods. The first will list all of the organizations the user’s refresh token is associated with. These orgs will be displayed by ID and display name. The second will list more detailed information for the first organization, such as ID, display name, name, create and updated dates, and SLA. This sample can be executed with the following command:

Python SDK Sample to list Org information

This SDK also has a new way to interact with the API and that is through the interactive interpreter! This can be done by creating the service object using the ‘create_vmc_client’ function while passing our refresh token. At this point, we can start accessing the available collections. The collections can by discovered in two main ways. First, by referring to the documentation that’s located in the ‘vmc.zip’ file that’s located in the ‘doc’ folder. Second, by using tab-complete. Some example code to obtain similar output to our prior example while in the interactive shell:

Interactive Shell Python Example Usage

vSphere Automation SDK for Java

The vSphere Automation SDK for Java offers a similar sample to the first Python example from above. However, this particular sample operates a little differently. The output from this sample will be the complete details of each org the user is associated with. This sample can be executed with the following command:

Java SDK Sample to list Org information

Summary

The open-sourced vSphere Automation SDKs have recently received an update. This update allows these SDKs to work with the RESTful API service of VMware Cloud on AWS. There are also a number of samples available within each SDK to list information about organizations, add and/or remove SDDCs, and manage the host count within an SDDC. These samples are easy to have operational within a few minutes of updating your local repo and are quite simple to integrate into your existing code!

Stay tuned for a more detailed look at getting started with each of these SDKs with the VMware Cloud on AWS service in the future.

For more information about the VMware Cloud on AWS service, see the following blog post: VMware Cloud on AWS Technical Overview
For more information about the VMware Cloud on AWS APIs, see the following blog post: An Overview of VMware Cloud on AWS APIs

vSphere APIs and SDKs at VMworld US 2017

VMworld Banner

It’s August and you know what that means… The countdown to VMworld has begun! This year we have a bunch of sessions focused on the vSphere APIs and SDKs. There are intro sessions, integration sessions with Chef and Ansible, hands-on guided workshops, and trainings. One of which, you’ll be able to learn how to manage your vSphere environment with Amazon’s Alexa! Hopefully you are already registered, if not what are you waiting for! This year is a can’t miss!

The following is going to break down each of the breakout sessions, group discussions, vBrownbag Community sessions, hands-on labs, and a very special event returning for its second year! Make sure to add them to your VMworld Schedule Builder.

Breakout Sessions

Monday, Aug 28, 12:00 p.m. – 1:30 p.m. – JJ Asghar & Alan Renouf
SER1906BU – VMware and Chef: Leveraging the vSphere API Together: In this session, you will get an overview of the current VMware vSphere API and how to use it in your own data center. We will explore some of the different methods and tools available to a variety of audiences when automating against the vSphere API. We will show how to use the new Chef integration to build a virtual machine using a DevOps workflow. Before that though, we will start with a basic overview of Chef, going into the knife-vcenter plug-in, comparing it to the legacy knife-vsphere, and introducing a new test-kitchen integration.

Tuesday, Aug 29, 5:30 p.m. – 6:30 p.m. – Alan Renouf & Steve Trefethen
SER1912BU – VMware Open-Source SDKs: From Getting Started to Web App in One Hour: In this session, you will get a brief introduction to the VMware vSphere software development kits (SDKs), the history of when VMware first introduced programmatic interfaces, and the recent fantastic enhancements made in this space. In this session, you will learn how to start with nothing, gain access to the vSphere automation SDKs, prototype simple API calls, and finally work with the SDKs and other free tools to produce your own simple web-based application.

Wednesday, Aug 30, 8:30 a.m. – 9:00 a.m. – Suresh Kemparaju & Tom Ralph & Gaurav Kumar
SER2068BU – How to Upgrade vSphere 6.5 with Zero Downtime: VMware IT upgraded its infrastructure of 140+ VMware vCenter servers, 3000 VMware vSphere servers, and 220,000 virtual machines from vSphere 6.0 to version 6.5, along with VMware NSX, VMware Site Recovery Manager, VMware vRealize Automation, VMware vCloud Director, and storage and backup solutions. We will discuss the product deployment sequencing required before upgrading to vSphere 6.5 along with our challenges and some lessons we learned. We will discuss the architectural and technical considerations of one of the largest deployments of vSphere. Finally, we will reveal how we automated much of the upgrade using the new upgrade APIs.

Group Discussions

Monday, Aug 28, 4:00 p.m. – 5:00 p.m. – Alan Renouf & Kyle Ruddy
SER3036GU – vSphere API Group Discussion: Join this discussion to learn about the vSphere APIs, how to use them, where to use them, and more!

VMTN vBrownbag Community Sessions

Monday, Aug 28, 12:30 p.m. – 12:45 p.m. – Jonathan Frappier
VMTN6616U – Using vSphere 6.5 APIs with Ansible: A quick look at home you can use vSphere 6.5 APIs within an Ansible playbook.

Wednesday, Aug 30, 8:30 a.m. – 9:00 a.m. – Steve Jin
VMTN6701U – Unifying VMware APIs for Fast Automation and DevOps: VMware has many different APIs of different flavors, formats, and qualities. How to effectively leverage these APIs for automation and DevOps could be quite challenging. In this tech talk, I will discuss these challenges, and how to overcome them with consistent and unified REST APIs to empower system admins and DevOps.

Thursday, Aug 31, 12:00 p.m. – 1:00 p.m. – Cody De Arkland
VMTN6653U – Furthering Amazon Echo and VMware Integration: Continuing our adventure from Monday’s Hack-a-Thon, we will dive deeper into integrating the Amazon Echo with the VMware suite of products. We’ll visit considerations of user interface with Clarity UI. We’ll review containerizing our application using vSphere Integrated Containers. Finally, we’ll dive into more advanced API integrations with NSX and vRealize Automation.

Expert Led Workshop

Monday, Aug 28, 12:00 p.m. – 1:30 p.m.
ELW181106U – vSphere Automation and Development – API and SDKs Workshop: The vSphere Automation API and SDKs are developer friendly and have simplified interfaces. In this expert led workshop, attendees will use the API and SDKs for Virtual Machine creation, modification and deletion and experience the consistent set of developer and automation tooling across different development platforms.

Hands-On Lab

SPL181106U – vSphere Automation and Development – API and SDKs: The vSphere Automation API and SDK are developer friendly and have simplified interfaces. You will use the API and SDK for Virtual Machine creation, modification and deletion and experience the consistent set of developer and automation tooling across different development platforms.

VMware Code Pre-Hackathon Trainings

Monday, Aug 28, 6:30 p.m. – 8:00 p.m. – Steve Trefethen
VMTN6720U – Getting Started with the vSphere Automation SDK for Python: Bring your laptop, GitHub account and your favorite IDE and learn how you can easily get started with Python and the vSphere Automation SDK for Python, at the end of this session you will know the basics of python and using it to call the VMware vCenter API to perform some key tasks.

Monday, Aug 28, 6:30 p.m. – 8:00 p.m. – Cody De Arkland
VMTN6721U – Integrating VMware APIs and Amazon Echo (Alexa): Bring your laptop, GitHub account and your favorite IDE and learn how you can easily get started integrating your VMware environments with your Amazon Echo devices. We will take you on a magic carpet ride exploring Python, the Flask web framework, VMware API endpoints, Clarity, and Docker. By the end of this session, you’ll be a few steps closer to having your Homelab talk back to you!

VMware Code Hackathon

Monday, Aug 28, 8:00 p.m. – 12:00 p.m. – VMware Code
VMTN6722U – Hackathon Event

The VMware Code Hackathon was probably my favorite event from VMworld last year. This event encouraged any VMworld attendee to come, join a team, and complete some form of project or challenge. Attendees varied from vSphere beginners to highly skilled programmers. Everyone had a blast, there were some terrific giveaways, special guests, and so forth.

This year, the Hackathon returns and it is only getting better! I can’t reveal any of the secrets planned, but it is certainly going to be one of the best evening events that week!

Additional Information: VMware Code Blog – VMworld Hackathon is Back

Featured Member: Cody De Arkland

As we like to bring up, there are a lot of people doing great work in and around the VMware {code} program. Back in March we introduced the “featured member” segment here on the VMware {code} blog: brief interviews with some of our key contributors to help everyone get a better sense of the kind of things the VMware {code} program aims to make possible.

Our second interview was done via email and is presented below (with minimal edits for consistency).

As always, we’d love to hear your feedback in the comments below, on Twitter or on Slack. If you’d like to be interviewed yourself or know anyone who might be interesting – please get in touch. Thanks!

Who are you and what brings you to VMware {code}?

Cody De Arkland profile image

Cody De Arkland

My name is Cody De Arkland, and I’m a Senior Systems Engineer with VMware out in Sacramento, California. My focus is on the Core VMware technologies; but my passion is around automation. I’m relatively new to VMware and in my previous role worked pretty heavily with VMware’s various APIs for a lot of automation and reporting work at a major utility company. I also run my own blog about my lab adventures, called The Humble Lab (https://www.thehumblelab.com). Finding the {code} community and seeing how active it is was really exciting to me. It felt like a natural place to visit any time I was going to code something in vRealize Orchestrator, or interact with the APIs. Knowing that most, if not all, of the API documentation was landing here made it really easy to “dig up” as needed. Lately I spend most of my time here browsing the code samples for things to include in my Alexa with VMware project (Codenamed Gideon) or reviewing API documentation.

How long have you been actively coding? What got you started?

I started writing code mostly as scripting in PowerShell probably around 5 or 6 years ago. I stayed pretty active working with PowerShell up until about 3 years ago when I took a role working on private cloud at the utility I mentioned previously. In this role, I had to work in vRealize Orchestrator a ton which heavily leverages JavaScript. I spent a few years working with and learning JavaScript in the vRO context. Eventually I found myself wanting to do more live custom reporting, and wanting to dig a bit deeper into VMware’s software development kits. I knew Python was a pretty active language with a strong community, and that it had a reputation for being easy to get started with. I made myself sit down and learn Python over the course of several months while I was off work with my newborn, during her nap times. This led me down learning more about taking Python code, and feeding into webpages to build various tools. Rest was history!

What languages do you use the most? Why?

I use PowerCLI/PowerNSX pretty much every day for something. With my current project (the Alexa and VMware one) I’m in Python a lot, also working with regular HTML quite a bit. I like PowerCLI and PowerNSX because how fast and programmatic you can be with configuring items. It’s also a great way to track your “changes” to an environment, or even track how to deploy something. Python I love because of the community that surrounds it. There’s a tool for every project in Python, and normally a huge community of people who are willing to help!

So we’re hearing you did a project recently using Amazon Echo. In a few sentences, what was it you were trying to accomplish?

When the Echo first came out, I found out that it was extensible using a number of developer-friendly languages. I had spent a few months while I was off work with my newborn learning Python during naps – and I felt like practical experience automating with the Echo would help me learn Python faster. I always learn better by doing; and diving into a code project like this seemed like it would teach me volumes – which it TOTALLY has. I’m also an avid homelabber, and I had this thought of “How cool would it be to be able to wake up, and say “Alexa, hows my lab running?” and have it respond with current status.

It started off as a simple project; but it’s quickly grown into something a bit bigger with a full UI, configuration options, and proper hosting configurations. It’s pretty exciting. I opted to use VMware’s ClarityUI because of how well documented it was, and how easy it was to get started with. It made my page look a lot cleaner than I typically could’ve done on my own. 

Was this a work project or just for fun (or both)?

I would say both. I wanted to learn to interact more with our SOAP API via Python since it’s a bit more full featured than the REST API is currently, but I also wanted to learn more about consuming the REST APIs. When I worked at the utility, I felt like learning more web development skills would help me answer the needs of the business more, and now that I’m working at VMware I also identify a bit more with what our customers are looking for in our platforms.

On the fun side, who didn’t grow up wanting their computer to interact with them? I thought it would be a fun thing that would grow from being only VMware focused into larger home automation stuff. Plus, I’d learn a ton. I feel like any day you don’t learn something new is a day wasted.

At VMware {code}, our goal is to provide developer with tools and resources to “learn, code and connect”. Were you able to make use of any of these resources? If so, how?

You guys are definitely meeting your goal! The amount of documentation available on “code”-related items on the VMware {code} site is unreal. It’s nice having a central repository for all the developer-centric documentation. The sample exchange encourages sharing back out to the community, and it’s really easy to jump in there and find good samples around the concepts I was working on. I spend a ton of time on the site. Furthermore, the Slack channels are great. There’s a channel for pretty much everything it seems and the community is pretty friendly when you have a question you’re stuck on.

Thanks very much, glad to hear! Now that your project is up and running, what’s next?

Ha! Nothing’s ever done…come on now 😉

I’ve certainly made some progress, but there’s a long way to go on this. One of the things I love most about the VMware {code} community is the community involvement. Since I tweeted out the stuff I was working on, I’ve had a lot of people internally and externally jump in offering to contribute and help out. It started out just being something to interact with vSphere and vRA, with a future view of NSX. Now I’m talking to a ton of different people about additional products that we could tie into APIs against.

Excellent! Thanks so much for taking the time. Finally, how might people best connect with you online?

Twitter is probably best; @CodyDeArkland, along with TheHumbleLab blog

* * *

Links:

Getting Started with the vSphere Automation SDK for Python

We’ve been looking at how to get started using the new vSphere Automation SDKs. In the last couple parts of the series we looked at the SDK for REST, which included Postman REST client samples, as well as some JavaScript samples. This time, we’ll be looking at the vSphere Automation SDK for Python!

Prerequisites

There are a handful of ways to install and setup Python on your workstation, I will be walking through a high level configuration for my Mac.

First things first, clone the vSphere Automation SDK for Python from the VMware GitHub repository located here.

Cloning the Python SDK repo

After that’s complete, we’ll need to install the Python prerequisites. These include the GNU Complier Collection (GCC), a C compiler, and HomeBrew, a package management system.

GCC can be installed in multiple ways, but I’ve found it’s easiest to obtain it by installing the Xcode Command Line Tools. This can be done with the following command, then following the prompts:
xcode-select --install

XCode Installation

Next, we’ll need to install the package manager, Homebrew. 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)"

Homebrew Installation

Finally, we’ll install Python in the recommended version of 3.6. It’s worth noting here that Mac OS X 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

Python 3.6 Installation

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 SDKDemo
source SDKDemo/bin/activate

Creating a virtual development environment

Lastly, 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:
pip3 install -r requirements.txt

Requirement Installation

One other item worth mentioning is an IDE. This will make editing, running and debugging the code a lot easier to deal with. I’ll be using PyCharm, which is available here.

Running Sample Setup Script

To really show off the capabilities of the SDK, there’s a sample script which takes an un-configured vSphere environment and performs all of those normal configuration steps. The environment requirements to run the script are as follows:

  • 1 vCenter Server
  • 2 ESXi Hosts
  • 1 NFS Datastore (minimum of 3GB free)

We’ll first need to open up the testbed.py file and populate lines 17 through 29 as follows:

Modification of the Testbed File

We’re now ready to set the environment up with the sample script! However, before running the script we should take a look at what it’s doing. The script is available in the SDK directory at: /bin/run_sample.sh The goal of the script is to do the following actions:

  • Create 2 Datacenters
  • Create a 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 ISO image downloaded from VMware’s bintray server to the Datastore
  • Create directories to add sample ports

One note before running the script, the ISO image being downloaded is roughly 200MB and could take a while depending on your bandwidth. My tests ranged anywhere between 30 seconds and 5 minutes.

The sample script can be run with the following command:
./run_sample.sh ../samples/vsphere/vcenter/setup/main.py –sv

Configuring an environment with the main.py file

Create a VM with Defaults

Now that our environment is setup, let’s take a look at adding a VM to this environment. The easiest way is to create a default VM. This action means the API takes care of all the configuration settings based on the Guest OS version. There are some prerequisites that are required for placement such as desired datacenter, VM folder, and datastore. These are all referenced from the testbed.py configuration file we referenced in the first example.

Updating the Testbed File for VM Tasks

The VM can then be created with the following command:
./run_sample.sh ../samples/vsphere/vcenter/vm/create/create_default_vm.py –v

Default VM Creation Sample

Notice in the above example how even though we didn’t populate all of the settings, the VM received required items like a CPU, a disk, and so forth.

To verify the VM does indeed exist, we can obtain a list of VMs with the following command:
./run_sample.sh ../samples/vsphere/vcenter/vm/list_vms.py –v

List VMs Sample

VM Power Actions

The next logical step after creating a VM is to power it on. There’s a sample within the vm folder named power.py, which shows off all of the power actions. This file references the ‘VM_NAME_DEFAULT’ input from the testbed.py file.

When running the command as is, the VM will be put through the following power states:

  • Obtain current power state
  • Power Off (NOTE: Only if the VM is currently powered on)
  • Power On
  • Suspend the VM
  • Resume the VM
  • Reset the VM

The command can be run with the following:
./run_sample.sh ../samples/vsphere/vcenter/vm/power.py –v

VM Power State Sample

Summary

This post shows you how to easily get started with the vSphere Automation SDK for Python. It takes you through the setup of an entire environment, as well as using 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 SDK 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 the series: