UNPKG

2.02 kBJavaScriptView Raw
1const ConfigFile = require('./abstract-config-file');
2
3// instance which stores the singleton
4let instance = null;
5
6const TYPE_AWS_LAMBDA_FUNCTION = 'AWS::Lambda::Function';
7const ERROR_MSG_RESOURCES_FIELD_EMPTY = '[Error]: Resources field must not be empty in regional template file';
8
9module.exports = class RegionalStackFile extends ConfigFile {
10 /**
11 * Constructor for RegionalStackFile class
12 * @param {string} filePath
13 * @throws {Error}
14 */
15 constructor(filePath) {
16 if (instance && instance.path === filePath) {
17 return instance;
18 }
19 // init by calling super() if instance not exists
20 super(filePath);
21 instance = this;
22 }
23
24 static getInstance() {
25 return instance;
26 }
27
28 static dispose() {
29 instance = null;
30 }
31
32 getResources() {
33 return this.getProperty(['Resources']);
34 }
35
36 getLambdaFunction() {
37 const resources = this.getResources();
38 if (!resources) {
39 return null;
40 }
41
42 for (const logicalId of Object.keys(resources)) {
43 if (resources[logicalId] && resources[logicalId].Type === TYPE_AWS_LAMBDA_FUNCTION) {
44 return this.getProperty(['Resources', logicalId]);
45 }
46 }
47 return null;
48 }
49
50 setLambdaFunctionCode(bucket, key, version) {
51 const resources = this.getResources();
52 if (!resources) {
53 throw new Error(ERROR_MSG_RESOURCES_FIELD_EMPTY);
54 }
55 for (const logicalId of Object.keys(resources)) {
56 if (resources[logicalId] && resources[logicalId].Type === TYPE_AWS_LAMBDA_FUNCTION) {
57 this.setProperty(['Resources', logicalId, 'Properties', 'Code', 'S3Bucket'], bucket);
58 this.setProperty(['Resources', logicalId, 'Properties', 'Code', 'S3Key'], key);
59 this.setProperty(['Resources', logicalId, 'Properties', 'Code', 'S3ObjectVersion'], version);
60 break;
61 }
62 }
63 }
64};