Working with NSXv API

NSX RESTful API allow to interact with NSX manager, read the state of the NSX and change configuration. Some of NSX task can only be done via API and not GUI.

The protocol using to communication with the NSX Manager is HTTPS, over secure channel we send request and response data in XML format.

In this blog post we will use Mozilla web browser as REST client.

 

Install Mozilla REST Client:

REST Client can download from:

https://addons.mozilla.org/en-US/firefox/addon/restclient/

To install the REST Client Click on the “Open Menu” Button and Then click on the “Add-ons” button:

API3

Click on “Install add-on from file”:

API4

Import the REST Client file from the folder you download and click “Install”.

API5

Note: Mozilla browser will restart

To open the REST Client click on the Red button:

API6

One time configuration of REST Client:

Choose “Basic authentication”

API75

Type in NSX Credential for NSX Manager and Click Okay.

API65

Select from menu “Headers” and  “Custom Header”

Type the Username and Password for NSX Manager and Click Okay.

API7

In name type “Content-Type”.

In Value type: “application/xml”

Click Save to favorite and Okay

API8

Now we can start to work with REST Client.

There are few different Method to read and configure the NSX Manager.

API111

The frequent tasks we will work with REST Client are:

GET: to read information from NSX Manager

POST: to change configuration in NSX Manager

DELETE: to DELETE configuration in the NSX Manager

 

In the URL we need to type the NSX Manager URL:

https://nsxmgr-l-01a.corp.local/

Our first exercise will be to configure Logical Switch.

Create Logical Switch with API

To do this we will need to know which transport zone will use this logical switch.

In my lab we have two transport zone Global-Transport-Zone and DMZ-Transport-Zone.

API16

NSX as different scope ID to represent different transport zone.

Assuming we would like to configure the logical switch in DMZ-Transport-Zone we need to find out what is the Transport zone scope.

Finding this scope can be done with API call:

https://nsxmgr-l-01a.corp.local/api/2.0/vdn/scopes

Click on the red “SEND” button.

The “status code” 200 mean we successfully get the API information

API17

After click on the Response Body we got results for DMZ-Transport-Zone we get vdnsope-2

API18

For Global-Transport-Zone we get vdnsope-1

API19

Since we want to configure this logical switch on DMZ-Transport-Zone we will use vdnsope-2 in the POST API Call.

Create Logical switch in DMZ-transport-Zone:

The Method: POST

URL: https://nsxmgr-l-01a.corp.local/api/2.0/vdn/scopes/vdnscope-2/virtualwires

Body is the Payload of the actual configuration.

Body type:

<name>DMZ-Logical-Switch-01</name>
<description>Created via REST API</description>
<tenantId>virtual wire tenant</tenantId>
<controlPlaneMode>UNICAST_MODE</controlPlaneMode>
</virtualWireCreateSpec>

 

“415 Unsupported Media Type”

The reason for this is typo in “content type”

API20

My typo configuration is: Con: application/xml instead of writing Content-Type: application/xml

API13

Another issue can lead to same error:

Ensure that the SSL certificate used by NSX Manager is accepted by the client [browser or fat client] before making the call. By default, NSX Manager uses self-signed certificate that is accepted by the user. In such cases, ensure that the NSX Manager certificate is accepted before making the REST API call

http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2094596

 

The REST APIs use a HTTP response message to inform clients of their request’s result (as defined in RFC 2616). 5 Categories Defined:

1xx: Informational Communicates transfer protocol-level information.

2xx: Success indicates that the client’s request was accepted successful.

3xx: Redirection indicates that the client must take some additional action in order to complete their request.

4xx: Client Error This category of error status codes points the finger at clients.

5xx: Server Error The server takes responsibility for these error status codes.

 

After fixing this typo and click on “SEND” we get status 201

API14

Each logical switch as unique virtual wire id:

API21

Form NSX GUI, Logical Switch tab we can see we successfully crested the Logical switch.

API15

Link to official API guide

http://pubs.vmware.com/NSX-61/topic/com.vmware.ICbase/PDF/nsx_61_api.pdf

 

 

Posted in API Tagged with: ,
4 comments on “Working with NSXv API
  1. Patel Ronak says:

    GET https://nsxmanager.spjsolutions.local/api/2.0/vdn/scopes

    I get vdnscope-5

    POST https://nsxmanager.spjsolutions.local/api/2.0/vdn/scopes/vdnscope-5/virtualwires

    My GET Function is working. but when i do post it is giving me 500 internal server error

    and in response body getting Error Code 100

    Please Help.

  2. Patel Ronak says:

    POST https://nsxmgr-l-01a.corp.local/api/2.0/vdn/scopes/vdnscope-5/virtualwires

    Logical-Switch-01
    Created via REST API
    virtual wire tenant
    UNICAST_MODE

  3. Scott O. says:

    Unfortunately you cannot create these in bulk by stacking the elements… the API pays attention only to the first.

    Also, if you get a 404 response, double check your vdnscope ID; make sure it exists.

    Happy NSXing 🙂

Leave a Reply