Nowadays everything is about automation. Organizations are moving away from the traditional static infrastructure to full automation and here the need of NSX is significant. There are many use-cases for NSX, but the common in all of them is that they all need to be automated.

VMware is investing heavily for different tools to ease the automation aspect of NSX but in order to take full advantage of it one needs to understand what happens under the hood. It is also important if someone wants to build their own custom automation tool or CMP (Cloud Management Platform). Many existing solutions like Openstack, Kubernetes, vRO and so on automate NSX-T using different plugins. In fact, those plugins are sending REST API calls to NSX Manager in order to automate logical topology CRUD(Create, Read, Update, Delete) operations.

Based on our experience we decided that NSX-T APIs will be based on JSON format following OpenAPI standard. The use of Open APIs is to enable third-party developers to build applications and services around NSX-T by standardizing on how REST APIs are described. This means one can use standard tools like Swagger to read and use those APIs. Below is a quick example from my Mac on how to generate from CLI a language bindings on any of the program languages swagger supports.

The above allows you to generate python bindings and documentation with 100% API coverage. You can create those bindings easily by replacing the keyword python with other (like go, ruby, php, etc) on the last swagger-codegen command. In Addtional Info section below you can find a link how to install swagger on Linux or Windows. This is just for information and I won’t spend more time on it as VMware makes your life as a developer even easier by taking care for this process and we produce official VMware supported language bindings. The goal of the sections below is to help you getting started with Python SDK for NSX-T.

Download and Install Python SDK

As of today, we support Python and Java SDKs that can be downloaded from downloads.vmware.com or https://code.vmware.com/sdks

Let me quickly give some example on how to install and use the Python SDK. Download vAPI runtime and dependencies as well as NSX-T SDK. They should be like following:

You will need python pip installed. Run the following from the folder you have the files above downloaded. On Ubuntu the installation looks like this:

On MacOS it is much easier:

In order to validate that it was installed successfully enter python interactive mode and try to import one of the packages.

The output should be like this:

 

If it doesn’t complain that there is no module named TransportZones you are fine, enter quit() and lets start with our first sample script.

Write simple test script

The workflow should look like following:

  1. Create a Stub Context with NSX Manager IP Address and credentials
  2. Instantiate a service for the API endpoint
  3. Create a payload object
  4. Call the service’s CRUD method

The first example reads and lists all Transport Zones.

Copy the code above and paste it in a file called list_tz.py. Don’t forget to change the  NSX Manager IP Address and password. Run python list_tz.py from the CLI and if you get a list of all Transport Zones together with their properties we have our first script ran successful.

Explore more CRUD operations

Lets extend the script using the same workflow. Now we will create a Transport Zone and a Logical Switch based on the newly created Transport Zone. After that we will update the name of the Transport Zone. At the end we will delete both recourses.

Copy the code above in a file and run it. After every operation there is a pause to give you the opportunity to review what happened. Don’t Forget to change NSX ip address and password!

We covered all CRUD operations with the above two scripts. Similarly one can create, read, update, or delete all the resources in NSX. I will try to place some more examples on github soon and will update with a link here.

 

Happy coding!!!

 

Additional info:

More Samples: https://github.com/vmwaresamples/nsx-t

VMware NSX-T Documentation: https://docs.vmware.com/en/VMware-NSX-T/index.html

All VMware Documentation: https://docs.vmware.com

VMware NSX YouTube Channel: https://www.youtube.com/VMwareNSX

NSX-T with vRO HTTP REST Plugin: https://youtu.be/yLS_UeEaH0k

VMware Official Site: https://www.vmware.com/

OpenAPI Initiative: https://www.openapis.org/

Install Swagger: https://github.com/swagger-api/swagger-codegen#prerequisites

Go lang bindings: https://github.com/vmware/go-vmware-nsxt