UNPKG

@csermet/multiprovider

Version:

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

88 lines (87 loc) 3.26 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 groupBy_1 = __importDefault(require("lodash/groupBy")); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const ec2_1 = __importDefault(require("aws-sdk/clients/ec2")); const logger_1 = __importDefault(require("../../properties/logger")); const utils_1 = require("../../utils"); const errorLog_1 = __importDefault(require("../../utils/errorLog")); const lt = { ...logger_1.default }; const { logger } = sdk_1.default; const serviceName = 'Network Interface'; const errorLog = new errorLog_1.default(serviceName); const endpoint = utils_1.initTestEndpoint(serviceName); const listNetworkInterfaces = async ({ ec2, region, nextToken: NextToken = '', networkInterfacesData, resolveRegion, }) => { let args = {}; if (NextToken) { args = { ...args, NextToken }; } ec2.describeNetworkInterfaces(args, (err, data) => { if (err) { errorLog.generateAwsErrorLog({ functionName: 'ec2:describeNetworkInterfaces', err, }); } /** * No Network Interfaces data for this region */ if (isEmpty_1.default(data)) { return resolveRegion(); } const { NextToken: nextToken, NetworkInterfaces: networkInterfaces } = data || {}; logger.debug(lt.fetchedNetworkInterfaces(networkInterfaces.length)); /** * No Network Interfaces Found */ if (isEmpty_1.default(networkInterfaces)) { return resolveRegion(); } /** * Check to see if there are more */ if (nextToken) { listNetworkInterfaces({ region, nextToken, ec2, networkInterfacesData, resolveRegion, }); } networkInterfacesData.push(...networkInterfaces.map(({ TagSet, ...networkInterface }) => ({ ...networkInterface, Tags: (TagSet || []) .map(({ Key, Value }) => ({ [Key]: Value })) .reduce((acc, curr) => ({ ...acc, ...curr }), {}), region, }))); /** * If this is the last page of data then return the interfaces */ if (!nextToken) { resolveRegion(); } }); }; exports.default = async ({ regions, config, }) => new Promise(async (resolve) => { const networkInterfacesData = []; // Get all the network interfaces for each region const regionPromises = regions.split(',').map(region => { const ec2 = new ec2_1.default({ ...config, region, endpoint }); return new Promise(resolveRegion => listNetworkInterfaces({ ec2, region, networkInterfacesData, resolveRegion, })); }); logger.debug(lt.lookingForNetworkInterfaces); await Promise.all(regionPromises); errorLog.reset(); resolve(groupBy_1.default(networkInterfacesData, 'region')); });