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

chore(core): Revert #24443 add stage prefix to stack name shortening process #25163

Merged
merged 1 commit into from
Apr 17, 2023
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
13 changes: 1 addition & 12 deletions packages/aws-cdk-lib/core/lib/private/unique-resource-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ interface MakeUniqueResourceNameOptions {
* @default - none
*/
readonly allowedSpecialCharacters?: string;

/**
* Prefix to be added into the stack name
*
* @default - none
*/
readonly prefix?: string;
}

/**
Expand All @@ -56,7 +49,6 @@ const HASH_LEN = 8;
export function makeUniqueResourceName(components: string[], options: MakeUniqueResourceNameOptions) {
const maxLength = options.maxLength ?? 256;
const separator = options.separator ?? '';
const prefix = options.prefix ?? '';
components = components.filter(x => x !== HIDDEN_ID);

if (components.length === 0) {
Expand All @@ -67,7 +59,7 @@ export function makeUniqueResourceName(components: string[], options: MakeUnique
// in order to support transparent migration of cloudformation templates to the CDK without the
// need to rename all resources.
if (components.length === 1) {
const topLevelResource = prefix + removeNonAllowedSpecialCharacters(components[0], separator, options.allowedSpecialCharacters);
const topLevelResource = removeNonAllowedSpecialCharacters(components[0], separator, options.allowedSpecialCharacters);

if (topLevelResource.length <= maxLength) {
return topLevelResource;
Expand All @@ -76,9 +68,6 @@ export function makeUniqueResourceName(components: string[], options: MakeUnique

// Calculate the hash from the full path, included unresolved tokens so the hash value is always unique
const hash = pathHash(components);
if (prefix) {
components.unshift(prefix);
}
const human = removeDupes(components)
.filter(pathElement => pathElement !== HIDDEN_FROM_HUMAN_ID)
.map(pathElement => removeNonAllowedSpecialCharacters(pathElement, separator, options.allowedSpecialCharacters))
Expand Down
17 changes: 6 additions & 11 deletions packages/aws-cdk-lib/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ export class Stack extends Construct implements ITaggable {
private generateStackName() {
const assembly = Stage.of(this);
const prefix = (assembly && assembly.stageName) ? `${assembly.stageName}-` : '';
return `${this.generateStackId(assembly, prefix)}`;
return `${prefix}${this.generateStackId(assembly)}`;
}

/**
Expand All @@ -1448,7 +1448,7 @@ export class Stack extends Construct implements ITaggable {
/**
* Generate an ID with respect to the given container construct.
*/
private generateStackId(container: IConstruct | undefined, prefix: string='') {
private generateStackId(container: IConstruct | undefined) {
const rootPath = rootPathTo(this, container);
const ids = rootPath.map(c => Node.of(c).id);

Expand All @@ -1458,7 +1458,7 @@ export class Stack extends Construct implements ITaggable {
throw new Error('unexpected: stack id must always be defined');
}

return makeStackName(ids, prefix);
return makeStackName(ids);
}

private resolveExportedValue(exportedValue: any): ResolvedExport {
Expand Down Expand Up @@ -1644,14 +1644,9 @@ export function rootPathTo(construct: IConstruct, ancestor?: IConstruct): IConst
* has only one component. Otherwise we fall back to the regular "makeUniqueId"
* behavior.
*/
function makeStackName(components: string[], prefix: string='') {
if (components.length === 1) {
const stack_name = prefix + components[0];
if (stack_name.length <= 128) {
return stack_name;
}
}
return makeUniqueResourceName(components, { maxLength: 128, prefix: prefix });
function makeStackName(components: string[]) {
if (components.length === 1) { return components[0]; }
return makeUniqueResourceName(components, { maxLength: 128 });
}

function getCreateExportsScope(stack: Stack) {
Expand Down
11 changes: 0 additions & 11 deletions packages/aws-cdk-lib/core/test/stage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ describe('stage', () => {
expect(stack.stackName).toEqual('MyStage-MyStack');
});

test('generated stack names will not exceed 128 characters', () => {
// WHEN
const app = new App();
const stage = new Stage(app, 'ThisStageNameIsVeryLongButWillOnlyBeTooLongWhenCombinedWithTheStackName');
const stack = new BogusStack(stage, 'ThisStackNameIsVeryLongButItWillOnlyBeTooLongWhenCombinedWithTheLongPrefix');

// THEN
expect(stack.stackName.length).toEqual(128);
expect(stack.stackName).toEqual('ThisStageNameIsVeryLongButWillOnlyBeTooLongWhenCombinedWithTsVeryLongButItWillOnlyBeTooLongWhenCombinedWithTheLongPrefix4CA9F65B');
});

test('Can not have dependencies to stacks outside the nested asm', () => {
// GIVEN
const app = new App();
Expand Down