From fef9b68e5a453ea56bb1cd7d68f790789f404b6c Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Thu, 10 Nov 2016 19:05:02 +0700 Subject: [PATCH] refactored naming-js method naming --- .../compile/events/apiGateway/lib/apiKeys.js | 2 +- .../events/apiGateway/lib/authorizers.js | 2 +- .../compile/events/apiGateway/lib/cors.js | 2 +- .../events/apiGateway/lib/deployment.js | 3 +- .../apiGateway/lib/method/authorization.js | 2 +- .../events/apiGateway/lib/method/index.js | 2 +- .../apiGateway/lib/method/integration.js | 2 +- .../events/apiGateway/lib/permissions.js | 6 +- .../events/apiGateway/lib/resources.js | 4 +- .../compile/events/apiGateway/lib/restApi.js | 2 +- .../compile/events/apiGateway/lib/validate.js | 2 +- .../aws/deploy/compile/events/s3/index.js | 8 +- .../deploy/compile/events/schedule/index.js | 8 +- .../aws/deploy/compile/events/sns/index.js | 6 +- .../aws/deploy/compile/events/stream/index.js | 2 +- .../aws/deploy/compile/functions/index.js | 4 +- .../aws/deploy/lib/mergeIamTemplates.js | 2 +- lib/plugins/aws/info/index.js | 2 +- lib/plugins/aws/lib/naming.js | 135 ++++++++--------- lib/plugins/aws/provider/awsProvider.js | 34 ++--- lib/plugins/aws/provider/awsProvider.test.js | 11 +- lib/plugins/aws/tests/naming.js | 136 ++++++++---------- 22 files changed, 173 insertions(+), 204 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js index 86558f479..646fa14aa 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/apiKeys.js @@ -18,7 +18,7 @@ module.exports = { } const apiKeyLogicalId = this.provider.naming - .getLogicalApiGatewayApiKeyName(apiKeyNumber); + .getApiKeyLogicalId(apiKeyNumber); _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { [apiKeyLogicalId]: { diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js index 48ccf1771..e0649b617 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/authorizers.js @@ -31,7 +31,7 @@ module.exports = { }); } - const authorizerLogicalId = this.provider.naming.getLogicalAuthorizerName(authorizer.name); + const authorizerLogicalId = this.provider.naming.getAuthorizerLogicalId(authorizer.name); _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { [authorizerLogicalId]: { diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/cors.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/cors.js index be1c79af3..465cfcda4 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/cors.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/cors.js @@ -10,7 +10,7 @@ module.exports = { const resourceName = this.getResourceName(path); const resourceRef = this.getResourceId(path); const corsMethodLogicalId = this.provider.naming - .getLogicalApiGatewayMethodName(resourceName, 'options'); + .getMethodLogicalId(resourceName, 'options'); const preflightHeaders = { 'Access-Control-Allow-Origin': `'${config.origins.join(',')}'`, diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/deployment.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/deployment.js index 8312b88c8..90b03aa87 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/deployment.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/deployment.js @@ -5,7 +5,8 @@ const BbPromise = require('bluebird'); module.exports = { compileDeployment() { - this.apiGatewayDeploymentLogicalId = this.provider.naming.getApiGatewayDeploymentId(); + this.apiGatewayDeploymentLogicalId = this.provider.naming + .generateApiGatewayDeploymentLogicalId(); _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { [this.apiGatewayDeploymentLogicalId]: { diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/authorization.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/authorization.js index 13530b9b2..54b42a32b 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/authorization.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/authorization.js @@ -4,7 +4,7 @@ module.exports = { getMethodAuthorization(http) { if (http.authorizer) { const authorizerLogicalId = this.provider.naming - .getLogicalAuthorizerName(http.authorizer.name); + .getAuthorizerLogicalId(http.authorizer.name); return { Properties: { diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/index.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/index.js index ebe85dd7f..07190ad86 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/index.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/index.js @@ -34,7 +34,7 @@ module.exports = { ); const methodLogicalId = this.provider.naming - .getLogicalApiGatewayMethodName(resourceName, event.http.method); + .getMethodLogicalId(resourceName, event.http.method); this.apiGatewayMethodLogicalIds.push(methodLogicalId); diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/integration.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/integration.js index 37a76a530..2d429b180 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/integration.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/method/integration.js @@ -5,7 +5,7 @@ const _ = require('lodash'); module.exports = { getMethodIntegration(http, functionName) { const lambdaLogicalId = this.provider.naming - .getLogicalLambdaName(functionName); + .getLambdaLogicalId(functionName); const integration = { IntegrationHttpMethod: 'POST', Type: http.integration, diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/permissions.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/permissions.js index e3c152021..3085327cf 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/permissions.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/permissions.js @@ -8,9 +8,9 @@ module.exports = { compilePermissions() { this.validated.events.forEach((event) => { const lambdaPermissionLogicalId = this.provider.naming - .getLambdaApiGatewayPermissionName(event.functionName); + .getLambdaApiGatewayPermissionLogicalId(event.functionName); const lambdaLogicalId = this.provider.naming - .getLogicalLambdaName(event.functionName); + .getLambdaLogicalId(event.functionName); _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { [lambdaPermissionLogicalId]: { @@ -28,7 +28,7 @@ module.exports = { if (event.http.authorizer) { const authorizer = event.http.authorizer; const authorizerPermissionLogicalId = this.provider.naming - .getLambdaApiGatewayPermissionName(authorizer.name); + .getLambdaApiGatewayPermissionLogicalId(authorizer.name); _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { [authorizerPermissionLogicalId]: { diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/resources.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/resources.js index bf63accbc..ea714f170 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/resources.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/resources.js @@ -14,8 +14,8 @@ module.exports = { // ['users', 'users/create', 'users/create/something'] resourcePaths.forEach(path => { const pathArray = path.split('/'); - const resourceName = this.provider.naming.getNormalizedApiGatewayResourceName(path); - const resourceLogicalId = this.provider.naming.getLogicalApiGatewayResourceName(path); + const resourceName = this.provider.naming.normalizePath(path); + const resourceLogicalId = this.provider.naming.getResourceLogicalId(path); const pathPart = pathArray.pop(); const parentPath = pathArray.join('/'); const parentRef = this.getResourceId(parentPath); diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/restApi.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/restApi.js index b0b2c38b3..ce75a5527 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/restApi.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/restApi.js @@ -5,7 +5,7 @@ const BbPromise = require('bluebird'); module.exports = { compileRestApi() { - this.apiGatewayRestApiLogicalId = this.provider.naming.getLogicalApiGatewayName(); + this.apiGatewayRestApiLogicalId = this.provider.naming.getRestApiLogicalId(); _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, { [this.apiGatewayRestApiLogicalId]: { diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js index 19d601371..9c974f3da 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js @@ -375,7 +375,7 @@ module.exports = { getLambdaArn(name) { this.serverless.service.getFunction(name); - const lambdaLogicalId = this.provider.naming.getLogicalLambdaName(name); + const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(name); return { 'Fn::GetAtt': [lambdaLogicalId, 'Arn'] }; }, diff --git a/lib/plugins/aws/deploy/compile/events/s3/index.js b/lib/plugins/aws/deploy/compile/events/s3/index.js index b3c8a809a..e98e883df 100644 --- a/lib/plugins/aws/deploy/compile/events/s3/index.js +++ b/lib/plugins/aws/deploy/compile/events/s3/index.js @@ -79,7 +79,7 @@ class AwsCompileS3Events { } const lambdaLogicalId = this.provider.naming - .getLogicalLambdaName(functionName); + .getLambdaLogicalId(functionName); // check if the bucket already defined // in another S3 event in the service @@ -139,7 +139,7 @@ class AwsCompileS3Events { }; const bucketLogicalId = this.provider.naming - .getLogicalBucketName(bucketName); + .getBucketLogicalId(bucketName); const bucketCFResource = { [bucketLogicalId]: bucketTemplate, }; @@ -152,7 +152,7 @@ class AwsCompileS3Events { // by adding Lambda::Permission resource for each s3EnabledFunctions.forEach(functionName => { const lambdaLogicalId = this.provider.naming - .getLogicalLambdaName(functionName); + .getLambdaLogicalId(functionName); const permissionTemplate = { Type: 'AWS::Lambda::Permission', Properties: { @@ -167,7 +167,7 @@ class AwsCompileS3Events { }, }; const lambdaPermissionLogicalId = this.provider.naming - .getLambdaS3PermissionName(functionName); + .getLambdaS3PermissionLogicalId(functionName); const permissionCFResource = { [lambdaPermissionLogicalId]: permissionTemplate, }; diff --git a/lib/plugins/aws/deploy/compile/events/schedule/index.js b/lib/plugins/aws/deploy/compile/events/schedule/index.js index 79d5c19d4..a2814f922 100644 --- a/lib/plugins/aws/deploy/compile/events/schedule/index.js +++ b/lib/plugins/aws/deploy/compile/events/schedule/index.js @@ -75,12 +75,12 @@ class AwsCompileScheduledEvents { } const lambdaLogicalId = this.provider.naming - .getLogicalLambdaName(functionName); + .getLambdaLogicalId(functionName); const scheduleLogicalId = this.provider.naming - .getCloudWatchEventName(functionName, scheduleNumberInFunction); + .getScheduleLogicalId(functionName, scheduleNumberInFunction); const lambdaPermissionLogicalId = this.provider.naming - .getLambdaCloudWatchEventPermissionName(functionName, scheduleNumberInFunction); - const scheduleId = this.provider.naming.getCloudWatchEventId(functionName); + .getLambdaSchedulePermissionLogicalId(functionName, scheduleNumberInFunction); + const scheduleId = this.provider.naming.getScheduleId(functionName); const scheduleTemplate = ` { diff --git a/lib/plugins/aws/deploy/compile/events/sns/index.js b/lib/plugins/aws/deploy/compile/events/sns/index.js index 194c0730c..83995d4c0 100644 --- a/lib/plugins/aws/deploy/compile/events/sns/index.js +++ b/lib/plugins/aws/deploy/compile/events/sns/index.js @@ -51,11 +51,11 @@ class AwsCompileSNSEvents { } const lambdaLogicalId = this.provider.naming - .getLogicalLambdaName(functionName); + .getLambdaLogicalId(functionName); const topicLogicalId = this.provider.naming - .getLogicalSnsTopicName(topicName); + .getTopicLogicalId(topicName); const lambdaPermissionLogicalId = this.provider.naming - .getLambdaSnsTopicPermissionName(functionName, topicName); + .getLambdaSnsPermissionLogicalId(functionName, topicName); const snsTemplate = ` { diff --git a/lib/plugins/aws/deploy/compile/events/stream/index.js b/lib/plugins/aws/deploy/compile/events/stream/index.js index feaa09ab4..0b878fdd2 100644 --- a/lib/plugins/aws/deploy/compile/events/stream/index.js +++ b/lib/plugins/aws/deploy/compile/events/stream/index.js @@ -61,7 +61,7 @@ class AwsCompileStreamEvents { const streamName = EventSourceArn.split('/')[1]; const lambdaLogicalId = this.provider.naming - .getLogicalLambdaName(functionName); + .getLambdaLogicalId(functionName); const streamLogicalId = this.provider.naming .getStreamLogicalId(functionName, streamType, streamName); diff --git a/lib/plugins/aws/deploy/compile/functions/index.js b/lib/plugins/aws/deploy/compile/functions/index.js index a7953ad56..7a2c28dd7 100644 --- a/lib/plugins/aws/deploy/compile/functions/index.js +++ b/lib/plugins/aws/deploy/compile/functions/index.js @@ -104,9 +104,9 @@ class AwsCompileFunctions { } const functionLogicalId = this.provider.naming - .getLogicalLambdaName(functionName); + .getLambdaLogicalId(functionName); const functionOutputLogicalId = this.provider.naming - .getLogicalLambdaArnName(functionName); + .getLambdaOutputLogicalId(functionName); const newFunctionObject = { [functionLogicalId]: newFunction, }; diff --git a/lib/plugins/aws/deploy/lib/mergeIamTemplates.js b/lib/plugins/aws/deploy/lib/mergeIamTemplates.js index f22dee26c..3df6998b5 100644 --- a/lib/plugins/aws/deploy/lib/mergeIamTemplates.js +++ b/lib/plugins/aws/deploy/lib/mergeIamTemplates.js @@ -80,7 +80,7 @@ module.exports = { this.serverless.service.getAllFunctions().forEach((functionName) => { const functionObject = this.serverless.service.getFunction(functionName); const logGroupLogicalId = this.provider.naming - .getLogicalLogGroupName(functionName); + .getLogGroupLogicalId(functionName); const logGroupTemplate = ` { "${logGroupLogicalId}": { diff --git a/lib/plugins/aws/info/index.js b/lib/plugins/aws/info/index.js index 83b85681b..b3fb39a1b 100644 --- a/lib/plugins/aws/info/index.js +++ b/lib/plugins/aws/info/index.js @@ -55,7 +55,7 @@ class AwsInfo { outputs = result.Stacks[0].Outputs; const lambdaArnOutputRegex = this.provider.naming - .getLogicalLambdaArnNameRegex(); + .getLambdaOutputLogicalIdRegex(); const serviceEndpointOutputRegex = this.provider.naming .getServiceEndpointRegex(); diff --git a/lib/plugins/aws/lib/naming.js b/lib/plugins/aws/lib/naming.js index c04663241..beb7fcda2 100644 --- a/lib/plugins/aws/lib/naming.js +++ b/lib/plugins/aws/lib/naming.js @@ -3,16 +3,15 @@ const _ = require('lodash'); module.exports = { - /* - * General Name Normalization - */ + + // General normalizeName(name) { return `${_.upperFirst(name)}`; }, normalizeNameToAlphaNumericOnly(name) { return this.normalizeName(name.replace(/[^0-9A-Za-z]/g, '')); }, - normalizePathtoCapitalAlphaNumbericOnlyWithReplacement(path) { + normalizePathPart(path) { return this.normalizeNameToAlphaNumericOnly( path.replace(/-/g, 'Dash') .replace(/\{(.*)\}/g, '$1Var')); @@ -22,21 +21,17 @@ module.exports = { return /^ServiceEndpoint/; }, - /* - * Stack Naming - */ + // Stack getStackName() { return `${this.sdk.serverless.service.service}-${this.sdk.getStage()}`; }, - getLogicalLogGroupName(functionName) { - return `${this.getNormalizedLambdaName(functionName)}LogGroup`; + getLogGroupLogicalId(functionName) { + return `${this.getNormalizedFunctionName(functionName)}LogGroup`; }, - /* - * Lambda Function Naming - */ - getNormalizedLambdaName(functionName) { + // Lambda + getNormalizedFunctionName(functionName) { return this.normalizeName(functionName); }, extractLambdaNameFromArn(functionArn) { @@ -49,126 +44,108 @@ module.exports = { const splitName = splitArn[splitArn.length - 1].split('-'); return splitName[splitName.length - 1]; }, - getLogicalLambdaName(functionName) { - return `${this.getNormalizedLambdaName(functionName)}LambdaFunction`; + getLambdaLogicalId(functionName) { + return `${this.getNormalizedFunctionName(functionName)}LambdaFunction`; }, - getLogicalLambdaNameRegex() { + getLambdaLogicalIdRegex() { return /LambdaFunction$/; }, - getLogicalLambdaArnName(functionName) { - return `${this.getLogicalLambdaName(functionName)}Arn`; + getLambdaOutputLogicalId(functionName) { + return `${this.getLambdaLogicalId(functionName)}Arn`; }, - getLogicalLambdaArnNameRegex() { + getLambdaOutputLogicalIdRegex() { return /LambdaFunctionArn$/; }, - /* - * ApiGateway Authorizer Lambda & Method Naming - */ - getApiGatewayName() { - return `${this.sdk.getStage()}-${this.sdk.serverless.service.service}`; - }, - getApiGatewayDeploymentId() { + // API Gateway + generateApiGatewayDeploymentLogicalId() { return `ApiGatewayDeployment${(new Date()).getTime().toString()}`; }, - getLogicalApiGatewayName() { + getRestApiLogicalId() { return 'ApiGatewayRestApi'; }, getNormalizedAuthorizerName(functionName) { - return this.getNormalizedLambdaName(functionName); + return this.getNormalizedFunctionName(functionName); }, - getLogicalAuthorizerName(functionName) { + getAuthorizerLogicalId(functionName) { return `${this.getNormalizedAuthorizerName(functionName)}ApiGatewayAuthorizer`; }, - getLogicalAuthorizerArnName(functionName) { - return `${this.getLogicalAuthorizerName(functionName)}Arn`; - }, - getNormalizedApiGatewayResourceName(resourcePath) { + normalizePath(resourcePath) { return resourcePath.split('/').map( - this.normalizePathtoCapitalAlphaNumbericOnlyWithReplacement.bind(this) + this.normalizePathPart.bind(this) ).join(''); }, - getLogicalApiGatewayResourceName(resourcePath) { - return `ApiGatewayResource${this.getNormalizedApiGatewayResourceName(resourcePath)}`; + getResourceLogicalId(resourcePath) { + return `ApiGatewayResource${this.normalizePath(resourcePath)}`; }, - extractResourceId(logicalApiGatewayResourceName) { - return logicalApiGatewayResourceName.match(/ApiGatewayResource(.*)/)[1]; + extractResourceId(resourceLogicalId) { + return resourceLogicalId.match(/ApiGatewayResource(.*)/)[1]; }, - getNormalizedApiGatewayMethodName(methodName) { + normalizeMethodName(methodName) { return this.normalizeName(methodName.toLowerCase()); }, - getLogicalApiGatewayMethodName(resourceId, methodName) { - return `ApiGatewayMethod${resourceId}${this.getNormalizedApiGatewayMethodName(methodName)}`; + getMethodLogicalId(resourceId, methodName) { + return `ApiGatewayMethod${resourceId}${this.normalizeMethodName(methodName)}`; }, - getLogicalApiGatewayApiKeyName(apiKeyNumber) { + getApiKeyLogicalId(apiKeyNumber) { return `ApiGatewayApiKey${apiKeyNumber}`; }, - getLogicalApiGatewayApiKeyRegex() { + getApiKeyLogicalIdRegex() { return /^ApiGatewayApiKey/; }, - /* - * S3 Bucket Naming - */ - getLogicalDeploymentBucketName() { + // S3 + getDeploymentBucketLogicalId() { return 'ServerlessDeploymentBucket'; }, - getLogicalDeploymentBucketOutputVariableName() { + getDeploymentBucketOutputLogicalId() { return 'ServerlessDeploymentBucketName'; }, - getNormalizedBucketName(bucketName) { + normalizeBucketName(bucketName) { return this.normalizeNameToAlphaNumericOnly(bucketName); }, - getLogicalBucketName(bucketName) { - return `S3Bucket${this.getNormalizedBucketName(bucketName)}`; + getBucketLogicalId(bucketName) { + return `S3Bucket${this.normalizeBucketName(bucketName)}`; }, - /* - * SNS Topic Naming - */ - getNormalizedSnsTopicName(topicName) { + // SNS + normalizeTopicName(topicName) { return this.normalizeNameToAlphaNumericOnly(topicName); }, - getLogicalSnsTopicName(topicName) { - return `SNSTopic${this.getNormalizedSnsTopicName(topicName)}`; + getTopicLogicalId(topicName) { + return `SNSTopic${this.normalizeTopicName(topicName)}`; }, - /* - * CloudWatch Event Naming - */ - getCloudWatchEventId(functionName) { + // Schedule + getScheduleId(functionName) { return `${functionName}Schedule`; }, - getCloudWatchEventName(functionName, scheduleIndex) { - return `${this.getNormalizedLambdaName(functionName)}EventsRuleSchedule${scheduleIndex}`; + getScheduleLogicalId(functionName, scheduleIndex) { + return `${this.getNormalizedFunctionName(functionName)}EventsRuleSchedule${scheduleIndex}`; }, - /* - * Stream Event Naming - */ + // Stream getStreamLogicalId(functionName, streamType, streamName) { return `${ - this.getNormalizedLambdaName(functionName) + this.getNormalizedFunctionName(functionName) }EventSourceMapping${ this.normalizeName(streamType) }${this.normalizeNameToAlphaNumericOnly(streamName)}`; }, - /* - * Lambda to S3 Bucket/SNS Topic/CloudWatch Event/ApiGateway Permissions Naming - */ - getLambdaS3PermissionName(functionName) { - return `${this.getNormalizedLambdaName(functionName)}LambdaPermissionS3`; + // Permissions + getLambdaS3PermissionLogicalId(functionName) { + return `${this.getNormalizedFunctionName(functionName)}LambdaPermissionS3`; }, - getLambdaSnsTopicPermissionName(functionName, topicName) { - return `${this.getNormalizedLambdaName(functionName)}LambdaPermission${ - this.getNormalizedSnsTopicName(topicName)}`; + getLambdaSnsPermissionLogicalId(functionName, topicName) { + return `${this.getNormalizedFunctionName(functionName)}LambdaPermission${ + this.normalizeTopicName(topicName)}`; }, - getLambdaCloudWatchEventPermissionName(functionName, scheduleIndex) { - return `${this.getNormalizedLambdaName(functionName)}LambdaPermissionEventsRuleSchedule${ + getLambdaSchedulePermissionLogicalId(functionName, scheduleIndex) { + return `${this.getNormalizedFunctionName(functionName)}LambdaPermissionEventsRuleSchedule${ scheduleIndex}`; }, - getLambdaApiGatewayPermissionName(functionName) { - return `${this.getNormalizedLambdaName(functionName)}LambdaPermissionApiGateway`; + getLambdaApiGatewayPermissionLogicalId(functionName) { + return `${this.getNormalizedFunctionName(functionName)}LambdaPermissionApiGateway`; }, }; diff --git a/lib/plugins/aws/provider/awsProvider.js b/lib/plugins/aws/provider/awsProvider.js index f0985eb39..af0dc9199 100644 --- a/lib/plugins/aws/provider/awsProvider.js +++ b/lib/plugins/aws/provider/awsProvider.js @@ -5,7 +5,7 @@ const BbPromise = require('bluebird'); const HttpsProxyAgent = require('https-proxy-agent'); const url = require('url'); -const Naming = require('../lib/naming.js'); +const naming = require('../lib/naming.js'); const constants = { providerName: 'aws', @@ -89,7 +89,7 @@ class AwsProvider { this.sdk = AWS; this.serverless.setProvider(constants.providerName, this); - Object.assign(this.naming, Naming); + Object.assign(this.naming, naming); // Use HTTPS Proxy (Optional) const proxy = process.env.proxy @@ -145,7 +145,7 @@ class AwsProvider { const errorMessage = [ 'AWS provider credentials not found.', ' You can find more info on how to set up provider', - ' credentials in our docs here: https://git.io/viZAC', + ' credentials in our docs here: https://git.io/vXsdd', ].join(''); err.message = errorMessage; } @@ -164,7 +164,7 @@ class AwsProvider { * @returns {{region: *}} */ getCredentials() { - const ret = { region: this.getRegion() }; + const returnValue = { region: this.getRegion() }; const credentials = {}; const stageUpper = this.getStage() ? this.getStage().toUpperCase() : null; @@ -177,21 +177,21 @@ class AwsProvider { impl.addEnvironmentProfile(credentials, `AWS_${stageUpper}`); if (Object.keys(credentials).length) { - ret.credentials = credentials; + returnValue.credentials = credentials; } - return ret; + return returnValue; } getRegion() { - let ret = 'us-east-1'; + let returnValue = 'us-east-1'; if (this.options && this.options.region) { - ret = this.options.region; + returnValue = this.options.region; } else if (this.serverless.config.region) { - ret = this.serverless.config.region; + returnValue = this.serverless.config.region; } else if (this.serverless.service.provider.region) { - ret = this.serverless.service.provider.region; + returnValue = this.serverless.service.provider.region; } - return ret; + return returnValue; } getServerlessDeploymentBucketName() { @@ -202,21 +202,21 @@ class AwsProvider { 'describeStackResource', { StackName: this.naming.getStackName(), - LogicalResourceId: this.naming.getLogicalDeploymentBucketName(), + LogicalResourceId: this.naming.getDeploymentBucketLogicalId(), } ).then((result) => result.StackResourceDetail.PhysicalResourceId); } getStage() { - let ret = 'dev'; + let returnValue = 'dev'; if (this.options && this.options.stage) { - ret = this.options.stage; + returnValue = this.options.stage; } else if (this.serverless.config.stage) { - ret = this.serverless.config.stage; + returnValue = this.serverless.config.stage; } else if (this.serverless.service.provider.stage) { - ret = this.serverless.service.provider.stage; + returnValue = this.serverless.service.provider.stage; } - return ret; + return returnValue; } } diff --git a/lib/plugins/aws/provider/awsProvider.test.js b/lib/plugins/aws/provider/awsProvider.test.js index b9e2fda8e..d23974672 100644 --- a/lib/plugins/aws/provider/awsProvider.test.js +++ b/lib/plugins/aws/provider/awsProvider.test.js @@ -183,7 +183,7 @@ describe('AwsProvider', () => { awsProvider.request('S3', 'error', {}) .then(() => done('Should not succeed')) .catch((err) => { - expect(err.message).to.contain('https://git.io/viZAC'); + expect(err.message).to.contain('https://git.io/vXsdd'); done(); }) .catch(done); @@ -353,7 +353,7 @@ describe('AwsProvider', () => { }); }); - describe('#getRegion', () => { + describe('#getRegion()', () => { let newAwsProvider; it('should prefer options over config or provider', () => { @@ -369,6 +369,7 @@ describe('AwsProvider', () => { expect(newAwsProvider.getRegion()).to.equal(newOptions.region); }); + it('should prefer config over provider in lieu of options', () => { const newOptions = {}; const config = { @@ -380,6 +381,7 @@ describe('AwsProvider', () => { expect(newAwsProvider.getRegion()).to.equal(config.region); }); + it('should use provider in lieu of options and config', () => { const newOptions = {}; const config = {}; @@ -389,6 +391,7 @@ describe('AwsProvider', () => { expect(newAwsProvider.getRegion()).to.equal(serverless.service.provider.region); }); + it('should use the default us-east-1 in lieu of options, config, and provider', () => { const newOptions = {}; const config = {}; @@ -399,7 +402,7 @@ describe('AwsProvider', () => { }); }); - describe('#getServerlessDeploymentBucketName', () => { + describe('#getServerlessDeploymentBucketName()', () => { it('should return the name of the serverless deployment bucket', () => { const describeStackResourcesStub = sinon .stub(awsProvider, 'request') @@ -444,7 +447,7 @@ describe('AwsProvider', () => { }); }); - describe('#getStage', () => { + describe('#getStage()', () => { it('should prefer options over config or provider', () => { const newOptions = { stage: 'optionsStage', diff --git a/lib/plugins/aws/tests/naming.js b/lib/plugins/aws/tests/naming.js index b0a6ddf24..741a71f0b 100644 --- a/lib/plugins/aws/tests/naming.js +++ b/lib/plugins/aws/tests/naming.js @@ -41,27 +41,27 @@ describe('#naming()', () => { }); describe('#normalizeNameToCapitalAlphaNumbericOnly()', () => { it('converts `-` to `Dash`', () => { - expect(sdk.naming.normalizePathtoCapitalAlphaNumbericOnlyWithReplacement( + expect(sdk.naming.normalizePathPart( 'a-path' )).to.equal('ADashpath'); }); it('converts variable declarations (`${var}`) to `VariableVar`', () => { - expect(sdk.naming.normalizePathtoCapitalAlphaNumbericOnlyWithReplacement( + expect(sdk.naming.normalizePathPart( '${variable}' )).to.equal('VariableVar'); }); it('converts variable declarations prefixes to `VariableVarPath`', () => { - expect(sdk.naming.normalizePathtoCapitalAlphaNumbericOnlyWithReplacement( + expect(sdk.naming.normalizePathPart( '${variable}Path' )).to.equal('VariableVarPath'); }); it('converts variable declarations suffixes to `PathvariableVar`', () => { - expect(sdk.naming.normalizePathtoCapitalAlphaNumbericOnlyWithReplacement( + expect(sdk.naming.normalizePathPart( 'path${variable}' )).to.equal('PathvariableVar'); }); it('converts variable declarations in center to `PathvariableVarDir`', () => { - expect(sdk.naming.normalizePathtoCapitalAlphaNumbericOnlyWithReplacement( + expect(sdk.naming.normalizePathPart( 'path${variable}Dir' )).to.equal('PathvariableVarDir'); }); @@ -86,9 +86,9 @@ describe('#naming()', () => { expect(sdk.naming.getStackName()).to.equal(`${serverless.service.service}-${options.stage}`); }); }); - describe('#getNormalizedLambdaName()', () => { + describe('#getNormalizedFunctionName()', () => { it('should normalize the given functionName', () => { - expect(sdk.naming.getNormalizedLambdaName('functionName')) + expect(sdk.naming.getNormalizedFunctionName('functionName')) .to.equal('FunctionName'); }); }); @@ -104,65 +104,59 @@ describe('#naming()', () => { expect(sdk.naming.extractLambdaNameFromArn(arn)).to.equal('my-dev-lambda'); }); }); - describe('#getLogicalLambdaName()', () => { + describe('#getLambdaLogicalId()', () => { it('should normalize the function name and add the logical suffix', () => { - expect(sdk.naming.getLogicalLambdaName('functionName')) + expect(sdk.naming.getLambdaLogicalId('functionName')) .to.equal('FunctionNameLambdaFunction'); }); }); - describe('#getLogicalLambdaNameRegex()', () => { + describe('#getLambdaLogicalIdRegex()', () => { it('should match the suffix', () => { - expect(sdk.naming.getLogicalLambdaNameRegex() + expect(sdk.naming.getLambdaLogicalIdRegex() .test('LambdaFunction')).to.equal(true); }); it('should not match a name without the suffix', () => { - expect(sdk.naming.getLogicalLambdaNameRegex() + expect(sdk.naming.getLambdaLogicalIdRegex() .test('LambdaFunctionNotTheSuffix')).to.equal(false); }); it('should match a name with the suffix', () => { - expect(sdk.naming.getLogicalLambdaNameRegex() + expect(sdk.naming.getLambdaLogicalIdRegex() .test('AFunctionNameLambdaFunction')).to.equal(true); }); }); - describe('#getLogicalLambdaArnName()', () => { + describe('#getLambdaOutputLogicalId()', () => { it('should normalize the function name and add the logical arn suffix', () => { expect( - sdk.naming.getLogicalLambdaArnName('functionName') + sdk.naming.getLambdaOutputLogicalId('functionName') ).to.equal('FunctionNameLambdaFunctionArn'); }); }); - describe('#getLogicalLambdaArnNameRegex()', () => { + describe('#getLambdaOutputLogicalIdRegex()', () => { it('should match the suffix', () => { - expect(sdk.naming.getLogicalLambdaArnNameRegex() + expect(sdk.naming.getLambdaOutputLogicalIdRegex() .test('aLambdaFunctionArn')).to.equal(true); }); it('should not match a name without the suffix', () => { - expect(sdk.naming.getLogicalLambdaArnNameRegex() + expect(sdk.naming.getLambdaOutputLogicalIdRegex() .test('LambdaFunctionArnNotTheSuffix')) .to.equal(false); }); it('should match a name with the suffix', () => { - expect(sdk.naming.getLogicalLambdaArnNameRegex() + expect(sdk.naming.getLambdaOutputLogicalIdRegex() .test('AFunctionArnNameLambdaFunctionArn')) .to.equal(true); }); }); - describe('#getApiGatewayName()', () => { - it('should return the composition of stage and service name', () => { - serverless.service.service = 'myService'; - expect(sdk.naming.getApiGatewayName()) - .to.equal(`dev-${serverless.service.service}`); - }); - }); - describe('#getApiGatewayDeploymentId()', () => { + describe('#generateApiGatewayDeploymentLogicalId()', () => { it('should return ApiGatewayDeployment with a date based suffix', () => { - expect(sdk.naming.getApiGatewayDeploymentId().match(/ApiGatewayDeployment(.*)/).length) + expect(sdk.naming.generateApiGatewayDeploymentLogicalId() + .match(/ApiGatewayDeployment(.*)/).length) .to.be.greaterThan(1); }); }); - describe('#getLogicalApiGatewayName()', () => { + describe('#getRestApiLogicalId()', () => { it('should return ApiGatewayRestApi', () => { - expect(sdk.naming.getLogicalApiGatewayName()).to.equal('ApiGatewayRestApi'); + expect(sdk.naming.getRestApiLogicalId()).to.equal('ApiGatewayRestApi'); }); }); describe('#getNormalizedAuthorizerName()', () => { @@ -171,9 +165,9 @@ describe('#naming()', () => { .to.equal('AuthorizerName'); }); }); - describe('#getLogicalAuthorizerName()', () => { + describe('#getAuthorizerLogicalId()', () => { it('should normalize the authorizer name and add the standard suffix', () => { - expect(sdk.naming.getLogicalAuthorizerName('authorizerName')) + expect(sdk.naming.getAuthorizerLogicalId('authorizerName')) .to.equal('AuthorizerNameApiGatewayAuthorizer'); }); }); @@ -183,23 +177,17 @@ describe('#naming()', () => { expect(sdk.naming.extractAuthorizerNameFromArn(arn)).to.equal('lambda'); }); }); - describe('#getLogicalAuthorizerArnName()', () => { - it('should normalize the authorizer name and add the standard arn suffix', () => { - expect(sdk.naming.getLogicalAuthorizerArnName('authorizerName')) - .to.equal('AuthorizerNameApiGatewayAuthorizerArn'); - }); - }); - describe('#getNormalizedApiGatewayResourceName()', () => { + describe('#normalizePath()', () => { it('should normalize each part of the resource path and remove non-alpha-numeric characters', () => { - expect(sdk.naming.getNormalizedApiGatewayResourceName( + expect(sdk.naming.normalizePath( 'my/path/to/a-${var}-resource' )).to.equal('MyPathToADashvarVarDashresource'); }); }); - describe('#getLogicalApiGatewayResourceName()', () => { + describe('#getResourceLogicalId()', () => { it('should normalize the resource and add the standard suffix', () => { - expect(sdk.naming.getLogicalApiGatewayResourceName( + expect(sdk.naming.getResourceLogicalId( 'my/path/to/a-${var}-resource' )).to.equal('ApiGatewayResourceMyPathToADashvarVarDashresource'); }); @@ -211,98 +199,98 @@ describe('#naming()', () => { )).to.equal('MyPathToADashvarVarDashResource'); }); }); - describe('#getNormalizedApiGatewayMethodName()', () => { + describe('#normalizeMethodName()', () => { it('should capitalize the first letter and lowercase any other characters', () => { - expect(sdk.naming.getNormalizedApiGatewayMethodName('gET')).to.equal('Get'); + expect(sdk.naming.normalizeMethodName('gET')).to.equal('Get'); }); }); - describe('#getLogicalApiGatewayMethodName()', () => { + describe('#getMethodLogicalId()', () => { it('', () => { - expect(sdk.naming.getLogicalApiGatewayMethodName( + expect(sdk.naming.getMethodLogicalId( 'ResourceId', 'get' )).to.equal('ApiGatewayMethodResourceIdGet'); }); }); - describe('#getLogicalApiGatewayApiKeyRegex()', () => { + describe('#getApiKeyLogicalIdRegex()', () => { it('should match the prefix', () => { - expect(sdk.naming.getLogicalApiGatewayApiKeyRegex() + expect(sdk.naming.getApiKeyLogicalIdRegex() .test('ApiGatewayApiKey')).to.equal(true); }); it('should not match a name without the prefix', () => { - expect(sdk.naming.getLogicalApiGatewayApiKeyRegex() + expect(sdk.naming.getApiKeyLogicalIdRegex() .test('NotThePrefixApiGatewayApiKey')).to.equal(false); }); it('should match a name with the prefix', () => { - expect(sdk.naming.getLogicalApiGatewayApiKeyRegex() + expect(sdk.naming.getApiKeyLogicalIdRegex() .test('ApiGatewayApiKeySuffix')).to.equal(true); }); }); - describe('#getLogicalDeploymentBucketName()', () => { + describe('#getDeploymentBucketLogicalId()', () => { it('should return "ServerlessDeploymentBucket"', () => { - expect(sdk.naming.getLogicalDeploymentBucketName()).to.equal('ServerlessDeploymentBucket'); + expect(sdk.naming.getDeploymentBucketLogicalId()).to.equal('ServerlessDeploymentBucket'); }); }); - describe('#getLogicalDeploymentBucketOutputVariableName()', () => { + describe('#getDeploymentBucketOutputLogicalId()', () => { it('should return "ServerlessDeploymentBucketName"', () => { - expect(sdk.naming.getLogicalDeploymentBucketOutputVariableName()) + expect(sdk.naming.getDeploymentBucketOutputLogicalId()) .to.equal('ServerlessDeploymentBucketName'); }); }); - describe('#getNormalizedBucketName()', () => { + describe('#normalizeBucketName()', () => { it('should remove all non-alpha-numeric characters and capitalize the first letter', () => { - expect(sdk.naming.getNormalizedBucketName('b!u@c#k$e%t^N&a*m(e')).to.equal('BucketName'); + expect(sdk.naming.normalizeBucketName('b!u@c#k$e%t^N&a*m(e')).to.equal('BucketName'); }); }); - describe('#getLogicalBucketName()', () => { + describe('#getBucketLogicalId()', () => { it('should normalize the bucket name and add the standard prefix', () => { - expect(sdk.naming.getLogicalBucketName('b!u@c#k$e%t^N&a*m(e')).to.equal('S3BucketBucketName'); + expect(sdk.naming.getBucketLogicalId('b!u@c#k$e%t^N&a*m(e')).to.equal('S3BucketBucketName'); }); }); - describe('#getNormalizedSnsTopicName()', () => { + describe('#normalizeTopicName()', () => { it('should remove all non-alpha-numeric characters and capitalize the first letter', () => { - expect(sdk.naming.getNormalizedSnsTopicName('t!o@p#i$c%N^a&m*e')).to.equal('TopicName'); + expect(sdk.naming.normalizeTopicName('t!o@p#i$c%N^a&m*e')).to.equal('TopicName'); }); }); - describe('#getLogicalSnsTopicName()', () => { + describe('#getTopicLogicalId()', () => { it('should remove all non-alpha-numeric characters and capitalize the first letter', () => { - expect(sdk.naming.getLogicalSnsTopicName('t!o@p#i$c%N^a&m*e')).to.equal('SNSTopicTopicName'); + expect(sdk.naming.getTopicLogicalId('t!o@p#i$c%N^a&m*e')).to.equal('SNSTopicTopicName'); }); }); - describe('#getCloudWatchEventId()', () => { + describe('#getScheduleId()', () => { it('should add the standard suffix', () => { - expect(sdk.naming.getCloudWatchEventId('functionName')).to.equal('functionNameSchedule'); + expect(sdk.naming.getScheduleId('functionName')).to.equal('functionNameSchedule'); }); }); - describe('#getCloudWatchEventName()', () => { + describe('#getScheduleLogicalId()', () => { it('should normalize the function name and add the standard suffix including the index', () => { - expect(sdk.naming.getCloudWatchEventName('functionName', 0)) + expect(sdk.naming.getScheduleLogicalId('functionName', 0)) .to.equal('FunctionNameEventsRuleSchedule0'); }); }); - describe('#getLambdaS3PermissionName()', () => { + describe('#getLambdaS3PermissionLogicalId()', () => { it('should normalize the function name and add the standard suffix', () => { - expect(sdk.naming.getLambdaS3PermissionName('functionName')) + expect(sdk.naming.getLambdaS3PermissionLogicalId('functionName')) .to.equal('FunctionNameLambdaPermissionS3'); }); }); - describe('#getLambdaSnsTopicPermissionName()', () => { + describe('#getLambdaSnsPermissionLogicalId()', () => { it('should normalize the function and topic names and add them as prefix and suffix to the ' + 'standard permission center', () => { - expect(sdk.naming.getLambdaSnsTopicPermissionName('functionName', 'topicName')) + expect(sdk.naming.getLambdaSnsPermissionLogicalId('functionName', 'topicName')) .to.equal('FunctionNameLambdaPermissionTopicName'); }); }); - describe('#getLambdaCloudWatchEventPermissionName()', () => { + describe('#getLambdaSchedulePermissionLogicalId()', () => { it('should normalize the function name and add the standard suffix including event index', () => { - expect(sdk.naming.getLambdaCloudWatchEventPermissionName('functionName', 0)) + expect(sdk.naming.getLambdaSchedulePermissionLogicalId('functionName', 0)) .to.equal('FunctionNameLambdaPermissionEventsRuleSchedule0'); }); }); - describe('#getLambdaApiGatewayPermissionName()', () => { + describe('#getLambdaApiGatewayPermissionLogicalId()', () => { it('should normalize the function name and append the standard suffix', () => { - expect(sdk.naming.getLambdaApiGatewayPermissionName('functionName')) + expect(sdk.naming.getLambdaApiGatewayPermissionLogicalId('functionName')) .to.equal('FunctionNameLambdaPermissionApiGateway'); }); });