As of vSphere 8.0 Update 3, developers can re-use a vSphere Management (VIM) session for making vSphere Automation (vAPI) calls, or re-use a vAPI session for making VIM calls. Examples follow.
Here is Python code to establish a vSphere Automation (vAPI) session to then invoke a vSphere Management (VIM) call:
import requests from pyVim.connect import SmartConnect from vmware.vapi.vsphere.client import create_vsphere_client vc_host = '10.168.203.112' user = '[email protected]' password = 'NKCF_n4PagP0jmz_' # vAPI setup session = requests.session() session.verify = False client = create_vsphere_client(vc_host, user, password, session=session) # Acquire session_id session_id = client.get_session_id() # Reuse session_id in SmartConnect si = SmartConnect(host=vc_host, port=443, disableSslCertValidation=True, sessionId=session_id) content = si.RetrieveContent() dc_moref = content.rootFolder.CreateDatacenter("TEST") print(dc_moref)
Here is Python code to establish a vSphere Management (VIM) session to then invoke a vSphere Automation (vAPI) call:
import requests from pyVim.connect import SmartConnect from vmware.vapi.vsphere.client import create_vsphere_client vc_host = '10.168.203.112' user = '[email protected]' password = 'NKCF_n4PagP0jmz_' # Login with pyvmomi si = SmartConnect(host=vc_host, port=443, user=user, pwd=password, disableSslCertValidation=True) # TODO GetSessionId documentation session_id = si._GetStub().GetSessionId() # vAPI setup session = requests.session() session.verify = False client = create_vsphere_client(vc_host, session=session, session_id=session_id) print(client.vcenter.compute.Policies.list())
Here are Curl commands to establish a VIM session via JSON, make a vAPI call, and then call the REST tag API. The second and third are get requests and do not apply anything. The second command returns a list of folders. The third returns a list of tags for folder group-d3
.
$ curl "https://$VC/sdk/vim25/8.0.2.0/SessionManager/SessionManager/Login" -H 'Content-Type: application/json' -d '{"userName": "[email protected]", "password": "$PSWD"}' -ki $ curl https://$VC/api/vcenter/folder -H "vmware-api-session-id: $VIM_SESSION" -k $ curl https://$VC/sdk/vim25/8.0.2.0/Folder/group-d3/tag -H "vmware-api-session-id: $VIM_SESSION" -k
Here are Curl commands to create a REST session and use it to call the VIM tag API:
$ curl -X POST https://$VC/api/session -u "[email protected]:$PSWD" -k $ curl "https://$VC/sdk/vim25/8.0.2.0/Folder/group-d1001/tag" -H "vmware-api-session-id: $VAPI_SESSION" -k
The following caveats and limitations apply to unified VIM/vAPI sessions:
- Session timeout can be configured with property
config.vmacore.soap.sessionTimeout
in file /etc/vmware-vpx/vpx.conf on vCenter Server, or using the vSphere Client UI.
- Default session lifespan can be configured with property
vpx.sso.sts.tokenLifetime
, also in vpx.conf, or using the vSphere Client UI. - The maximum number of inflight sessions can be configured with property
config.vmacore.soap.maxSessionCount
, also in vpx.conf, or using the vSphere Client UI. -
Session IDs from either endpoint work in the other endpoint. Clients that want the benefit of single session should adjust to accept variable size Session ID tokens.