@csermet/multiprovider
Version:
cloud-graph provider plugin for AWS used to fetch AWS cloud data.
120 lines (119 loc) • 4.67 kB
JavaScript
;
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 elasticbeanstalk_1 = __importDefault(require("aws-sdk/clients/elasticbeanstalk"));
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
const index_1 = require("../../utils/index");
const logger_1 = __importDefault(require("../../properties/logger"));
const utils_1 = require("../../utils");
const errorLog_1 = __importDefault(require("../../utils/errorLog"));
const data_1 = require("../elasticBeanstalkApplication/data");
const lt = { ...logger_1.default };
const { logger } = sdk_1.default;
const serviceName = 'ElasticBeanstalkEnv';
const errorLog = new errorLog_1.default(serviceName);
const endpoint = utils_1.initTestEndpoint(serviceName);
const MAX_ITEMS = 1000;
const listEnvironments = async (eb) => new Promise(async (resolve) => {
const environments = [];
const listAllEnvironmentsOpts = {
MaxRecords: MAX_ITEMS,
};
const listAllEnvironments = (token) => {
if (token) {
listAllEnvironmentsOpts.NextToken = token;
}
try {
eb.describeEnvironments(listAllEnvironmentsOpts, (err, data) => {
const { Environments = [], NextToken: nextToken } = data || {};
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elasticBeanstalk:describeEnvironments',
err,
});
}
environments.push(...Environments);
if (nextToken) {
logger.debug(lt.foundAnotherThousand);
listAllEnvironments(nextToken);
}
else {
resolve(environments);
}
});
}
catch (error) {
resolve([]);
}
};
listAllEnvironments();
});
const getConfigSettingsForEnv = async (eb, ApplicationName, EnvironmentName) => new Promise(resolve => {
eb.describeConfigurationSettings({
ApplicationName,
EnvironmentName,
}, (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elasticBeanstalk:describeConfigurationSettings',
err,
});
}
const { ConfigurationSettings: settings = [] } = data || {};
resolve(settings);
});
});
const getResourcesForEnv = async (eb, EnvironmentName) => new Promise(resolve => {
eb.describeEnvironmentResources({
EnvironmentName,
}, (err, data) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'elasticBeanstalk:describeEnvironmentResources',
err,
});
}
const { EnvironmentResources: resources = {} } = data || {};
resolve(resources);
});
});
const getEnvironments = async (eb) => {
const envs = await listEnvironments(eb);
if (!isEmpty_1.default(envs)) {
// We wait for all env promises to settle
return index_1.settleAllPromises(
// We use the list of envs that we got before to fetch the rest of the needed data
envs.map(async (env) => {
const { ApplicationName: appName, EnvironmentName: envName, EnvironmentArn: envArn, } = env;
const promises = [
getResourcesForEnv(eb, envName),
getConfigSettingsForEnv(eb, appName, envName),
data_1.getResourceTags(eb, envArn),
];
// We wait for all data to be fetched and settled
const [resources, settings, Tags] = await index_1.settleAllPromises(promises);
return { ...env, resources, settings, Tags };
}));
}
};
exports.default = async ({ regions, credentials, }) => new Promise(async (resolve) => {
let numberOfEnvs = 0;
const output = {};
// First we get all applications for all regions
await Promise.all(regions.split(',').map(region => {
const eb = new elasticbeanstalk_1.default({ region, credentials, endpoint });
output[region] = [];
return new Promise(async (resolveRegion) => {
const envs = (await getEnvironments(eb)) || [];
output[region] = envs;
numberOfEnvs += envs.length;
resolveRegion();
});
}));
errorLog.reset();
logger.debug(lt.fetchedElasticBeanstalkEnvs(numberOfEnvs));
resolve(output);
});