Skip to content

fix: #287 don't include Tags property if no tags provided #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,25 @@ stepFunctions:

You can specify tags on each state machine. Additionally any global tags (specified under `provider` section in your `serverless.yml`) would be merged in as well.

If you _don't_ want for global tags to be merged into your state machine, you can include the `inheritGlobalTags` property for your state machine.

```yaml
provider:
tags:
app: myApp
department: engineering
stepFunctions:
stateMachines:
hellostepfunc1:
name: myStateMachine
inheritGlobalTags: false
tags:
score: 42
definition: something
```

As a result, `hellostepfunc1` will only have the tag of `score: 42`, and _not_ the tags at the provider level

## Commands

### deploy
Expand Down
12 changes: 10 additions & 2 deletions lib/deploy/stepFunctions/compileStateMachines.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ module.exports = {
let RoleArn;
let DependsOn = [];
let LoggingConfiguration;
const Tags = toTags(this.serverless.service.provider.tags);
let Tags;
if (stateMachineObj.inheritGlobalTags === false) {
Tags = [];
} else {
Tags = toTags(this.serverless.service.provider.tags);
}

const { error, value } = Joi.validate(stateMachineObj, schema, { allowUnknown: false });
if (error) {
Expand Down Expand Up @@ -202,17 +207,20 @@ module.exports = {

const stateMachineOutputLogicalId = this
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);

const stateMachineTemplate = {
Type: 'AWS::StepFunctions::StateMachine',
Properties: {
DefinitionString,
RoleArn,
Tags,
StateMachineType: stateMachineObj.type,
LoggingConfiguration,
},
DependsOn,
};
if (Tags.length > 0) {
stateMachineTemplate.Properties.Tags = Tags;
}

const newStateMachineObject = {
[stateMachineLogicalId]: stateMachineTemplate,
Expand Down
3 changes: 3 additions & 0 deletions lib/deploy/stepFunctions/compileStateMachines.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const definition = Joi.alternatives().try(
Joi.object(),
);

const inheritGlobalTags = Joi.boolean();

const dependsOn = Joi.alternatives().try(
Joi.string(),
Joi.array().items(Joi.string()),
Expand Down Expand Up @@ -54,6 +56,7 @@ const schema = Joi.object().keys({
notifications,
type,
loggingConfig,
inheritGlobalTags,
});

module.exports = schema;
70 changes: 70 additions & 0 deletions lib/deploy/stepFunctions/compileStateMachines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,76 @@ describe('#compileStateMachines', () => {
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
});

it('should not add tags property to state machine if none are provided', () => {
serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
definition: 'definition1',
name: 'stateMachineBeta1',
},
},
};
serverlessStepFunctions.compileStateMachines();
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta1;
expect(stateMachineBeta1.Properties).to.not.have.property('Tags');
});

it('should not inherit global tags if inheritGlobalTags is set to false', () => {
serverless.service.provider.tags = {
team: 'core',
};

serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
definition: 'definition1',
name: 'stateMachineBeta1',
inheritGlobalTags: false,
tags: {
score: 42,
},
},
myStateMachine2: {
definition: 'definition2',
name: 'stateMachineBeta2',
tags: {
score: 42,
},
},
myStateMachine3: {
definition: 'definition3',
name: 'stateMachineBeta3',
inheritGlobalTags: true,
tags: {
question: 'Meaning of life',
answer: 42,
},
},
},
};
serverlessStepFunctions.compileStateMachines();
const stateMachineBeta1 = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta1;
const stateMachineBeta2 = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta2;
const stateMachineBeta3 = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources
.StateMachineBeta3;
expect(stateMachineBeta1.Properties.Tags).to.have.lengthOf(1);
expect(stateMachineBeta2.Properties.Tags).to.have.lengthOf(2);
expect(stateMachineBeta3.Properties.Tags).to.have.lengthOf(3);
expect(stateMachineBeta1.Properties.Tags)
.to.deep.eq([{ Key: 'score', Value: '42' }]);
expect(stateMachineBeta2.Properties.Tags)
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'score', Value: '42' }]);
expect(stateMachineBeta3.Properties.Tags)
.to.deep.eq([{ Key: 'team', Value: 'core' }, { Key: 'question', Value: 'Meaning of life' }, { Key: 'answer', Value: '42' }]);
});

it('should throw error when tags property contains malformed tags', () => {
serverless.service.stepFunctions = {
stateMachines: {
Expand Down