Skip to content

Commit 1b9bc7f

Browse files
committed
Refactor API event
1 parent c708a8f commit 1b9bc7f

File tree

11 files changed

+115
-96
lines changed

11 files changed

+115
-96
lines changed

lib/deploy/events/apiGateway/deployment.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module.exports = {
1919
},
2020
});
2121

22-
// create CLF Output for endpoint
2322
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs, {
2423
ServiceEndpoint: {
2524
Description: 'URL of the service endpoint',
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const BbPromise = require('bluebird');
4+
5+
module.exports = {
6+
getEndpointInfo() {
7+
const stackName = this.provider.naming.getStackName(this.options.stage);
8+
9+
// Get info from CloudFormation Outputs
10+
return this.provider.request('CloudFormation',
11+
'describeStacks',
12+
{ StackName: stackName },
13+
this.options.stage,
14+
this.options.region)
15+
.then((result) => {
16+
if (result) {
17+
const serviceEndpointOutputRegex = this.provider.naming
18+
.getServiceEndpointRegex();
19+
20+
// Endpoints
21+
result.Stacks[0].Outputs.filter(x => x.OutputKey.match(serviceEndpointOutputRegex))
22+
.forEach(x => {
23+
this.endpointInfo = x.OutputValue;
24+
});
25+
}
26+
27+
return BbPromise.resolve();
28+
});
29+
},
30+
};
File renamed without changes.

lib/deploy/events/apiGateway/methods.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ module.exports = {
77

88
compileMethods() {
99
this.apiGatewayMethodLogicalIds = [];
10-
11-
if (this.isCoreHttpevents()) {
12-
_.forEach(this.serverless.service.functions, (functionObject) => {
13-
_.forEach(functionObject.events, (event) => {
14-
if (_.has(event, 'http')) {
15-
const methodLogicalId = this.provider.naming
16-
.getMethodLogicalId(this.getResourceName(event.http.path), event.http.method);
17-
this.apiGatewayMethodLogicalIds.push(methodLogicalId);
18-
}
19-
});
20-
});
21-
}
22-
2310
this.pluginhttpValidated.events.forEach((event) => {
2411
const resourceId = this.getResourceId(event.http.path);
2512
const resourceName = this.getResourceName(event.http.path);

lib/deploy/events/apiGateway/resources.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = {
1111
this.apiGatewayResourceNames = {};
1212
this.apiGatewayResourceLogicalIds = {};
1313

14-
// ['users', 'users/create', 'users/create/something']
1514
resourcePaths.forEach(path => {
1615
const pathArray = path.split('/');
1716
const resourceName = this.provider.naming.normalizePath(path);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
const BbPromise = require('bluebird');
5+
6+
module.exports = {
7+
compileRestApi() {
8+
this.apiGatewayRestApiLogicalId = this.provider.naming.getRestApiLogicalId();
9+
10+
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
11+
[this.apiGatewayRestApiLogicalId]: {
12+
Type: 'AWS::ApiGateway::RestApi',
13+
Properties: {
14+
Name: this.provider.naming.getApiGatewayName(),
15+
},
16+
},
17+
});
18+
19+
return BbPromise.resolve();
20+
},
21+
};

lib/deploy/events/apiGateway/validate.js

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,6 @@
22

33
const _ = require('lodash');
44

5-
const DEFAULT_STATUS_CODES = {
6-
200: {
7-
pattern: '',
8-
},
9-
400: {
10-
pattern: '.*\\[400\\].*',
11-
},
12-
401: {
13-
pattern: '.*\\[401\\].*',
14-
},
15-
403: {
16-
pattern: '.*\\[403\\].*',
17-
},
18-
404: {
19-
pattern: '.*\\[404\\].*',
20-
},
21-
422: {
22-
pattern: '.*\\[422\\].*',
23-
},
24-
500: {
25-
pattern: '.*(Process\\s?exited\\s?before\\s?completing\\s?request|\\[500\\]).*',
26-
},
27-
502: {
28-
pattern: '.*\\[502\\].*',
29-
},
30-
504: {
31-
pattern: '.*\\[504\\].*',
32-
},
33-
};
34-
355
module.exports = {
366
httpValidate() {
377
const events = [];
@@ -42,23 +12,8 @@ module.exports = {
4212
_.forEach(stateMachineObj.events, (event) => {
4313
if (_.has(event, 'http')) {
4414
const http = this.getHttp(event, stateMachineName);
45-
4615
http.path = this.getHttpPath(http, stateMachineName);
4716
http.method = this.getHttpMethod(http, stateMachineName);
48-
http.request = {};
49-
50-
http.request.passThrough = 'NEVER';
51-
http.response = {};
52-
53-
if (http.response.statusCodes) {
54-
http.response.statusCodes = _.assign({}, http.response.statusCodes);
55-
56-
if (!_.some(http.response.statusCodes, code => code.pattern === '')) {
57-
http.response.statusCodes['200'] = DEFAULT_STATUS_CODES['200'];
58-
}
59-
} else {
60-
http.response.statusCodes = DEFAULT_STATUS_CODES;
61-
}
6217

6318
events.push({
6419
stateMachineName,
@@ -85,7 +40,7 @@ module.exports = {
8540
});
8641
},
8742

88-
getHttp(event, functionName) {
43+
getHttp(event, stateMachineName) {
8944
if (typeof event.http === 'object') {
9045
return event.http;
9146
} else if (typeof event.http === 'string') {
@@ -95,7 +50,7 @@ module.exports = {
9550
};
9651
}
9752
const errorMessage = [
98-
`Invalid http event in function "${functionName}"`,
53+
`Invalid http event in stateMachine "${stateMachineName}"`,
9954
' in serverless.yml.',
10055
' If you define an http event, make sure you pass a valid value for it,',
10156
' either as string syntax, or object syntax.',
@@ -104,12 +59,12 @@ module.exports = {
10459
throw new this.serverless.classes.Error(errorMessage);
10560
},
10661

107-
getHttpPath(http, functionName) {
62+
getHttpPath(http, stateMachineName) {
10863
if (typeof http.path === 'string') {
10964
return http.path.replace(/^\//, '').replace(/\/$/, '');
11065
}
11166
const errorMessage = [
112-
`Missing or invalid "path" property in function "${functionName}"`,
67+
`Missing or invalid "path" property in stateMachine "${stateMachineName}"`,
11368
' for http event in serverless.yml.',
11469
' If you define an http event, make sure you pass a valid value for it,',
11570
' either as string syntax, or object syntax.',
@@ -118,7 +73,7 @@ module.exports = {
11873
throw new this.serverless.classes.Error(errorMessage);
11974
},
12075

121-
getHttpMethod(http, functionName) {
76+
getHttpMethod(http, stateMachineName) {
12277
if (typeof http.method === 'string') {
12378
const method = http.method.toLowerCase();
12479

@@ -127,15 +82,15 @@ module.exports = {
12782
];
12883
if (allowedMethods.indexOf(method) === -1) {
12984
const errorMessage = [
130-
`Invalid APIG method "${http.method}" in function "${functionName}".`,
85+
`Invalid APIG method "${http.method}" in stateMachine "${stateMachineName}".`,
13186
` AWS supported methods are: ${allowedMethods.join(', ')}.`,
13287
].join('');
13388
throw new this.serverless.classes.Error(errorMessage);
13489
}
13590
return method;
13691
}
13792
const errorMessage = [
138-
`Missing or invalid "method" property in function "${functionName}"`,
93+
`Missing or invalid "method" property in stateMachine "${stateMachineName}"`,
13994
' for http event in serverless.yml.',
14095
' If you define an http event, make sure you pass a valid value for it,',
14196
' either as string syntax, or object syntax.',

lib/deploy/stepFunctions/compileActivities.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
this.getAllActivities().forEach((activityName) => {
99
const Name = this.getActivity(activityName);
1010
const activityLogicalId = this.getActivityLogicalId(activityName);
11+
const activityOutputLogicalId = this.getActivityOutputLogicalId(activityName);
1112
const activityTemplate = `
1213
{
1314
"Type": "AWS::StepFunctions::Activity",
@@ -23,6 +24,21 @@ module.exports = {
2324

2425
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
2526
newActivityObject);
27+
28+
const activityOutPutObject = {
29+
Description: 'Current StateMachine Arn',
30+
Value: {
31+
Ref: activityLogicalId,
32+
},
33+
};
34+
35+
const newActivityOutPutObject = {
36+
[activityOutputLogicalId]: activityOutPutObject,
37+
};
38+
39+
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs,
40+
newActivityOutPutObject
41+
);
2642
return BbPromise.resolve();
2743
});
2844
}

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = {
4848
}
4949

5050
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName);
51+
const stateMachineOutputLogicalId = this.getStateMachineOutputLogicalId(stateMachineName);
5152

5253
const stateMachineTemplate = `
5354
{
@@ -67,6 +68,21 @@ module.exports = {
6768

6869
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
6970
newStateMachineObject);
71+
72+
const stateMachineOutPutObject = {
73+
Description: 'Current StateMachine Arn',
74+
Value: {
75+
Ref: stateMachineLogicalId,
76+
},
77+
};
78+
79+
const newStateMachineOutPutObject = {
80+
[stateMachineOutputLogicalId]: stateMachineOutPutObject,
81+
};
82+
83+
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Outputs,
84+
newStateMachineOutPutObject
85+
);
7086
return BbPromise.resolve();
7187
});
7288
}

lib/index.js

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ const compileIamRole = require('./deploy/stepFunctions/compileIamRole');
77
const httpValidate = require('./deploy/events/apiGateway/validate');
88
const httpResources = require('./deploy/events/apiGateway/resources');
99
const httpMethods = require('./deploy/events/apiGateway/methods');
10-
const httpIamRole = require('./deploy/events/apiGateway/role');
10+
const httpIamRole = require('./deploy/events/apiGateway/iamRole');
1111
const httpDeployment = require('./deploy/events/apiGateway/deployment');
12-
const coreCompileRestApi =
13-
require('serverless/lib/plugins/aws/deploy/compile/events/apiGateway/lib/restApi');
14-
const coreInfo =
15-
require('serverless/lib/plugins/aws/info/getStackInfo');
12+
const httpRestApi = require('./deploy/events/apiGateway/restApi');
13+
const httpInfo = require('./deploy/events/apiGateway/endpointInfo');
1614
const yamlParser = require('./yamlParser');
1715
const naming = require('./naming');
1816
const _ = require('lodash');
@@ -27,23 +25,13 @@ class ServerlessStepFunctions {
2725
this.region = this.provider.getRegion();
2826
this.stage = this.provider.getStage();
2927

30-
_.forEach(this.serverless.pluginManager.getPlugins(), (pluginObject, index) => {
31-
if (pluginObject.constructor.toString().match(/class AwsCompileApigEvents/)) {
32-
this.awsCompileApigEventsKey = index;
33-
}
34-
});
35-
36-
this.awsCompileApigEvents = this.serverless.pluginManager
37-
.getPlugins()[this.awsCompileApigEventsKey];
38-
// .validate().events.length;
39-
4028
Object.assign(
4129
this,
4230
compileStateMachines,
4331
compileActivities,
4432
compileIamRole,
45-
coreCompileRestApi,
46-
coreInfo,
33+
httpRestApi,
34+
httpInfo,
4735
httpValidate,
4836
httpResources,
4937
httpMethods,
@@ -104,19 +92,17 @@ class ServerlessStepFunctions {
10492

10593
if (this.pluginhttpValidated.events.length === 0) {
10694
return BbPromise.resolve();
107-
} else if (!this.isCoreHttpevents()) {
108-
return BbPromise.bind(this)
109-
.then(this.compileRestApi)
110-
.then(this.compileResources)
111-
.then(this.compileMethods)
112-
.then(this.compileHttpIamRole)
113-
.then(this.compileDeployment);
11495
}
96+
11597
return BbPromise.bind(this)
116-
.then(this.compileResources);
98+
.then(this.compileRestApi)
99+
.then(this.compileResources)
100+
.then(this.compileMethods)
101+
.then(this.compileHttpIamRole)
102+
.then(this.compileDeployment);
117103
},
118104
'after:deploy:deploy': () => BbPromise.bind(this)
119-
.then(this.getStackInfo)
105+
.then(this.getEndpointInfo)
120106
.then(this.display),
121107
};
122108
}
@@ -128,8 +114,8 @@ class ServerlessStepFunctions {
128114
display() {
129115
let message = '';
130116

131-
const info = this.gatheredData.info;
132-
message += `${chalk.yellow.underline('Serverless StepFunctions OutPut')}\n`;
117+
const endpointInfo = this.endpointInfo;
118+
message += `${chalk.yellow.underline('Serverless StepFunctions OutPuts')}\n`;
133119
message += `${chalk.yellow('endpoints:')}`;
134120
_.forEach(this.getAllStateMachines(), (stateMachineName) => {
135121
const stateMachineObj = this.getStateMachine(stateMachineName);
@@ -146,7 +132,7 @@ class ServerlessStepFunctions {
146132
path = event.http.split(' ')[1];
147133
}
148134
path = path !== '/' ? `/${path.split('/').filter(p => p !== '').join('/')}` : '';
149-
message += `\n ${method} - ${info.endpoint}${path}`;
135+
message += `\n ${method} - ${endpointInfo}${path}`;
150136
}
151137
});
152138
});

0 commit comments

Comments
 (0)