vcenter-client
Version:
Library which consumes directly VCenter API
181 lines (153 loc) • 4.59 kB
JavaScript
const https = require('https');
const axios = require('axios').default;
const log4js = require('log4js')
const LOGGER = log4js.getLogger();
LOGGER.level = "debug";
/**
* Logins into VCenter API
*
* @param {*} baseUrl URL pointing to VCenter server. Should match to the following pattern: https://vcenter-server
* @param {*} user Login user
* @param {*} password Login password
* @returns Authentication Token to be used in the rest of the services
*/
exports.login = async (baseUrl, user, password) => {
https.globalAgent.options.rejectUnauthorized = false;
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
const headers = {
"Accept": "*/*",
"Authorization": "Basic " + Buffer.from(user + ":" + password).toString("base64")
}
let res = await instance.post(baseUrl + "/rest/com/vmware/cis/session", null, {
headers: headers
});
return res.data.value;
}
/**
*
* @param {*} baseUrl
* @param {*} token
* @param {*} vmMoRef
* @param {*} resourceType
* @returns
*/
exports.getResourceAttachedToVM = async (baseUrl, token, vmMoRef, resourceType) => {
try {
https.globalAgent.options.rejectUnauthorized = false;
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
const headers = {
"vmware-api-session-id": token
}
let res = await instance.get(`${baseUrl}/rest/vcenter/vm/${vmMoRef}/hardware/${resourceType}`, {
headers: headers
});
return res.data.value;
} catch (e) {
return e;
}
}
exports.getRelationStoragePoliciesAndVm = async (baseUrl, token, storageMoRef) => {
https.globalAgent.options.rejectUnauthorized = false;
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
const headers = {
"vmware-api-session-id": token
}
let res = null;
try {
res = await instance.get(`${baseUrl}/api/vcenter/storage/policies/${storageMoRef}/vm`, {
headers: headers
});
return res.data;
} catch (e) {
res = await instance.get(`${baseUrl}/rest/vcenter/storage/policies/${storageMoRef}/vm`, {
headers: headers
})
}
return res.data.value.reduce((previous, actual) => Object.assign(previous, {[actual.key]: actual.value}), {});
}
exports.vcenterCall = async (url, token) => {
https.globalAgent.options.rejectUnauthorized = false;
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
const headers = {
"vmware-api-session-id": token
}
let res = await instance.get(`${url}`, {
headers: headers
});
return res;
}
/**
*
* @param {*} baseUrl
* @param {*} token
* @param {*} vmMoRef
* @param {*} resourceType
* @param {*} resourceId
* @returns
*/
exports.getResourceDetailAttachedToVM = async (baseUrl, token, vmMoRef, resourceType, resourceId) => {
try {
return await _getResourceDetailAttachedToVM(baseUrl, token, vmMoRef, resourceType, resourceId);
} catch(ex) {
const MAX_RETRIES = 3;
let retries = 0;
let lastException;
while(retries < MAX_RETRIES) {
try {
retries = retries + 1;
return await _getResourceDetailAttachedToVM(baseUrl, token, vmMoRef, resourceType, resourceId);
} catch(ex) {
lastException = ex;
LOGGER.warn(`Error calling getResourceDetailAttachedToVM - Retry [${retries}] / MAX_RETRIES [${MAX_RETRIES}]`);
}
}
// If we come to that point, we had an error
throw lastException;
}
}
async function _getResourceDetailAttachedToVM(baseUrl, token, vmMoRef, resourceType, resourceId) {
https.globalAgent.options.rejectUnauthorized = false;
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
const headers = {
"vmware-api-session-id": token
}
let res = await instance.get(`${baseUrl}/rest/vcenter/vm/${vmMoRef}/hardware/${resourceType}/${resourceId}`, {
headers: headers
});
return res.data.value;
}
exports.getVirtualMachineByMoRef = async (baseUrl, token, vmMoRef) => {
https.globalAgent.options.rejectUnauthorized = false;
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
const headers = {
"vmware-api-session-id": token
}
let res = await instance.get(`${baseUrl}/rest/vcenter/vm/${vmMoRef}`, {
headers: headers
});
return res.data.value;
}