Skip to content

Commit

Permalink
Merge branch 'release/2.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
svdgraaf committed Dec 11, 2018
2 parents bcfbc20 + e26c50e commit 5d0ed1c
Show file tree
Hide file tree
Showing 3 changed files with 793 additions and 784 deletions.
79 changes: 44 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@
class ServerlessAWSPseudoParameters {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.options = options || {};
this.hooks = {
'after:aws:package:finalize:mergeCustomProviderResources': this.addParameters.bind(this),
'after:aws:package:finalize:mergeCustomProviderResources': this.addParameters.bind(this)
};
this.skipRegionReplace = get(serverless.service, 'custom.pseudoParameters.skipRegionReplace', true)
this.allowReferences = get(serverless.service, 'custom.pseudoParameters.allowReferences', true)
this.skipRegionReplace = get(serverless.service, 'custom.pseudoParameters.skipRegionReplace', true);
this.allowReferences = get(serverless.service, 'custom.pseudoParameters.allowReferences', true);
this.colors = get(this.serverless,'processedInput.options.color', true);
this.debug = this.options.debug || process.env.SLS_DEBUG;
}

addParameters() {

const template = this.serverless.service.provider.compiledCloudFormationTemplate;
const skipRegionReplace = this.skipRegionReplace;
const allowReferences = this.allowReferences;
const colors = this.colors;
const debug = this.debug;
const consoleLog = this.serverless.cli.consoleLog;

consoleLog(yellow(underline('AWS Pseudo Parameters')));
if (debug) consoleLog(yellow(underline('AWS Pseudo Parameters')));

if (skipRegionReplace) {
if (skipRegionReplace && debug) {
consoleLog('Skipping automatic replacement of regions with account region!');
}

// loop through the entire template, and check all (string) properties for any #{AWS::}
// reference. If found, replace the value with an Fn::Sub reference
Object.keys(template).forEach(identifier => {
replaceChildNodes(template[identifier], identifier)
replaceChildNodes(template[identifier], identifier);
});


Expand All @@ -36,31 +40,31 @@ class ServerlessAWSPseudoParameters {
}

function isArray(v) {
return Object.prototype.toString.call(v) === '[object Array]'
return Object.prototype.toString.call(v) === '[object Array]';
}

function regions() {
return [
"ap-northeast-1",
"ap-northeast-2",
"ap-south-1",
"ap-southeast-1",
"ap-southeast-2",
"ca-central-1",
"eu-central-1",
"eu-west-1",
"eu-west-2",
"eu-west-3",
"sa-east-1",
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2"
]
'ap-northeast-1',
'ap-northeast-2',
'ap-south-1',
'ap-southeast-1',
'ap-southeast-2',
'ca-central-1',
'eu-central-1',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'sa-east-1',
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2'
];
}

function containsRegion(v) {
return new RegExp(regions().join("|")).test(v);
return new RegExp(regions().join('|')).test(v);
}

function replaceChildNodes(dictionary, name) {
Expand All @@ -69,15 +73,15 @@ class ServerlessAWSPseudoParameters {
let value = dictionary[key];
// if a region name is mentioned, replace it with a reference (unless we are skipping automatic replacements)
if (typeof value === 'string' && !skipRegionReplace && containsRegion(value)) {
const regionFinder = new RegExp(regions().join("|"));
const regionFinder = new RegExp(regions().join('|'));
value = value.replace(regionFinder, '#{AWS::Region}');
}

var aws_regex;
if (allowReferences) {
aws_regex = /#{([^}]+)}/g;
} else {
aws_regex = /#{(AWS::[a-zA-Z]+)}/g
aws_regex = /#{(AWS::[a-zA-Z]+)}/g;
}

// we only want to possibly replace strings with an Fn::Sub
Expand All @@ -89,15 +93,18 @@ class ServerlessAWSPseudoParameters {
dictionary[key] = replacedString;
} else {
dictionary[key] = {
"Fn::Sub": replacedString
'Fn::Sub': replacedString
};
}

// do some fancy logging
let m = aws_regex.exec(value);
while (m) {
consoleLog('AWS Pseudo Parameter: ' + name + '::' + key + ' Replaced ' + yellow(m[1]) + ' with ' + yellow('${' + m[1] + '}'));
m = aws_regex.exec(value);
if (debug) {
// do some fancy logging
let m = aws_regex.exec(value);
while (m) {
consoleLog('AWS Pseudo Parameter: ' + name + '::' + key + ' Replaced ' + yellow(m[1]) + ' with ' + yellow(
'${' + m[1] + '}'));
m = aws_regex.exec(value);
}
}
}

Expand All @@ -111,11 +118,13 @@ class ServerlessAWSPseudoParameters {
}

function yellow(str) {
return '\u001B[33m' + str + '\u001B[39m';
if (colors) return '\u001B[33m' + str + '\u001B[39m';
return str;
}

function underline(str) {
return '\u001B[4m' + str + '\u001B[24m';
if (colors) return '\u001B[4m' + str + '\u001B[24m';
return str;
}
}
}
Expand Down
Loading

0 comments on commit 5d0ed1c

Please sign in to comment.