Skip to content

Commit

Permalink
feat: make GuDistributionBucketParameter a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
akash1810 committed Mar 19, 2021
1 parent 2025612 commit 9b0e839
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/constructs/autoscaling/user-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("GuUserData", () => {
const props: GuUserDataProps = {
app,
distributable: {
bucket: new GuDistributionBucketParameter(stack),
bucket: GuDistributionBucketParameter.getInstance(stack),
fileName: "my-app.deb",
executionStatement: `dpkg -i /${app}/my-app.deb`,
},
Expand Down Expand Up @@ -73,7 +73,7 @@ describe("GuUserData", () => {
const props: GuUserDataProps = {
app,
distributable: {
bucket: new GuDistributionBucketParameter(stack),
bucket: GuDistributionBucketParameter.getInstance(stack),
fileName: "my-app.deb",
executionStatement: `dpkg -i /${app}/my-app.deb`,
},
Expand Down
19 changes: 16 additions & 3 deletions src/constructs/core/parameters/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ import type { GuStack } from "../stack";
import { GuStringParameter } from "./base";

export class GuDistributionBucketParameter extends GuStringParameter {
public static parameterName = "DistributionBucketName";
private static instance: GuDistributionBucketParameter | undefined;

constructor(scope: GuStack, id: string = GuDistributionBucketParameter.parameterName) {
super(scope, id, {
// eslint-disable-next-line custom-rules/valid-constructors -- TODO be better
private constructor(scope: GuStack) {
super(scope, "DistributionBucketName", {
description: "SSM parameter containing the S3 bucket name holding distribution artifacts",
default: "/account/services/artifact.bucket",
fromSSM: true,
});
}

public static getInstance(stack: GuStack): GuDistributionBucketParameter {
// Resources can only live in the same App so return a new instance where necessary.
// See https://github.com/aws/aws-cdk/blob/0ea4b19afd639541e5f1d7c1783032ee480c307e/packages/%40aws-cdk/core/lib/private/refs.ts#L47-L50
const isSameStack = this.instance?.node.root === stack.node.root;

if (!this.instance || !isSameStack) {
this.instance = new GuDistributionBucketParameter(stack);
}

return this.instance;
}
}

export class GuPrivateConfigBucketParameter extends GuStringParameter {
Expand Down
2 changes: 1 addition & 1 deletion src/constructs/iam/policies/s3-get-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe("The GuGetDistributablePolicy construct", () => {
const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

const parameterKeys = Object.keys(json.Parameters);
const expectedKeys = ["Stage", GuDistributionBucketParameter.parameterName];
const expectedKeys = ["Stage", GuDistributionBucketParameter.getInstance(stack).id];
expect(parameterKeys).toEqual(expectedKeys);

expect(json.Parameters.DistributionBucketName).toEqual({
Expand Down
2 changes: 1 addition & 1 deletion src/constructs/iam/policies/s3-get-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class GuGetDistributablePolicy extends GuGetS3ObjectsPolicy {
const path = [scope.stack, scope.stage, props.app, "*"].join("/");
super(scope, AppIdentity.suffixText(props, "GetDistributablePolicy"), {
...props,
bucketName: new GuDistributionBucketParameter(scope).valueAsString,
bucketName: GuDistributionBucketParameter.getInstance(scope).valueAsString,
paths: [path],
});
AppIdentity.taggedConstruct(props, this);
Expand Down

0 comments on commit 9b0e839

Please sign in to comment.