vsphere-infra
Version:
163 lines • 7.33 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VsphereConnection = void 0;
const https = __importStar(require("https"));
const cookie = __importStar(require("cookie"));
const _ = __importStar(require("lodash"));
const soap = __importStar(require("soap"));
const uuid = __importStar(require("uuid"));
const vsphere_caller_1 = require("./vsphere.caller");
const ts_basis_1 = require("ts-basis");
const soap_request_1 = require("./soap.request");
class VsphereConnection extends ts_basis_1.ix.Entity {
constructor($dc, url, options, connectionAllowed) {
super('vcenter-conn');
this.$dc = $dc;
this.url = url;
this.options = options;
this.connectionAllowed = connectionAllowed;
this.connId = uuid.v4();
this.traffic = { up: 0, down: 0, baseOverhead: 200 };
$dc.lifecycle.manage(this);
}
exec(command, params) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const paramsClone = this.sanitizeParams(_.cloneDeep(params));
const callback = this.client.VimService.VimPort[command];
const result = yield new Promise((resolve, reject) => {
callback(paramsClone, (err, res) => {
if (err) {
return reject(err);
}
if (res === null) {
return reject(new Error('No result returned from vSphere'));
}
resolve(res.returnval);
});
});
return { result, headers: (_a = this.client.lastResponseHeaders) !== null && _a !== void 0 ? _a : {} };
});
}
connect() {
return __awaiter(this, void 0, void 0, function* () {
this.$dc.debugOutput(`connecting... (connId=${this.connId})`);
if (!this.connectionAllowed) {
throw new Error(`Cannot connect to ${this.url}, connection not granted.`);
}
let url = this.url;
if (!url.startsWith('https://') && !url.startsWith('http://')) {
url = 'https://' + url;
}
if (!url.endsWith('/sdk/vimService.wsdl')) {
if (url.endsWith('/')) {
url += 'sdk/vimService.wsdl';
}
else {
url += '/sdk/vimService.wsdl';
}
}
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
soap.createClientAsync(url, {
endpoint: url,
httpClient: new soap_request_1.CustomHttpClient({
sdkCache: this.$dc.vinfra.behavior.useSdkCache,
}),
}, url).then(client => {
this.client = client;
this.client.setSecurity(new soap.ClientSSLSecurity(null, null, null, {
strictSSL: true,
forever: true,
ca: https.globalAgent.options.ca
}));
this.$dc.debugOutput(`client created (clientId=${this.connId})`);
this.client.on('request', (xml, eid) => {
this.traffic.up += xml.length + this.traffic.baseOverhead;
});
this.client.on('response', (body, eid) => {
if (body) {
this.traffic.down += body.length + this.traffic.baseOverhead;
}
});
this.client.on('soapError', (error, eid) => {
if (error.body) {
this.traffic.down += error.body.length + this.traffic.baseOverhead;
}
});
this.api = new vsphere_caller_1.VsphereCaller(this.$dc, this.client);
resolve(true);
}).catch(e => {
this.$dc.debugOutput(e);
return resolve(false);
});
}));
});
}
login(username, password) {
return __awaiter(this, void 0, void 0, function* () {
try {
const res = yield this.exec('Login', { _this: { $value: 'SessionManager' }, userName: username, password });
const header = res.headers['set-cookie'];
if (header && header.length > 0) {
const sessionId = cookie.parse(header[0]).vmware_soap_session;
this.client.addHttpHeader('Cookie', cookie.serialize('vmware_soap_session', sessionId));
return true;
}
else {
return false;
}
}
catch (err) {
if (err instanceof Error && err.message.includes('Cannot complete login due to an incorrect user name or password.')) {
throw new Error(`vSphere rejected login credentials for '${username}' target '${this.url}'`);
}
throw err;
}
});
}
sanitizeParams(params) {
Object.keys(params).forEach(key => {
if (key === 'connection') {
delete params[key];
}
else if (typeof params[key] === 'object') {
this.sanitizeParams(params[key]);
}
});
return params;
}
}
exports.VsphereConnection = VsphereConnection;
//# sourceMappingURL=connection.js.map