Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VCenter ReST API authentication

I am following this VMware documentation. What headers are to be provided to authenticate to vCenter Server while using REST API?

like image 905
codec Avatar asked Nov 28 '22 22:11

codec


2 Answers

For python:

import requests

# https://vdc-download.vmware.com/vmwb-repository/dcr-public/1cd28284-3b72-4885-9e31-d1c6d9e26686/71ef7304-a6c9-43b3-a3cd-868b2c236c81/doc/operations/com/vmware/vcenter/vm.list-operation.html

sess = requests.post("https://XXXXXXXX/rest/com/vmware/cis/session", auth=('USERNAME', 'PASSWORD'), verify=False)
session_id = sess.json()['value']

resp = requests.get("https://XXXXXXXX/rest/vcenter/vm", verify=False, headers={
    "vmware-api-session-id": session_id
})
print(u"resp.text = %s" % str(resp.text))
like image 133
arturgspb Avatar answered Dec 06 '22 04:12

arturgspb


Let me illustrate what you would exactly need to do in order to obtain a list of VMs from Vcenter for example.

First, you need to issue a POST request to https://vcsa/rest/com/vmware/cis/session in order to get a session id.

You then use a GET request to https://vcsa/rest/vcenter/vm with the HTTP header vmware-api-session-id set to the previously obtained session id.

Here is some example code in PHP:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, '[email protected]' . ":" . 'password');

$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
  echo 'Curl Error: ' . curl_error($ch);
  exit;
}
$sid = $out->value;

curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");

$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);

curl_close($ch);
like image 45
Marki Avatar answered Dec 06 '22 05:12

Marki