Skip to content

Commit 11134c9

Browse files
authored
Merge pull request #88 from rplute/issue-87
add support for new update state machine feature
2 parents 923ed19 + ed51761 commit 11134c9

File tree

9 files changed

+1374
-192
lines changed

9 files changed

+1374
-192
lines changed

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ describe('#compileIamRole', () => {
2626
serverless.service.stepFunctions = {
2727
stateMachines: {
2828
myStateMachine1: {
29+
name: 'stateMachine1',
2930
definition: 'definition',
3031
role: 'role',
3132
},
3233
myStateMachine2: {
34+
name: 'stateMachine2',
3335
definition: 'definition',
3436
role: 'role',
3537
},
@@ -46,6 +48,7 @@ describe('#compileIamRole', () => {
4648
serverless.service.stepFunctions = {
4749
stateMachines: {
4850
myStateMachine2: {
51+
name: 'stateMachine2',
4952
definition: 'definition',
5053
},
5154
},
@@ -67,10 +70,12 @@ describe('#compileIamRole', () => {
6770
serverless.service.stepFunctions = {
6871
stateMachines: {
6972
myStateMachine1: {
73+
name: 'stateMachine1',
7074
definition: 'definition',
7175
role: 'role',
7276
},
7377
myStateMachine2: {
78+
name: 'stateMachin2',
7479
definition: 'definition',
7580
},
7681
},

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ module.exports = {
7272
[stateMachineLogicalId]: JSON.parse(stateMachineTemplate),
7373
};
7474

75+
if (Name) {
76+
newStateMachineObject[stateMachineLogicalId].Properties.StateMachineName = Name;
77+
}
78+
7579
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
7680
newStateMachineObject);
7781

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,65 @@ describe('#compileStateMachines', () => {
2121
serverlessStepFunctions = new ServerlessStepFunctions(serverless);
2222
});
2323

24-
it('should create corresponding resources when definition property is given', () => {
24+
it('should create corresponding resources when definition and name property is given', () => {
25+
serverless.service.stepFunctions = {
26+
stateMachines: {
27+
myStateMachine1: {
28+
name: 'stateMachineBeta1',
29+
definition: 'definition1',
30+
},
31+
myStateMachine2: {
32+
name: 'stateMachineBeta2',
33+
definition: 'definition2',
34+
},
35+
},
36+
};
37+
38+
serverlessStepFunctions.compileStateMachines();
39+
40+
expect(serverlessStepFunctions.serverless.service
41+
.provider.compiledCloudFormationTemplate.Resources
42+
.StateMachineBeta1.Type
43+
).to.equal('AWS::StepFunctions::StateMachine');
44+
expect(serverlessStepFunctions.serverless.service
45+
.provider.compiledCloudFormationTemplate.Resources
46+
.StateMachineBeta2.Type
47+
).to.equal('AWS::StepFunctions::StateMachine');
48+
expect(serverlessStepFunctions.serverless.service
49+
.provider.compiledCloudFormationTemplate.Resources
50+
.StateMachineBeta1.Properties.DefinitionString
51+
).to.equal('"definition1"');
52+
expect(serverlessStepFunctions.serverless.service
53+
.provider.compiledCloudFormationTemplate.Resources
54+
.StateMachineBeta2.Properties.DefinitionString
55+
).to.equal('"definition2"');
56+
expect(serverlessStepFunctions.serverless.service
57+
.provider.compiledCloudFormationTemplate.Resources
58+
.StateMachineBeta1.Properties.RoleArn['Fn::GetAtt'][0]
59+
).to.equal('IamRoleStateMachineExecution');
60+
expect(serverlessStepFunctions.serverless.service
61+
.provider.compiledCloudFormationTemplate.Resources
62+
.StateMachineBeta2.Properties.RoleArn['Fn::GetAtt'][0]
63+
).to.equal('IamRoleStateMachineExecution');
64+
expect(serverlessStepFunctions.serverless.service
65+
.provider.compiledCloudFormationTemplate.Resources
66+
.StateMachineBeta1.DependsOn
67+
).to.equal('IamRoleStateMachineExecution');
68+
expect(serverlessStepFunctions.serverless.service
69+
.provider.compiledCloudFormationTemplate.Resources
70+
.StateMachineBeta2.DependsOn
71+
).to.equal('IamRoleStateMachineExecution');
72+
expect(serverlessStepFunctions.serverless.service
73+
.provider.compiledCloudFormationTemplate.Outputs
74+
.StateMachineBeta1Arn.Value.Ref
75+
).to.equal('StateMachineBeta1');
76+
expect(serverlessStepFunctions.serverless.service
77+
.provider.compiledCloudFormationTemplate.Outputs
78+
.StateMachineBeta2Arn.Value.Ref
79+
).to.equal('StateMachineBeta2');
80+
});
81+
82+
it('should create corresponding resources when definition property is given and no name', () => {
2583
serverless.service.stepFunctions = {
2684
stateMachines: {
2785
myStateMachine1: {
@@ -129,10 +187,12 @@ describe('#compileStateMachines', () => {
129187
serverless.service.stepFunctions = {
130188
stateMachines: {
131189
myStateMachine1: {
190+
name: 'stateMachineBeta1',
132191
definition: 'definition1',
133192
role: 'arn:aws:role1',
134193
},
135194
myStateMachine2: {
195+
name: 'stateMachineBeta2',
136196
definition: 'definition2',
137197
role: 'arn:aws:role2',
138198
},
@@ -142,36 +202,36 @@ describe('#compileStateMachines', () => {
142202
serverlessStepFunctions.compileStateMachines();
143203
expect(serverlessStepFunctions.serverless.service
144204
.provider.compiledCloudFormationTemplate.Resources
145-
.MyStateMachine1StepFunctionsStateMachine.Type
205+
.StateMachineBeta1.Type
146206
).to.equal('AWS::StepFunctions::StateMachine');
147207
expect(serverlessStepFunctions.serverless.service
148208
.provider.compiledCloudFormationTemplate.Resources
149-
.MyStateMachine2StepFunctionsStateMachine.Type
209+
.StateMachineBeta2.Type
150210
).to.equal('AWS::StepFunctions::StateMachine');
151211
expect(serverlessStepFunctions.serverless.service
152212
.provider.compiledCloudFormationTemplate.Resources
153-
.MyStateMachine1StepFunctionsStateMachine.Properties.DefinitionString
213+
.StateMachineBeta1.Properties.DefinitionString
154214
).to.equal('"definition1"');
155215
expect(serverlessStepFunctions.serverless.service
156216
.provider.compiledCloudFormationTemplate.Resources
157-
.MyStateMachine2StepFunctionsStateMachine.Properties.DefinitionString
217+
.StateMachineBeta2.Properties.DefinitionString
158218
).to.equal('"definition2"');
159219
expect(serverlessStepFunctions.serverless.service
160220
.provider.compiledCloudFormationTemplate.Resources
161-
.MyStateMachine1StepFunctionsStateMachine.Properties.RoleArn
221+
.StateMachineBeta1.Properties.RoleArn
162222
).to.equal('arn:aws:role1');
163223
expect(serverlessStepFunctions.serverless.service
164224
.provider.compiledCloudFormationTemplate.Resources
165-
.MyStateMachine2StepFunctionsStateMachine.Properties.RoleArn
225+
.StateMachineBeta2.Properties.RoleArn
166226
).to.equal('arn:aws:role2');
167227
expect(serverlessStepFunctions.serverless.service
168228
.provider.compiledCloudFormationTemplate.Outputs
169-
.MyStateMachine1StepFunctionsStateMachineArn.Value.Ref
170-
).to.equal('MyStateMachine1StepFunctionsStateMachine');
229+
.StateMachineBeta1Arn.Value.Ref
230+
).to.equal('StateMachineBeta1');
171231
expect(serverlessStepFunctions.serverless.service
172232
.provider.compiledCloudFormationTemplate.Outputs
173-
.MyStateMachine2StepFunctionsStateMachineArn.Value.Ref
174-
).to.equal('MyStateMachine2StepFunctionsStateMachine');
233+
.StateMachineBeta2Arn.Value.Ref
234+
).to.equal('StateMachineBeta2');
175235
});
176236

177237
it('should throw error when definition property is not given', () => {
@@ -188,9 +248,11 @@ describe('#compileStateMachines', () => {
188248
serverless.service.stepFunctions = {
189249
stateMachines: {
190250
myStateMachine1: {
251+
name: 'stateMachineBeta1',
191252
definition: 'definition1\n',
192253
},
193254
myStateMachine2: {
255+
name: 'stateMachineBeta2',
194256
definition: 'definition2\n',
195257
},
196258
},
@@ -199,19 +261,19 @@ describe('#compileStateMachines', () => {
199261
serverlessStepFunctions.compileStateMachines();
200262
expect(serverlessStepFunctions.serverless.service
201263
.provider.compiledCloudFormationTemplate.Resources
202-
.MyStateMachine1StepFunctionsStateMachine.Type
264+
.StateMachineBeta1.Type
203265
).to.equal('AWS::StepFunctions::StateMachine');
204266
expect(serverlessStepFunctions.serverless.service
205267
.provider.compiledCloudFormationTemplate.Resources
206-
.MyStateMachine2StepFunctionsStateMachine.Type
268+
.StateMachineBeta2.Type
207269
).to.equal('AWS::StepFunctions::StateMachine');
208270
expect(serverlessStepFunctions.serverless.service
209271
.provider.compiledCloudFormationTemplate.Resources
210-
.MyStateMachine1StepFunctionsStateMachine.Properties.DefinitionString
272+
.StateMachineBeta1.Properties.DefinitionString
211273
).to.equal('"definition1"');
212274
expect(serverlessStepFunctions.serverless.service
213275
.provider.compiledCloudFormationTemplate.Resources
214-
.MyStateMachine2StepFunctionsStateMachine.Properties.DefinitionString
276+
.StateMachineBeta2.Properties.DefinitionString
215277
).to.equal('"definition2"');
216278
});
217279

@@ -221,6 +283,7 @@ describe('#compileStateMachines', () => {
221283
myStateMachine1: {
222284
definition: 'definition1',
223285
role: 'srn:aws:role1',
286+
name: 'stateMachineBeta1',
224287
},
225288
},
226289
};
@@ -231,6 +294,7 @@ describe('#compileStateMachines', () => {
231294
serverless.service.stepFunctions = {
232295
stateMachines: {
233296
myStateMachine1: {
297+
name: 'stateMachineBeta1',
234298
definition: 'definition1',
235299
role: { 'arn:aws:role1': 'ss' },
236300
},
@@ -241,6 +305,7 @@ describe('#compileStateMachines', () => {
241305
serverless.service.stepFunctions = {
242306
stateMachines: {
243307
myStateMachine1: {
308+
name: 'stateMachineBeta2',
244309
definition: 'definition1',
245310
role: ['arn:aws:role1'],
246311
},

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)