Skip to content

Commit

Permalink
fix: stack naming to correct format
Browse files Browse the repository at this point in the history
  • Loading branch information
aripalo committed Aug 19, 2022
1 parent dfe1ebb commit 3465f46
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
10 changes: 10 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### AccountWrapper <a name="AccountWrapper" id="@alma-cdk/project.AccountWrapper"></a>

Wrapper for account-level stacks.

#### Initializers <a name="Initializers" id="@alma-cdk/project.AccountWrapper.Initializer"></a>

```typescript
Expand Down Expand Up @@ -89,6 +91,8 @@ The tree node.

### EnvironmentWrapper <a name="EnvironmentWrapper" id="@alma-cdk/project.EnvironmentWrapper"></a>

Wrapper for environmental stacks.

#### Initializers <a name="Initializers" id="@alma-cdk/project.EnvironmentWrapper.Initializer"></a>

```typescript
Expand Down Expand Up @@ -1461,6 +1465,8 @@ public readonly config: {[ key: string ]: any};

### AccountStrategyOneProps <a name="AccountStrategyOneProps" id="@alma-cdk/project.AccountStrategyOneProps"></a>

Props `AccountStrategy.one`.

#### Initializer <a name="Initializer" id="@alma-cdk/project.AccountStrategyOneProps.Initializer"></a>

```typescript
Expand Down Expand Up @@ -1500,6 +1506,8 @@ public readonly mock: AccountConfiguration;

### AccountStrategyThreeProps <a name="AccountStrategyThreeProps" id="@alma-cdk/project.AccountStrategyThreeProps"></a>

Props `AccountStrategy.three`.

#### Initializer <a name="Initializer" id="@alma-cdk/project.AccountStrategyThreeProps.Initializer"></a>

```typescript
Expand Down Expand Up @@ -1561,6 +1569,8 @@ public readonly mock: AccountConfiguration;

### AccountStrategyTwoProps <a name="AccountStrategyTwoProps" id="@alma-cdk/project.AccountStrategyTwoProps"></a>

Props `AccountStrategy.two`.

#### Initializer <a name="Initializer" id="@alma-cdk/project.AccountStrategyTwoProps.Initializer"></a>

