Skip to content
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

feat!: make GuDistributionBucketParameter a singleton #341

Merged
merged 1 commit into from
Mar 24, 2021
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
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