-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (48 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var request = require('request-promise-native');
// Disable SSL for simplicity and enable the cookie jar for REST API session ID
request = request.defaults({strictSSL: false, jar: true});
let host = "https://10.160.105.69";
let username = '[email protected]';
let password = 'Admin!23';
// Generic HTTP GET
async function get(path) {
let result = await request({ url: `${host}/${path}` });
return JSON.parse(result);
}
// Login using Basic Auth
async function login() {
await request({
url : `${host}/rest/com/vmware/cis/session`,
method: 'POST',
headers : { "Authorization" : "Basic " + new Buffer(`${username}:${password}`).toString("base64") }
});
}
function log(obj) {
console.log(JSON.stringify(obj, null, 2));
}
// Make a few API calls
async function callApi() {
// Fetch a list of VMs
let vms = await get('rest/vcenter/vm');
log(vms);
let vm = null;
// Fetch details of the first vm in the list
if(vms.value.length > 0) {
vm = await get(`rest/vcenter/vm/${vms.value[0].vm}`);
console.log(`\nDetails of VM: ${vms.value[0].vm}`);
log(vm);
}
let consolecli = await get('rest/appliance/access/consolecli');
console.log(consolecli.value); // <- .enabled
let power = await get(`rest/vcenter/vm/${vms.value[0].vm}/power`);
console.log(power.value.state)
}
login().then(() => {
callApi();
}, (error) => {
if(error.response) {
console.log(error.response.statusMessage);
} else {
console.log(error.message);
}
});