UNPKG

@csermet/multiprovider

Version:

cloud-graph provider plugin for AWS used to fetch AWS cloud data.

114 lines (113 loc) 5.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.caseInsensitiveIncludes = exports.checkAndMergeConnections = exports.settleAllPromises = exports.initTestConfig = exports.initTestEndpoint = exports.setAwsRetryOptions = exports.getCredentials = exports.getAccountId = void 0; const aws_sdk_1 = __importDefault(require("aws-sdk")); const sdk_1 = __importDefault(require("@cloudgraph/sdk")); const sts_1 = __importDefault(require("aws-sdk/clients/sts")); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const isEqual_1 = __importDefault(require("lodash/isEqual")); const unionWith_1 = __importDefault(require("lodash/unionWith")); const environment_1 = __importDefault(require("../config/environment")); const constants_1 = require("../config/constants"); const { logger } = sdk_1.default; async function getAccountId({ credentials, }) { try { return new Promise((resolve, reject) => new sts_1.default({ credentials }).getCallerIdentity((err, data) => { if (err) { return reject(err); } return resolve({ accountId: data.Account }); })); } catch (e) { return { accountId: '' }; } } exports.getAccountId = getAccountId; function getCredentials(opts) { return new Promise(resolve => { aws_sdk_1.default.config.getCredentials((err) => { if (err) { opts.logger.log(err); throw new Error('Unable to find Credentials for AWS, They could be stored in env variables or .aws/credentials file'); } else { resolve(aws_sdk_1.default.config.credentials); } }); }); } exports.getCredentials = getCredentials; /* Method to inject to set aws global config settings, logger utility or to use a particular aws config profile */ const setAwsRetryOptions = (opts) => { const { global = false, maxRetries = constants_1.MAX_FAILED_AWS_REQUEST_RETRIES, baseDelay: base = constants_1.BASE_CUSTOM_RETRY_DELAY, profile = undefined, configObj = undefined, ...rest } = opts; // logger.log = logger.debug const config = { maxRetries, // logger, retryDelayOptions: { base, }, ...rest, }; if (profile && configObj) { configObj.profile = profile; } global && aws_sdk_1.default.config.update(config); return config; }; exports.setAwsRetryOptions = setAwsRetryOptions; function initTestEndpoint(service) { const endpoint = (environment_1.default.NODE_ENV === 'test' && environment_1.default.LOCALSTACK_AWS_ENDPOINT) || undefined; service && endpoint && logger.info(`${service} getData in test mode!`); return endpoint; } exports.initTestEndpoint = initTestEndpoint; function initTestConfig() { jest.setTimeout(900000); } exports.initTestConfig = initTestConfig; const settleAllPromises = async (promises) => (await Promise.allSettled(promises)).map( /** We force the PromiseFulfilledResult interface * because all promises that we input to Promise.allSettled * are always resolved, that way we suppress the compiler error complaining * that Promise.allSettled returns an Array<PromiseFulfilledResult | PromiseRejectedResult> * and that the value property doesn't exist for the PromiseRejectedResult interface */ i => i.value); exports.settleAllPromises = settleAllPromises; const checkAndMergeConnections = (serviceConnections, connectionsToMerge) => { let connections = serviceConnections; // IF we have no pre existing connections for this service, use new connections // IF we have pre existing connections, check if its for the same serivce id, if so // check if the connections list for that id is empty, use new connections for that id if so. // otherwise, merge connections by unioning on id of the connections if (!isEmpty_1.default(connections)) { const entries = Object.entries(connectionsToMerge); for (const [key] of entries) { // If there are no service connections for this entity i.e. { [serviceId]: [] } // use new connections for that key if (connections[key]) { if (isEmpty_1.default(connections[key])) { connections[key] = connectionsToMerge[key] ?? []; } else { connections[key] = unionWith_1.default(connections[key], connectionsToMerge[key] ?? [], isEqual_1.default); } } else { Object.assign(connections, connectionsToMerge); } } return connections; } return connectionsToMerge; }; exports.checkAndMergeConnections = checkAndMergeConnections; const caseInsensitiveIncludes = (arr, s1) => !isEmpty_1.default(arr) && arr.filter(str => str.toLowerCase().includes(s1.toLowerCase())).length > 0; exports.caseInsensitiveIncludes = caseInsensitiveIncludes;