UNPKG

@csermet/multiprovider

Version:

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

117 lines (116 loc) 4.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHostedZoneData = exports.listHostedZones = void 0; const sdk_1 = __importDefault(require("@cloudgraph/sdk")); const groupBy_1 = __importDefault(require("lodash/groupBy")); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const route53_1 = __importDefault(require("aws-sdk/clients/route53")); const logger_1 = __importDefault(require("../../properties/logger")); const utils_1 = require("../../utils"); const errorLog_1 = __importDefault(require("../../utils/errorLog")); const constants_1 = require("../../config/constants"); const regions_1 = require("../../enums/regions"); const lt = { ...logger_1.default }; const { logger } = sdk_1.default; const serviceName = 'Route53 Hosted Zones'; const errorLog = new errorLog_1.default(serviceName); const endpoint = utils_1.initTestEndpoint(serviceName); const customRetrySettings = utils_1.setAwsRetryOptions({ baseDelay: constants_1.ROUTE_53_CUSTOM_DELAY, }); const listHostedZones = async (route53, hostedZonesIds) => new Promise(async (resolveList) => { const listZonesOpts = {}; const listZones = (marker) => { if (marker) { listZonesOpts.Marker = marker; } route53.listHostedZones(listZonesOpts, async (err, data) => { if (err) { errorLog.generateAwsErrorLog({ functionName: 'route53:listHostedZones', err, }); } /** * No Data for the region */ if (isEmpty_1.default(data)) { return resolveList(); } const { HostedZones: zones = [], NextMarker: marker, IsTruncated: truncated, } = data || {}; logger.debug(lt.fetchedRoute53Zones(zones.length)); /** * Check to see if there are more */ if (truncated) { listZones(marker); } /** * If there are not, then add these to the zoneIds */ hostedZonesIds.push(...zones.map(({ Id }) => ({ Id }))); /** * If this is the last page of data then return the zones */ if (!truncated) { return resolveList(); } }); }; listZones(); }); exports.listHostedZones = listHostedZones; const getHostedZoneData = async (route53, hostedZonesIds, hostedZonesData) => Promise.all(hostedZonesIds.map(({ Id }) => { const zonePromise = new Promise(resolveZone => route53.getHostedZone({ Id }, (err, data) => { if (err) { errorLog.generateAwsErrorLog({ functionName: 'route53:getHostedZone', err, }); } /** * No Data for the region */ if (isEmpty_1.default(data)) { return resolveZone(); } /** * Add this zone to the zoneData and return */ hostedZonesData.push({ ...data.HostedZone, DelegationSet: data.DelegationSet, VPCs: data.VPCs, region: regions_1.globalRegionName, }); return resolveZone(); })); return zonePromise; })); exports.getHostedZoneData = getHostedZoneData; /** * Route53 Hosted Zones */ exports.default = async ({ config, }) => new Promise(async (resolve) => { const zoneIds = []; const hostedZonesData = []; const route53 = new route53_1.default({ ...config, endpoint, ...customRetrySettings, }); /** * Step 1) for all regions, list all the hosted zones */ await exports.listHostedZones(route53, zoneIds); /** * Step 2) now that we have all of the hosted zones, get the individual zone data */ await exports.getHostedZoneData(route53, zoneIds, hostedZonesData); errorLog.reset(); logger.debug(lt.doneFetchingRoute53HostedZoneData); resolve(groupBy_1.default(hostedZonesData, 'region')); });