Skip to content

Commit

Permalink
add parameter helper functions on GuStack
Browse files Browse the repository at this point in the history
Helper functions to get/set parameters on the GuStack. An `Error` is thrown when getting a parameter that hasn't been set; this is to catch the error at compile time.
  • Loading branch information
akash1810 committed Jan 15, 2021
1 parent 093cff5 commit cba8f4a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/constructs/core/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ export interface GuParameterProps extends CfnParameterProps {
export type GuNoTypeParameterProps = Omit<GuParameterProps, "type">;

export class GuParameter extends CfnParameter {
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);
}
}

Expand Down
22 changes: 20 additions & 2 deletions src/constructs/core/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import "@aws-cdk/assert/jest";
import { SynthUtils } from "@aws-cdk/assert";
import { Role, ServicePrincipal } from "@aws-cdk/aws-iam";
import { App } from "@aws-cdk/core";
import { simpleGuStackForTesting } from "../../../test/utils/simple-gu-stack";
import type { SynthedStack } from "../../../test/utils/synthed-stack";
import { simpleGuStackForTesting } from "../../../test/utils";
import type { SynthedStack } from "../../../test/utils";
import { Stage, Stages } from "../../constants";
import { GuParameter } from "./parameters";
import { GuStack } from "./stack";

describe("The GuStack construct", () => {
Expand Down Expand Up @@ -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" });
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", { app: "MyApp" });

expect(() => stack.getParam<GuParameter>("i-do-not-exist")).toThrowError(
"Attempting to read parameter i-do-not-exist which does not exist"
);
});
});
17 changes: 17 additions & 0 deletions src/constructs/core/stack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { App, StackProps } from "@aws-cdk/core";
import { Stack, Tags } from "@aws-cdk/core";
import type { GuParameter } from "./parameters";
import { GuStackParameter, GuStageParameter } from "./parameters";

export interface GuStackProps extends StackProps {
Expand All @@ -13,6 +14,8 @@ export class GuStack extends Stack {
private readonly _stack: GuStackParameter;
private readonly _app: string;

private params: Map<string, GuParameter>;

get stage(): string {
return this._stage.valueAsString;
}
Expand All @@ -29,6 +32,18 @@ export class GuStack extends Stack {
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;
}

// 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);
Expand All @@ -37,6 +52,8 @@ export class GuStack extends Stack {
this._stack = new GuStackParameter(this);
this._app = props.app;

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

this.addTag("Stack", this.stack);
this.addTag("Stage", this.stage);
this.addTag("App", props.app);
Expand Down

0 comments on commit cba8f4a

Please sign in to comment.