```typescript
Expand Down
12 changes: 6 additions & 6 deletions src/smartstack/name/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('SmartStack', () => {
environmentType: 'testing',
stackId: 'my-stack',
},
expected: 'MyProject-Testing-Environment-MyStack',
expected: 'MyProject-Environment-Testing-MyStack',
},
{
name: 'environmentType missing',
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('SmartStack', () => {
environmentType: 'testing',
stackId: 'n'.repeat(49),
},
expected: 'MyProject-Testing-Environment-'+capitalizeFirstLetter('n'.repeat(48)),
expected: 'MyProject-Environment-Testing-'+capitalizeFirstLetter('n'.repeat(48)),
},
{
name: 'too long project name',
Expand All @@ -98,7 +98,7 @@ describe('SmartStack', () => {
environmentType: 'testing',
stackId: 'my-stack',
},
expected: capitalizeFirstLetter('n'.repeat(32))+'-Testing-Environment-MyStack',
expected: capitalizeFirstLetter('n'.repeat(32))+'-Environment-Testing-MyStack',
},
{
name: 'too long accountType name',
Expand All @@ -118,7 +118,7 @@ describe('SmartStack', () => {
environmentType: 'n'.repeat(33),
stackId: 'my-stack',
},
expected: 'MyProject-'+capitalizeFirstLetter('n'.repeat(32))+'-Environment-MyStack',
expected: 'MyProject-Environment-'+capitalizeFirstLetter('n'.repeat(32))+'-MyStack',
},
{
name: 'everything too long',
Expand All @@ -128,8 +128,8 @@ describe('SmartStack', () => {
environmentType: 'n'.repeat(33),
stackId: 'n'.repeat(49),
},
expected: capitalizeFirstLetter('n'.repeat(32))+'-'+capitalizeFirstLetter('n'.repeat(32))+'-Environment-'+capitalizeFirstLetter('n'.repeat(48)),
expected: capitalizeFirstLetter('n'.repeat(32))+'-Environment-'+capitalizeFirstLetter('n'.repeat(32))+'-'+capitalizeFirstLetter('n'.repeat(48)),
},
].map(runTest);
});
});
});
44 changes: 22 additions & 22 deletions src/smartstack/name/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { renderTemplate, TemplateContext } from '../../template';
import { isSet } from '../../utils/isSet';

const template = `{#
const template = `{#
Template for Stack Name prop
--------------------------------------------------------------------------------
AWS CloudFormation stack names have a maximum length of 128 characters.
This template truncates various values if they exceed their limit:
Max Lenghts:
- No Environment or Account present:
- No Environment or Account present:
32+1+48 = 81
- Environment present:
32+1+32+13+48 = 126
- Account present:
- Account present:
32+1+32+9+48 = 122
#}{#
#}{#
"MyProject-"
#}{{ projectName | pascal | truncate(32, true, "") | append("-") }}{#
#}{% if environment | notEmpty %}{#
"Staging-Environment-"
#}{{ environment | pascal | stripUnderscore | truncate(32, true, "") | append("-Environment-") }}{#
#}{% elif account | notEmpty %}{#
#}{{ projectName | pascal | truncate(32, true, "") | append("-") }}{#
#}{% if environment | notEmpty %}{#
"Environment-Staging-"
#}{{ environment | pascal | stripUnderscore | truncate(32, true, "") | prepend("Environment-") | append("-") }}{#
#}{% elif account | notEmpty %}{#
"Dev-Account-"
#}{{ account | pascal| truncate(32, true, "") | append("-Account-") }}{#
#}{% endif %}{#
#}{{ account | pascal| truncate(32, true, "") | append("-Account-") }}{#
#}{% endif %}{#
"MyStack"
#}{{ stackId | pascal | truncate(48, true, "")}}`;

Expand Down Expand Up @@ -63,4 +63,4 @@ export function formatName(props: NameProps): string {
account: props.accountType,
environment: props.environmentType,
});
}
}
6 changes: 5 additions & 1 deletion src/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ env.addFilter('notEmpty', function(str: string) {
return typeof str === 'string' && str.length > 0;
});

env.addFilter('prepend', function(str: string, leader: string) {
return `${leader}${str}`;
});

env.addFilter('append', function(str: string, trailer: string) {
return `${str}${trailer}`;
});
Expand All @@ -24,4 +28,4 @@ export type TemplateContext = Record<string, TemplateContextValue>;

export function renderTemplate<T extends TemplateContext>(template: string, context: T): string {
return env.renderString(template, context);
}
}
6 changes: 3 additions & 3 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Integration', () => {
},
});

expect(stack.stackName).toBe('MyCoolProject-Development-Environment-TestStack');
expect(stack.stackName).toBe('MyCoolProject-Environment-Development-TestStack');
expect(stack.terminationProtection).toBeFalsy();
expect(stack.region).toBe('eu-west-1');

Expand Down Expand Up @@ -124,7 +124,7 @@ describe('Integration', () => {
},
});

expect(stack.stackName).toBe('MyCoolProject-FeatureAbc123-Environment-TestStack');
expect(stack.stackName).toBe('MyCoolProject-Environment-FeatureAbc123-TestStack');
expect(stack.terminationProtection).toBeFalsy();
expect(stack.region).toBe('eu-west-1');

Expand Down Expand Up @@ -204,7 +204,7 @@ describe('Integration', () => {
},
});

expect(stack.stackName).toBe('MyCoolProject-Production-Environment-TestStack');
expect(stack.stackName).toBe('MyCoolProject-Environment-Production-TestStack');
expect(stack.terminationProtection).toBeTruthy();
expect(stack.region).toBe('eu-west-1');

Expand Down

0 comments on commit 3465f46

Please sign in to comment.