serverless-step-functions
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -28,3 +28,3 @@ 'use strict'; | ||
_.merge(template, | ||
this.getMethodIntegration(event.stateMachineName, stateMachineObj.name, event.http), | ||
this.getMethodIntegration(event.stateMachineName, stateMachineObj, event.http), | ||
this.getMethodResponses(event.http) | ||
@@ -46,4 +46,3 @@ ); | ||
getMethodIntegration(stateMachineName, customName, http) { | ||
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, customName); | ||
getMethodIntegration(stateMachineName, stateMachineObj, http) { | ||
const apiToStepFunctionsIamRoleLogicalId = this.getApiToStepFunctionsIamRoleLogicalId(); | ||
@@ -72,28 +71,7 @@ const integration = { | ||
PassthroughBehavior: 'NEVER', | ||
RequestTemplates: { | ||
'application/json': { | ||
'Fn::Join': [ | ||
'', [ | ||
"#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n", | ||
'{"input": "$body","name": "$context.requestId","stateMachineArn":"', | ||
{ | ||
Ref: `${stateMachineLogicalId}`, | ||
}, | ||
'"}', | ||
], | ||
], | ||
}, | ||
'application/x-www-form-urlencoded': { | ||
'Fn::Join': [ | ||
'', [ | ||
"#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n", | ||
'{"input": "$body","name": "$context.requestId","stateMachineArn":"', | ||
{ | ||
Ref: `${stateMachineLogicalId}`, | ||
}, | ||
'"}', | ||
], | ||
], | ||
}, | ||
}, | ||
RequestTemplates: this.getIntegrationRequestTemplates( | ||
stateMachineName, | ||
stateMachineObj, | ||
http | ||
), | ||
}; | ||
@@ -140,2 +118,36 @@ | ||
getIntegrationRequestTemplates(stateMachineName, stateMachineObj, http) { | ||
const defaultRequestTemplates = this.getDefaultRequestTemplates( | ||
stateMachineName, | ||
stateMachineObj | ||
); | ||
return Object.assign( | ||
defaultRequestTemplates, | ||
_.get(http, ['request', 'template']) | ||
); | ||
}, | ||
getDefaultRequestTemplates(stateMachineName, stateMachineObj) { | ||
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, stateMachineObj); | ||
return { | ||
'application/json': this.buildDefaultRequestTemplate(stateMachineLogicalId), | ||
'application/x-www-form-urlencoded': this.buildDefaultRequestTemplate(stateMachineLogicalId), | ||
}; | ||
}, | ||
buildDefaultRequestTemplate(stateMachineLogicalId) { | ||
return { | ||
'Fn::Join': [ | ||
'', [ | ||
"#set( $body = $util.escapeJavaScript($input.json('$')) ) \n\n", | ||
'{"input": "$body","name": "$context.requestId","stateMachineArn":"', | ||
{ | ||
Ref: `${stateMachineLogicalId}`, | ||
}, | ||
'"}', | ||
], | ||
], | ||
}; | ||
}, | ||
getMethodResponses(http) { | ||
@@ -142,0 +154,0 @@ const methodResponse = { |
@@ -93,4 +93,4 @@ 'use strict'; | ||
() => { | ||
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', 'custom').Properties | ||
.Integration.RequestTemplates['application/json']['Fn::Join'][1][2].Ref) | ||
expect(serverlessStepFunctions.getMethodIntegration('stateMachine', { name: 'custom' }) | ||
.Properties.Integration.RequestTemplates['application/json']['Fn::Join'][1][2].Ref) | ||
.to.be.equal('Custom'); | ||
@@ -119,2 +119,89 @@ }); | ||
describe('#getIntegrationRequestTemplates()', () => { | ||
it('should set stateMachinelogical ID in default templates when customName is not set', () => { | ||
const requestTemplates = serverlessStepFunctions | ||
.getIntegrationRequestTemplates('stateMachine'); | ||
expect(requestTemplates['application/json']['Fn::Join'][1][2].Ref) | ||
.to.be.equal('StateMachineStepFunctionsStateMachine'); | ||
}); | ||
it('should set custom stateMachinelogical ID in default templates when customName is set', | ||
() => { | ||
const requestTemplates = serverlessStepFunctions | ||
.getIntegrationRequestTemplates('stateMachine', { name: 'custom' }); | ||
expect(requestTemplates['application/json']['Fn::Join'][1][2].Ref) | ||
.to.be.equal('Custom'); | ||
}); | ||
it('should return the default template for application/json when one is not given', () => { | ||
const httpWithoutRequestTemplate = { | ||
path: 'foo/bar1', | ||
method: 'post', | ||
request: { | ||
template: { | ||
'application/x-www-form-urlencoded': 'custom template', | ||
}, | ||
}, | ||
}; | ||
const requestTemplates = serverlessStepFunctions | ||
.getMethodIntegration('stateMachine', undefined, httpWithoutRequestTemplate) | ||
.Properties.Integration.RequestTemplates; | ||
expect(requestTemplates['application/json']['Fn::Join'][1][2].Ref) | ||
.to.be.equal('StateMachineStepFunctionsStateMachine'); | ||
}); | ||
it('should return a custom template for application/json when one is given', () => { | ||
const httpWithRequestTemplate = { | ||
path: 'foo/bar1', | ||
method: 'post', | ||
request: { | ||
template: { | ||
'application/json': 'custom template', | ||
}, | ||
}, | ||
}; | ||
const requestTemplates = serverlessStepFunctions | ||
.getMethodIntegration('stateMachine', undefined, httpWithRequestTemplate) | ||
.Properties.Integration.RequestTemplates; | ||
expect(requestTemplates['application/json']) | ||
.to.be.equal('custom template'); | ||
}); | ||
it('should return the default for application/x-www-form-urlencoded when one is not given', | ||
() => { | ||
const httpWithoutRequestTemplate = { | ||
path: 'foo/bar1', | ||
method: 'post', | ||
request: { | ||
template: { | ||
'application/json': 'custom template', | ||
}, | ||
}, | ||
}; | ||
const requestTemplates = serverlessStepFunctions | ||
.getMethodIntegration('stateMachine', undefined, httpWithoutRequestTemplate) | ||
.Properties.Integration.RequestTemplates; | ||
expect(requestTemplates['application/x-www-form-urlencoded']['Fn::Join'][1][2].Ref) | ||
.to.be.equal('StateMachineStepFunctionsStateMachine'); | ||
}); | ||
it('should return a custom template for application/x-www-form-urlencoded when one is given', | ||
() => { | ||
const httpWithRequestTemplate = { | ||
path: 'foo/bar1', | ||
method: 'post', | ||
request: { | ||
template: { | ||
'application/x-www-form-urlencoded': 'custom template', | ||
}, | ||
}, | ||
}; | ||
const requestTemplates = serverlessStepFunctions | ||
.getMethodIntegration('stateMachine', undefined, httpWithRequestTemplate) | ||
.Properties.Integration.RequestTemplates; | ||
expect(requestTemplates['application/x-www-form-urlencoded']) | ||
.to.be.equal('custom template'); | ||
}); | ||
}); | ||
describe('#getMethodResponses()', () => { | ||
@@ -121,0 +208,0 @@ it('should return a corresponding methodResponses resource', () => { |
@@ -77,3 +77,3 @@ 'use strict'; | ||
const stateMachineLogicalId = this | ||
.getStateMachineLogicalId(stateMachineName, stateMachineObj.name); | ||
.getStateMachineLogicalId(stateMachineName, stateMachineObj); | ||
const scheduleLogicalId = this | ||
@@ -80,0 +80,0 @@ .getScheduleLogicalId(stateMachineName, scheduleNumberInFunction); |
@@ -13,8 +13,3 @@ 'use strict'; | ||
let DependsOn; | ||
let Name; | ||
if (stateMachineObj.name) { | ||
Name = stateMachineObj.name; | ||
} | ||
if (stateMachineObj.definition) { | ||
@@ -56,5 +51,6 @@ DefinitionString = JSON.stringify(stateMachineObj.definition); | ||
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, Name); | ||
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName, | ||
stateMachineObj); | ||
const stateMachineOutputLogicalId = this | ||
.getStateMachineOutputLogicalId(stateMachineName, Name); | ||
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj); | ||
@@ -77,4 +73,5 @@ const stateMachineTemplate = ` | ||
if (Name) { | ||
newStateMachineObject[stateMachineLogicalId].Properties.StateMachineName = Name; | ||
if (stateMachineObj.name) { | ||
newStateMachineObject[stateMachineLogicalId].Properties.StateMachineName | ||
= stateMachineObj.name; | ||
} | ||
@@ -81,0 +78,0 @@ |
@@ -8,11 +8,5 @@ 'use strict'; | ||
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]); | ||
@@ -19,0 +13,0 @@ const stackName = this.provider.naming.getStackName(this.options.stage); |
'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 | ||
@@ -12,6 +15,9 @@ .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 | ||
@@ -18,0 +24,0 @@ .getNormalizedFunctionName(stateMachineName)}StepFunctionsStateMachineArn`; |
@@ -37,4 +37,5 @@ 'use strict'; | ||
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'); | ||
}); | ||
@@ -45,3 +46,4 @@ }); | ||
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'); | ||
@@ -51,2 +53,32 @@ }); | ||
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()', () => { | ||
@@ -53,0 +85,0 @@ it('should normalize the activity name and add the standard suffix', () => { |
@@ -16,4 +16,5 @@ 'use strict'; | ||
.parse(serverlessYmlPath) | ||
.then((serverlessFileParam) => { | ||
this.serverless.variables.populateObject(serverlessFileParam).then((parsedObject) => { | ||
.then(serverlessFileParam => | ||
this.serverless.variables.populateObject(serverlessFileParam) | ||
.then(parsedObject => { | ||
this.serverless.service.stepFunctions = {}; | ||
@@ -43,4 +44,4 @@ this.serverless.service.stepFunctions.stateMachines | ||
return BbPromise.resolve(); | ||
}); | ||
}); | ||
}) | ||
); | ||
}, | ||
@@ -47,0 +48,0 @@ |
{ | ||
"name": "serverless-step-functions", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "The module is AWS Step Functions plugin for Serverless Framework", | ||
@@ -24,2 +24,4 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"coveralls": "^3.0.1", | ||
"eslint": "^3.3.1", | ||
@@ -31,8 +33,6 @@ "eslint-config-airbnb": "^10.0.1", | ||
"eslint-plugin-react": "^6.1.1", | ||
"serverless": "^1.26.1", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^3.0.2", | ||
"mocha": "^5.2.0", | ||
"mocha-lcov-reporter": "^1.2.0", | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.12", | ||
"serverless": "^1.26.1", | ||
"sinon": "^1.17.5" | ||
@@ -39,0 +39,0 @@ }, |
@@ -89,2 +89,26 @@ [![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) [![Build Status](https://travis-ci.org/horike37/serverless-step-functions.svg?branch=master)](https://travis-ci.org/horike37/serverless-step-functions) [![npm version](https://badge.fury.io/js/serverless-step-functions.svg)](https://badge.fury.io/js/serverless-step-functions) [![Coverage Status](https://coveralls.io/repos/github/horike37/serverless-step-functions/badge.svg?branch=master)](https://coveralls.io/github/horike37/serverless-step-functions?branch=master) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) | ||
### 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 | ||
@@ -190,2 +214,28 @@ 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. | ||
#### Customizing request body mapping templates | ||
The plugin generates default body mapping templates for `application/json` and `application/x-www-form-urlencoded` content types. If you'd like to add more content types or customize the default ones, you can do so by including them in `serverless.yml`: | ||
```yml | ||
stepFunctions: | ||
stateMachines: | ||
hello: | ||
events: | ||
- http: | ||
path: posts/create | ||
method: POST | ||
request: | ||
template: | ||
application/json: | | ||
#set( $body = $util.escapeJavaScript($input.json('$')) ) | ||
#set( $name = $util.escapeJavaScript($input.json('$.data.attributes.order_id')) ) | ||
{ | ||
"input": "$body", | ||
"name": "$name", | ||
"stateMachineArn":"arn:aws:states:#{AWS::Region}:#{AWS::AccountId}:stateMachine:processOrderFlow-${opt:stage}" | ||
} | ||
name: processOrderFlow-${opt:stage} | ||
definition: | ||
``` | ||
#### Send request to an API | ||
@@ -304,3 +354,3 @@ You can input an value as json in request body, the value is passed as the input value of your statemachine | ||
### deploy | ||
Runn `sls deploy`, the defined Stepfunctions are deployed. | ||
Run `sls deploy`, the defined Stepfunctions are deployed. | ||
@@ -307,0 +357,0 @@ ### invoke |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
206590
50
4831
628