-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #853 from guardian/aa-mixin-app-aware
refactor: Add `GuAppAwareConstruct` mixin
- Loading branch information
Showing
5 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/utils/mixin/__snapshots__/app-aware-contruct.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`The GuAppAwareConstruct mixin should add the app tag 1`] = ` | ||
Object { | ||
"Parameters": Object { | ||
"Stage": Object { | ||
"AllowedValues": Array [ | ||
"CODE", | ||
"PROD", | ||
], | ||
"Default": "CODE", | ||
"Description": "Stage name", | ||
"Type": "String", | ||
}, | ||
}, | ||
"Resources": Object { | ||
"MyBucketTest262E966E": Object { | ||
"DeletionPolicy": "Retain", | ||
"Properties": Object { | ||
"Tags": Array [ | ||
Object { | ||
"Key": "App", | ||
"Value": "Test", | ||
}, | ||
Object { | ||
"Key": "gu:cdk:version", | ||
"Value": "TEST", | ||
}, | ||
Object { | ||
"Key": "gu:repo", | ||
"Value": "guardian/cdk", | ||
}, | ||
Object { | ||
"Key": "Stack", | ||
"Value": "test-stack", | ||
}, | ||
Object { | ||
"Key": "Stage", | ||
"Value": Object { | ||
"Ref": "Stage", | ||
}, | ||
}, | ||
], | ||
}, | ||
"Type": "AWS::S3::Bucket", | ||
"UpdateReplacePolicy": "Retain", | ||
}, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Construct } from "@aws-cdk/core"; | ||
import { AppIdentity } from "../../constructs/core/identity"; | ||
import type { AnyConstructor } from "./types"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -- mixin | ||
export function GuAppAwareConstruct<TBase extends AnyConstructor>(BaseClass: TBase) { | ||
class Mixin extends BaseClass { | ||
/** | ||
* The ID of the construct with the App suffix. | ||
* This should be used in place of `id` when trying to reference the construct. | ||
*/ | ||
readonly idWithApp: string; | ||
|
||
// eslint-disable-next-line custom-rules/valid-constructors, @typescript-eslint/no-explicit-any -- mixin | ||
protected constructor(...args: any[]) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- mixin | ||
const [scope, id, props, ...rest] = args; | ||
|
||
if (!AppIdentity.isAppIdentity(props)) { | ||
throw new Error("Cannot use the GuAppAwareConstruct mixin without an AppIdentity"); | ||
} | ||
|
||
const app: string = props.app; | ||
const idWithApp = AppIdentity.suffixText({ app }, id as string); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- mixin | ||
const newArgs = [scope, idWithApp, props, ...rest]; | ||
super(...newArgs); | ||
|
||
this.idWithApp = idWithApp; | ||
|
||
/* | ||
Add the `App` tag to the construct. | ||
Although not every resource can be tagged, it's still safe to make the call. | ||
If AWS support tags on a new resource one day, our test suite will fail and we can celebrate! | ||
See https://docs.aws.amazon.com/ARG/latest/userguide/supported-resources.html | ||
*/ | ||
if (Construct.isConstruct(this)) { | ||
AppIdentity.taggedConstruct({ app }, this); | ||
} | ||
} | ||
} | ||
return Mixin; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { SynthUtils } from "@aws-cdk/assert"; | ||
import type { BucketProps } from "@aws-cdk/aws-s3"; | ||
import { Bucket } from "@aws-cdk/aws-s3"; | ||
import type { GuStack } from "../../constructs/core"; | ||
import type { AppIdentity } from "../../constructs/core/identity"; | ||
import { simpleGuStackForTesting } from "../test"; | ||
import { GuAppAwareConstruct } from "./app-aware-construct"; | ||
|
||
// `GuAppAwareConstruct` should only operate if `props` has an `app` property, | ||
// so usage here should be a no-op. | ||
class TestConstruct extends GuAppAwareConstruct(Bucket) { | ||
constructor(scope: GuStack, id: string, props: BucketProps) { | ||
super(scope, id, props); | ||
} | ||
} | ||
|
||
interface TestAppAwareConstructProps extends BucketProps, AppIdentity {} | ||
|
||
class TestAppAwareConstruct extends GuAppAwareConstruct(Bucket) { | ||
constructor(scope: GuStack, id: string, props: TestAppAwareConstructProps) { | ||
super(scope, id, props); | ||
} | ||
} | ||
|
||
describe("The GuAppAwareConstruct mixin", () => { | ||
// demonstrates usage of `GuAppAwareConstruct` | ||
it("should throw if no app identifier is provided", () => { | ||
const stack = simpleGuStackForTesting(); | ||
|
||
expect(() => { | ||
new TestConstruct(stack, "MyBucket", {}); | ||
}).toThrowError("Cannot use the GuAppAwareConstruct mixin without an AppIdentity"); | ||
}); | ||
|
||
it("should suffix the id with the app identifier", () => { | ||
const stack = simpleGuStackForTesting(); | ||
const bucket = new TestAppAwareConstruct(stack, "MyBucket", { app: "Test" }); | ||
|
||
expect(bucket.idWithApp).toBe("MyBucketTest"); | ||
}); | ||
|
||
it("should add the app tag", () => { | ||
const stack = simpleGuStackForTesting(); | ||
new TestAppAwareConstruct(stack, "MyBucket", { app: "Test" }); | ||
expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot(); | ||
}); | ||
}); |