diff --git a/lib/deploy/stepFunctions/compileStateMachines.js b/lib/deploy/stepFunctions/compileStateMachines.js index 53b769ea..f9b2a384 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.js +++ b/lib/deploy/stepFunctions/compileStateMachines.js @@ -7,6 +7,25 @@ function isIntrinsic(obj) { return isObject && Object.keys(obj).some((k) => k.startsWith('Fn::') || k.startsWith('Ref')); } +function toTags(obj, serverless) { + const tags = []; + + if (!obj) { + return tags; + } + + if (_.isPlainObject(obj)) { + _.forEach( + obj, + (Value, Key) => tags.push({ Key, Value: Value.toString() })); + } else { + throw new serverless.classes + .Error('Unable to parse tags, it should be an object.'); + } + + return tags; +} + module.exports = { isIntrinsic, compileStateMachines() { @@ -16,7 +35,7 @@ module.exports = { let DefinitionString; let RoleArn; let DependsOn = []; - const Tags = []; + const Tags = toTags(this.serverless.service.provider.tags, this.serverless); if (stateMachineObj.definition) { if (typeof stateMachineObj.definition === 'string') { @@ -85,14 +104,8 @@ module.exports = { } if (stateMachineObj.tags) { - if (_.isPlainObject(stateMachineObj.tags)) { - _.forEach( - stateMachineObj.tags, - (Value, Key) => Tags.push({ Key, Value: Value.toString() })); - } else { - throw new this.serverless.classes - .Error('Unable to parse tags, it should be an object.'); - } + const stateMachineTags = toTags(stateMachineObj.tags, this.serverless); + _.forEach(stateMachineTags, tag => Tags.push(tag)); } const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, diff --git a/lib/deploy/stepFunctions/compileStateMachines.test.js b/lib/deploy/stepFunctions/compileStateMachines.test.js index 85cc75d9..e1f79d1b 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.test.js +++ b/lib/deploy/stepFunctions/compileStateMachines.test.js @@ -521,6 +521,79 @@ describe('#compileStateMachines', () => { .to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]); }); + it('should add global tags', () => { + serverless.service.provider.tags = { + team: 'core', + score: 42, + }; + + serverless.service.stepFunctions = { + stateMachines: { + myStateMachine1: { + definition: 'definition1', + name: 'stateMachineBeta1', + }, + myStateMachine2: { + definition: 'definition2', + name: 'stateMachineBeta2', + }, + }, + }; + + serverlessStepFunctions.compileStateMachines(); + const stateMachineBeta1 = serverlessStepFunctions.serverless.service + .provider.compiledCloudFormationTemplate.Resources + .StateMachineBeta1; + const stateMachineBeta2 = serverlessStepFunctions.serverless.service + .provider.compiledCloudFormationTemplate.Resources + .StateMachineBeta2; + expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2); + expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2); + expect(stateMachineBeta1.Properties.Tags) + .to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]); + expect(stateMachineBeta2.Properties.Tags) + .to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]); + }); + + it('should merge global and state machine tags', () => { + serverless.service.provider.tags = { + team: 'core', + }; + + serverless.service.stepFunctions = { + stateMachines: { + myStateMachine1: { + definition: 'definition1', + name: 'stateMachineBeta1', + tags: { + score: 42, + }, + }, + myStateMachine2: { + definition: 'definition2', + name: 'stateMachineBeta2', + tags: { + score: 42, + }, + }, + }, + }; + + serverlessStepFunctions.compileStateMachines(); + const stateMachineBeta1 = serverlessStepFunctions.serverless.service + .provider.compiledCloudFormationTemplate.Resources + .StateMachineBeta1; + const stateMachineBeta2 = serverlessStepFunctions.serverless.service + .provider.compiledCloudFormationTemplate.Resources + .StateMachineBeta2; + expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(2); + expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2); + expect(stateMachineBeta1.Properties.Tags) + .to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]); + expect(stateMachineBeta2.Properties.Tags) + .to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]); + }); + it('should throw error when tags property contains malformed tags', () => { serverless.service.stepFunctions = { stateMachines: {