Skip to content

Commit e48e648

Browse files
committed
add activity deployment and removing
1 parent ff684f1 commit e48e648

File tree

8 files changed

+203
-47
lines changed

8 files changed

+203
-47
lines changed

lib/activity.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
const BbPromise = require('bluebird');
3+
const _ = require('lodash');
4+
5+
module.exports = {
6+
activityArns: {},
7+
deployedActivities: {},
8+
getActivityName(name) {
9+
return `${this.service}-${this.stage}-${name}`;
10+
},
11+
createActivity(name) {
12+
const activity = name || this.options.name;
13+
return this.provider.request('StepFunctions',
14+
'createActivity',
15+
{ name: this.getActivityName(activity) },
16+
this.options.stage,
17+
this.options.region)
18+
.then(() => BbPromise.resolve());
19+
},
20+
21+
deleteActivity(name) {
22+
const activity = name || this.options.name;
23+
return this.provider.request('StepFunctions',
24+
'deleteActivity',
25+
{ activityArn: this.activityArns[activity] },
26+
this.options.stage,
27+
this.options.region)
28+
.then(() => BbPromise.resolve());
29+
},
30+
31+
describeActivity(name) {
32+
const activity = name || this.options.name;
33+
return this.provider.request('StepFunctions',
34+
'describeActivity',
35+
{ activityArn: this.activityArns[activity] },
36+
this.options.stage,
37+
this.options.region)
38+
.then(() => {
39+
this.deployedActivities[activity] = 'deployed';
40+
return BbPromise.resolve();
41+
}).catch(() => {
42+
this.deployedActivities[activity] = 'notDeployed';
43+
return BbPromise.resolve();
44+
});
45+
},
46+
47+
checkActivitySetting(name) {
48+
const activity = name || this.options.name;
49+
if (this.serverless.service.stepFunctions.activities.indexOf(activity) < 0) {
50+
const errorMessage = [
51+
`Activity "${activity}" is not exists in serverless.yml`,
52+
].join('');
53+
throw new this.serverless.classes.Error(errorMessage);
54+
}
55+
},
56+
57+
getActivityArn(name) {
58+
const activity = name || this.options.name;
59+
return this.provider.request('STS',
60+
'getCallerIdentity',
61+
{},
62+
this.options.stage,
63+
this.options.region)
64+
.then((result) => {
65+
this.activityArns[activity] =
66+
`arn:aws:states:${this.region}:${result.Account}:activity:${this.getActivityName(activity)}`;
67+
return BbPromise.resolve();
68+
});
69+
},
70+
};

