diff --git a/src/constructs/core/parameters.ts b/src/constructs/core/parameters.ts index 975efd2aa..ba93880dd 100644 --- a/src/constructs/core/parameters.ts +++ b/src/constructs/core/parameters.ts @@ -10,11 +10,16 @@ export interface GuParameterProps extends CfnParameterProps { export type GuNoTypeParameterProps = Omit; export class GuParameter extends CfnParameter { + public readonly id: string; + constructor(scope: GuStack, id: string, props: GuParameterProps) { super(scope, id, { ...props, type: props.fromSSM ? `AWS::SSM::Parameter::Value<${props.type ?? "String"}>` : props.type, }); + + this.id = id; + scope.setParam(this); } } diff --git a/src/constructs/core/stack.test.ts b/src/constructs/core/stack.test.ts index e4bf1cb66..59e21867d 100644 --- a/src/constructs/core/stack.test.ts +++ b/src/constructs/core/stack.test.ts @@ -7,6 +7,7 @@ import { alphabeticalTags, simpleGuStackForTesting } from "../../../test/utils"; import type { SynthedStack } from "../../../test/utils"; import { Stage, Stages } from "../../constants"; import { TrackingTag } from "../../constants/library-info"; +import { GuParameter } from "./parameters"; import { GuStack } from "./stack"; describe("The GuStack construct", () => { @@ -63,4 +64,21 @@ describe("The GuStack construct", () => { expect(stack.app).toBe("MyApp"); }); + + it("should return a parameter that exists", () => { + const stack = new GuStack(new App(), "Test", { app: "MyApp", stack: "test" }); + const testParam = new GuParameter(stack, "MyTestParam", {}); + stack.setParam(testParam); + + const actual = stack.getParam("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", { app: "MyApp", stack: "test" }); + + expect(() => stack.getParam("i-do-not-exist")).toThrowError( + "Attempting to read parameter i-do-not-exist which does not exist" + ); + }); }); diff --git a/src/constructs/core/stack.ts b/src/constructs/core/stack.ts index 0780b4eb8..df1a97f03 100644 --- a/src/constructs/core/stack.ts +++ b/src/constructs/core/stack.ts @@ -1,6 +1,7 @@ import type { App, StackProps } from "@aws-cdk/core"; import { Stack, Tags } from "@aws-cdk/core"; import { TrackingTag } from "../../constants/library-info"; +import type { GuParameter } from "./parameters"; import { GuStageParameter } from "./parameters"; export interface GuStackProps extends StackProps { @@ -43,6 +44,8 @@ export class GuStack extends Stack { private readonly _stack: string; private readonly _app: string; + private params: Map; + public readonly migratedFromCloudFormation: boolean; get stage(): string { @@ -73,12 +76,26 @@ export class GuStack extends Stack { Tags.of(this).add(key, value, { applyToLaunchedInstances }); } + setParam(value: GuParameter): void { + this.params.set(value.id, value); + } + + getParam(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; + } + // eslint-disable-next-line custom-rules/valid-constructors -- GuStack is the exception as it must take an App constructor(app: App, id: string, props: GuStackProps) { super(app, id, props); this.migratedFromCloudFormation = !!props.migratedFromCloudFormation; + this.params = new Map(); + this._stage = new GuStageParameter(this); this._stack = props.stack; this._app = props.app;