@codification/cutwater-build-aws
Version:
Tasks for working with AWS.
113 lines • 4.75 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiGatewayDeploymentUpdater = void 0;
const cutwater_build_core_1 = require("@codification/cutwater-build-core");
const md5_1 = __importDefault(require("md5"));
const yaml_cfn_1 = require("yaml-cfn");
class ApiGatewayDeploymentUpdater {
constructor(system = cutwater_build_core_1.System.create()) {
this.system = system;
this.REST_API_TYPE = 'AWS::ApiGateway::RestApi';
this.DEPLOYMENT_TYPE = 'AWS::ApiGateway::Deployment';
this.BODY_PROPERTY = 'Body';
this.TRANSFORM_PROPERTY = 'Fn::Transform';
this.OPENAPI_LOCATION_PROPERTY = 'Location';
this.REST_API_ID_PROPERTY = 'RestApiId';
}
load(templateFile) {
this.template = this.system.toFileReference(templateFile).readObjectSyncSafe(yaml_cfn_1.schema);
}
performOpenApiMerges(outputFile) {
this.validateState();
const apiToHash = {};
this.findRestApiResourceNames().forEach((rn) => {
const hash = this.mergeOpenApi(rn);
if (!!hash) {
apiToHash[rn] = hash;
}
});
const deployToHash = {};
Object.keys(apiToHash).forEach((api) => {
const deployName = this.findDeploymentResourceName(api);
if (!!deployName) {
deployToHash[deployName] = `${deployName}${apiToHash[api]}`;
const deployResource = this.template.Resources[deployName];
delete this.template.Resources[deployName];
this.template.Resources[deployToHash[deployName]] = deployResource;
}
});
this.updateDeploymentReferences(deployToHash);
this.system.toFileReference(outputFile).writeObjectSync(yaml_cfn_1.schema);
this.template = {};
}
findRestApiResourceNames() {
return this.findResourcesByType(this.REST_API_TYPE);
}
findResourcesByType(resourceType) {
this.validateState();
return Object.keys(this.template.Resources).filter((rn) => this.template.Resources[rn].Type === resourceType);
}
findDeploymentResourceName(restApiName) {
this.validateState();
return this.findResourcesByType(this.DEPLOYMENT_TYPE).find((name) => {
return (JSON.stringify(this.template.Resources[name].Properties[this.REST_API_ID_PROPERTY]).indexOf(restApiName) !== -1);
});
}
updateDeploymentReferences(deployNameMapping, context = this.template) {
Object.keys(context).forEach((property) => {
if (typeof context[property] === 'string') {
context[property] = this.replace(context[property], deployNameMapping);
}
else if (typeof context[property] === 'object') {
this.updateDeploymentReferences(deployNameMapping, context[property]);
}
});
}
replace(value, deployNameMapping) {
let rval = value;
if (typeof value === 'string') {
Object.keys(deployNameMapping).some((srcName) => {
const matcher = new RegExp('(?=\\W*)' + srcName + '(?=\\W*)', 'g');
if (matcher.test(value)) {
rval = value.replace(matcher, deployNameMapping[srcName]);
return true;
}
});
}
return rval;
}
mergeOpenApi(restApiName) {
const openApi = this.loadOpenApi(restApiName);
if (!!openApi) {
this.template.Resources[restApiName].Properties[this.BODY_PROPERTY] = openApi;
}
return !!openApi ? this.openApiHash(openApi) : undefined;
}
loadOpenApi(restApiName) {
let rval;
const body = this.toRestApiBody(restApiName);
if (!!body && !!body[this.OPENAPI_LOCATION_PROPERTY]) {
return this.system.toFileReference(body[this.OPENAPI_LOCATION_PROPERTY]).readObjectSync();
}
else if (!!body && !body[this.TRANSFORM_PROPERTY]) {
rval = body;
}
return rval;
}
toRestApiBody(restApiName) {
return this.template.Resources[restApiName].Properties[this.BODY_PROPERTY];
}
openApiHash(openApi) {
return (0, md5_1.default)(JSON.stringify(openApi)).substr(0, 7);
}
validateState() {
if (!this.template || !this.template.Resources) {
throw new Error('A CloudFormation template has not been loaded!');
}
}
}
exports.ApiGatewayDeploymentUpdater = ApiGatewayDeploymentUpdater;
//# sourceMappingURL=ApiGatewayDeploymentUpdater.js.map