Skip to content

Instantly share code, notes, and snippets.

@mathse
Forked from kaitoii11/vcenter-api.md
Created July 17, 2021 17:56
Show Gist options
  • Save mathse/f9b08e67278470a9ebb42365295d115e to your computer and use it in GitHub Desktop.
Save mathse/f9b08e67278470a9ebb42365295d115e to your computer and use it in GitHub Desktop.
Sample on how to use vCenter API
  • Authenticate to vCenter to get user credential.
# curl -k -X POST https://10.0.0.27/rest/com/vmware/cis/session -u <USERNAME>@<DOMAIN>:<PASSWORD> | jq
{
  "value": "SESSIONID"
}
  • Save the session id to a variable
# ID=SESSIONID
  • Access the end point with the session id
# curl -k -X GET -H "vmware-api-session-id: $ID" https:/ /<VCENTERIP or FQDN>/rest/vcenter/vm |jq
{
  "value": [
    {
      "memory_size_MiB": 16384,
      "vm": "vm-1",
      "name": "VM01",
      "power_state": "POWERED_ON",
      "cpu_count": 4
    },
    {
      "memory_size_MiB": 16384,
      "vm": "vm-2",
      "name": "VM02",
      "power_state": "POWERED_ON",
      "cpu_count": 4
    }
  ]
}
# curl -k -X GET -H "vmware-api-session-id: $ID" https://<VCENTERIP or FQDN>/rest/vcenter/datastore |jq
{
  "value": [
    {
      "datastore": "datastore-1",
      "name": "DS1",
      "type": "VMFS",
      "free_space": 290276245504,
      "capacity": 291789340672
    },
    {
      "datastore": "datastore-2",
      "name": "DS2",
      "type": "VMFS",
      "free_space": 3349917204480,
      "capacity": 3599451029504
    }
  ]
}

List of apis can be found below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment