Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws): add possibility to define lambda name format #84

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/BaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const fse = require('fs-extra');
const chalk = require('chalk');
const dotenv = require('dotenv');

// eslint-disable-next-line no-template-curly-in-string
const DEFAULT_ACTION_FORMAT = '/${packageName}/${baseName}/${version}';

/**
* @field name Name of function including the version. eg `[email protected]`
* @field baseName Name of the function w/o the version. eg `my-action`
Expand Down Expand Up @@ -62,7 +65,9 @@ class BaseConfig {
updatedBy: null,
targets: [],
functionURL: '',
format: {},
format: {
aws: DEFAULT_ACTION_FORMAT,
},
properties: {},
_logger: null,
});
Expand Down Expand Up @@ -386,7 +391,7 @@ class BaseConfig {
}

withFormat(value) {
this.format = value;
Object.assign(this.format, value || {});
return this;
}

Expand Down Expand Up @@ -526,8 +531,7 @@ class BaseConfig {
description: 'Action formats',
type: 'object',
default: {
// eslint-disable-next-line no-template-curly-in-string
aws: '/${packageName}/${baseName}/${version}',
aws: DEFAULT_ACTION_FORMAT,
},
})
.option('property', {
Expand Down
15 changes: 15 additions & 0 deletions src/deploy/AWSConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* governing permissions and limitations under the License.
*/

// eslint-disable-next-line no-template-curly-in-string
const DEFAULT_LAMBDA_FORMAT = '${packageName}--${baseName}';

class AWSConfig {
constructor() {
Object.assign(this, {
Expand All @@ -18,6 +21,7 @@ class AWSConfig {
apiId: '',
cleanUpBuckets: false,
createRoutes: false,
lambdaFormat: DEFAULT_LAMBDA_FORMAT,
});
}

Expand All @@ -26,6 +30,7 @@ class AWSConfig {
.withAWSRegion(argv.awsRegion)
.withAWSRole(argv.awsRole)
.withAWSApi(argv.awsApi)
.withAWSLambdaFormat(argv.awsLambdaFormat)
.withAWSCleanUpBuckets(argv.awsCleanupBuckets)
.withAWSCreateRoutes(argv.awsCreateRoutes);
}
Expand All @@ -45,6 +50,11 @@ class AWSConfig {
return this;
}

withAWSLambdaFormat(value) {
this.lambdaFormat = value;
return this;
}

withAWSCleanUpBuckets(value) {
this.cleanUpBuckets = value;
return this;
Expand Down Expand Up @@ -78,6 +88,11 @@ class AWSConfig {
type: 'boolean',
default: false,
})
.option('aws-lambda-format', {
description: 'Format to use to create lambda functions (note that all dots (\'.\') will be replaced with underscores.',
type: 'string',
default: DEFAULT_LAMBDA_FORMAT,
})
.option('aws-cleanup-buckets', {
description: 'Cleans up stray temporary S3 buckets',
type: 'boolean',
Expand Down
2 changes: 1 addition & 1 deletion src/deploy/AWSDeployer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AWSDeployer extends BaseDeployer {
get functionName() {
if (!this._functionName) {
const { cfg } = this;
this._functionName = `${cfg.packageName.replace(/\./g, '_')}--${cfg.baseName.replace(/\./g, '_')}`;
this._functionName = ActionBuilder.substitute(this._cfg.lambdaFormat, { ...cfg, ...cfg.properties }).replace(/\./g, '_');
}
return this._functionName;
}
Expand Down
74 changes: 74 additions & 0 deletions test/aws.deployer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */
const assert = require('assert');
const BaseConfig = require('../src/BaseConfig.js');
const AWSConfig = require('../src/deploy/AWSConfig.js');
const AWSDeployer = require('../src/deploy/AWSDeployer.js');
const ActionBuilder = require('../src/ActionBuilder.js');

describe('AWS Deployer Test', () => {
it('sets the default lambda name', async () => {
const cfg = new BaseConfig()
.withName('/helix-services/[email protected]');
const awsCfg = new AWSConfig();
const builder = new ActionBuilder().withConfig(cfg);
await builder.validate();
const aws = new AWSDeployer(cfg, awsCfg);

assert.strictEqual(aws.functionName, 'helix-services--static');
});

it('sets the default lambda with dots', async () => {
const cfg = new BaseConfig()
.withName('/helix-services/[email protected]');
const awsCfg = new AWSConfig();
const builder = new ActionBuilder().withConfig(cfg);
await builder.validate();
const aws = new AWSDeployer(cfg, awsCfg);

assert.strictEqual(aws.functionName, 'helix-services--gorky_v8');
});

it('sets the default function path', async () => {
const cfg = new BaseConfig()
.withName('/helix-services/[email protected]');
const awsCfg = new AWSConfig();
const builder = new ActionBuilder().withConfig(cfg);
await builder.validate();
const aws = new AWSDeployer(cfg, awsCfg);

assert.strictEqual(aws.functionPath, '/helix-services/static/1.18.2');
});

it('can configure lambda name and format', async () => {
const cfg = new BaseConfig()
.withVersion('4.3.1')
.withProperties({
scriptName: 'html',
})
.withFormat({
// eslint-disable-next-line no-template-curly-in-string
aws: '/pages_${version}/${scriptName}',
});
const awsCfg = new AWSConfig()
// eslint-disable-next-line no-template-curly-in-string
.withAWSLambdaFormat('pages--${scriptName}');
const builder = new ActionBuilder().withConfig(cfg);
await builder.validate();
const aws = new AWSDeployer(cfg, awsCfg);

assert.strictEqual(aws.functionName, 'pages--html');
assert.strictEqual(aws.functionPath, '/pages_4.3.1/html');
});
});