I am using API for quite a bit now in our prod environment which is based on vCloud Director and many times API’s had proved a handy way to troubleshoot issues where GUI was not providing a way to proceed.
Inspired by vCD API’s, i decided to test that in my vSphere 6.5 lab and in this post I will try to demonstrate few queries which can be helpful in fetching info in your infrastructure.
In my lab I am exploring REST API’s using a linux tool called curl.
1: You can browse list of API’s by browsing https://vcenter-fqdn/ and clicking on “Browse vSphere Rest API’s”

2: To start with you can use below query to see what are the different options available
# curl -sik -H ‘Accept:application/json’ -u “vc-user” -X GET https://vcenter-fqdn/rest/
You will see below URL’s in output:
| 1 2 3 | https://vc-fqdn/rest/com/vmware/vapi/rest/navigation/component https://vc-fqdn/rest/com/vmware/vapi/rest/navigation/resource | 
3: You can list the available components which can be explored via REST API by using below query
# curl -sik -H ‘Accept:application/json’ -u “vcadmin@alex” -X GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/component
Available Options
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.vcenter.ovf GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=applmgmt GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.cis GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.vcenter.inventory GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.vcenter GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.vapi.vcenter GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.cis.tagging GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.content GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=vmon_vapi_provider GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.vapi.rest.navigation GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.vcenter.iso GET https://vcentersrv03.alex.local/rest/com/vmware/vapi/rest/navigation/service?component_id=com.vmware.vapi | 
Let’s start with exploring available options for Datacenter and Clusters
List all datacenter in a vCenter
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: cf2cc0c3b07bd3a01224f06aa00fea59’ -X GET https://vcentersrv01.alex.local/rest/vcenter/datacenter
Output of above query will contain name of all datacenter that has been created in a vCenter
| 1 2 3 | {"name":"Galaxy","datacenter":"datacenter-2"} {"name":"manish","datacenter":"datacenter-58"} | 
Details about particular datacenter
You can fetch details like host_folder, network_folder etc for a specific datacenter by using below query. You can use those details in doing scripting for automated deployment of stuffs.
Note: Do not use datacenter name in below query. Use id which you obtained in above step.
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: cf2cc0c3b07bd3a01224f06aa00fea59’ -X GET https://vcentersrv01.alex.local/rest/vcenter/datacenter/datacenter-2
| 1 2 3 4 5 | "datastore_folder":"group-s5" "host_folder":"group-h4" "network_folder":"group-n6" "name":"Galaxy" "vm_folder":"group-v3" | 
Delete an empty datacenter
You can delete an empty datacenter using below query
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: cf2cc0c3b07bd3a01224f06aa00fea59’ -X DELETE https://vcentersrv01.alex.local/rest/vcenter/datacenter/datacenter-58
If you get Http 200 OK it means query have been executed successfully. You can also verify successful deletion of datacenter from vCenter Web Client.
| 1 2 3 | HTTP/1.1 200 OK Date: Thu, 15 Dec 2016 18:16:17 GMT Content-Length: 0 | 
Force removal of datacenter (when datacenter is not empty)
If a datacenter is not empty, you can force delete the contents and datacenter by specifying force=true in the API query
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: cf2cc0c3b07bd3a01224f06aa00fea59′ -X DELETE https://vcentersrv01.alex.local/rest/vcenter/datacenter/datacenter-68?force=true’
Cluster
Show all cluster in a vCenter
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: 4ca9afd3971255748599bc9e47c4f4a1’ -X GET https://vcentersrv01.alex.local/rest/vcenter/cluster
Output of above query will list all clusters present in a vCenter. Also you will get cluster id and details about cluster properties like HA/DRS etc.

List particular Cluster
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: 4ca9afd3971255748599bc9e47c4f4a1’ -X GET https://vcentersrv01.alex.local/rest/vcenter/cluster?filter.names=Cybertron
Output will tell you the resource_pool id which is associated with cluster ‘cybertron’

List all cluster in a given Datacenter
If you have multiple virtual datacenters in vcenter and you want to fetch details of all clusters which are in a given datacenter, you can do so by applying a filter with datacenter id in the query.
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: 4ca9afd3971255748599bc9e47c4f4a1’ -X GET https://vcentersrv01.alex.local/rest/vcenter/cluster?filter.datacenters=datacenter-2
Details of a particular cluster
Details of a particular cluster can be fetched using below query.
Note that after /cluster I have given cluster id and not the cluster name. If you specify cluster name here, you will get error that ‘resource do not exist’
# curl -sik -H ‘Accept: application/json’ -H ‘vmware-api-session-id: 4ca9afd3971255748599bc9e47c4f4a1’ -X GET
 https://vcentersrv01.alex.local/rest/vcenter/cluster/domain-c7

Thats it for this post. This a very basic overview of using API calls. There is a lot to explore and I will try to dive a bit deep in future posts of this series.
 
			
5 thoughts on “Exploring vSphere 6.5 API-Part 1: Datacenter & Cluster”