Skip to content

Commit ed51761

Browse files
committed
fixed bugs when invoking
1 parent 69d6ee1 commit ed51761

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class ServerlessStepFunctions {
2727
this.service = this.serverless.service.service;
2828
this.region = this.provider.getRegion();
2929
this.stage = this.provider.getStage();
30-
3130
Object.assign(
3231
this,
3332
compileStateMachines,
@@ -86,6 +85,7 @@ class ServerlessStepFunctions {
8685

8786
this.hooks = {
8887
'invoke:stepf:invoke': () => BbPromise.bind(this)
88+
.then(this.yamlParse)
8989
.then(this.invoke),
9090
'package:initialize': () => BbPromise.bind(this)
9191
.then(this.yamlParse),

lib/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ describe('#index', () => {
5050
it('should run invoke:stepf:invoke promise chain in order', () => {
5151
const invokeStub = sinon
5252
.stub(serverlessStepFunctions, 'invoke').returns(BbPromise.resolve());
53+
const yamlParseStub = sinon
54+
.stub(serverlessStepFunctions, 'yamlParse').returns(BbPromise.resolve());
5355
return serverlessStepFunctions.hooks['invoke:stepf:invoke']()
5456
.then(() => {
5557
expect(invokeStub.calledOnce).to.be.equal(true);
58+
expect(yamlParseStub.calledOnce).to.be.equal(true);
59+
serverlessStepFunctions.yamlParse.restore();
5660
serverlessStepFunctions.invoke.restore();
5761
});
5862
});

lib/invoke/invoke.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ const path = require('path');
55

66
module.exports = {
77
getStateMachineArn() {
8-
const stateMachineOutputKey =
9-
this.getStateMachineOutputLogicalId(this.options.name);
8+
let stateMachineOutputKey;
9+
if (this.serverless.service.stepFunctions.stateMachines[this.options.name].name) {
10+
stateMachineOutputKey =
11+
this.getStateMachineOutputLogicalId(this.options.name
12+
, this.serverless.service.stepFunctions.stateMachines[this.options.name].name);
13+
} else {
14+
stateMachineOutputKey =
15+
this.getStateMachineOutputLogicalId(this.options.name);
16+
}
17+
1018
const stackName = this.provider.naming.getStackName(this.options.stage);
1119

1220
return this.provider.request('CloudFormation',

lib/invoke/invoke.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,48 @@ describe('invoke', () => {
7171
});
7272
});
7373

74+
it('should return arn when correct params given with specifing name statement in serverless.yml'
75+
, () => {
76+
getStateMachineStub = sinon.stub(serverlessStepFunctions.provider, 'request')
77+
.returns(BbPromise.resolve({
78+
Stacks: [
79+
{
80+
Outputs: [
81+
{
82+
OutputKey: 'StatemachinenameArn',
83+
OutputValue: 'arn:aws:states:us-east-1:xxxx',
84+
Description: 'Current StateMachine Arn' },
85+
],
86+
},
87+
],
88+
})
89+
);
90+
91+
serverless.service.stepFunctions = {
92+
stateMachines: {
93+
myStateMachine: {
94+
name: 'statemachinename',
95+
definition: 'definition1',
96+
},
97+
},
98+
};
99+
100+
serverlessStepFunctions.getStateMachineArn()
101+
.then(() => {
102+
expect(getStateMachineStub.calledOnce).to.be.equal(true);
103+
expect(getStateMachineStub.calledWithExactly(
104+
'CloudFormation',
105+
'describeStacks',
106+
{ StackName: 'new-service-dev' },
107+
serverlessStepFunctions.options.stage,
108+
serverlessStepFunctions.options.region
109+
)).to.be.equal(true);
110+
expect(serverlessStepFunctions.stateMachineArn).to.be
111+
.equal('arn:aws:states:us-east-1:xxxx');
112+
serverlessStepFunctions.provider.request.restore();
113+
});
114+
});
115+
74116
it('should throw error if correct params is not given'
75117
, () => {
76118
getStateMachineStub = sinon.stub(serverlessStepFunctions.provider, 'request')

lib/yamlParser.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ module.exports = {
2323
= serverlessFileParam.stepFunctions
2424
&& serverlessFileParam.stepFunctions.activities
2525
? serverlessFileParam.stepFunctions.activities : [];
26+
27+
if (!this.serverless.pluginManager.cliOptions.stage) {
28+
this.serverless.pluginManager.cliOptions.stage = this.options.stage
29+
|| (this.serverless.service.provider && this.serverless.service.provider.stage)
30+
|| 'dev';
31+
}
32+
33+
if (!this.serverless.pluginManager.cliOptions.region) {
34+
this.serverless.pluginManager.cliOptions.region = this.options.region
35+
|| (this.serverless.service.provider && this.serverless.service.provider.region)
36+
|| 'us-east-1';
37+
}
38+
2639
this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
2740
return BbPromise.resolve();
2841
});

0 commit comments

Comments
 (0)