Skip to content

Commit

Permalink
feat: warn about usage of legacy and/or compat tagging behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
aripalo committed Nov 25, 2024
1 parent ab4ba94 commit 34b4063
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 89 deletions.
46 changes: 38 additions & 8 deletions src/__test__/TestableProjectStack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import {
Project,
SmartStack,
Expand All @@ -14,6 +15,21 @@ interface TestStackInSharedAccountProps {
accountType: AccountType;
environmentType?: string;
stackProps?: cdk.StackProps;
appContext?: Record<string, any>;
}

class TestContextProvider extends Construct {
constructor(
scope: Construct,
id: string,
appContext: Record<string, any> = {},
) {
super(scope, id);

Object.entries(appContext).forEach(([key, value]) => {
this.node.setContext(key, value);
});
}
}

export class TestableProjectStack extends SmartStack {
Expand All @@ -27,6 +43,7 @@ export class TestableProjectStack extends SmartStack {
environmentType,
defaultRegion,
stackProps,
appContext,
} = props;

const projectName = "test-project";
Expand All @@ -41,18 +58,31 @@ export class TestableProjectStack extends SmartStack {
accounts,
});

project.node.setContext("account-type", accountType);
project.node.setContext("account", accountType);
const ctx: Record<string, any> = {};

if (typeof environmentType === "string") {
ctx["environment-type"] = environmentType;
ctx.environment = environmentType;
ctx.env = environmentType;
}

const testContextProvider = new TestContextProvider(
project,
"TestContextProvider",
{
...appContext,
"account-type": accountType,
account: accountType,
...ctx,
},
);

let wrapper: AccountWrapper | EnvironmentWrapper;

if (environmentType) {
project.node.setContext("environment-type", environmentType);
project.node.setContext("environment", environmentType);
project.node.setContext("env", environmentType);
wrapper = new EnvironmentWrapper(project);
if (typeof environmentType === "string") {
wrapper = new EnvironmentWrapper(testContextProvider);
} else {
wrapper = new AccountWrapper(project);
wrapper = new AccountWrapper(testContextProvider);
}

const stackConstructId = "TestStack";
Expand Down
53 changes: 0 additions & 53 deletions src/project/deprecation-warnings.test.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/project/deprecation-warnings.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Construct } from "constructs";
import { Account, ProjectConfiguration } from "./interfaces";
import { resolveDefaultRegion } from "./resolve-region";
import { addError } from "../error";
import { warnAboutDeprecatedTags } from "./deprecation-warnings";

/** Props given to `Project`.
*
Expand Down Expand Up @@ -96,7 +95,5 @@ export class Project extends App {
[Project.CONTEXT_SCOPE]: config, // and inject project context
},
});

warnAboutDeprecatedTags(this);
}
}
42 changes: 42 additions & 0 deletions src/smartstack/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TestableProjectStack } from "../__test__/TestableProjectStack";
import { AccountStrategy } from "../configurations";
import { SmartStack } from "./stack";
import { AccountType } from "../configurations/accounts";
import { LEGACY_TAGS_CONTEXT_KEY, V0_TAGS_CONTEXT_KEY } from "../feature-flags";

test("TestableProjectStack is instance of SmartStack", () => {
const stack = new TestableProjectStack({
Expand Down Expand Up @@ -162,3 +163,44 @@ describe("env", () => {
expect(stack.region).toEqual("eu-central-1");
});
});

describe("deprecation warnings", () => {
test("warns about legacy tags", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.one({
shared: { id: "111111111111" },
}),
accountType: AccountType.SHARED,
appContext: {
[LEGACY_TAGS_CONTEXT_KEY]: true,
},
});

expectErrorMetadata(
stack,
expect.stringContaining("@alma-cdk/project@v1:legacy-tags"),
);
});

test("warns about compatibility v0 tags", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.one({ shared: { id: "111111111111" } }),
accountType: AccountType.SHARED,
appContext: { [V0_TAGS_CONTEXT_KEY]: true },
});

expectErrorMetadata(
stack,
expect.stringContaining("@alma-cdk/project@v1:compatibility-v0-tags"),
);
});

test("does not warn if no legacy or compatibility v0 tags", () => {
const stack = new TestableProjectStack({
accounts: AccountStrategy.one({ shared: { id: "111111111111" } }),
accountType: AccountType.SHARED,
});

expectErrorMetadata(stack, undefined);
});
});
22 changes: 21 additions & 1 deletion src/smartstack/stack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { env } from "process";
import { Stack, StackProps } from "aws-cdk-lib";
import { Stack, StackProps, Annotations } from "aws-cdk-lib";
import { Construct } from "constructs";
import { formatDescription } from "./description";
import { formatName } from "./name";
import { addTags } from "./tags";
import { decideTerminationProtection } from "./termination";
import { addError } from "../error";
import {
LEGACY_TAGS_CONTEXT_KEY,
useCompatibilityV0Tags,
useLegacyTags,
V0_TAGS_CONTEXT_KEY,
} from "../feature-flags";
import { ProjectContext } from "../project";

export class SmartStack extends Stack {
Expand Down Expand Up @@ -52,6 +58,20 @@ export class SmartStack extends Stack {
this.validateDescriptionMaxLength(props);

addTags(this);

if (useLegacyTags(this)) {
Annotations.of(this).addWarningV2(
"@alma-cdk/project@v1:legacy-tags",
`Using @almamedia-cdk/tag-and-name (for AWS CDK v1) construct's legacy tagging behavior via "${LEGACY_TAGS_CONTEXT_KEY}" context key. This is not encouraged and will be removed in v2.`,
);
}

if (useCompatibilityV0Tags(this)) {
Annotations.of(this).addWarningV2(
"@alma-cdk/project@v1:compatibility-v0-tags",
`Using @alma-cdk/project@v0 construct's tagging behavior via "${V0_TAGS_CONTEXT_KEY}" context key. You should migrate to using the default tagging behavior as this feature flag will be removed in v2.`,
);
}
}

private validateDescriptionMinLength(props: StackProps) {
Expand Down

0 comments on commit 34b4063

Please sign in to comment.