Skip to content

Support custom logicalId separate from name #124

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 1 commit into from
Jul 9, 2018
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ plugins:
- serverless-step-functions
```

### Adding a custom logical id for a stateMachine
You can use a custom logical id that is only unique within the stack as opposed to the name that needs to be unique globally. This can make referencing the state machine easier/simpler because you don't have to duplicate the interpolation logic everywhere you reference the state machine.

```yml
service: messager

functions:
sendMessage:
handler: handler.sendMessage

stepFunctions:
stateMachines:
sendMessageFunc:
id: SendMessageStateMachine
name: sendMessageFunc-${self:custom.service}-${opt:stage}
definition:
<your definition>

plugins:
- serverless-step-functions
```

You can then `Ref: SendMessageStateMachine` in various parts of CloudFormation or serverless.yml

#### Current Gotcha
Please keep this gotcha in mind if you want to reference the `name` from the `resources` section. To generate Logical ID for CloudFormation, the plugin transforms the specified name in serverless.yml based on the following scheme.

Expand Down
21 changes: 14 additions & 7 deletions lib/deploy/events/apiGateway/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
};

_.merge(template,
this.getMethodIntegration(event.stateMachineName, stateMachineObj.name, event.http),
this.getMethodIntegration(event.stateMachineName, stateMachineObj, event.http),
this.getMethodResponses(event.http)
);

Expand All @@ -42,7 +42,7 @@ module.exports = {
return BbPromise.resolve();
},

getMethodIntegration(stateMachineName, customName, http) {
getMethodIntegration(stateMachineName, stateMachineObj, http) {
const apiToStepFunctionsIamRoleLogicalId = this.getApiToStepFunctionsIamRoleLogicalId();
const integration = {
IntegrationHttpMethod: 'POST',
Expand All @@ -66,7 +66,11 @@ module.exports = {
],
},
PassthroughBehavior: 'NEVER',
RequestTemplates: this.getIntegrationRequestTemplates(stateMachineName, customName, http),
RequestTemplates: this.getIntegrationRequestTemplates(
stateMachineName,
stateMachineObj,
http
),
};

const integrationResponse = {
Expand Down Expand Up @@ -108,16 +112,19 @@ module.exports = {
};
},

getIntegrationRequestTemplates(stateMachineName, customName, http) {
const defaultRequestTemplates = this.getDefaultRequestTemplates(stateMachineName, customName);
getIntegrationRequestTemplates(stateMachineName, stateMachineObj, http) {
const defaultRequestTemplates = this.getDefaultRequestTemplates(
stateMachineName,
stateMachineObj
);
return Object.assign(
defaultRequestTemplates,
_.get(http, ['request', 'template'])
);
},

getDefaultRequestTemplates(stateMachineName, customName) {
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, customName);
getDefaultRequestTemplates(stateMachineName, stateMachineObj) {
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, stateMachineObj);
return {
'application/json': this.buildDefaultRequestTemplate(stateMachineLogicalId),
'application/x-www-form-urlencoded': this.buildDefaultRequestTemplate(stateMachineLogicalId),
Expand Down
15 changes: 14 additions & 1 deletion lib/deploy/events/apiGateway/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ describe('#methods()', () => {
.to.have.property('Integration');
});

it('should set stateMachinelogical ID to RequestTemplates when customName is not set', () => {
expect(serverlessStepFunctions.getMethodIntegration('stateMachine').Properties
.Integration.RequestTemplates['application/json']['Fn::Join'][1][2].Ref)
.to.be.equal('StateMachineStepFunctionsStateMachine');
});

it('should set custom stateMachinelogical ID to RequestTemplates when customName is set',
() => {
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', { name: 'custom' })
.Properties.Integration.RequestTemplates['application/json']['Fn::Join'][1][2].Ref)
.to.be.equal('Custom');
});

it('should set Access-Control-Allow-Origin header when cors is true',
() => {
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', 'custom', {
Expand Down Expand Up @@ -113,7 +126,7 @@ describe('#methods()', () => {
it('should set custom stateMachinelogical ID in default templates when customName is set',
() => {
const requestTemplates = serverlessStepFunctions
.getIntegrationRequestTemplates('stateMachine', 'custom');
.getIntegrationRequestTemplates('stateMachine', { name: 'custom' });
expect(requestTemplates['application/json']['Fn::Join'][1][2].Ref)
.to.be.equal('Custom');
});
Expand Down
2 changes: 1 addition & 1 deletion lib/deploy/events/schedule/compileScheduledEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = {
}

const stateMachineLogicalId = this
.getStateMachineLogicalId(stateMachineName, stateMachineObj.name);
.getStateMachineLogicalId(stateMachineName, stateMachineObj);
const scheduleLogicalId = this
.getScheduleLogicalId(stateMachineName, scheduleNumberInFunction);
const scheduleIamRoleLogicalId = this
Expand Down
15 changes: 6 additions & 9 deletions lib/deploy/stepFunctions/compileStateMachines.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ module.exports = {
let DefinitionString;
let RoleArn;
let DependsOn;
let Name;

if (stateMachineObj.name) {
Name = stateMachineObj.name;
}

if (stateMachineObj.definition) {
DefinitionString = JSON.stringify(stateMachineObj.definition);
Expand Down Expand Up @@ -52,9 +47,10 @@ module.exports = {
DependsOn = 'IamRoleStateMachineExecution';
}

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

const stateMachineTemplate = `
{
Expand All @@ -72,8 +68,9 @@ module.exports = {
[stateMachineLogicalId]: JSON.parse(stateMachineTemplate),
};

if (Name) {
newStateMachineObject[stateMachineLogicalId].Properties.StateMachineName = Name;
if (stateMachineObj.name) {
newStateMachineObject[stateMachineLogicalId].Properties.StateMachineName
= stateMachineObj.name;
}

_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
Expand Down
12 changes: 3 additions & 9 deletions lib/invoke/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ const path = require('path');

module.exports = {
getStateMachineArn() {
let stateMachineOutputKey;
if (this.serverless.service.stepFunctions.stateMachines[this.options.name].name) {
stateMachineOutputKey =
this.getStateMachineOutputLogicalId(this.options.name
, this.serverless.service.stepFunctions.stateMachines[this.options.name].name);
} else {
stateMachineOutputKey =
this.getStateMachineOutputLogicalId(this.options.name);
}
const stateMachineOutputKey =
this.getStateMachineOutputLogicalId(this.options.name,
this.serverless.service.stepFunctions.stateMachines[this.options.name]);

const stackName = this.provider.naming.getStackName(this.options.stage);

Expand Down
18 changes: 12 additions & 6 deletions lib/naming.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
'use strict';

module.exports = {
getStateMachineLogicalId(stateMachineName, customName) {
if (customName) {
return `${this.provider.naming.getNormalizedFunctionName(customName)}`;
getStateMachineLogicalId(stateMachineName, stateMachine) {
const custom = stateMachine ? stateMachine.id || stateMachine.name : null;

if (custom) {
return `${this.provider.naming.getNormalizedFunctionName(custom)}`;
}

return `${this.provider.naming
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachine`;
},

getStateMachineOutputLogicalId(stateMachineName, customName) {
if (customName) {
return `${this.provider.naming.getNormalizedFunctionName(customName)}Arn`;
getStateMachineOutputLogicalId(stateMachineName, stateMachine) {
const custom = stateMachine ? stateMachine.id || stateMachine.name : null;

if (custom) {
return `${this.provider.naming.getNormalizedFunctionName(custom)}Arn`;
}

return `${this.provider.naming
.getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachineArn`;
},
Expand Down
38 changes: 35 additions & 3 deletions lib/naming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,50 @@ describe('#naming', () => {

describe('#getStateMachineLogicalId() -- Named', () => {
it('should normalize the stateMachine name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineLogicalId('stateMachine', 'alphaNumeric')).to
.equal('AlphaNumeric');
expect(serverlessStepFunctions.getStateMachineLogicalId('stateMachine',
{ name: 'alphaNumeric' }))
.to.equal('AlphaNumeric');
});
});

describe('#getStateMachineOutputLogicalId() -- Named', () => {
it('should normalize the stateMachine output name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineOutputLogicalId('stateMachine', 'alphaNumeric'))
expect(serverlessStepFunctions.getStateMachineOutputLogicalId('stateMachine',
{ name: 'alphaNumeric' }))
.to.equal('AlphaNumericArn');
});
});

describe('#getStateMachineLogicalId() -- With Id', () => {
it('should normalize the stateMachine name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineLogicalId('stateMachine',
{ id: 'foo', name: 'alphaNumeric' }))
.to.equal('Foo');
});
});

describe('#getStateMachineOutputLogicalId() -- With Id', () => {
it('should normalize the stateMachine output name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineOutputLogicalId('stateMachine',
{ id: 'foo', name: 'alphaNumeric' }))
.to.equal('FooArn');
});
});

describe('#getStateMachineLogicalId() -- With Only Id', () => {
it('should normalize the stateMachine name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineLogicalId('stateMachine', { id: 'foo' })).to
.equal('Foo');
});
});

describe('#getStateMachineOutputLogicalId() -- With Only Id', () => {
it('should normalize the stateMachine output name and add the standard suffix', () => {
expect(serverlessStepFunctions.getStateMachineOutputLogicalId('stateMachine', { id: 'foo' }))
.to.equal('FooArn');
});
});

describe('#getActivityLogicalId()', () => {
it('should normalize the activity name and add the standard suffix', () => {
expect(serverlessStepFunctions.getActivityLogicalId('activity')).to
Expand Down