@csermet/multiprovider
Version:
cloud-graph provider plugin for AWS used to fetch AWS cloud data.
406 lines (405 loc) • 13.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const elbv2_1 = __importDefault(require("aws-sdk/clients/elbv2"));
const sdk_1 = __importDefault(require("@cloudgraph/sdk"));
const head_1 = __importDefault(require("lodash/head"));
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 constants_1 = require("../../config/constants");
const lt = { ...logger_1.default };
/**
* ALB
*/
const { logger } = sdk_1.default;
const serviceName = 'ALB';
const errorLog = new errorLog_1.default(serviceName);
const endpoint = utils_1.initTestEndpoint(serviceName);
const customRetrySettings = utils_1.setAwsRetryOptions({ baseDelay: constants_1.ALB_CUSTOM_DELAY });
exports.default = async ({ regions, config, }) => new Promise(async (resolve) => {
const albData = [];
const tagPromises = [];
const regionPromises = [];
const listenerPromises = [];
const targetGroupPromises = [];
const targetHealthPromises = [];
/**
* Step 1) for all regions, list all the albs
*/
const describeAlbs = async ({ region, marker: Marker = '', elbv2, resolveRegion, }) => {
let args = {};
if (Marker) {
args = { ...args, Marker };
}
return elbv2.describeLoadBalancers(args, async (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elbv2:describeLoadBalancers',
err,
});
}
/**
* No Data for the region
*/
if (isEmpty_1.default(data)) {
return resolveRegion();
}
const { LoadBalancers: albs, NextMarker: marker } = data;
logger.debug(lt.fetchedAlbs(albs.length));
/**
* No Albs
*/
if (isEmpty_1.default(albs)) {
return resolveRegion();
}
/**
* Check to see if there are more
*/
if (marker) {
describeAlbs({ region, marker, elbv2, resolveRegion });
}
/**
* If there are not, then add these to the albs
*/
albData.push(...albs.map(alb => ({
...alb,
region,
Tags: {},
listeners: [],
targetIds: [],
attributes: {},
targetGroups: [],
})));
/**
* If this is the last page of data then return the zones
*/
if (!marker) {
resolveRegion();
}
});
};
regions.split(',').map(region => regionPromises.push(new Promise(resolveRegion => describeAlbs({
region,
elbv2: new elbv2_1.default({
...config,
region,
endpoint,
...customRetrySettings,
}),
resolveRegion,
}))));
await Promise.all(regionPromises);
/**
* Step 2, get all the listeners for each alb
*/
const getTagsForAlb = async ({ alb, elbv2, resolveTags, ResourceArns, }) => elbv2.describeTags({ ResourceArns }, async (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elbv2:describeTags',
err,
});
}
/**
* No tags
*/
if (isEmpty_1.default(data)) {
return resolveTags();
}
const { TagDescriptions: allTags = [] } = data || {};
const tags = (head_1.default(allTags) || { Tags: [] }).Tags;
logger.debug(lt.fetchedAlbTags(tags.length, ResourceArns.toString()));
/**
* No tags found
*/
if (isEmpty_1.default(tags)) {
return resolveTags();
}
/**
* Add the tags to the alb
*/
const result = {};
tags.map(({ Key, Value }) => {
result[Key] = Value;
});
alb.Tags = format_1.convertAwsTagsToTagMap(tags);
resolveTags();
});
albData.map(alb => {
const { LoadBalancerArn: arn, region } = alb;
const elbv2 = new elbv2_1.default({
...config,
region,
endpoint,
...customRetrySettings,
});
tagPromises.push(new Promise(resolveTags => {
getTagsForAlb({
alb,
elbv2,
resolveTags,
ResourceArns: [arn],
});
}));
});
await Promise.all(tagPromises);
/**
* Step 3, get all the attributed for each alb
*/
const getAttributesForAlb = async ({ alb, elbv2, resolveAttributes, LoadBalancerArn, }) => elbv2.describeLoadBalancerAttributes({ LoadBalancerArn }, async (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elbv2:describeLoadBalancerAttributes',
err,
});
}
/**
* No attributes
*/
if (isEmpty_1.default(data)) {
return resolveAttributes();
}
const { Attributes: attributes = [] } = data || {};
logger.debug(lt.fetchedAlbAttributes(attributes.length, LoadBalancerArn));
/**
* No attributes found
*/
if (isEmpty_1.default(attributes)) {
return resolveAttributes();
}
/**
* Add the attributes to the alb
*/
const result = {};
attributes.map(({ Key, Value }) => {
result[Key] = Value;
});
alb.attributes = result;
resolveAttributes();
});
albData.map(alb => {
const { LoadBalancerArn, region } = alb;
const elbv2 = new elbv2_1.default({
...config,
region,
endpoint,
...customRetrySettings,
});
tagPromises.push(new Promise(resolveAttributes => {
getAttributesForAlb({
alb,
elbv2,
resolveAttributes,
LoadBalancerArn,
});
}));
});
await Promise.all(tagPromises);
/**
* Step 4, get all the listeners for each alb
*/
const describeListenersForAlb = async ({ alb, elbv2, marker: Marker = '', LoadBalancerArn, resolveListeners, }) => {
let args = { LoadBalancerArn };
if (Marker) {
args = {
...args,
Marker,
};
}
return elbv2.describeListeners(args, async (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elbv2:describeListeners',
err,
});
}
/**
* No listeners
*/
if (isEmpty_1.default(data)) {
return resolveListeners();
}
const { Listeners: listeners = [], NextMarker: marker } = data || {};
logger.debug(lt.fetchedAlbListeners(listeners.length, LoadBalancerArn));
/**
* No listeners found
*/
if (isEmpty_1.default(listeners)) {
return resolveListeners();
}
/**
* Check to see if there are more
*/
if (marker) {
describeListenersForAlb({
alb,
elbv2,
LoadBalancerArn,
resolveListeners,
marker,
});
}
/**
* If there are not, then add the listeners to the alb's listeners
*/
alb.listeners.push(...listeners);
/**
* If this is the last page of data then return
*/
if (!marker) {
resolveListeners();
}
});
};
albData.map(alb => {
const { LoadBalancerArn, region } = alb;
const elbv2 = new elbv2_1.default({
...config,
region,
endpoint,
...customRetrySettings,
});
const listenerPromise = new Promise(resolveListeners => {
describeListenersForAlb({
alb,
elbv2,
LoadBalancerArn,
resolveListeners,
});
});
listenerPromises.push(listenerPromise);
});
await Promise.all(listenerPromises);
/**
* Step 5, get all the target groups for each alb
*/
const describeTargetGroupsForAlb = async ({ alb, elbv2, marker: Marker = '', LoadBalancerArn, resolveTargetGroups, }) => {
let args = { LoadBalancerArn };
if (Marker) {
args = {
...args,
Marker,
};
}
return elbv2.describeTargetGroups(args, async (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elbv2:describeTargetGroups',
err,
});
}
/**
* No target groups
*/
if (isEmpty_1.default(data)) {
return resolveTargetGroups();
}
const { TargetGroups: targetGroups = [], NextMarker: marker } = data || {};
logger.debug(lt.fetchedAlbTargetGroups(targetGroups.length, LoadBalancerArn));
/**
* No targetGroups found
*/
if (isEmpty_1.default(targetGroups)) {
return resolveTargetGroups();
}
/**
* Check to see if there are more
*/
if (marker) {
describeTargetGroupsForAlb({
alb,
elbv2,
marker,
LoadBalancerArn,
resolveTargetGroups,
});
}
/**
* If there are not, then add the targetGroups to the alb's targetGroups
*/
alb.targetGroups.push(...targetGroups);
/**
* If this is the last page of data then return
*/
if (!marker) {
resolveTargetGroups();
}
});
};
albData.map(alb => {
const { LoadBalancerArn, region } = alb;
const elbv2 = new elbv2_1.default({
...config,
region,
endpoint,
...customRetrySettings,
});
const targetGroupPromise = new Promise(resolveTargetGroups => {
describeTargetGroupsForAlb({
alb,
elbv2,
LoadBalancerArn,
resolveTargetGroups,
});
});
targetGroupPromises.push(targetGroupPromise);
});
await Promise.all(targetGroupPromises);
/**
* Step 6, use the describeTargetHealth method to get the target IDs
*/
const describeTargetHealth = async ({ alb, elbv2, TargetGroupArn, resolveTargetHealth, }) => elbv2.describeTargetHealth({ TargetGroupArn }, async (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elbv2:describeTargetHealth',
err,
});
}
/**
* No target health info
*/
if (isEmpty_1.default(data)) {
return resolveTargetHealth();
}
const { TargetHealthDescriptions: targetHealth = [] } = data || {};
logger.debug(lt.fetchedAlbTargetIds(targetHealth.length, TargetGroupArn));
/**
* No target health info found
*/
if (isEmpty_1.default(targetHealth)) {
return resolveTargetHealth();
}
/**
* Add the ids to the alb's targetIds
*/
alb.targetIds.push(...targetHealth.map(({ Target: { Id = '' } = {} }) => Id));
resolveTargetHealth();
});
albData.map(alb => {
const { region, targetGroups = [] } = alb;
const elbv2 = new elbv2_1.default({
...config,
region,
endpoint,
...customRetrySettings,
});
targetGroups.map(({ TargetGroupArn }) => {
targetHealthPromises.push(new Promise(resolveTargetHealth => {
describeTargetHealth({
alb,
elbv2,
TargetGroupArn,
resolveTargetHealth,
});
}));
});
});
await Promise.all(targetHealthPromises);
errorLog.reset();
resolve(groupBy_1.default(albData, 'region'));
});