API Community How to VMware {code}

Integrating VI/JSON in pyVmomi scripts

As pyVmomi and VI/JSON APIs provide means of communication to the same set of services, integrating them in a single script is easily achievable. Such integrations may come in handy on different occasions, for example, connecting workflows or working around issues/constraints present in the pyVmomi product.

This blog post showcases how to:

  • Reuse pyVmomi’s session in VI/JSON API calls.
  • Pass Managed Object References from pyVmomi objects to VI/JSON API calls.
  • Invoking VI/JSON operations from the pyVmomi script.

All points above are tied together in a sample interacting with the Event History Collector API.

The example API calls utilize Managed Object Classes and Managed Object References, integral to VI/JSON API. Please refer to the vSphere Web Services SDK Programming Guide (8.0U1) for more details about interacting with the vSphere APIs.

Acquiring pyVmomi’s session

Most pyVmomi scripts incorporate a SmartConnect invocation responsible for carrying out login:

After successful connection, the user can obtain the persisted cookie, containing the user’s authentication information, via the ServiceInstance object’s stub as follows:

The cookie_str  variable contains the cookie string. From it we need to extract the session ID, which is a valid value for the vmware-api-session-id header. Extraction can be done in various ways. A standard approach is to utilize the baked-in http.cookie module as follows:

Now, the session_id value can be passed as the value of the vmware-api-session-id header, enabling VI/JSON API calls to operations requiring authentication (additional examples involving the vmware-api-session-id are present in Automating vSphere Workflows with VI/JSON).

Acquiring Event Manager managed object ID from pyVmomi

Using the service_instance object (from the section above) it is easy to acquire the Event Manager managed object and its ID:

Invoking VI/JSON operations

With all of the obtained information from above we are ready to create an Event History Collector object. This is done through the CreateCollectorForEvents operation from the EventManager API in VI/JSON. The operation takes as an argument an EventFilterSpec object, which describes the sort of events our collector is interested in tracking (filtering). In our sample, the collector will acquire all types of events, in other words, it will filter through all events. A blank EventFilterSpec will create such a collector:

The sample utilizes the requests package for HTTP request invocations. Invoking the CreateCollectorForEvents operation:

In order to refer to the newly created collector in our next requests, we need to extract the managed object ID from the response above:

With the event_history_collector_moid in place, we are ready to acquire the latest events on our VC with EventHistoryCollector ‘s API method latestPage:

As with the collector’s managed object ID, we deserialize the response into a Python list of events:

For more information regarding the EventManagerEventHistoryCollector  and all other VI/JSON APIs refer to the Virtual Infrastructure JSON API documentation.

The sample code is available at vSphere Virtual Infrastructure JSON API – pyVmomi integration as a Jupyter notebook.

This blog post is part of a series with examples that examine the protocol in depth.

Follow us on Twitter @VMware_API_team and send us any feedback.