Skip to content

Commit 8632132

Browse files
committed
Fix #4 and #3
1 parent 8b8678a commit 8632132

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

index.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ class ServerlessStepFunctions {
88
this.serverless = serverless;
99
this.options = options || {};
1010
this.provider = this.serverless.getProvider('aws');
11+
this.service = this.serverless.service.service;
12+
this.region = this.provider.getRegion();
13+
this.stage = this.provider.getStage();
1114
this.awsStateLanguage = {};
1215
this.functionArns = {};
13-
const region = this.options.region || 'us-east-1';
14-
this.iamRoleName = `serverless-step-functions-executerole-${region}`;
15-
this.iamPolicyName = `serverless-step-functions-executepolicy-${region}`;
16+
this.iamRoleName = `serverless-step-functions-executerole-${this.region}`;
17+
this.iamPolicyName = `serverless-step-functions-executepolicy-${this.region}`;
1618
this.iamPolicyStatement = `{
1719
"Version": "2012-10-17",
1820
"Statement": [
@@ -33,7 +35,7 @@ class ServerlessStepFunctions {
3335
{
3436
"Effect": "Allow",
3537
"Principal": {
36-
"Service": "states.${region}.amazonaws.com"
38+
"Service": "states.${this.region}.amazonaws.com"
3739
},
3840
"Action": "sts:AssumeRole"
3941
}
@@ -55,6 +57,14 @@ class ServerlessStepFunctions {
5557
shortcut: 't',
5658
required: true,
5759
},
60+
stage: {
61+
usage: 'Stage of the service',
62+
shortcut: 's',
63+
},
64+
region: {
65+
usage: 'Region of the service',
66+
shortcut: 'r',
67+
},
5868
},
5969
},
6070
},
@@ -72,6 +82,14 @@ class ServerlessStepFunctions {
7282
shortcut: 't',
7383
required: true,
7484
},
85+
stage: {
86+
usage: 'Stage of the service',
87+
shortcut: 's',
88+
},
89+
region: {
90+
usage: 'Region of the service',
91+
shortcut: 'r',
92+
},
7593
},
7694
},
7795
},
@@ -93,6 +111,14 @@ class ServerlessStepFunctions {
93111
usage: 'String data to be passed as an event to your step function',
94112
shortcut: 'd',
95113
},
114+
stage: {
115+
usage: 'Stage of the service',
116+
shortcut: 's',
117+
},
118+
region: {
119+
usage: 'Region of the service',
120+
shortcut: 'r',
121+
},
96122
},
97123
},
98124
},
@@ -138,6 +164,10 @@ class ServerlessStepFunctions {
138164
.then(this.describeExecution);
139165
}
140166

167+
getStateMachineName() {
168+
return `${this.service}-${this.stage}-${this.options.state}`;
169+
}
170+
141171
getIamRole() {
142172
return this.provider.request('IAM',
143173
'getRole',
@@ -164,10 +194,9 @@ class ServerlessStepFunctions {
164194
this.options.stage,
165195
this.options.region)
166196
.then((result) => {
167-
const region = this.options.region || 'us-east-1';
168197
_.forEach(this.serverless.service.functions, (value, key) => {
169198
this.functionArns[key]
170-
= `arn:aws:lambda:${region}:${result.Account}:function:${value.name}`;
199+
= `arn:aws:lambda:${this.region}:${result.Account}:function:${value.name}`;
171200
});
172201
return BbPromise.resolve();
173202
});
@@ -212,10 +241,8 @@ class ServerlessStepFunctions {
212241
this.options.stage,
213242
this.options.region)
214243
.then((result) => {
215-
const region = this.options.region || 'us-east-1';
216-
const stage = this.options.stage || 'dev';
217244
this.stateMachineArn =
218-
`arn:aws:states:${region}:${result.Account}:stateMachine:${this.options.state}-${stage}`;
245+
`arn:aws:states:${this.region}:${result.Account}:stateMachine:${this.getStateMachineName()}`;
219246
return BbPromise.resolve();
220247
});
221248
}
@@ -232,19 +259,18 @@ class ServerlessStepFunctions {
232259
}
233260

234261
createStateMachine() {
235-
const stage = this.options.stage || 'dev';
236262
return this.provider.request('StepFunctions',
237263
'createStateMachine',
238264
{
239265
definition: this.awsStateLanguage[this.options.state],
240-
name: `${this.options.state}-${stage}`,
266+
name: this.getStateMachineName(),
241267
roleArn: this.iamRoleArn,
242268
},
243269
this.options.stage,
244270
this.options.region)
245271
.then(() => {
246272
this.serverless.cli.consoleLog('');
247-
this.serverless.cli.log(`Finish to deploy ${this.options.state}-${stage} step function`);
273+
this.serverless.cli.log(`Finish to deploy ${this.getStateMachineName()} step function`);
248274
return BbPromise.resolve();
249275
}).catch((error) => {
250276
if (error.message.match(/State Machine is being deleted/)) {

index.test.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ const ServerlessStepFunctions = require('./index');
99

1010
describe('ServerlessStepFunctions', () => {
1111
let serverless;
12+
let provider;
1213
let serverlessStepFunctions;
1314

1415
beforeEach(() => {
1516
serverless = new Serverless();
1617
serverless.servicePath = true;
17-
18+
serverless.service.service = 'step-functions';
1819
serverless.service.functions = {
1920
first: {
2021
handler: true,
@@ -34,6 +35,7 @@ describe('ServerlessStepFunctions', () => {
3435

3536
serverless.init();
3637
serverless.setProvider('aws', new AwsProvider(serverless));
38+
provider = serverless.getProvider('aws');
3739
serverlessStepFunctions = new ServerlessStepFunctions(serverless, options);
3840
});
3941

@@ -47,6 +49,12 @@ describe('ServerlessStepFunctions', () => {
4749
expect(serverlessStepFunctions.serverless).to.deep.equal(serverless);
4850
});
4951

52+
it('should set the region variable', () =>
53+
expect(serverlessStepFunctions.region).to.be.equal(provider.getRegion()));
54+
55+
it('should set the stage variable', () =>
56+
expect(serverlessStepFunctions.stage).to.be.equal(provider.getStage()));
57+
5058
it('should set the iamRoleName variable', () =>
5159
expect(serverlessStepFunctions.iamRoleName).to.be
5260
.equal('serverless-step-functions-executerole-us-east-1'));
@@ -185,6 +193,13 @@ describe('ServerlessStepFunctions', () => {
185193
});
186194
});
187195

196+
describe('#getStateMachineName', () => {
197+
it('should return stateMachineName', () => {
198+
expect(serverlessStepFunctions.getStateMachineName())
199+
.to.be.equal('step-functions-dev-stateMachine');
200+
});
201+
});
202+
188203
describe('#getIamRole()', () => {
189204
let getRoleStub;
190205
beforeEach(() => {
@@ -303,7 +318,7 @@ describe('ServerlessStepFunctions', () => {
303318
serverlessStepFunctions.options.region
304319
)).to.be.equal(true);
305320
expect(serverlessStepFunctions.stateMachineArn).to.be
306-
.equal('arn:aws:states:us-east-1:1234:stateMachine:stateMachine-dev');
321+
.equal('arn:aws:states:us-east-1:1234:stateMachine:step-functions-dev-stateMachine');
307322
serverlessStepFunctions.provider.request.restore();
308323
})
309324
);
@@ -344,15 +359,16 @@ describe('ServerlessStepFunctions', () => {
344359
it('should createStateMachine with correct params'
345360
, () => serverlessStepFunctions.createStateMachine()
346361
.then(() => {
362+
const stage = serverlessStepFunctions.options.stage;
363+
const state = serverlessStepFunctions.options.state;
347364
expect(createStateMachineStub.calledOnce).to.be.equal(true);
348365
expect(createStateMachineStub.calledWithExactly(
349366
'StepFunctions',
350367
'createStateMachine',
351368
{
352369
definition: serverlessStepFunctions
353370
.awsStateLanguage[serverlessStepFunctions.options.state],
354-
name:
355-
`${serverlessStepFunctions.options.state}-${serverlessStepFunctions.options.stage}`,
371+
name: `${serverless.service.service}-${stage}-${state}`,
356372
roleArn: serverlessStepFunctions.iamRoleArn,
357373
},
358374
serverlessStepFunctions.options.stage,

0 commit comments

Comments
 (0)