lib/dataProcessing.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ module.exports = {
1616
return this.serverless.yamlParser
1717
.parse(serverlessYmlPath)
1818
.then((serverlessFileParam) => {
19-
this.serverless.service.stepFunctions = serverlessFileParam.stepFunctions.stateMachines;
19+
this.serverless.service.stepFunctions = {};
20+
this.serverless.service.stepFunctions.stateMachines
21+
= serverlessFileParam.stepFunctions.stateMachines;
22+
this.serverless.service.stepFunctions.activities
23+
= serverlessFileParam.stepFunctions.activities;
2024
this.serverless.variables.populateService(this.serverless.pluginManager.cliOptions);
2125
return BbPromise.resolve();
2226
});
@@ -51,50 +55,54 @@ module.exports = {
5155
},
5256

5357
compile() {
54-
if (!this.serverless.service.stepFunctions) {
58+
if (!this.serverless.service.stepFunctions.stateMachines) {
5559
const errorMessage = [
5660
'stepFunctions statement does not exists in serverless.yml',
5761
].join('');
5862
throw new this.serverless.classes.Error(errorMessage);
5963
}
6064

61-
if (typeof this.serverless.service.stepFunctions[this.options.name] === 'undefined') {
65+
if (typeof this.serverless.service.stepFunctions.stateMachines[this.options.name]
66+
=== 'undefined') {
6267
const errorMessage = [
6368
`Step function "${this.options.name}" is not exists`,
6469
].join('');
6570
throw new this.serverless.classes.Error(errorMessage);
6671
}
6772

68-
this.serverless.service.stepFunctions[this.options.name] =
69-
JSON.stringify(this.serverless.service.stepFunctions[this.options.name]);
73+
this.serverless.service.stepFunctions.stateMachines[this.options.name] =
74+
JSON.stringify(this.serverless.service.stepFunctions.stateMachines[this.options.name]);
7075
_.forEach(this.functionArns, (value, key) => {
7176
const regExp = new RegExp(`"Resource":"${key}"`, 'g');
72-
this.serverless.service.stepFunctions[this.options.name] =
73-
this.serverless.service.stepFunctions[this.options.name]
77+
this.serverless.service.stepFunctions.stateMachines[this.options.name] =
78+
this.serverless.service.stepFunctions.stateMachines[this.options.name]
7479
.replace(regExp, `"Resource":"${value}"`);
7580
});
7681
return BbPromise.resolve();
7782
},
7883

7984
compileAll() {
80-
if (!this.serverless.service.stepFunctions) {
85+
if (!this.serverless.service.stepFunctions.stateMachines) {
8186
const errorMessage = [
8287
'stepFunctions statement does not exists in serverless.yml',
8388
].join('');
8489
throw new this.serverless.classes.Error(errorMessage);
8590
}
8691

87-
_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
88-
this.serverless.service.stepFunctions[stepFunctionKey] = JSON.stringify(stepFunctionObj);
89-
});
92+
_.forEach(this.serverless.service.stepFunctions.stateMachines,
93+
(stepFunctionObj, stepFunctionKey) => {
94+
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey]
95+
= JSON.stringify(stepFunctionObj);
96+
});
9097

9198
_.forEach(this.functionArns, (functionObj, functionKey) => {
9299
const regExp = new RegExp(`"Resource":"${functionKey}"`, 'g');
93-
_.forEach(this.serverless.service.stepFunctions, (stepFunctionObj, stepFunctionKey) => {
94-
this.serverless.service.stepFunctions[stepFunctionKey] =
95-
this.serverless.service.stepFunctions[stepFunctionKey]
100+
_.forEach(this.serverless.service.stepFunctions.stateMachines,
101+
(stepFunctionObj, stepFunctionKey) => {
102+
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey] =
103+
this.serverless.service.stepFunctions.stateMachines[stepFunctionKey]
96104
.replace(regExp, `"Resource":"${functionObj}"`);
97-
});
105+
});
98106
});
99107
return BbPromise.resolve();
100108
},

