Skip to content

Commit 76d1100

Browse files
authored
Merge pull request #1 from maafk/add-testing
Add jest and first set of tests as well as linting
2 parents 4743984 + 23a2789 commit 76d1100

File tree

5 files changed

+8552
-39
lines changed

5 files changed

+8552
-39
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
coverage/
3+
.DS_Store

index.js

+41-37
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
"use strict";
2-
var fs = require("fs");
3-
var https = require("https");
1+
'use strict';
2+
var fs = require('fs');
3+
var https = require('https');
44

55
class ServerlessPrivateAWSRegions {
66
constructor(serverless, options) {
77
this.serverless = serverless;
88
this.sdk = this.serverless.providers.aws.sdk;
9-
this.options = options;
9+
this.options = options || {};
1010

1111
this.commands = {
1212
region_setup: {
13-
usage: "Sets up Serverless Framework to work in private AWS regions",
14-
lifecycleEvents: ["setup"]
13+
usage: 'Sets up Serverless Framework to work in private AWS regions',
14+
lifecycleEvents: ['setup']
1515
}
1616
};
1717

1818
this.hooks = {
19-
"region_setup:setup": this.setup.bind(this),
20-
"before:aws:common:validate:validate": this.prepRegion.bind(this),
21-
"before:deploy:deploy": this.prepRegion.bind(this),
22-
"before:remove:remove": this.prepRegion.bind(this),
23-
"before:deploy:function:initialize": this.prepRegion.bind(this),
24-
"after:aws:package:finalize:mergeCustomProviderResources": this.updatePrincipals.bind(
19+
'region_setup:setup': this.setup.bind(this),
20+
'before:aws:common:validate:validate': this.prepRegion.bind(this),
21+
'before:deploy:deploy': this.prepRegion.bind(this),
22+
'before:remove:remove': this.prepRegion.bind(this),
23+
'before:deploy:function:initialize': this.prepRegion.bind(this),
24+
'after:aws:package:finalize:mergeCustomProviderResources': this.updatePrincipals.bind(
2525
this
2626
)
2727
};
@@ -70,12 +70,12 @@ class ServerlessPrivateAWSRegions {
7070
if (bundle) {
7171
} else {
7272
throw new this.serverless.classes.Error(
73-
"Make sure to define the AWS_CA_BUNDLE environment variable"
73+
'Make sure to define the AWS_CA_BUNDLE environment variable'
7474
);
7575
}
7676
const certs = [fs.readFileSync(bundle)];
7777
this.sdk.config.region = this.serverless.service.provider.region;
78-
this.sdk.config.signatureVersion = "v4";
78+
this.sdk.config.signatureVersion = 'v4';
7979
var endpoint = this.getCustomEndpoint();
8080
if (endpoint) {
8181
this.sdk.config.endpoint = endpoint;
@@ -96,36 +96,40 @@ class ServerlessPrivateAWSRegions {
9696
const template_resources = this.serverless.service.provider
9797
.compiledCloudFormationTemplate.Resources;
9898
Object.keys(template_resources).forEach(resource => {
99-
if (template_resources[resource].Type == "AWS::Lambda::Permission") {
99+
if (template_resources[resource].Type == 'AWS::Lambda::Permission') {
100100
// now check principal
101101
var principal = template_resources[resource].Properties.Principal;
102-
if (typeof principal == "string") {
103-
service = principal.split(".")[0];
102+
if (typeof principal == 'string') {
103+
service = principal.split('.')[0];
104104

105-
var new_principal = custom_principals.find(x => x.service === service);
105+
var new_principal = custom_principals.find(
106+
x => x.service === service
107+
);
106108

107109
if (new_principal) {
108-
new_principal = new_principal.principal
110+
new_principal = new_principal.principal;
109111
this.pluginLog(
110112
`Changing Principal from ${principal} to ${new_principal}`
111113
);
112114
principal = new_principal;
113115
}
114-
} else if ("Fn::Join" in principal) {
116+
} else if ('Fn::Join' in principal) {
115117
// using the join intrinsic function to piece together the principal
116-
var join_principal = principal["Fn::Join"][1][0];
117-
var service = join_principal.replace(/\.+$/, "");
118-
var new_principal = custom_principals.find(x => x.service === service);
118+
var join_principal = principal['Fn::Join'][1][0];
119+
var service = join_principal.replace(/\.+$/, '');
120+
var new_principal = custom_principals.find(
121+
x => x.service === service
122+
);
119123

120124
if (new_principal) {
121-
new_principal = new_principal.principal
125+
new_principal = new_principal.principal;
122126
this.pluginLog(
123127
`Changing Principal from ${principal} to ${new_principal}`
124128
);
125129
principal = new_principal;
126130
}
127131
} else {
128-
console.log("something else");
132+
console.log('something else');
129133
console.log(typeof principal);
130134
}
131135
template_resources[resource].Properties.Principal = principal;
@@ -137,40 +141,40 @@ class ServerlessPrivateAWSRegions {
137141
if (!s3_custom) {
138142
return;
139143
}
140-
if (!s3_custom["pattern"] || !s3_custom["return"]) {
144+
if (!s3_custom['pattern'] || !s3_custom['return']) {
141145
throw new this.serverless.classes.Error(
142-
"For custom regions, define both a `pattern` and `return` value for the S3Endpoint"
146+
'For custom regions, define both a `pattern` and `return` value for the S3Endpoint'
143147
);
144148
}
145149
var linesToAdd = [];
146-
if (s3_custom["comment"]) {
147-
linesToAdd.push(`// ${s3_custom["comment"]}`);
150+
if (s3_custom['comment']) {
151+
linesToAdd.push(`// ${s3_custom['comment']}`);
148152
}
149153
const custom_endpoint_line = `if (strRegion.match(/${
150-
s3_custom["pattern"]
151-
}/)) return \`${s3_custom["return"]}\`;`.replace(/\\/g, "");
154+
s3_custom['pattern']
155+
}/)) return \`${s3_custom['return']}\`;`.replace(/\\/g, '');
152156

153157
linesToAdd.push(custom_endpoint_line);
154158
const filePath = `${this.serverless.config.serverlessPath}/plugins/aws/utils/getS3EndpointForRegion.js`;
155159
this.addLinesToFile(
156160
filePath,
157-
"const strRegion = region.toLowerCase();",
161+
'const strRegion = region.toLowerCase();',
158162
linesToAdd,
159163
2
160164
);
161165
}
162166

163167
addLinesToFile(filePath, findLine, appendedLines, prepending_spaces = 0) {
164-
this.pluginLog(`Adding \n${appendedLines.join("\n")}\n\nto ${filePath}`);
168+
this.pluginLog(`Adding \n${appendedLines.join('\n')}\n\nto ${filePath}`);
165169
this.restoreOrig(filePath);
166170
this.backupOrig(filePath);
167171
var file_text = fs
168172
.readFileSync(filePath)
169173
.toString()
170-
.split("\n");
174+
.split('\n');
171175
const trimmed = file_text.map(s => s.trim());
172176
var appendedLines = appendedLines.map(s => {
173-
return `${" ".repeat(prepending_spaces)}${s}`;
177+
return `${' '.repeat(prepending_spaces)}${s}`;
174178
});
175179
const line_no = trimmed.indexOf(findLine);
176180
if (line_no < 0) {
@@ -179,7 +183,7 @@ class ServerlessPrivateAWSRegions {
179183
);
180184
} else {
181185
file_text.splice(line_no + 1, 0, ...appendedLines);
182-
fs.writeFileSync(filePath, file_text.join("\n"), err => {
186+
fs.writeFileSync(filePath, file_text.join('\n'), err => {
183187
if (err) throw err;
184188
this.pluginLog(`Updated ${filePath}`);
185189
});
@@ -203,7 +207,7 @@ class ServerlessPrivateAWSRegions {
203207
}
204208

205209
setup() {
206-
this.pluginLog("Running setup for private region");
210+
this.pluginLog('Running setup for private region');
207211
this.alterS3EndpointFunction();
208212
this.configureAwsSdk();
209213
}

0 commit comments

Comments
 (0)