Skip to content

Commit

Permalink
replacing task arn
Browse files Browse the repository at this point in the history
  • Loading branch information
horike37 committed Feb 18, 2017
1 parent ab882fb commit 31e5d07
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
32 changes: 26 additions & 6 deletions lib/dataProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ module.exports = {

this.serverless.service.stepFunctions.stateMachines[this.options.name] =
JSON.stringify(this.serverless.service.stepFunctions.stateMachines[this.options.name]);
_.forEach(this.functionArns, (value, key) => {
const regExp = new RegExp(`"Resource":"${key}"`, 'g');
_.forEach(this.functionArns, (functionArn, functionName) => {
const regExp = new RegExp(`"Resource":"${functionName}"`, 'g');
this.serverless.service.stepFunctions.stateMachines[this.options.name] =
this.serverless.service.stepFunctions.stateMachines[this.options.name]
.replace(regExp, `"Resource":"${value}"`);
.replace(regExp, `"Resource":"${functionArn}"`);
});

_.forEach(this.activityArns, (activityArn, activityName) => {
const regExp = new RegExp(`"Resource":"${activityName}"`, 'g');
_.forEach(this.serverless.service.stepFunctions.stateMachines,
(stepFunctionObj, stepFunctionKey) => {
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey] =
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey]
.replace(regExp, `"Resource":"${activityArn}"`);
});
});
return BbPromise.resolve();
},
Expand All @@ -95,13 +105,23 @@ module.exports = {
= JSON.stringify(stepFunctionObj);
});

_.forEach(this.functionArns, (functionObj, functionKey) => {
const regExp = new RegExp(`"Resource":"${functionKey}"`, 'g');
_.forEach(this.functionArns, (functionArn, functionName) => {
const regExp = new RegExp(`"Resource":"${functionName}"`, 'g');
_.forEach(this.serverless.service.stepFunctions.stateMachines,
(stepFunctionObj, stepFunctionKey) => {
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey] =
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey]
.replace(regExp, `"Resource":"${functionArn}"`);
});
});

_.forEach(this.activityArns, (activityArn, activityName) => {
const regExp = new RegExp(`"Resource":"${activityName}"`, 'g');
_.forEach(this.serverless.service.stepFunctions.stateMachines,
(stepFunctionObj, stepFunctionKey) => {
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey] =
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey]
.replace(regExp, `"Resource":"${functionObj}"`);
.replace(regExp, `"Resource":"${activityArn}"`);
});
});
return BbPromise.resolve();
Expand Down
69 changes: 69 additions & 0 deletions lib/dataProcessing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,49 @@ describe('dataProsessing', () => {
.to.be.equal(a);
});
});

it('should comple with replacing activityArn', () => {
serverless.service.stepFunctions.stateMachines = {
hellofunc: {
States: {
HelloWorld: {
Resource: 'someActivity',
},
},
},
};
serverlessStepFunctions.activityArns.someActivity = 'activityArn';
serverlessStepFunctions.compile().then(() => {
expect(serverlessStepFunctions.serverless.service.stepFunctions.stateMachines.hellofunc)
.to.be.equal('{"States":{"HelloWorld":{"Resource":"activityArn"}}}');
});
});

it('should comple with correct params when nested Resource with replacing activityArn', () => {
serverlessStepFunctions.serverless.service.stepFunctions.stateMachines = {
hellofunc: {
States: {
HelloWorld: {
Resource: 'someActivity',
HelloWorld: {
Resource: 'someActivity',
HelloWorld: {
Resource: 'someActivity',
},
},
},
},
},
};

let a = '{"States":{"HelloWorld":{"Resource":"activityArn","HelloWorld"';
a += ':{"Resource":"activityArn","HelloWorld":{"Resource":"activityArn"}}}}}';
serverlessStepFunctions.activityArns.someActivity = 'activityArn';
serverlessStepFunctions.compile().then(() => {
expect(serverlessStepFunctions.serverless.service.stepFunctions.stateMachines.hellofunc)
.to.be.equal(a);
});
});
});

describe('#parseInputdate()', () => {
Expand Down Expand Up @@ -242,5 +285,31 @@ describe('dataProsessing', () => {
.to.be.equal(a);
});
});

it('should comple with correct params when nested Resource with replacing activityArn', () => {
serverlessStepFunctions.serverless.service.stepFunctions.stateMachines = {
hellofunc: {
States: {
HelloWorld: {
Resource: 'someActivity',
HelloWorld: {
Resource: 'someActivity',
HelloWorld: {
Resource: 'someActivity',
},
},
},
},
},
};

let a = '{"States":{"HelloWorld":{"Resource":"activityArn","HelloWorld"';
a += ':{"Resource":"activityArn","HelloWorld":{"Resource":"activityArn"}}}}}';
serverlessStepFunctions.activityArns.someActivity = 'activityArn';
serverlessStepFunctions.compileAll().then(() => {
expect(serverlessStepFunctions.serverless.service.stepFunctions.stateMachines.hellofunc)
.to.be.equal(a);
});
});
});
});
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ class ServerlessStepFunctions {
.then(this.yamlParse)
.then(this.getStateMachineArn)
.then(this.getFunctionArns)
.then(this.getActivityArns)
.then(this.compile)
.then(this.getIamRole)
.then(this.deleteStateMachine)
Expand Down
6 changes: 5 additions & 1 deletion lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ describe('ServerlessStepFunctions', () => {
.stub(serverlessStepFunctions, 'getStateMachineArn').returns(BbPromise.resolve());
const getFunctionArnsStub = sinon
.stub(serverlessStepFunctions, 'getFunctionArns').returns(BbPromise.resolve());
const getActivityArnsStub = sinon
.stub(serverlessStepFunctions, 'getActivityArns').returns(BbPromise.resolve());
const compileStub = sinon
.stub(serverlessStepFunctions, 'compile').returns(BbPromise.resolve());
const getIamRoleStub = sinon
Expand All @@ -266,14 +268,16 @@ describe('ServerlessStepFunctions', () => {
expect(yamlParseStub.calledOnce).to.be.equal(true);
expect(getStateMachineArnStub.calledAfter(yamlParseStub)).to.be.equal(true);
expect(getFunctionArnsStub.calledAfter(getStateMachineArnStub)).to.be.equal(true);
expect(compileStub.calledAfter(getFunctionArnsStub)).to.be.equal(true);
expect(getActivityArnsStub.calledAfter(getFunctionArnsStub)).to.be.equal(true);
expect(compileStub.calledAfter(getActivityArnsStub)).to.be.equal(true);
expect(getIamRoleStub.calledAfter(compileStub)).to.be.equal(true);
expect(deleteStateMachineStub.calledAfter(getIamRoleStub)).to.be.equal(true);
expect(createStateMachineStub.calledAfter(deleteStateMachineStub)).to.be.equal(true);

serverlessStepFunctions.yamlParse.restore();
serverlessStepFunctions.getStateMachineArn.restore();
serverlessStepFunctions.getFunctionArns.restore();
serverlessStepFunctions.getActivityArns.restore();
serverlessStepFunctions.compile.restore();
serverlessStepFunctions.getIamRole.restore();
serverlessStepFunctions.deleteStateMachine.restore();
Expand Down

0 comments on commit 31e5d07

Please sign in to comment.