Skip to content

Commit

Permalink
Merge branch 'beta' into beta-v1-launch-prep-part-1
Browse files Browse the repository at this point in the history
  • Loading branch information
aripalo committed Nov 25, 2024
2 parents 0845bfe + f61a339 commit 5537c33
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 90 deletions.
56 changes: 56 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 40 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,9 +15,25 @@ 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 {
public readonly project: Project;
public readonly projectName: string;
public readonly stackConstructId: string;

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

const projectName = "test-project";
Expand All @@ -41,18 +59,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 All @@ -62,6 +93,7 @@ export class TestableProjectStack extends SmartStack {
...stackProps,
});

this.project = project;
this.projectName = projectName;
this.stackConstructId = stackConstructId;
}
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.

2 changes: 1 addition & 1 deletion src/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./interfaces";
export { Project, ProjectProps } from "./project";
export { Project, ProjectProps, Acknowledgeable } from "./project";
export { ProjectContext, ProjectContext as PC } from "./project-context";
export { AccountType } from "./account-type";
export { EnvironmentType } from "./environment-type";
Expand Down
50 changes: 50 additions & 0 deletions src/project/project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Annotations } from "aws-cdk-lib";
import * as assertions from "aws-cdk-lib/assertions";
import { Construct } from "constructs";
import { TestableProjectStack } from "../__test__/TestableProjectStack";

describe("Acknowledge warnings", () => {
test("warning present if not acknowledged", () => {
const stack = new TestableProjectStack({
accounts: {
shared: {
id: "111111111111",
},
},
accountType: "shared",
});

const warningId = "my-test-warning";

const withWarning = new Construct(stack, "WithWarning");
Annotations.of(withWarning).addWarningV2(warningId, "don't do this");

assertions.Annotations.fromStack(stack).hasWarning(
"*",
assertions.Match.stringLikeRegexp(warningId),
);
});

test("acknowledges warnings", () => {
const stack = new TestableProjectStack({
accounts: {
shared: {
id: "111111111111",
},
},
accountType: "shared",
});

const warningId = "my-test-warning";

const withWarning = new Construct(stack, "WithWarning");
Annotations.of(withWarning).addWarningV2(warningId, "don't do this");

stack.project.acknowledgeWarnings([{ id: warningId, message: "is okay" }]);

assertions.Annotations.fromStack(stack).hasNoWarning(
"*",
assertions.Match.stringLikeRegexp(warningId),
);
});
});
25 changes: 22 additions & 3 deletions src/project/project.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { App, AppProps } from "aws-cdk-lib";
import { Annotations, App, AppProps, Stack } from "aws-cdk-lib";
import { Construct } from "constructs";
import { Account, ProjectConfiguration } from "./interfaces";
import { resolveDefaultRegion } from "./resolve-region";
import { addError } from "../error";
import { warnAboutDeprecatedTags } from "./deprecation-warnings";

/**
* Interface for acknowledging warnings.
*/
export interface Acknowledgeable {
readonly id: string;
readonly message?: string;
}

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

warnAboutDeprecatedTags(this);
/**
* Acknowledge warnings for all stacks in the project.
*/
public acknowledgeWarnings(acknowledgements: Acknowledgeable[]) {
const stacks = this.node
.findAll()
.filter((x): x is Stack => x instanceof Stack);
stacks.map((stack) => {
acknowledgements.map((ack) => {
Annotations.of(stack).acknowledgeWarning(ack.id, ack.message);
});
});
}
}
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);
});
});
Loading

0 comments on commit 5537c33

Please sign in to comment.