vSphere REST API and C++ (Part 2)

By | June 13, 2017

This is the second part in an ongoing series looking at using the vSphere REST API and C++ with the cpp rest sdk (formerly known as “Casablanca”). In the first part we authenticated against SSO and got some information from the vCenter.

In this part we are going to refine our methods so that we can reuse our code. We will introduce some structs and functions that allow us to work with results.

Connecting

The first thing we are going to introduce is a struct that will manage our connection data. It is as follows:

All the members are annotated with how they will be used.

We will now assemble a ConnectAsync function which will return a task (and a pointer to a vc_info object). The code looks as follows:

As you can see,this function will wrap the call to /rest/com/vmware/cis/session and provision our object to hold information about the “connection”.

It takes a server name, username and password and attempts to connect, returninf a vc_info object with completed information whether it is successful or not. The task chain is similar to what we wrote in part 1, with the exception that we now store the SSO token in the vc_info object. We will use a function “CheckJson” to format the result into an object hierarchy and we will be creating this in the next section.

This is how our ConnectAsync function can be used in a synchronous fashion.

Handling JSON

As most calls will return information in a JSON format, we will use the following two structs to store and organise information.The CPP Rest SDK contains some JSON classes, so when we call

on a response, we get a

object that we can use as we desire. We will transform this into our own objects – vc_property and vc_object, which are defined below.

As noted, as unrestricted unions were/are not supported on the version of Visual Studio I wrote this on, we exclude the variable “s” from the union. Possibly fixed or working in another complier thoughI haven’t checked.

We now need a function that we will call to extrapolate our JSON and structure it so we can use it. Because objects can contain other objects we may need to call this function recursively. The function takes the name of the key/value pair, the value and the parent object.

Note: I believe the array code won’t work at this time – I haven’t worked with any arrays to test yet – we’ll take a look at this later. It will however, handle objects as children and fill in the properties as required.

As an example, when authenticating with ConnectAsync the following information is returned:

We deal with this data with the following code:

The code above should be fairly straight forward – the code will transform the result to a vc_object object called data, and we get the value of the key/value pair with the key of “value” and assign it to the pInfo->token member property.

We are still missing a “get_object_property” function to make the code work. As the code to walk object properties for a specific entry will be commonly used it has been pulled out into it’s own function. This is straight forward and below:

Tasks

To do things with the API I’ve written a generic function that can send requests – this is very basic at the moment. One of the ways we can improve this later is to allow for the passing of values in a POST request. The body of the function is below:

The function takes:

  • The type of request – i.e. GET, POST, PUT, DELETE
  • The vc_info object for the server.
  • The URI to send the request to.

It will return a vc_task object that will contain the status code and the parsed JSON from the response body. The vc_task struct is defined as:

Example

Now we’ve done all of this, we can put this together and try it out – we’ll get information about the hosts. This is the pertinent section of our code:

When running this against my home lab (please ignore the different DNS domains :p) I get the following output:

The code is available at the github repo – github.com/nelons/vSphereRestApi – please try it out and give me any feedback you have 🙂

Next Time

Now we’ve tidied up our structure to allow reuse we’ll look at tags and getting relevant objects.