UNPKG

f5-conx-core

Version:

F5 SDK for JavaScript with Typescript type definitions

185 lines 8.69 kB
"use strict"; /* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable prefer-const */ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/ban-types */ 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.as3AppStats = exports.as3AppsInTenant = exports.mapAs3 = exports.targetDecsBool = exports.tenantFromDec = void 0; const misc_1 = require("../utils/misc"); /** * extracts tenant name and target from declaration * used for crafting tenant delete calls * @param dec AS3/ADC declaration * @returns { tenant, schemaVersion, target} */ function tenantFromDec(dec) { return __awaiter(this, void 0, void 0, function* () { let tenant; if (dec.class === "AS3") { dec = dec.declaration; } const schemaVersion = dec.schemaVersion; const target = dec.target; // loop through declaration (adc) level Object.entries(dec).forEach(([key, val]) => { if ((0, misc_1.isObject)(val) && key !== 'target' && key !== 'controls') { tenant = key; } }); return { tenant, schemaVersion, target }; }); } exports.tenantFromDec = tenantFromDec; /** * returns true if working with targets * @param declare /declare endpoint output */ function targetDecsBool(declare) { var _a; return __awaiter(this, void 0, void 0, function* () { // if array from bigiq/targets, assign, else put in array const declareArray = Array.isArray(declare) ? declare : [declare]; if ((_a = declareArray[0]) === null || _a === void 0 ? void 0 : _a.target) { return true; } else { return false; } }); } exports.targetDecsBool = targetDecsBool; /** * returns as3 application index of target?/tenant/app/app-stats * * ```ts * * ``` * * @param declare /declare endpoint output */ function mapAs3(declare) { return __awaiter(this, void 0, void 0, function* () { // if array from bigiq/targets, assign, else put in array const declareArray = Array.isArray(declare) ? declare : [declare]; const as3Map = {}; /** * this map represents what I feel is a more modern approach to building json structures. The F5 ATC method for building json structures heavily utilizes named objects which can be very difficult to crawl/discover, since one has to constantly loop and check to see what kind of data the key holds. This method can be very clean and concise * * The other method, which seems to be the more common method, makes the structure bigger but more predictable without having to inspect each object param to see what kind it is. !Example, the github api has many nested objects and lists, but rarely any 'named' objects. This predictable structure of object keys makes it way easier to type objects for typescript and get information from within the structure without having to discover each key/value (just look for the key, not inspect the keys value for another key/value pair). * * This new map is not returned as part of the function output, but built within this function as a demonstration and excercise to explore both options. The thought it to move this function to f5-conx-core and utilize across the projects */ const as3MapNew = []; // go through each item in the targets array declareArray.map((el) => { var _a, _b; const tenants = {}; const tenantsNew = []; // get target if defined const target = ((_a = el === null || el === void 0 ? void 0 : el.target) === null || _a === void 0 ? void 0 : _a.address) ? el.target.address : ((_b = el === null || el === void 0 ? void 0 : el.target) === null || _b === void 0 ? void 0 : _b.hostname) ? el.target.hostname : undefined; // loop through declaration (adc) level Object.entries(el).forEach(([key, val]) => { // named object for each tenant if ((0, misc_1.isObject)(val) && key !== 'target' && key !== 'controls') { let apps2 = {}; let appsNew = []; // loop through items of the tenant Object.entries(val).forEach(([tKey, tVal]) => { // if we are at an application object if ((0, misc_1.isObject)(tVal)) { const appProps = {}; // loop through the items of the application Object.entries(tVal).forEach(([aKey, aVal]) => { // look at the objects (application pieces) if ((0, misc_1.isObject)(aVal) && (aVal === null || aVal === void 0 ? void 0 : aVal.class)) { const appVal = aVal; // capture the class of each application piece if ((appVal === null || appVal === void 0 ? void 0 : appVal.class) in appProps) { // already have this key, so add one appProps[appVal.class] = appProps[appVal.class] + 1; } else { // key not detected, so create it appProps[appVal.class] = 1; } } }); apps2[tKey] = appProps; appsNew.push({ app: tKey, components: appProps }); } }); tenants[key] = apps2; tenantsNew.push({ tenant: key, apps: appsNew }); } }); if (target) { as3Map[target] = tenants; as3MapNew.push({ target, tenants: tenantsNew }); } else { Object.assign(as3Map, tenants); as3MapNew.push(...tenantsNew); } }); return as3Map; }); } exports.mapAs3 = mapAs3; function as3AppsInTenant(as3Tenant) { return __awaiter(this, void 0, void 0, function* () { const apps = []; // loop through the items of the tenant Object.entries(as3Tenant).forEach(([aKey, aVal]) => { // look at the objects (application pieces) if ((0, misc_1.isObject)(aVal)) { apps.push(aKey); } }); return apps; }); } exports.as3AppsInTenant = as3AppsInTenant; function as3AppStats(as3App) { return __awaiter(this, void 0, void 0, function* () { const appProps = {}; // loop through the items of the application Object.entries(as3App).forEach(([aKey, aVal]) => { // look at the objects (application pieces) if ((0, misc_1.isObject)(aVal) && (aVal === null || aVal === void 0 ? void 0 : aVal.class)) { const appVal = aVal; // capture the class of each application piece if ((appVal === null || appVal === void 0 ? void 0 : appVal.class) in appProps) { // already have this key, so add one appProps[appVal.class] + 1; } else { // key not detected, so create it appProps[appVal.class] = 1; } } }); return appProps; }); } exports.as3AppStats = as3AppStats; //# sourceMappingURL=as3Tools.js.map