Skip to content

feat: Add parameters getter on GuStack #1313

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

Merged
merged 1 commit into from
Jun 1, 2022
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
1 change: 0 additions & 1 deletion src/constructs/core/parameters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class GuParameter extends CfnParameter {
});

this.id = id;
scope.setParam(this);
}
}

Expand Down
18 changes: 0 additions & 18 deletions src/constructs/core/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Queue } from "aws-cdk-lib/aws-sqs";
import { CfnInclude } from "aws-cdk-lib/cloudformation-include";
import { ContextKeys, LibraryInfo, TagKeys } from "../../constants";
import { GuTemplate } from "../../utils/test";
import { GuParameter } from "./parameters";
import type { GuStackProps } from "./stack";
import { GuStack } from "./stack";

Expand Down Expand Up @@ -53,23 +52,6 @@ describe("The GuStack construct", () => {
});
});

it("should return a parameter that exists", () => {
const stack = new GuStack(new App(), "Test", { stack: "test", stage: "TEST" });
const testParam = new GuParameter(stack, "MyTestParam", {});
stack.setParam(testParam);

const actual = stack.getParam<GuParameter>("MyTestParam");
expect(actual).toBe(testParam);
});

it("should throw on attempt to get a parameter that doesn't exist", () => {
const stack = new GuStack(new App(), "Test", { stack: "test", stage: "TEST" });

expect(() => stack.getParam<GuParameter>("i-do-not-exist")).toThrowError(
"Attempting to read parameter i-do-not-exist which does not exist"
);
});

it("can persist the logicalId for any resource (consumer's POV)", () => {
class MigratingStack extends GuStack {
// eslint-disable-next-line custom-rules/valid-constructors -- for testing purposes
Expand Down
31 changes: 11 additions & 20 deletions src/constructs/core/stack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Annotations, App, LegacyStackSynthesizer, Stack, Tags } from "aws-cdk-lib";
import { Annotations, App, CfnParameter, LegacyStackSynthesizer, Stack, Tags } from "aws-cdk-lib";
import type { CfnElement, StackProps } from "aws-cdk-lib";
import { Template } from "aws-cdk-lib/assertions";
import { CfnInclude } from "aws-cdk-lib/cloudformation-include";
Expand All @@ -8,7 +8,6 @@ import { ContextKeys, LibraryInfo, TagKeys, TrackingTag } from "../../constants"
import { gitRemoteOriginUrl } from "../../utils/git";
import type { StackStageIdentity } from "./identity";
import type { GuStaticLogicalId } from "./migrating";
import type { GuParameter } from "./parameters";

export interface GuStackProps extends Omit<StackProps, "stackName"> {
/**
Expand Down Expand Up @@ -62,8 +61,6 @@ export class GuStack extends Stack implements StackStageIdentity {
private readonly _stack: string;
private readonly _stage: string;

private params: Map<string, GuParameter>;

get stage(): string {
return this._stage;
}
Expand All @@ -88,20 +85,16 @@ export class GuStack extends Stack implements StackStageIdentity {
Tags.of(this).add(key, value, { applyToLaunchedInstances });
}

setParam(value: GuParameter): void {
this.params.set(value.id, value);
}

getParam<T extends GuParameter>(key: string): T {
if (!this.params.has(key)) {
throw new Error(`Attempting to read parameter ${key} which does not exist`);
}

return this.params.get(key) as T;
}

get parameterKeys(): string[] {
return Array.from(this.params.keys());
/**
* Returns all the parameters on this stack.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html
*/
get parameters(): Record<string, CfnParameter> {
Copy link
Member Author

@akash1810 akash1810 May 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today, this is only used in the GuStackSet construct below.

return this.node
.findAll()
.filter((construct) => construct instanceof CfnParameter)
.reduce((acc, param) => ({ ...acc, [param.node.id]: param as CfnParameter }), {});
}

// eslint-disable-next-line custom-rules/valid-constructors -- GuStack is the exception as it must take an App
Expand All @@ -117,8 +110,6 @@ export class GuStack extends Stack implements StackStageIdentity {
synthesizer: new LegacyStackSynthesizer(),
});

this.params = new Map<string, GuParameter>();

this._stack = stack;
this._stage = stage.toUpperCase();

Expand Down
4 changes: 3 additions & 1 deletion src/constructs/stack-set/stack-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ export class GuStackSet extends CfnStackSet {
const stackSetInstanceParameters = props.stackSetInstanceParameters ?? {};

const params = Object.keys(stackSetInstanceParameters);
const undefinedStackSetParams = props.stackSetInstance.parameterKeys.filter((_) => !params.includes(_));
const parameterKeys = Object.keys(props.stackSetInstance.parameters);

const undefinedStackSetParams = parameterKeys.filter((_) => !params.includes(_));

if (undefinedStackSetParams.length !== 0) {
throw new Error(`There are undefined stack set parameters: ${undefinedStackSetParams.join(", ")}`);
Comment on lines +145 to 150
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our current model is to feed stack set instance's parameters from the parent stack, rather than have the stack set instance read from parameter store, as the orchestration of this is tricky.

Here we check that all parameters on the stack set instance also exist on the parent stack, throwing if not.

Expand Down