Skip to content

Commit 35fce72

Browse files
authored
Merge pull request #183 from theburningmonk/feature/support_tagging
support global tags
2 parents 0f8fb73 + bd65732 commit 35fce72

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ function isIntrinsic(obj) {
77
return isObject && Object.keys(obj).some((k) => k.startsWith('Fn::') || k.startsWith('Ref'));
88
}
99

10+
function toTags(obj, serverless) {
11+
const tags = [];
12+
13+
if (!obj) {
14+
return tags;
15+
}
16+
17+
if (_.isPlainObject(obj)) {
18+
_.forEach(
19+
obj,
20+
(Value, Key) => tags.push({ Key, Value: Value.toString() }));
21+
} else {
22+
throw new serverless.classes
23+
.Error('Unable to parse tags, it should be an object.');
24+
}
25+
26+
return tags;
27+
}
28+
1029
module.exports = {
1130
isIntrinsic,
1231
compileStateMachines() {
@@ -16,7 +35,7 @@ module.exports = {
1635
let DefinitionString;
1736
let RoleArn;
1837
let DependsOn = [];
19-
const Tags = [];
38+
const Tags = toTags(this.serverless.service.provider.tags, this.serverless);
2039

2140
if (stateMachineObj.definition) {
2241
if (typeof stateMachineObj.definition === 'string') {
@@ -85,14 +104,8 @@ module.exports = {
85104
}
86105

87106
if (stateMachineObj.tags) {
88-
if (_.isPlainObject(stateMachineObj.tags)) {
89-
_.forEach(
90-
stateMachineObj.tags,
91-
(Value, Key) => Tags.push({ Key, Value: Value.toString() }));
92-
} else {
93-
throw new this.serverless.classes
94-
.Error('Unable to parse tags, it should be an object.');
95-
}
107+
const stateMachineTags = toTags(stateMachineObj.tags, this.serverless);
108+
_.forEach(stateMachineTags, tag => Tags.push(tag));
96109
}
97110

98111
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName,

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,79 @@ describe('#compileStateMachines', () => {
521521
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
522522
});
523523

524+
it('should add global tags', () => {
525+
serverless.service.provider.tags = {
526+
team: 'core',
527+
score: 42,
528+
};
529+
530+
serverless.service.stepFunctions = {
531+
stateMachines: {
532+
myStateMachine1: {
533+
definition: 'definition1',
534+
name: 'stateMachineBeta1',
535+
},
536+
myStateMachine2: {
537+
definition: 'definition2',
538+
name: 'stateMachineBeta2',
539+
},
540+
},
541+
};
542+
543+
serverlessStepFunctions.compileStateMachines();
544+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
545+
.provider.compiledCloudFormationTemplate.Resources
546+
.StateMachineBeta1;
547+
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
548+
.provider.compiledCloudFormationTemplate.Resources
549+
.StateMachineBeta2;
550+
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2);
551+
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
552+
expect(stateMachineBeta1.Properties.Tags)
553+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
554+
expect(stateMachineBeta2.Properties.Tags)
555+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
556+
});
557+
558+
it('should merge global and state machine tags', () => {
559+
serverless.service.provider.tags = {
560+
team: 'core',
561+
};
562+
563+
serverless.service.stepFunctions = {
564+
stateMachines: {
565+
myStateMachine1: {
566+
definition: 'definition1',
567+
name: 'stateMachineBeta1',
568+
tags: {
569+
score: 42,
570+
},
571+
},
572+
myStateMachine2: {
573+
definition: 'definition2',
574+
name: 'stateMachineBeta2',
575+
tags: {
576+
score: 42,
577+
},
578+
},
579+
},
580+
};
581+
582+
serverlessStepFunctions.compileStateMachines();
583+
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
584+
.provider.compiledCloudFormationTemplate.Resources
585+
.StateMachineBeta1;
586+
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
587+
.provider.compiledCloudFormationTemplate.Resources
588+
.StateMachineBeta2;
589+
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2);
590+
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
591+
expect(stateMachineBeta1.Properties.Tags)
592+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
593+
expect(stateMachineBeta2.Properties.Tags)
594+
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
595+
});
596+
524597
it('should throw error when tags property contains malformed tags', () => {
525598
serverless.service.stepFunctions = {
526599
stateMachines: {

0 commit comments

Comments
 (0)