Creating an SDDC in Python, By DALL-E
How to SDK

Creating an SDDC in Python on VMC: The Hard Way

This blog shows how to create an SDDC in Python on VMC. As part of my move to the VMC product marketing team (working on developer stuff), I wanted to understand what the life of a developer is like, after 3+ years on the Cloud Economics team. So, I decided to start by coding up a simple example using the core VMC APIs. The example I picked was creating a simple 1 node SDDC. I mean, how hard could it be, right?

There are a decent number of blogs and code to use the “Automation” APIs, which is where I started my journey. These APIs allow you to anything you can do in the console, more or less, in code. Kyle Ruddy has a great example on how do to this in a PowerCLI context. He also has a foundational blog on how the VMC API works. I decided to do mine in code, specifically in Python.

Creating an SDDC in Python
Creating an SDDC in Python(ish), By DALL-E

VMC Automation APIs

VMware has pages and pages of APIs, as can be seen in the Developer Center. There are the APIs for vSphere, vRealize (now Aria), HCX and much more. In order to work with VMware Cloud, you need to be familiar with several APIs.

The first is the foundational Cloud Services Platform API, which is our cross-cutting set of APIs for things like authentication, authorization, identity, service discovery, and so on. We will need this to be able to get credentials in order to do any work on VMware Cloud.

The second set of APIs are the VMware Cloud on AWS API. This allows for all manner of manipulation of VMware Cloud on AWS, provided you have the right permissions. This includes the ability to create and delete SDDCs.

The final set of APIs, not covered here, are APIs you use us to talk to vSphere and vCenter. These remain the same, since VMware Cloud on AWS is vSphere running on AWS hardware, more or less. It should be noted that there is also a vSphere SDK available for Python, which you can find at GitHub. For the purposes of this article, I am going to focus on REST calls only, essentially doing this the hard way, as I feel it’s more instructive. (Shameless plug: I am also helping out with the Python project PyVMC, which is the Python Client for VMware Cloud on AWS. Cool code, try it.)

Creating an SDDC in Python: One Node

In this example, we are going to create and deploy a 1 Node SDDC in US-WEST-2 (which is the closest region to where I am writing this). The documentation on how to create an SDDC is sufficient to give you an idea of what to do, but it leaves a few things out. If you like to dive in like me, you’ll see there are more details that appear at first blush.

To get started, we’ll need to get an API token. To do this, login to the VMware Cloud Console. Once you are logged in, launch the VMware Cloud Service. From there, click on your name in the upper right corner, making sure you have the correct Organization selected, if you have multiple of them. From there, click the “API Tokens” tab, then click the “Generate Token” link. This will generate a token for you in a pop-up. Make sure to copy it and store it someplace safe., as we’ll need it in the next step. Tokens do expire, so you’ll have to be mindful of this.

Now we get to the code, which can be found in its entirety on GitHub.

Editor’s note: yes, some of the characters are mangled. We’re looking for solutions…

In this example, we attempt to login to VMC. We do this by pulling the API token from a file (“key.txt”) via get_api_key() and then we call the REST authentication method as described in the documentation. Don’t forget to store your key in key.txt, or you’ll get an error.


Next, we do the setup process before we call the API to create an SDDC. Here’s what it looks like:

In essence, we have to do the following:

  1. Get the object-id of the organization we’re part of. (Note: I’m scanning by the name of the org I am looking for. Not terribly sophisticated, but neither is it obscurantist).
  2. Get the connected-account, essentially the object-id of the connected AWS account. You need this for billing purposes.
  3. Determine a reasonable subnet for our new SDDC to connect on.

    Here’s the code for each of these three steps, in order. First, let’s show how we got the organization.

Next, we take the Organization ID and pull the related object with represents the connected account. For reference, we also print out the accounts with their associated ID and Owner, using PrettyTable, a table display library in Python.

Here’s where things get a little convoluted when creating an SDDC in Python, since you need to pass some account and network info.

Here’s what we’ll need to do: we’ll scan the VPCs connected to our linkedAccount and pull a subnet for our SDDC to use by using the appropriate REST call from the API, whose documentation is here. Now I found this part of the architecture a little opaque, so I included pulling the VMC Map and iterating across it so users could see the full list, printed with PrettyTable. We need to do this in order to tell VMware Cloud on AWS how our newly created SDDC should get to the internet. New SDDCs do not, by default, come with connectivity to the internet. You have to configure it.

Now that we have the organization’s ID, the connected account, and the subnet we want to use, as well as the region we want to create it in(US_WEST_2, see above), we can call create_sddc to call the REST API to create our SDDC.

It can take a little while for an SDDC to get created. For my account, it takes around 2 hours. As a result, if the “create” call is successful, it will return a Task object, and an HTTP status code 202. You can then poll the Task object and see the steps of the creation of the SDDC, using the code below.

This code will poll the task every 15 second, and print out the status and the substatus, which will show you the steps the automation goes through to create a new SDDC.  I found the messages fairly revealing. Meanwhile, you can also see the status of your SDDC creation in the console at http://console.cloud.vmware.com/. Once the SDDC is created, you can begin setting it up, adding VMs, adding firewall rules, etc.

That’s how to create an SDDC in python on VMC. Would you like more sample code? Send your requests to @BillRothVMware on Twitter or message me on the {code} slack server.

Released Resources