From 6bb55dbd631f0a7cba2cbdf91c6ff035875671fa Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Wed, 15 Jun 2016 18:39:48 +0200 Subject: [PATCH] Refactoring and code cleanup So that the code looks everywhere the familiar. --- .../awsCompileApigEvents.js | 4 +- .../lib/compileDeployment.js | 18 +++---- .../lib/compileMethods.js | 44 ++++++++--------- .../lib/compilePermissions.js | 12 +++-- .../lib/compileResources.js | 4 +- .../lib/compileRestApi.js | 25 +++++----- .../awsCompileApigEvents/lib/compileStage.js | 35 ++++++-------- lib/templates/serverless.yaml | 48 +++++++++---------- 8 files changed, 92 insertions(+), 98 deletions(-) diff --git a/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js b/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js index 7d8e357ad..97e4dc7c9 100644 --- a/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js +++ b/lib/plugins/awsCompileApigEvents/awsCompileApigEvents.js @@ -1,7 +1,7 @@ 'use strict'; const BbPromise = require('bluebird'); -const _ = require('lodash'); +const forEach = require('lodash').forEach; const compileRestApi = require('./lib/compileRestApi'); const compileResources = require('./lib/compileResources'); @@ -32,7 +32,7 @@ class AwsCompileApigEvents { let noEndpoints = true; - _.forEach(this.serverless.service.functions, functionObj => { + forEach(this.serverless.service.functions, functionObj => { if (functionObj.events && functionObj.events.aws && functionObj.events.aws.http_endpoints) { noEndpoints = false; diff --git a/lib/plugins/awsCompileApigEvents/lib/compileDeployment.js b/lib/plugins/awsCompileApigEvents/lib/compileDeployment.js index 2997494ee..13d25a9ec 100644 --- a/lib/plugins/awsCompileApigEvents/lib/compileDeployment.js +++ b/lib/plugins/awsCompileApigEvents/lib/compileDeployment.js @@ -6,15 +6,15 @@ const BbPromise = require('bluebird'); module.exports = { compileDeployment() { const deploymentTemplate = ` - { - "Type" : "AWS::ApiGateway::Deployment", - "DependsOn" : "${this.methodDep}", - "Properties" : { - "RestApiId" : { "Ref": "RestApiApigEvent" }, - "StageName" : "${this.options.stage}" - } - } - `; + { + "Type" : "AWS::ApiGateway::Deployment", + "DependsOn" : "${this.methodDep}", + "Properties" : { + "RestApiId" : { "Ref": "RestApiApigEvent" }, + "StageName" : "${this.options.stage}" + } + } + `; const newDeploymentObject = { DeploymentApigEvent: JSON.parse(deploymentTemplate), diff --git a/lib/plugins/awsCompileApigEvents/lib/compileMethods.js b/lib/plugins/awsCompileApigEvents/lib/compileMethods.js index e06cbce85..38874a6b0 100644 --- a/lib/plugins/awsCompileApigEvents/lib/compileMethods.js +++ b/lib/plugins/awsCompileApigEvents/lib/compileMethods.js @@ -26,29 +26,29 @@ module.exports = { '/invocations'; const methodTemplate = ` - { - "Type" : "AWS::ApiGateway::Method", - "Properties" : { - "AuthorizationType" : "NONE", - "HttpMethod" : "${method.toUpperCase()}", - "MethodResponses" : [ - { - "ResponseModels" : {}, - "ResponseParameters" : {}, - "StatusCode" : "200" - } - ], - "RequestParameters" : {}, - "Integration" : { - "IntegrationHttpMethod" : "${method.toUpperCase()}", - "Type" : "AWS", - "Uri" : "${lambdaUri}" - }, - "ResourceId" : { "Ref": "${resourceLogicalId}" }, - "RestApiId" : { "Ref": "RestApiApigEvent" } - } + { + "Type" : "AWS::ApiGateway::Method", + "Properties" : { + "AuthorizationType" : "NONE", + "HttpMethod" : "${method.toUpperCase()}", + "MethodResponses" : [ + { + "ResponseModels" : {}, + "ResponseParameters" : {}, + "StatusCode" : "200" + } + ], + "RequestParameters" : {}, + "Integration" : { + "IntegrationHttpMethod" : "${method.toUpperCase()}", + "Type" : "AWS", + "Uri" : "${lambdaUri}" + }, + "ResourceId" : { "Ref": "${resourceLogicalId}" }, + "RestApiId" : { "Ref": "RestApiApigEvent" } } - `; + } + `; const methodObject = { [`${normalizedMethod}MethodApigEvent${extractedResourceId}`]: JSON.parse(methodTemplate), diff --git a/lib/plugins/awsCompileApigEvents/lib/compilePermissions.js b/lib/plugins/awsCompileApigEvents/lib/compilePermissions.js index 196913555..2dc6c2f58 100644 --- a/lib/plugins/awsCompileApigEvents/lib/compilePermissions.js +++ b/lib/plugins/awsCompileApigEvents/lib/compilePermissions.js @@ -1,12 +1,13 @@ 'use strict'; const BbPromise = require('bluebird'); -const _ = require('lodash'); +const forEach = require('lodash').forEach; +const merge = require('lodash').merge; module.exports = { compilePermissions() { - _.forEach(this.serverless.service.functions, (functionObj, functionName) => { - _.forEach(functionObj.events.aws.http_endpoints, (path, method) => { + forEach(this.serverless.service.functions, (functionObj, functionName) => { + forEach(functionObj.events.aws.http_endpoints, (path, method) => { const normalizedMethod = method[0].toUpperCase() + method.substr(1); const permissionTemplate = ` { @@ -16,7 +17,8 @@ module.exports = { "Action": "lambda:InvokeFunction", "Principal": "apigateway.amazonaws.com" } - }`; + } + `; const permissionLogicalId = `ApigPermission${normalizedMethod}${this .resourcePaths.indexOf(path)}`; @@ -24,7 +26,7 @@ module.exports = { [permissionLogicalId]: JSON.parse(permissionTemplate), }; - _.merge(this.serverless.service.resources.aws.Resources, newPermissionObject); + merge(this.serverless.service.resources.aws.Resources, newPermissionObject); }); }); diff --git a/lib/plugins/awsCompileApigEvents/lib/compileResources.js b/lib/plugins/awsCompileApigEvents/lib/compileResources.js index dbbfb1d6e..24f545c76 100644 --- a/lib/plugins/awsCompileApigEvents/lib/compileResources.js +++ b/lib/plugins/awsCompileApigEvents/lib/compileResources.js @@ -9,8 +9,8 @@ module.exports = { this.resourcePaths = []; this.resourceLogicalIds = {}; - forEach(this.serverless.service.functions, (functionObj) => { - forEach(functionObj.events.aws.http_endpoints, (pathParam) => { + forEach(this.serverless.service.functions, (functionObject) => { + forEach(functionObject.events.aws.http_endpoints, (pathParam) => { let path = pathParam; while (path !== '') { if (this.resourcePaths.indexOf(path) === -1) { diff --git a/lib/plugins/awsCompileApigEvents/lib/compileRestApi.js b/lib/plugins/awsCompileApigEvents/lib/compileRestApi.js index d0df1c23b..b8b7cd5e3 100644 --- a/lib/plugins/awsCompileApigEvents/lib/compileRestApi.js +++ b/lib/plugins/awsCompileApigEvents/lib/compileRestApi.js @@ -5,22 +5,21 @@ const BbPromise = require('bluebird'); module.exports = { compileRestApi() { - this.serverless.service.getAllFunctions().forEach((functionName) => { - const functionObject = this.serverless.service.getFunction(functionName); - const restApiTemplate = ` - { - "Type" : "AWS::ApiGateway::RestApi", - "Properties" : { - "Name" : "${this.options.stage}-${this.serverless.service.service}" - } + this.serverless.service.getAllFunctions().forEach(() => { + const restApiTemplate = ` + { + "Type" : "AWS::ApiGateway::RestApi", + "Properties" : { + "Name" : "${this.options.stage}-${this.serverless.service.service}" } - `; + } + `; - const newRestApiObject = { - RestApiApigEvent: JSON.parse(restApiTemplate), - }; + const newRestApiObject = { + RestApiApigEvent: JSON.parse(restApiTemplate), + }; - merge(this.serverless.service.resources.aws.Resources, newRestApiObject); + merge(this.serverless.service.resources.aws.Resources, newRestApiObject); }); return BbPromise.resolve(); diff --git a/lib/plugins/awsCompileApigEvents/lib/compileStage.js b/lib/plugins/awsCompileApigEvents/lib/compileStage.js index 4f37bbab2..5c9451f7a 100644 --- a/lib/plugins/awsCompileApigEvents/lib/compileStage.js +++ b/lib/plugins/awsCompileApigEvents/lib/compileStage.js @@ -5,30 +5,23 @@ const BbPromise = require('bluebird'); module.exports = { compileStage() { - this.serverless.service.getAllFunctions().forEach((functionName) => { - const functionObject = this.serverless.service.getFunction(functionName); - - // checking all three levels in the obj tree - // to avoid "can't read property of undefined" error - if (functionObject.events && functionObject.events.aws - && functionObject.events.aws.http_endpoints) { - const stageTemplate = ` - { - "Type" : "AWS::ApiGateway::Stage", - "Properties" : { - "DeploymentId": { "Ref": "DeploymentApigEvent" }, - "RestApiId" : { "Ref": "RestApiApigEvent" }, - "StageName" : "${this.options.stage}" - } + this.serverless.service.getAllFunctions().forEach(() => { + const stageTemplate = ` + { + "Type" : "AWS::ApiGateway::Stage", + "Properties" : { + "DeploymentId": { "Ref": "DeploymentApigEvent" }, + "RestApiId" : { "Ref": "RestApiApigEvent" }, + "StageName" : "${this.options.stage}" } - `; + } + `; - const newStageObject = { - [`StageApigEvent`]: JSON.parse(stageTemplate), - }; + const newStageObject = { + StageApigEvent: JSON.parse(stageTemplate), + }; - merge(this.serverless.service.resources.aws.Resources, newStageObject); - } + merge(this.serverless.service.resources.aws.Resources, newStageObject); }); return BbPromise.resolve(); diff --git a/lib/templates/serverless.yaml b/lib/templates/serverless.yaml index 8c54e1c3d..e43eb97bf 100644 --- a/lib/templates/serverless.yaml +++ b/lib/templates/serverless.yaml @@ -13,30 +13,30 @@ default_providers: &default_providers disabled: false functions: # if this gets too big, you can always use JSON-REF - hello: - name_template: ${stage}-${service}-${name} # "name" references the function name, service the whole service name - handler: handler.hello - # only the following paths will be included in the resulting artefact uploaded to Lambda. Without specific include everything in the current folder will be included - include: - - lib - - functions - # The following paths will be excluded from the resulting artefact. If both include and exclude are defined we first apply the include, then the exclude so files are guaranteed to be excluded. - exclude: - - tmp - - .git - provider: - <<: *default_providers - events: - aws: - # s3: - # - first-bucket - http_endpoints: - post: users/create - # schedule: rate(10 minutes) - azure: - http_endpoint: - direction: in - name: req + hello: + name_template: ${stage}-${service}-${name} # "name" references the function name, service the whole service name + handler: handler.hello + # only the following paths will be included in the resulting artefact uploaded to Lambda. Without specific include everything in the current folder will be included + include: + - lib + - functions + # The following paths will be excluded from the resulting artefact. If both include and exclude are defined we first apply the include, then the exclude so files are guaranteed to be excluded. + exclude: + - tmp + - .git + provider: + <<: *default_providers + events: + aws: + # s3: + # - first-bucket + http_endpoints: + post: users/create + # schedule: rate(10 minutes) + azure: + http_endpoints: + direction: in + name: req resources: aws_name_template: ${stage}-${service}-${name} # "name" references the resource name, service the whole service name