f5-conx-core
Version:
F5 SDK for JavaScript with Typescript type definitions
269 lines • 10.7 kB
JavaScript
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-empty-function */
/*
* Copyright 2020. F5 Networks, Inc. See End User License Agreement ("EULA") for
* license terms. Notwithstanding anything to the contrary in the EULA, Licensee
* may copy and modify this software product for its internal business purposes.
* Further, Licensee may upload, publish and distribute the modified version of
* the software product on devcentral.f5.com.
*/
'use strict';
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.IhealthClient = void 0;
const externalHttps_1 = require("./externalHttps");
/**
* basic frame work to interact with iHealth
* from Sergio Pereira
* https://clouddocs.f5.com/api/ihealth/Authentication.html
*/
class IhealthClient extends externalHttps_1.ExtHttp {
constructor(username, password) {
super();
this._api_host = 'https://ihealth-api.f5.com';
this._host = 'https://api.f5.com';
this._headers = { 'Accept': 'application/vnd.f5.ihealth.api.v1.0+json' };
this._authURL = '/auth/pub/sso/login/ihealth-api';
/**
* list/add/delete qkviews endpoint
* https://clouddocs.f5.com/api/ihealth/QKView_Collection_Methods.html
*/
this._collectionURL = '/qkview-analyzer/api/qkviews';
/**
* qkview metadata for listing and fetching further qkview details
*/
this.qkviews = [];
this.username = username;
this._password = password;
// this.userAgent = userAgent
// this.init();
this.axios.defaults.withCredentials = true;
}
/**
* Login to iHealth with user creds and save auth cookie
*/
login() {
return __awaiter(this, void 0, void 0, function* () {
return this.makeRequest({
method: 'POST',
url: `${this._host}${this._authURL}`,
data: {
user_id: this.username,
user_secret: this._password
}
})
.then(resp => {
this._cookies = resp.headers['set-cookie'];
this._cookiesExpiration = resp.data.expires;
// current time in seconds
const now = Date.now() / 1000;
// token expire time in seconds, rounded down
const expiresInSeconds = Math.floor(this._cookiesExpiration - now);
this.events.emit('log-info', `iHealthClient login successful, token expires in ${expiresInSeconds}`);
this.events.emit('log-debug', {
message: `iHealthClient login cookie details`,
cookies: this._cookies,
expires: this._cookiesExpiration
});
this.tokenTimer();
return {
cookies: this._cookies,
expires: this._cookiesExpiration,
expiresInSeconds
};
});
});
}
/**
* clear login token
* - to be used when we detect login failure scenario
*/
clearLogin() {
return __awaiter(this, void 0, void 0, function* () {
this.events.emit('log-info', `iHealthClient clearing token/timer with ${this.tokenTimeout} left`);
const tokenTimeOut = this.tokenTimeout;
this._cookies = undefined;
// this._token = undefined;
clearInterval(this._tokenIntervalId);
return tokenTimeOut;
});
}
/**
* bigip auth token lifetime countdown
* will clear auth token details when finished
* prompting the next http call to get a new token
*/
tokenTimer() {
return __awaiter(this, void 0, void 0, function* () {
this.events.emit('iHealthClient-token-timer-start', `Starting token timer: ${this.tokenTimeout}`);
// clear any timer we are currently tracking
clearInterval(this._tokenIntervalId);
this._tokenIntervalId = setInterval(() => {
this.tokenTimeout--;
// capture the self timer instance
const timerId = this._tokenIntervalId;
this.events.emit('iHealthClient-token-timer-count', this.tokenTimeout);
// kill the token 10 seconds early to give us time to get a new one with all the other calls going on
if (this.tokenTimeout <= 10) {
this._cookies = undefined; // clearing token details should get a new token
}
// keep running the timer so everything looks good, but clear the rest when it reaches 0
if (this.tokenTimeout <= 0) {
clearInterval(this._tokenIntervalId);
// just in case this timer got orphaned from the main class, also clear using self reference
clearInterval(timerId);
this.events.emit('iHealthClient-token-timer-expired', 'authToken expired -> will refresh with next HTTPS call');
// this.clearToken();
}
}, 1000);
});
}
/**
* list qkviews/IDs = '/qkview-analyzer/api/qkviews/'
*
* **includes all metaData -> held in qkviews class param**
*/
listQkviews() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._cookies) {
yield this.login();
}
yield this.makeRequest({
url: `${this._api_host}${this._collectionURL}`,
headers: {
// cookie : this._cookies
}
})
.then((resp) => __awaiter(this, void 0, void 0, function* () {
// get the qkview IDs
const qkviewIds = resp.data.id;
// array for async promises
const promiseArray = [];
// loop through each qkview ID and gather meta data
yield Promise.all(qkviewIds.map((id) => __awaiter(this, void 0, void 0, function* () {
// GET and push the qkveiw details to local array
yield this.qkviewMetaData(id)
.then(resp => {
// inject the qkview id back into metadata response
resp.data.id = id;
this.qkviews.push(resp.data);
});
})));
// wait for all the qkview metadata requests to finish
yield Promise.all(promiseArray);
// return the qkview details back to caller
return this.qkviews;
}));
});
}
/**
* list qkviews/IDs = '/qkview-analyzer/api/qkviews/'
*/
qkviewMetaData(id) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._cookies) {
yield this.login();
}
return this.makeRequest({
url: `${this._api_host}${this._collectionURL}/${id}`,
headers: {
// 'cookie' : this._cookies
}
});
});
}
/**
* list qkviews/IDs = '/qkview-analyzer/api/qkviews/'
*/
uploadiHealth(file) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._cookies) {
yield this.login();
}
const form = new FormData()
.append('qkview', file.base);
return this.makeRequest({
method: 'POST',
url: `${this._api_host}${this._collectionURL}`,
headers: {
// 'cookie' : this._cookies,
'Content-Type': 'multipart/form-data'
},
data: {
FormData: form
}
});
});
}
/**
* list commands = '/qkview-analyzer/api/qkviews/{qkview_id}/commands'
*
* https://clouddocs.f5.com/api/ihealth/QKView_Command_Output.html
*
* @param id qkview-id
*/
listCommands(id) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._cookies) {
yield this.login();
}
return this.makeRequest({
url: `${this._api_host}${this._collectionURL}/${id}/commands`,
headers: {
// 'cookie' : this._cookies
}
});
});
}
/**
* cmd output = '/qkview-analyzer/api/qkviews/{qkview_id}/commands/{a}'
*
* https://clouddocs.f5.com/api/ihealth/QKView_Command_Output.html
*
* @param id qkview-id
* @param cmd command to execute on qkview
*/
qkviewCommand(id, cmd) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._cookies) {
yield this.login();
}
return this.makeRequest({
url: `${this._api_host}${this._collectionURL}/${id}/commands/${cmd}`,
headers: {
// 'cookie' : this._cookies
}
});
});
}
/**
* get diagnostics for qkview
* - '/qkview-analyzer/api/qkviews/{qkview_id}/diagnostics?set=hit'
* https://clouddocs.f5.com/api/ihealth/QKView_Diagnostics.html
* @param id qkview-id
*/
getDiagnostics(id) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._cookies) {
yield this.login();
}
return this.makeRequest({
url: `${this._api_host}${this._collectionURL}/${id}/diagnostics`,
headers: {
// 'cookie' : this._cookies
}
});
});
}
}
exports.IhealthClient = IhealthClient;
//# sourceMappingURL=iHealthClient.js.map