UNPKG

@csermet/multiprovider

Version:

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

102 lines (101 loc) 3.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const sdk_1 = __importDefault(require("@cloudgraph/sdk")); const elasticache_1 = __importDefault(require("aws-sdk/clients/elasticache")); const groupBy_1 = __importDefault(require("lodash/groupBy")); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const logger_1 = __importDefault(require("../../properties/logger")); const format_1 = require("../../utils/format"); const errorLog_1 = __importDefault(require("../../utils/errorLog")); const utils_1 = require("../../utils"); const lt = { ...logger_1.default }; const { logger } = sdk_1.default; const serviceName = 'ElastiCache replication group'; const errorLog = new errorLog_1.default(serviceName); const endpoint = utils_1.initTestEndpoint(serviceName); const getElasticacheReplicationGroups = async (elastiCache) => new Promise(resolve => { const groupList = []; const descGroupOpts = {}; const listAllGroups = (token) => { if (token) { descGroupOpts.Marker = token; } try { elastiCache.describeReplicationGroups(descGroupOpts, (err, data) => { const { Marker, ReplicationGroups = [] } = data || {}; if (err) { errorLog.generateAwsErrorLog({ functionName: 'elastiCache:describeReplicationGroups', err, }); } groupList.push(...ReplicationGroups); if (Marker) { listAllGroups(Marker); } else { resolve(groupList); } }); } catch (error) { resolve([]); } }; listAllGroups(); }); const getResourceTags = async (elastiCache, arn) => new Promise(resolve => { try { elastiCache.listTagsForResource({ ResourceName: arn }, (err, data) => { if (err) { errorLog.generateAwsErrorLog({ functionName: 'elastiCache:listTagsForResource', err, }); return resolve({}); } const { TagList: tags = [] } = data || {}; resolve(format_1.convertAwsTagsToTagMap(tags)); }); } catch (error) { resolve({}); } }); exports.default = async ({ regions, config, }) => new Promise(async (resolve) => { const elastiCacheData = []; const regionPromises = []; const tagsPromises = []; // Get all the replication groups for the region regions.split(',').map(region => { const regionPromise = new Promise(async (resolveRegion) => { const elastiCache = new elasticache_1.default({ ...config, region, endpoint }); const groups = await getElasticacheReplicationGroups(elastiCache); if (!isEmpty_1.default(groups)) { elastiCacheData.push(...groups.map(group => ({ ...group, region, }))); } resolveRegion(); }); regionPromises.push(regionPromise); }); await Promise.all(regionPromises); logger.debug(lt.fetchedElasticacheClusters(elastiCacheData.length)); // get all tags for each replication group elastiCacheData.map(({ ARN: arn, region }, idx) => { const elastiCache = new elasticache_1.default({ ...config, region, endpoint }); const tagsPromise = new Promise(async (resolveTags) => { elastiCacheData[idx].Tags = await getResourceTags(elastiCache, arn); resolveTags(); }); tagsPromises.push(tagsPromise); }); await Promise.all(tagsPromises); errorLog.reset(); resolve(groupBy_1.default(elastiCacheData, 'region')); });