1 | const ConfigFile = require('./abstract-config-file');
|
2 |
|
3 |
|
4 | let instance = null;
|
5 |
|
6 | const TYPE_AWS_LAMBDA_FUNCTION = 'AWS::Lambda::Function';
|
7 | const ERROR_MSG_RESOURCES_FIELD_EMPTY = '[Error]: Resources field must not be empty in regional template file';
|
8 |
|
9 | module.exports = class RegionalStackFile extends ConfigFile {
|
10 | |
11 |
|
12 |
|
13 |
|
14 |
|
15 | constructor(filePath) {
|
16 | if (instance && instance.path === filePath) {
|
17 | return instance;
|
18 | }
|
19 |
|
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 | };
|