Skip to content

Commit

Permalink
Merge pull request serverless-operations#183 from theburningmonk/feat…
Browse files Browse the repository at this point in the history
…ure/support_tagging

support global tags
  • Loading branch information
horike37 committed Mar 26, 2019
2 parents 99d63b5 + 45b8a77 commit b619b91
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/deploy/stepFunctions/compileStateMachines.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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') {
Expand Down Expand Up @@ -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,
Expand Down
73 changes: 73 additions & 0 deletions lib/deploy/stepFunctions/compileStateMachines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit b619b91

Please sign in to comment.