Skip to content

Commit

Permalink
Fix #4 and #3
Browse files Browse the repository at this point in the history
  • Loading branch information
horike37 committed Jan 8, 2017
1 parent 8b8678a commit 8632132
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
50 changes: 38 additions & 12 deletions index.js
Expand Up @@ -8,11 +8,13 @@ class ServerlessStepFunctions {
this.serverless = serverless;
this.options = options || {};
this.provider = this.serverless.getProvider('aws');
this.service = this.serverless.service.service;
this.region = this.provider.getRegion();
this.stage = this.provider.getStage();
this.awsStateLanguage = {};
this.functionArns = {};
const region = this.options.region || 'us-east-1';
this.iamRoleName = `serverless-step-functions-executerole-${region}`;
this.iamPolicyName = `serverless-step-functions-executepolicy-${region}`;
this.iamRoleName = `serverless-step-functions-executerole-${this.region}`;
this.iamPolicyName = `serverless-step-functions-executepolicy-${this.region}`;
this.iamPolicyStatement = `{
"Version": "2012-10-17",
"Statement": [
Expand All @@ -33,7 +35,7 @@ class ServerlessStepFunctions {
{
"Effect": "Allow",
"Principal": {
"Service": "states.${region}.amazonaws.com"
"Service": "states.${this.region}.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
Expand All @@ -55,6 +57,14 @@ class ServerlessStepFunctions {
shortcut: 't',
required: true,
},
stage: {
usage: 'Stage of the service',
shortcut: 's',
},
region: {
usage: 'Region of the service',
shortcut: 'r',
},
},
},
},
Expand All @@ -72,6 +82,14 @@ class ServerlessStepFunctions {
shortcut: 't',
required: true,
},
stage: {
usage: 'Stage of the service',
shortcut: 's',
},
region: {
usage: 'Region of the service',
shortcut: 'r',
},
},
},
},
Expand All @@ -93,6 +111,14 @@ class ServerlessStepFunctions {
usage: 'String data to be passed as an event to your step function',
shortcut: 'd',
},
stage: {
usage: 'Stage of the service',
shortcut: 's',
},
region: {
usage: 'Region of the service',
shortcut: 'r',
},
},
},
},
Expand Down Expand Up @@ -138,6 +164,10 @@ class ServerlessStepFunctions {
.then(this.describeExecution);
}

getStateMachineName() {
return `${this.service}-${this.stage}-${this.options.state}`;
}

getIamRole() {
return this.provider.request('IAM',
'getRole',
Expand All @@ -164,10 +194,9 @@ class ServerlessStepFunctions {
this.options.stage,
this.options.region)
.then((result) => {
const region = this.options.region || 'us-east-1';
_.forEach(this.serverless.service.functions, (value, key) => {
this.functionArns[key]
= `arn:aws:lambda:${region}:${result.Account}:function:${value.name}`;
= `arn:aws:lambda:${this.region}:${result.Account}:function:${value.name}`;
});
return BbPromise.resolve();
});
Expand Down Expand Up @@ -212,10 +241,8 @@ class ServerlessStepFunctions {
this.options.stage,
this.options.region)
.then((result) => {
const region = this.options.region || 'us-east-1';
const stage = this.options.stage || 'dev';
this.stateMachineArn =
`arn:aws:states:${region}:${result.Account}:stateMachine:${this.options.state}-${stage}`;
`arn:aws:states:${this.region}:${result.Account}:stateMachine:${this.getStateMachineName()}`;
return BbPromise.resolve();
});
}
Expand All @@ -232,19 +259,18 @@ class ServerlessStepFunctions {
}

createStateMachine() {
const stage = this.options.stage || 'dev';
return this.provider.request('StepFunctions',
'createStateMachine',
{
definition: this.awsStateLanguage[this.options.state],
name: `${this.options.state}-${stage}`,
name: this.getStateMachineName(),
roleArn: this.iamRoleArn,
},
this.options.stage,
this.options.region)
.then(() => {
this.serverless.cli.consoleLog('');
this.serverless.cli.log(`Finish to deploy ${this.options.state}-${stage} step function`);
this.serverless.cli.log(`Finish to deploy ${this.getStateMachineName()} step function`);
return BbPromise.resolve();
}).catch((error) => {
if (error.message.match(/State Machine is being deleted/)) {
Expand Down
24 changes: 20 additions & 4 deletions index.test.js
Expand Up @@ -9,12 +9,13 @@ const ServerlessStepFunctions = require('./index');

describe('ServerlessStepFunctions', () => {
let serverless;
let provider;
let serverlessStepFunctions;

beforeEach(() => {
serverless = new Serverless();
serverless.servicePath = true;

serverless.service.service = 'step-functions';
serverless.service.functions = {
first: {
handler: true,
Expand All @@ -34,6 +35,7 @@ describe('ServerlessStepFunctions', () => {

serverless.init();
serverless.setProvider('aws', new AwsProvider(serverless));
provider = serverless.getProvider('aws');
serverlessStepFunctions = new ServerlessStepFunctions(serverless, options);
});

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

it('should set the region variable', () =>
expect(serverlessStepFunctions.region).to.be.equal(provider.getRegion()));

it('should set the stage variable', () =>
expect(serverlessStepFunctions.stage).to.be.equal(provider.getStage()));

it('should set the iamRoleName variable', () =>
expect(serverlessStepFunctions.iamRoleName).to.be
.equal('serverless-step-functions-executerole-us-east-1'));
Expand Down Expand Up @@ -185,6 +193,13 @@ describe('ServerlessStepFunctions', () => {
});
});

describe('#getStateMachineName', () => {
it('should return stateMachineName', () => {
expect(serverlessStepFunctions.getStateMachineName())
.to.be.equal('step-functions-dev-stateMachine');
});
});

describe('#getIamRole()', () => {
let getRoleStub;
beforeEach(() => {
Expand Down Expand Up @@ -303,7 +318,7 @@ describe('ServerlessStepFunctions', () => {
serverlessStepFunctions.options.region
)).to.be.equal(true);
expect(serverlessStepFunctions.stateMachineArn).to.be
.equal('arn:aws:states:us-east-1:1234:stateMachine:stateMachine-dev');
.equal('arn:aws:states:us-east-1:1234:stateMachine:step-functions-dev-stateMachine');
serverlessStepFunctions.provider.request.restore();
})
);
Expand Down Expand Up @@ -344,15 +359,16 @@ describe('ServerlessStepFunctions', () => {
it('should createStateMachine with correct params'
, () => serverlessStepFunctions.createStateMachine()
.then(() => {
const stage = serverlessStepFunctions.options.stage;
const state = serverlessStepFunctions.options.state;
expect(createStateMachineStub.calledOnce).to.be.equal(true);
expect(createStateMachineStub.calledWithExactly(
'StepFunctions',
'createStateMachine',
{
definition: serverlessStepFunctions
.awsStateLanguage[serverlessStepFunctions.options.state],
name:
`${serverlessStepFunctions.options.state}-${serverlessStepFunctions.options.stage}`,
name: `${serverless.service.service}-${stage}-${state}`,
roleArn: serverlessStepFunctions.iamRoleArn,
},
serverlessStepFunctions.options.stage,
Expand Down

0 comments on commit 8632132

Please sign in to comment.