Skip to content

Commit 71a498b

Browse files
author
Hazel Court
authored
Add top-level Rules support to merge, and rule-specific intrinsic functions (#120)
* Add Rules support to merge * Fixture update * rule-specific intrinsic functions
1 parent 2ad8c61 commit 71a498b

File tree

57 files changed

+211
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+211
-20
lines changed

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const intrinsic = require('./lib/intrinsic');
44
const conditions = require('./lib/conditions');
5+
const rules = require('./lib/rules');
56
const pseudo = require('./lib/pseudo');
67
const build = require('./lib/build');
78
const validate = require('./lib/validate');
@@ -29,6 +30,10 @@ Object.keys(conditions).forEach((key) => {
2930
cloudfriend[key] = conditions[key];
3031
});
3132

33+
Object.keys(rules).forEach((key) => {
34+
cloudfriend[key] = rules[key];
35+
});
36+
3237
Object.keys(pseudo).forEach((key) => {
3338
cloudfriend[key] = pseudo[key];
3439
});

lib/merge.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const assert = require('assert');
1111
* @name merge
1212
* @param {...object} template - a CloudFormation template to merge with
1313
* @returns {object} a CloudFormation template including all the Metadata,
14-
* Parameters, Mappings, Conditions, Resources, and Outputs from the input
15-
* templates
14+
* Parameters, Rules, Mappings, Conditions, Resources, and Outputs from the
15+
* input templates
1616
* @throws errors when there is overlap in logical resource names between
1717
* templates
1818
*/
@@ -23,6 +23,7 @@ module.exports = function() {
2323
AWSTemplateFormatVersion: '2010-09-09',
2424
Metadata: {},
2525
Parameters: {},
26+
Rules: {},
2627
Mappings: {},
2728
Conditions: {},
2829
Resources: {},
@@ -34,6 +35,7 @@ module.exports = function() {
3435
const names = {
3536
Metadata: new Set(),
3637
Parameters: new Set(),
38+
Rules: new Set(),
3739
Mappings: new Set(),
3840
Conditions: new Set(),
3941
Resources: new Set(),
@@ -62,6 +64,16 @@ module.exports = function() {
6264
names.Parameters.add(key);
6365
});
6466

67+
if (arg.Rules) Object.keys(arg.Rules).forEach((key) => {
68+
if (names.Rules.has(key)) {
69+
try { assert.deepEqual(template.Rules[key], arg.Rules[key]); }
70+
catch (err) { throw new Error('Rules name used more than once: ' + key); }
71+
}
72+
73+
template.Rules[key] = arg.Rules[key];
74+
names.Rules.add(key);
75+
});
76+
6577
if (arg.Mappings) Object.keys(arg.Mappings).forEach((key) => {
6678
if (names.Mappings.has(key)) {
6779
try { assert.deepEqual(template.Mappings[key], arg.Mappings[key]); }

lib/rules.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
const rules = module.exports = {};
4+
5+
/**
6+
* [The rule function
7+
* Fn::Contains](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-contains)
8+
* returns true if a specified string matches at least one value in a list of
9+
* strings.
10+
*
11+
* @static
12+
* @memberof cloudfriend
13+
* @name contains
14+
* @param {array} strings
15+
* @param {string} s
16+
*/
17+
rules.contains = (strings, s) => {
18+
return { 'Fn::Contains': [strings, s] };
19+
};
20+
21+
/**
22+
* [The rule function
23+
* Fn::EachMemberEquals](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-eachmemberequals)
24+
* returns true if a specified string matches all values in a list.
25+
*
26+
* @static
27+
* @memberof cloudfriend
28+
* @name eachMemberEquals
29+
* @param {array} strings
30+
* @param {string} s
31+
*/
32+
rules.eachMemberEquals = (strings, s) => {
33+
return { 'Fn::EachMemberEquals': [strings, s] };
34+
};
35+
36+
/**
37+
* [The rule function
38+
* Fn::EachMemberIn](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-eachmemberin)
39+
* returns true if each member in a list of strings matches at least one value
40+
* in a second list of strings.
41+
*
42+
* @static
43+
* @memberof cloudfriend
44+
* @name eachMemberIn
45+
* @param {array} stringsToCheck
46+
* @param {array} stringsToMatch
47+
*/
48+
rules.eachMemberIn = (stringsToCheck, stringsToMatch) => {
49+
return { 'Fn::EachMemberIn': [stringsToCheck, stringsToMatch] };
50+
};
51+
52+
/**
53+
* [The rule function Fn::RefAll](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-refall) returns all values for a specified parameter type.
54+
*
55+
* @static
56+
* @memberof cloudfriend
57+
* @name refAll
58+
* @param {string} parameterType An AWS-specific parameter type, such as AWS::EC2::SecurityGroup::Id or AWS::EC2::VPC::Id.
59+
*/
60+
rules.refAll = (parameterType) => {
61+
return { 'Fn::RefAll': parameterType };
62+
};
63+
64+
/**
65+
* [The rule function Fn::ValueOf](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-valueof) returns an attribute value or list of values for a specific parameter and attribute.
66+
*
67+
* @static
68+
* @memberof cloudfriend
69+
* @name valueOf
70+
* @param {string} parameterLogicalId The name of a parameter for which you want to retrieve attribute values. The parameter must be declared in the Parameters section of the template.
71+
* @param {*} attribute The name of an attribute from which you want to retrieve a value. For more information about attributes, see [Supported Attributes](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#rules-parameter-attributes).
72+
*/
73+
rules.valueOf = (parameterLogicalId, attribute) => {
74+
return { 'Fn::ValueOf': [parameterLogicalId, attribute] };
75+
};
76+
77+
/**
78+
* [The rule function Fn::ValueOfAll](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-valueofall) returns a list of all attribute values for a given parameter type and attribute.
79+
*
80+
* @static
81+
* @memberof cloudfriend
82+
* @name valueOfAll
83+
* @param {string} parameterType An AWS-specific parameter type, such as AWS::EC2::SecurityGroup::Id or AWS::EC2::VPC::Id.
84+
* @param {string} attribute The name of an attribute from which you want to retrieve a value. For more information about attributes, see [Supported Attributes](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#rules-parameter-attributes).
85+
*/
86+
rules.valueOfAll = (parameterType, attribute) => {
87+
return { 'Fn::ValueOfAll': [parameterType, attribute] };
88+
};

readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ sub(str, variables) | [Fn::Sub](http://docs.aws.amazon.com/AWSCloudFormation/lat
3434
importValue(sharedValue) | [Fn::ImportValue](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html),
3535
arn(service, suffix) | [Fn::Sub](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html) designed for an ARN
3636
transform(name, parameters) | [Fn::Transform](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-transform.html)
37+
contains(strings, s) | [Fn::Contains](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-contains)
38+
eachMemberEquals(strings, s) | [Fn::EachMemberEquals](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-eachmemberequals)
39+
eachMemberIn(stringsToCheck, stringsToMatch) | [Fn::EachMemberIn](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-eachmemberin)
40+
refAll(parameterType) | [Fn::RefAll](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-refall)
41+
valueOf(parameterLogicalId, attribute) | [Fn::ValueOf](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-valueof)
42+
valueOfAll(parameterType, attribute) | [Fn::ValueOfAll](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-rules.html#fn-valueofall)
43+
3744

3845
## Pseudo parameters
3946

test/fixtures/shortcuts/cross-account-role-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/cross-account-role-no-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/event-lambda-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/event-lambda-full.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/firehose-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/firehose-with-stream.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/glue-database-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/glue-database-no-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/glue-json-table-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/glue-json-table-no-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/glue-orc-table-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/glue-orc-table-no-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/glue-parquet-table-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/glue-parquet-table-no-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/glue-table-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/glue-table-no-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/glue-view-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {

test/fixtures/shortcuts/glue-view-no-defaults.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {
78
"Always": {

test/fixtures/shortcuts/hookshot-github-secret-ref.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"Type": "String"
77
}
88
},
9+
"Rules": {},
910
"Mappings": {},
1011
"Conditions": {},
1112
"Resources": {
@@ -27,7 +28,7 @@
2728
"Type": "AWS::ApiGateway::Stage",
2829
"Properties": {
2930
"DeploymentId": {
30-
"Ref": "PassDeployment636bae51"
31+
"Ref": "PassDeployment15ce21d4"
3132
},
3233
"StageName": "hookshot",
3334
"RestApiId": {
@@ -46,7 +47,7 @@
4647
]
4748
}
4849
},
49-
"PassDeployment636bae51": {
50+
"PassDeployment15ce21d4": {
5051
"Type": "AWS::ApiGateway::Deployment",
5152
"DependsOn": "PassMethod",
5253
"Properties": {

test/fixtures/shortcuts/hookshot-github-secret-string.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {
@@ -23,7 +24,7 @@
2324
"Type": "AWS::ApiGateway::Stage",
2425
"Properties": {
2526
"DeploymentId": {
26-
"Ref": "PassDeployment636bae51"
27+
"Ref": "PassDeployment15ce21d4"
2728
},
2829
"StageName": "hookshot",
2930
"RestApiId": {
@@ -42,7 +43,7 @@
4243
]
4344
}
4445
},
45-
"PassDeployment636bae51": {
46+
"PassDeployment15ce21d4": {
4647
"Type": "AWS::ApiGateway::Deployment",
4748
"DependsOn": "PassMethod",
4849
"Properties": {

test/fixtures/shortcuts/hookshot-github.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Metadata": {},
44
"Parameters": {},
5+
"Rules": {},
56
"Mappings": {},
67
"Conditions": {},
78
"Resources": {
@@ -23,7 +24,7 @@
2324
"Type": "AWS::ApiGateway::Stage",
2425
"Properties": {
2526
"DeploymentId": {
26-
"Ref": "PassDeployment636bae51"
27+
"Ref": "PassDeployment15ce21d4"
2728
},
2829
"StageName": "hookshot",
2930
"RestApiId": {
@@ -42,7 +43,7 @@
4243
]
4344
}
4445
},
45-
"PassDeployment636bae51": {
46+
"PassDeployment15ce21d4": {
4647
"Type": "AWS::ApiGateway::Deployment",
4748
"DependsOn": "PassMethod",
4849
"Properties": {

0 commit comments

Comments
 (0)