Monthly Archives: February 2017

Nutanix REST API with Golang/Go Tutorial – Part 3 – Retrieve information about a VM (IP address, vCPU, MEM)

In this part I will focus on retrieving data from the Nutanix REST API. So basically this will show two important parts.

1. How to get data from the API !

2. How to parse the data which is in JSON format within GO !

We will cover these two examples:

  • Retrieve cluster config and settings
  • Retrieve VM configs (vCPU, MEM,IP Address ….)

Retrieve cluster config and settings

The URI “https://192.168.178.130:9440/PrismGateway/services/rest/v1/clusters/”  will retrieve a list of the clusters. Which in my case is only one.

cluster_rest_try

As you can see the data is in JSON format and needs to be parsed if we would like to use it. So there are two ways to do this.

  1. parsing JSON with the help of structs when the structure of the data is known
  2. parsing JSON with generic interface{} based for arbitrary data

For more details on this topic have a read here JSON-AND-GO

Parsing JSON with the help of structs when the structure of the data is known

In this case we got a valid response. This means we do know the structure of the data now. What I prefer is to create a struct which should represent the data within go. To do this it would be great to have a documentation or SDK which defines a representation of all responses. With AOS 5.0 there is a way to use swagger to generate this.

But today we can just create our own struct based on the response. So copy/paste the response to JSON-to-GO will return this struct.

We can just use this struct and parse the response to it. You may think: “This will end up with a lot of structs and they are just huge”.  This could be the point where you have a look at the generic interface approach.

I created a simple example which extends the basic authentication part and adds the following:

  1. I am adding a a struct

2. Define a GET to receive the cluster info

3. Send the GET and retrieve the data into bodyText which is an array of bytes ( byte[])

Create a variable based on the struct clustersGet

Making use of json.Unmarshal to parse the data into the variable clusterGetResp and print a few values to std out.

Retrieve VM config (vCPU, Mem, IP Address….)

In the last example we made use of parsing json into structs. I prefer this way but there is a “simpler way” to achieve the same. Making use of the interface. The aim of this example is to retrieve some information about a specific VM.

So we would like to get the information about a VM called “docker-mac”. BTW: “I am using this VM to demonstrate the integration of Nutanix and docker.”

The first step would be to retrieve the UUID of the VM because the Nutanix REST API makes use of a unique identifier.

Lets get a list of all VMs and extract the UUID for the VM with the name “docker-mac”.

 

So we request a list of all VM’s first and then search for the name to get the UUID (Unique Universal ID) of the VM called “docker-mac”

Now we are able to request more info about this specific VM when requesting where bd74362d-2cb0-4d06-a95b-dfb7403c5a01 is the UUID.

 

A special case ! Getting the IP Address!

You may have noticed that there is no key/value which shows the IP Address of the VM. So first of all there are two different kind of IPs for a VM. If you are using AHV than you are able to retrieve an IP for a VM from IP pools which are managed by AHV itself. The other maybe more common way is that the VM get an IP via DHCP or it is statically set all without the “knowledge” of Nutanix.

Example!

AHV managed IP

To retrieve the IP when it is managed via the AHV IP pool the way to retrieve the IP Address is as follow. Just add the include_vm_nic_config paramter to the GET request and the NIC details will be included.

Keep an eye on the key “requested_ip_address”

The implementation is a little bit ugly because of the interfaces/mapping etc. but can be done.

 

IP Address of the VM if not managed by AHV

At the moment the v2 of the API is not able to provide the IP. So there is a fallback to the API v1 which just includes the IP. So a simple GET to https://192.168.178.130:9440/PrismGateway/services/rest/v1/vms/ will return all VMs and their IP Addresses.

The implementation is pretty simple because the info is direct at the main level of the VM.