NSX vSphere RAML Client


Up until now, my programmatic interactions with the NSX API have all been hacked together based on the NSX API Guide, due to the lack of a specific NSX API scripting interface.

 

For typical vSphere scripting, there has been PowerCli and also pyvmomi and I am sure a few more I don’t know about, but nothing created specifically for NSX. This meant that for individuals like myself, who wanted to write scripts to interact with the NSX-v API through python, we would have to create all the API calls from scratch which is time consuming and then learn how to parse the responses and content.

 

What I also found, is that as I have written more scripts and understood how to parse XML better, my scripts have changed over time so how I used to do something in an early script, is not how I do it in my more recent scripts.

 

This week I stumbled across a couple of interesting VMware repositories on GitHub which hopefully will make interacting with the NSX-v API a hell of a lot easier through Python and Ansible.

 

The repositories are:
NSXRAML
A RAML specification of the NSX for vSphere 6.x API.

 

NSXRAMLCLIENT
A Python client which can utilise the NSX RAML file above.

 

NSXANSIBLE
This repository contains a number of Ansible modules, written in Python, that can be used to create, read, update and delete objects in NSX for vSphere.

 

So after a quick google to figure out what an actual RAML file is (click here to open the RAML website in a new window which explains it all), I thought I would jump straight into the deep end and give both the NSXRAML and NSXRAMLCLIENT a shot.

 

Obviously the first port of call is to download both the source for NSXRAML and the NSXRAMLCLIENT

 

Next we will user pip to install the nsxramlclient
After a little wait, you should have the NSXRAMLCLIENT installed.

Now apparently we just jump into our python environment

and import the nsxramlclient module, set some variables, remembering that the path to the RAML file is the absolute path, not a relative path.

Create a session to our nsx manager.
Now lets do something simple, like check the sso configuration
Ok this makes things a hell of a lot easier, as I don’t have to know how to parse XML to read the content, I can just use python ordered dictionaries.
Lets try something a bit more in-depth like querying all edges. But first we need to find out the syntax and I will use the HTML version of the RAML file, which makes browsing the API really simplistic.
RAML-01
As I just want to query all edges, I don’t need to use a parameter.
And then we get a whole heap of data. Do it again and save it as a variable this time.
Import the YAML module, which can be used to display the output in a more readable format.
If you change the default_flow_style to False, it makes it a bit more readable again.
Now let run through some more basic tasks

Save some of the first level items as variables
Display all edges

Display the first edge details.

Display the second edge details.

Saves all edge details to a variable.

Loop through the edges and print the objectId, name and tenantId
Get the actual config of the ComputeFW and display the output.

RAML-02

To display a list of NICs on the edge.
Show interfaces with an IP address configured.
Now that I have got my head around how to work with the response data, I am going to see if I can re-write some of my other scripts using the NSXRAMLCLIENT. After I have finished my first one, I will put up a post comparing both scripts so it’s easy to see the differences.

Leave a Reply