Skip to content

Commit

Permalink
Merge pull request #44 from vodlogic/support-escaped-tokens
Browse files Browse the repository at this point in the history
Support escaped tokens
  • Loading branch information
svdgraaf authored Oct 7, 2019
2 parents a864a7e + 2237d3a commit 774ad6a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,15 @@ custom:
pseudoParameters:
allowReferences: false
```

Escaping tokens
-----------------------------------
You can prevent tokens from being replaced by escaping with the `@` character after the token's hash character
```yaml
DynamoDBInputS3OutputHive:
Type: AWS::DataPipeline::Pipeline
Properties:
PipelineObjects:
- Key: "directoryPath"
StringValue: "#@{myOutputS3Loc}/#@{format(@scheduledStartTime, 'YYYY-MM-dd-HH-mm-ss')}"
```
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ class ServerlessAWSPseudoParameters {
}
}


var escaped_regex = /#@{([^}]+)}/g;
if (typeof value === 'string' && value.search(escaped_regex) >= 0) {
let replacedString = value.replace(escaped_regex, '#{$1}');
dictionary[key] = replacedString;
}

// dicts and arrays need to be looped through
if (isDict(value) || isArray(value)) {
dictionary[key] = replaceChildNodes(value, name + '::' + key);
Expand Down
11 changes: 8 additions & 3 deletions lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe('Plugin', () => {
Reference: "#{SomeResource}",
Substitution: {
"Fn::Sub": "#{SomeResource}"
},
Escaping: {
StringValue: "#@{myOutputS3Loc}/#@{format(@scheduledStartTime, 'YYYY-MM-dd-HH-mm-ss')}"
}
}
}
Expand All @@ -41,11 +44,11 @@ describe('Plugin', () => {
serverlessPseudoParamsPlugin.serverless.service.service = 'foo-service';
serverlessPseudoParamsPlugin.addParameters();
resultTemplate = serverlessPseudoParamsPlugin.serverless.service.provider.compiledCloudFormationTemplate;
expect(Object.keys(resultTemplate.Resources.acmeResource.Properties).length).toEqual(10);
expect(Object.keys(resultTemplate.Resources.acmeResource.Properties).length).toEqual(11);
});

it('replaces #{AWS::[VAR]} with the correct CF pseudo parameter', () => {
expect(Object.keys(resultTemplate.Resources.acmeResource.Properties).length).toEqual(10);
expect(Object.keys(resultTemplate.Resources.acmeResource.Properties).length).toEqual(11);
});

it('replaces #{AWS::AccountId} with the ${AWS::AccountId} pseudo parameter', () => {
Expand Down Expand Up @@ -78,7 +81,9 @@ describe('Plugin', () => {
it('should not add Fn::Sub to items with Fn::Sub already', () => {
expect(resultTemplate.Resources.acmeResource.Properties.Substitution).toEqual({ 'Fn::Sub': '${SomeResource}'});
});

it('should not replace escaped items', () => {
expect(resultTemplate.Resources.acmeResource.Properties.Escaping).toEqual({ 'StringValue': '#{myOutputS3Loc}/#{format(@scheduledStartTime, \'YYYY-MM-dd-HH-mm-ss\')}'});
});
});

describe('Using pseudo parameters with allowReferences', () => {
Expand Down

0 comments on commit 774ad6a

Please sign in to comment.