mira
Version:
NearForm Accelerator for Cloud Native Serverless AWS
112 lines • 4.05 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInstanceResourcesByType = exports.getInstanceResources = exports.getInstaceStacks = exports.getStacks = exports.getStackResources = exports.getCfn = exports.getFullStackName = void 0;
const aws_sdk_1 = __importDefault(require("aws-sdk"));
const config_1 = __importDefault(require("config"));
const app_1 = require("../cdk/app");
const assume_role_1 = require("../assume-role");
const mira_config_1 = require("../config/mira-config");
/**
* Gets the full stack name.
* @todo Refactor this into a more proper place.
*/
exports.getFullStackName = () => {
return `${app_1.getStackName()}-${mira_config_1.MiraConfig.getEnvironment().name}`;
};
let miraCfn;
/**
* Gets a Cfn object.
*/
exports.getCfn = async () => {
if (miraCfn) {
return miraCfn;
}
const awsConfig = await assume_role_1.assumeRole(assume_role_1.getRoleArn(config_1.default.get(`accounts.${mira_config_1.MiraConfig.getEnvironment().name}.profile`)));
awsConfig.region = mira_config_1.MiraConfig.getEnvironment().env.region;
aws_sdk_1.default.config = awsConfig;
miraCfn = new aws_sdk_1.default.CloudFormation({ apiVersion: '2010-05-15' });
return miraCfn;
};
/**
* Gets the stacks that are in progress. This can be used to detect if a stack
* is in progress of being pushed.
* @todo
*/
// export const getInProgressStacks = (): void => {
// }
/**
* Gets the resources included in a stack.
*/
exports.getStackResources = async (StackName) => {
const cfn = await exports.getCfn();
const result = await cfn.listStackResources({
StackName
}).promise();
return result;
};
/**
* Gets all stacks that have a state suffix of `_COMPLETE`.
*/
exports.getStacks = async (filter) => {
const cfn = (await exports.getCfn());
const result = await cfn.listStacks({
StackStatusFilter: [
'IMPORT_COMPLETE',
'UPDATE_ROLLBACK_COMPLETE',
'UPDATE_COMPLETE',
'ROLLBACK_COMPLETE',
'CREATE_COMPLETE'
]
}).promise();
if (result && result.StackSummaries) {
let stacks = result.StackSummaries;
if (filter) {
stacks = result.StackSummaries.filter(({ StackName }) => StackName.startsWith(filter));
}
return stacks;
}
return {};
};
/**
* Gets all stacks are associated with this Mira instance.
*/
exports.getInstaceStacks = async () => {
const stackName = `${app_1.getStackName()}-${mira_config_1.MiraConfig.getEnvironment().name}`;
return exports.getStacks(stackName);
};
/**
* Gets all of the resources for this particular instance.
*/
exports.getInstanceResources = async () => {
const stackResources = {};
const stacks = await exports.getInstaceStacks();
for (const stack of stacks) {
stackResources[stack.StackName] = await exports.getStackResources(stack.StackName);
stackResources[stack.StackName] = stackResources[stack.StackName].StackResourceSummaries;
}
return stackResources;
};
/**
* Gets all of the resources for this particular instance.
*/
exports.getInstanceResourcesByType = async () => {
const stackResources = await exports.getInstanceResources();
const stackResourcesByType = {};
for (const stackName in stackResources) {
if (!stackResourcesByType[stackName]) {
stackResourcesByType[stackName] = {};
}
for (const stackResource of stackResources[stackName]) {
const resourceType = stackResource.ResourceType;
if (!stackResourcesByType[stackName][resourceType]) {
stackResourcesByType[stackName][resourceType] = [];
}
stackResourcesByType[stackName][resourceType].push(stackResources[stackName]);
}
}
return stackResourcesByType;
};
//# sourceMappingURL=cloudformation.js.map
;