lib/dataProcessing.test.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('dataProsessing', () => {
1515
serverless = new Serverless();
1616
serverless.servicePath = true;
1717
serverless.service.service = 'step-functions';
18+
serverless.service.stepFunctions = {};
1819
serverless.service.functions = {
1920
first: {
2021
handler: true,
@@ -42,15 +43,21 @@ describe('dataProsessing', () => {
4243
let yamlParserStub;
4344
beforeEach(() => {
4445
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
45-
.returns(BbPromise.resolve({ stepFunctions: { stateMachines: 'stepFunctions' } }));
46+
.returns(BbPromise.resolve({
47+
stepFunctions: {
48+
stateMachines: 'stepFunctions',
49+
activities: 'my-activity',
50+
},
51+
}));
4652
serverlessStepFunctions.serverless.config.servicePath = 'servicePath';
4753
});
4854

4955
it('should yamlParse with correct params'
5056
, () => serverlessStepFunctions.yamlParse()
5157
.then(() => {
5258
expect(yamlParserStub.calledOnce).to.be.equal(true);
53-
expect(serverless.service.stepFunctions).to.be.equal('stepFunctions');
59+
expect(serverless.service.stepFunctions.stateMachines).to.be.equal('stepFunctions');
60+
expect(serverless.service.stepFunctions.activities).to.be.equal('my-activity');
5461
serverlessStepFunctions.serverless.yamlParser.parse.restore();
5562
})
5663
);
@@ -67,11 +74,17 @@ describe('dataProsessing', () => {
6774
it('should return resolve when variables exists in the yaml', () => {
6875
serverlessStepFunctions.serverless.yamlParser.parse.restore();
6976
yamlParserStub = sinon.stub(serverlessStepFunctions.serverless.yamlParser, 'parse')
70-
.returns(BbPromise.resolve({ stepFunctions: { stateMachines: '${self:defaults.region}' } }));
77+
.returns(BbPromise.resolve({
78+
stepFunctions: {
79+
stateMachines: '${self:defaults.region}',
80+
activities: '${self:defaults.region}',
81+
},
82+
}));
7183
serverlessStepFunctions.yamlParse()
7284
.then(() => {
7385
expect(yamlParserStub.calledOnce).to.be.equal(true);
74-
expect(serverless.service.stepFunctions).to.be.equal('us-east-1');
86+
expect(serverless.service.stepFunctions.stateMachines).to.be.equal('us-east-1');
87+
expect(serverless.service.stepFunctions.activities).to.be.equal('us-east-1');
7588
});
7689
});
7790
});
@@ -87,7 +100,7 @@ describe('dataProsessing', () => {
87100
});
88101

89102
it('should comple with correct params', () => {
90-
serverless.service.stepFunctions = {
103+
serverless.service.stepFunctions.stateMachines = {
91104
hellofunc: {
92105
States: {
93106
HelloWorld: {
@@ -98,13 +111,13 @@ describe('dataProsessing', () => {
98111
};
99112
serverlessStepFunctions.functionArns.first = 'lambdaArn';
100113
serverlessStepFunctions.compile().then(() => {
101-
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc)
114+
expect(serverlessStepFunctions.serverless.service.stepFunctions.stateMachines.hellofunc)
102115
.to.be.equal('{"States":{"HelloWorld":{"Resource":"lambdaArn"}}}');
103116
});
104117
});
105118

106119
it('should comple with correct params when nested Resource', () => {
107-
serverlessStepFunctions.serverless.service.stepFunctions = {
120+
serverlessStepFunctions.serverless.service.stepFunctions.stateMachines = {
108121
hellofunc: {
109122
States: {
110123
HelloWorld: {
@@ -124,7 +137,8 @@ describe('dataProsessing', () => {
124137
a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}';
125138
serverlessStepFunctions.functionArns.first = 'lambdaArn';
126139
serverlessStepFunctions.compile().then(() => {
127-
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a);
140+
expect(serverlessStepFunctions.serverless.service.stepFunctions.stateMachines.hellofunc)
141+
.to.be.equal(a);
128142
});
129143
});
130144
});
@@ -203,7 +217,7 @@ describe('dataProsessing', () => {
203217
});
204218

205219
it('should comple with correct params when nested Resource', () => {
206-
serverlessStepFunctions.serverless.service.stepFunctions = {
220+
serverlessStepFunctions.serverless.service.stepFunctions.stateMachines = {
207221
hellofunc: {
208222
States: {
209223
HelloWorld: {
@@ -223,7 +237,8 @@ describe('dataProsessing', () => {
223237
a += ':{"Resource":"lambdaArn","HelloWorld":{"Resource":"lambdaArn"}}}}}';
224238
serverlessStepFunctions.functionArns.first = 'lambdaArn';
225239
serverlessStepFunctions.compileAll().then(() => {
226-
expect(serverlessStepFunctions.serverless.service.stepFunctions.hellofunc).to.be.equal(a);
240+
expect(serverlessStepFunctions.serverless.service.stepFunctions.stateMachines.hellofunc)
241+
.to.be.equal(a);
227242
});
228243
});
229244
});

lib/iam.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module.exports = {
6464

6565
getIamRoles() {
6666
const promises = [];
67-
_.forEach(this.serverless.service.stepFunctions, (value, key) => {
67+
_.forEach(this.serverless.service.stepFunctions.stateMachines, (value, key) => {
6868
promises.push(key);
6969
});
7070

@@ -152,7 +152,7 @@ module.exports = {
152152

153153
deleteIamRoles() {
154154
const promises = [];
155-
_.forEach(this.serverless.service.stepFunctions, (value, key) => {
155+
_.forEach(this.serverless.service.stepFunctions.stateMachines, (value, key) => {
156156
promises.push(key);
157157
});
158158

lib/iam.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('iam', () => {
1515
serverless = new Serverless();
1616
serverless.servicePath = true;
1717
serverless.service.service = 'step-functions';
18+
serverless.service.stepFunctions = {};
1819
serverless.service.functions = {
1920
first: {
2021
handler: true,
@@ -178,7 +179,7 @@ describe('iam', () => {
178179
describe('#getIamRoles()', () => {
179180
let getIamRoleStub;
180181
beforeEach(() => {
181-
serverless.service.stepFunctions = {
182+
serverless.service.stepFunctions.stateMachines = {
182183
helloHello: 'value',
183184
hogeHoge: 'value',
184185
sssss: 'value',
@@ -199,7 +200,7 @@ describe('iam', () => {
199200
describe('#deleteIamRoles()', () => {
200201
let deleteIamRoleStub;
201202
beforeEach(() => {
202-
serverless.service.stepFunctions = {
203+
serverless.service.stepFunctions.stateMachines = {
203204
helloHello: 'value',
204205
hogeHoge: 'value',
205206
};

0 commit comments

Comments
 (0)