From b6d106d4e73b796b4bb987b14583105a38491d7f Mon Sep 17 00:00:00 2001 From: Sergii Kovalev Date: Fri, 10 May 2019 15:09:52 +0300 Subject: [PATCH] CR Changes: Fix for function names - getFunctionVersionName and getAliasVersionName function added to utils --- lib/stackops/apiGateway.js | 4 ++-- lib/stackops/snsEvents.js | 12 ++++++------ lib/utils.js | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/stackops/apiGateway.js b/lib/stackops/apiGateway.js index 93103ab..96bd9c6 100644 --- a/lib/stackops/apiGateway.js +++ b/lib/stackops/apiGateway.js @@ -278,8 +278,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac // Adjust permission to reference the function aliases _.forOwn(apiLambdaPermissions, (permission, name) => { const functionName = _.replace(name, /LambdaPermissionApiGateway$/, ''); - const versionName = _.find(_.keys(versions), version => _.startsWith(version, `${functionName}LambdaVersion`)); - const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, `${functionName}Alias`)); + const versionName = utils.getFunctionVersionName(versions, functionName); + const aliasName = utils.getAliasVersionName(aliases, functionName); const isExternalRef = isExternalRefAuthorizerPredicate(permission.Properties.FunctionName); // Adjust references and alias permissions diff --git a/lib/stackops/snsEvents.js b/lib/stackops/snsEvents.js index dc5c7cd..01767a9 100644 --- a/lib/stackops/snsEvents.js +++ b/lib/stackops/snsEvents.js @@ -31,8 +31,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac _.forOwn(lambdaSubscriptions, subscription => { const functionNameRef = utils.findAllReferences(_.get(subscription, 'Endpoint')); const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, ''); - const versionName = _.find(_.keys(versions), version => _.startsWith(version, `${functionName}LambdaVersion`)); - const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, `${functionName}Alias`)); + const versionName = utils.getFunctionVersionName(versions, functionName); + const aliasName = utils.getAliasVersionName(aliases, functionName); subscription.Endpoint = { Ref: aliasName }; @@ -54,8 +54,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac const functionNameRef = utils.findAllReferences(_.get(subscription.Properties, 'Endpoint')); const functionName = _.replace(_.get(functionNameRef, '[0].ref', ''), /LambdaFunction$/, ''); - const versionName = _.find(_.keys(versions), version => _.startsWith(version, `${functionName}LambdaVersion`)); - const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, `${functionName}Alias`)); + const versionName = utils.getFunctionVersionName(versions, functionName); + const aliasName = utils.getAliasVersionName(aliases, functionName); subscription.Properties.Endpoint = { Ref: aliasName }; subscription.DependsOn = [ versionName, aliasName ]; @@ -72,8 +72,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac // Adjust permission to reference the function aliases _.forOwn(snsLambdaPermissions, (permission, name) => { const functionName = _.replace(name, /LambdaPermission.*$/, ''); - const versionName = _.find(_.keys(versions), version => _.startsWith(version, `${functionName}LambdaVersion`)); - const aliasName = _.find(_.keys(aliases), alias => _.startsWith(alias, `${functionName}Alias`)); + const versionName = utils.getFunctionVersionName(versions, functionName); + const aliasName = utils.getAliasVersionName(aliases, functionName); // Adjust references and alias permissions permission.Properties.FunctionName = { Ref: aliasName }; diff --git a/lib/utils.js b/lib/utils.js index 8623331..fcc8d53 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -105,6 +105,23 @@ class Utils { return false; } + /** + * @param {object} versions + * @param {string} functionName + * @returns {string} + */ + static getFunctionVersionName(versions, functionName) { + return _.find(_.keys(versions), version => version === `${functionName}LambdaVersion`); + } + + /** + * @param {object} aliases + * @param {string} functionName + * @returns {string} + */ + static getAliasVersionName(aliases, functionName) { + return _.find(_.keys(aliases), alias => alias === `${functionName}Alias`); + } } module.exports = Utils;