forked from awslabs/generative-ai-cdk-constructs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): generatephysicalname handles short names (awslabs#196)
Resources for awslabs#169 need physical names shorter than 32 characters with only lowercase letters, numbers, and hyphens. This change preserves existing behavior as much as possible. When `maxLength` is greater than `prefixLength` + `stackIdGuidLength` + 3, the original algorithm is used. Otherwise, a new algorithm is used that requires an `IConstruct` for the stack name and node unique id. An optional boolean parameter `lower` was added. When true, `allParts` ias also made lower case. It throws errors if the resulting name is longer than `maxLength` or if `resource` is needed by not provided. Fixes awslabs#195
- Loading branch information
Showing
2 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
import * as cdk from 'aws-cdk-lib'; | ||
import { generatePhysicalName } from '../../../src/common/helpers/utils'; | ||
|
||
|
||
describe('generatePhysicalName', () => { | ||
let testResourceA: TestResource; | ||
let testResourceB: TestResource; | ||
let testStack: cdk.Stack; | ||
|
||
afterAll(() => { | ||
console.log('Test completed'); | ||
}); | ||
|
||
beforeAll(() => { | ||
const app = new cdk.App(); | ||
testStack = new cdk.Stack(app, 'TestStack', { env: { account: '012345678912', region: 'bermuda-triangle-1' } }); | ||
|
||
testResourceA = new TestResource(testStack, 'A'); | ||
testResourceB = new TestResource(testResourceA, 'B'); | ||
}); | ||
|
||
test('short physical name', () => { | ||
const nameA = generatePhysicalName( | ||
'testPrefix', | ||
['Foo', 'Bar', 'Baz'], | ||
32, | ||
true, | ||
testResourceA, | ||
); | ||
const nameB = generatePhysicalName( | ||
'testPrefix', | ||
['Foo', 'Bar', 'Baz'], | ||
32, | ||
true, | ||
testResourceB, | ||
); | ||
expect(nameA).toEqual('testprefixfoobarbaz-900740fe'); | ||
expect(nameB).toEqual('testprefixfoobarbaz-99f0d580'); | ||
expect(nameA).not.toEqual(nameB); | ||
expect(nameA.length).toBeLessThanOrEqual(32); | ||
expect(nameB.length).toBeLessThanOrEqual(32); | ||
}); | ||
|
||
test('long physical name', () => { | ||
const maxLogGroupNameLength = 255; | ||
const logGroupPrefix = '/aws/vendedlogs/states/constructs/'; | ||
const maxGeneratedNameLength = maxLogGroupNameLength - logGroupPrefix.length; | ||
const nameParts: string[] = [ | ||
testStack.stackName, // Name of the stack | ||
'StateMachineLogRag', // Literal string for log group name portion | ||
]; | ||
const logGroupName = generatePhysicalName(logGroupPrefix, nameParts, maxGeneratedNameLength); | ||
expect(logGroupName).toMatch(new RegExp('^/aws/vendedlogs/states/constructs/TestStackStateMachineLogRag-\\$\{Token\[TOKEN\.[0-9]+\]\}$')); | ||
expect(logGroupName.length).toBeLessThanOrEqual(maxLogGroupNameLength); | ||
}); | ||
|
||
test('lowercase long physical name', () => { | ||
const maxLogGroupNameLength = 255; | ||
const logGroupPrefix = '/aws/vendedlogs/states/constructs/'; | ||
const maxGeneratedNameLength = maxLogGroupNameLength - logGroupPrefix.length; | ||
const nameParts: string[] = [ | ||
testStack.stackName, // Name of the stack | ||
'StateMachineLogRag', // Literal string for log group name portion | ||
]; | ||
const logGroupName = generatePhysicalName(logGroupPrefix, nameParts, maxGeneratedNameLength, true); | ||
expect(logGroupName).toMatch(new RegExp('^/aws/vendedlogs/states/constructs/teststackstatemachinelograg-\\$\{Token\[TOKEN\.[0-9]+\]\}$')); | ||
expect(logGroupName.length).toBeLessThanOrEqual(maxLogGroupNameLength); | ||
}); | ||
|
||
test('short name with no resource', () => { | ||
expect(() => { | ||
generatePhysicalName( | ||
'test', | ||
['Foo', 'Bar', 'Baz'], | ||
32, | ||
true, | ||
); | ||
}, | ||
).toThrow('The resource parameter is required for short names.'); | ||
}); | ||
|
||
test('name too long', () => { | ||
expect(() => { | ||
generatePhysicalName( | ||
'/aws/vendedlogs/states/constructs/', | ||
['Foo', 'Bar', 'Baz'], | ||
32, | ||
true, | ||
testResourceA, | ||
); | ||
}).toThrow('The generated name is longer than the maximum length of'); | ||
}); | ||
}); | ||
|
||
|
||
class TestResource extends cdk.Resource {} |