In my first article I gave an overview of the new vSphere vCenter Rest API as part of vSphere 6.5. The API currently focuses on VM lifecycle features which I'm going to provide a few examples of.

As part of the GA release of vSphere 6.5 VMware will be providing a SDK for the Rest API. This contains the REST API documentation and a great selection of examples you can load using Postman. Postman is a great application to design, build and test Rest APIs. They have clients available for macOS, Windows and Linux. With this you will be able to get working with the API quickly and build your knowledge of how it works before using it with your own application.

Authentication

To work with the API you first have to authenticate and get a session token. This is done with a POST request with user credentials for the vCenter to the address: https://{vc}/rest/com/vmware/cis/session

postman authentication

If this has been successful you should get a http response of 200 and a session value in the body of the response.

Listing all Virtual Machines

Now with a session token we can start to use the API. Getting a list of virtual machines is very easy. You just need to do use a GET request to the url: https://{{vc}}/rest/vcenter/vm

This returns a list of all of the VM's for the vCenter you are connected to with some information about the servers CPU count and RAM.

postman - list vms

Get Virtual Machine details

By taking the MoRef of a Virtual Machine from the list above we can get more detailed information. The MoRef is added to the end of the same URL used above. The post request will look like this

https://{{vc}}/rest/vcenter/vm/vm-42

postman - vm details

This will return a JSON data set like the example below:

{
  "value": {
  "cdroms": [
  {
  "value": {
  "start_connected": false,
  "backing": {
  "device_access_type": "PASSTHRU",
  "type": "CLIENT_DEVICE"
  },
  "allow_guest_control": true,
  "label": "CD/DVD drive 1",
  "state": "NOT_CONNECTED",
  "type": "SATA",
  "sata": {
  "bus": 0,
  "unit": 0
  }
  },
  "key": "16000"
  }
  ],
  "memory": {
  "size_MiB": 4096,
  "hot_add_enabled": false
  },
  "disks": [
  {
  "value": {
  "scsi": {
  "bus": 0,
  "unit": 0
  },
  "backing": {
  "vmdk_file": "[lab] lab-test/lab-test.vmdk",
  "type": "VMDK_FILE"
  },
  "label": "Hard disk 1",
  "type": "SCSI",
  "capacity": 42949672960
  },
  "key": "2000"
  }
  ],
  "parallel_ports": [],
  "sata_adapters": [
  {
  "value": {
  "bus": 0,
  "label": "SATA controller 0",
  "type": "AHCI"
  },
  "key": "15000"
  }
  ],
  "cpu": {
  "hot_remove_enabled": false,
  "count": 1,
  "hot_add_enabled": false,
  "cores_per_socket": 1
  },
  "scsi_adapters": [
  {
  "value": {
  "scsi": {
  "bus": 0,
  "unit": 7
  },
  "label": "SCSI controller 0",
  "type": "LSILOGICSAS",
  "sharing": "NONE"
  },
  "key": "1000"
  }
  ],
  "power_state": "POWERED_OFF",
  "floppies": [
  {
  "value": {
  "start_connected": false,
  "backing": {
  "type": "CLIENT_DEVICE"
  },
  "allow_guest_control": true,
  "label": "Floppy drive 1",
  "state": "NOT_CONNECTED"
  },
  "key": "8000"
  }
  ],
  "name": "lab-test",
  "nics": [
  {
  "value": {
  "start_connected": true,
  "backing": {
  "network_name": "Lab Network",
  "type": "STANDARD_PORTGROUP",
  "network": "network-31"
  },
  "mac_address": "00:50:56:9d:fa:6a",
  "mac_type": "ASSIGNED",
  "allow_guest_control": true,
  "wake_on_lan_enabled": true,
  "label": "Network adapter 1",
  "state": "NOT_CONNECTED",
  "type": "E1000E"
  },
  "key": "4000"
  }
  ],
  "boot": {
  "delay": 0,
  "retry_delay": 10000,
  "enter_setup_mode": false,
  "type": "BIOS",
  "retry": false
  },
  "serial_ports": [],
  "guest_OS": "WINDOWS_9_SERVER_64",
  "boot_devices": [],
  "hardware": {
  "upgrade_policy": "NEVER",
  "upgrade_status": "NONE",
  "version": "VMX_11"
  }
  }
}


Comments

comments powered by Disqus