Exploring vSphere 6.5 API-Part 1: Datacenter & Cluster

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”

api-url

 

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:

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

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

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

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.

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.

all-cluster

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’

cluseter-identifier

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

only-1-cluster

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.