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:
1 2 3 4 5 6 |
from pyVim.connect import SmartConnect service_instance = SmartConnect( host='127.0.0.1', pwd='pass', user='admin' ) |
After successful connection, the user can obtain the persisted cookie, containing the user’s authentication information, via the ServiceInstance
object’s stub as follows:
1 2 |
from pyVim.connect import GetStub cookie_str = GetStub().cookie |
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:
1 2 3 |
import http.cookies cookie = http.cookies.SimpleCookie(cookie_str) session_id = cookie['vmware_soap_session'].value |
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:
1 2 |
event_manager = service_instance.content.eventManager event_manager_moid = event_manager._moId |
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:
1 2 3 4 5 6 |
event_filter_spec = ''' { "filter":{ "_typeName": "EventFilterSpec" } }''' |
The sample utilizes the requests
package for HTTP request invocations. Invoking the CreateCollectorForEvents operation:
1 2 3 4 5 6 |
import requests resp = requests.post( url=f'https://{vcenter_host}/sdk/vim25/8.0.1.0/EventManager/{event_manager_moid}/CreateCollectorForEvents', headers={'Content-type': 'application/json', 'vmware-api-session-id': session_id}, data=event_filter_spec) |
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:
1 2 3 |
import json event_history_collector_moref = json.loads(resp.content) event_history_collector_moid = event_history_collector_moref['value'] |
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:
1 2 3 4 |
resp = requests.get( url=f'https://{vcenter_host}/sdk/vim25/8.0.1.0/EventHistoryCollector/{event_history_collector_moid}/latestPage', headers={'Content-type': 'application/json', 'vmware-api-session-id': session_id}) |
As with the collector’s managed object ID, we deserialize the response into a Python list of events:
1 |
events = json.loads(resp.content) |
For more information regarding the EventManager
, EventHistoryCollector
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.