From dfb64d8f84417bd87620e31647481ecd265b4c3e Mon Sep 17 00:00:00 2001 From: Akash Askoolum Date: Tue, 2 Feb 2021 21:21:23 +0000 Subject: [PATCH] Add `X-Gu-CDK-Version` tag to GuStack We can use this to track which stacks have been created using the library, and with which version. The absence of this tag suggests the stack hasn't been created with the cdk library. --- src/constants/library-info.ts | 10 +++++ .../__snapshots__/lambda-alarms.test.ts.snap | 8 ++++ src/constructs/core/stack.test.ts | 6 ++- src/constructs/core/stack.ts | 3 ++ .../policies/__snapshots__/ses.test.ts.snap | 4 ++ .../__snapshots__/instance-role.test.ts.snap | 12 ++++++ .../lambda/__snapshots__/lambda.test.ts.snap | 40 +++++++++++++++++-- src/constructs/rds/instance.test.ts | 6 ++- .../scheduled-lambda.test.ts.snap | 8 ++++ .../__snapshots__/sns-lambda.test.ts.snap | 12 ++++++ tsconfig.json | 3 +- 11 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 src/constants/library-info.ts diff --git a/src/constants/library-info.ts b/src/constants/library-info.ts new file mode 100644 index 000000000..1a45f3500 --- /dev/null +++ b/src/constants/library-info.ts @@ -0,0 +1,10 @@ +import { version } from "../../package.json"; + +export const LibraryInfo = { + VERSION: version, +}; + +export const TrackingTag = { + Key: "X-Gu-CDK-Version", + Value: LibraryInfo.VERSION, +}; diff --git a/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap b/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap index 67793394d..16ecf7282 100644 --- a/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap +++ b/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap @@ -55,6 +55,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], "Timeout": 30, }, @@ -105,6 +109,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", diff --git a/src/constructs/core/stack.test.ts b/src/constructs/core/stack.test.ts index 5fea24753..fea9c49b1 100644 --- a/src/constructs/core/stack.test.ts +++ b/src/constructs/core/stack.test.ts @@ -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 { TrackingTag } from "../../constants/library-info"; import { GuStack } from "./stack"; describe("The GuStack construct", () => { @@ -54,6 +55,7 @@ describe("The GuStack construct", () => { Ref: "Stage", }, }, + TrackingTag, ], }); }); diff --git a/src/constructs/core/stack.ts b/src/constructs/core/stack.ts index 896f026b7..6891e7a55 100644 --- a/src/constructs/core/stack.ts +++ b/src/constructs/core/stack.ts @@ -1,5 +1,6 @@ import type { App, StackProps } from "@aws-cdk/core"; import { Stack, Tags } from "@aws-cdk/core"; +import { TrackingTag } from "../../constants/library-info"; import { GuStackParameter, GuStageParameter } from "./parameters"; export interface GuStackProps extends StackProps { @@ -42,6 +43,8 @@ export class GuStack extends Stack { this._stack = new GuStackParameter(this); this._app = props.app; + this.addTag(TrackingTag.Key, TrackingTag.Value); + this.addTag("Stack", this.stack); this.addTag("Stage", this.stage); this.addTag("App", props.app); diff --git a/src/constructs/iam/policies/__snapshots__/ses.test.ts.snap b/src/constructs/iam/policies/__snapshots__/ses.test.ts.snap index 87f8f0aae..46bec0962 100644 --- a/src/constructs/iam/policies/__snapshots__/ses.test.ts.snap +++ b/src/constructs/iam/policies/__snapshots__/ses.test.ts.snap @@ -104,6 +104,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", diff --git a/src/constructs/iam/roles/__snapshots__/instance-role.test.ts.snap b/src/constructs/iam/roles/__snapshots__/instance-role.test.ts.snap index c3440faa4..5f47d89d5 100644 --- a/src/constructs/iam/roles/__snapshots__/instance-role.test.ts.snap +++ b/src/constructs/iam/roles/__snapshots__/instance-role.test.ts.snap @@ -145,6 +145,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", @@ -404,6 +408,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", @@ -616,6 +624,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", diff --git a/src/constructs/lambda/__snapshots__/lambda.test.ts.snap b/src/constructs/lambda/__snapshots__/lambda.test.ts.snap index 15170802f..b19d298e8 100644 --- a/src/constructs/lambda/__snapshots__/lambda.test.ts.snap +++ b/src/constructs/lambda/__snapshots__/lambda.test.ts.snap @@ -109,6 +109,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], "Timeout": 30, }, @@ -159,6 +163,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", @@ -234,6 +242,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::ApiGateway::RestApi", @@ -418,11 +430,15 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", }, - "lambdaapi2Deployment4ECB93738ba48d7cb88b4c2b9ec5c442ee1c99e9": Object { + "lambdaapi2Deployment4ECB937324608a148e4c74ee74e20f580623b2d9": Object { "DependsOn": Array [ "lambdaapi2proxyANY3F8E1D36", "lambdaapi2proxyE68FDFBC", @@ -439,7 +455,7 @@ Object { "lambdaapi2DeploymentStageprodCC9E3016": Object { "Properties": Object { "DeploymentId": Object { - "Ref": "lambdaapi2Deployment4ECB93738ba48d7cb88b4c2b9ec5c442ee1c99e9", + "Ref": "lambdaapi2Deployment4ECB937324608a148e4c74ee74e20f580623b2d9", }, "RestApiId": Object { "Ref": "lambdaapi239350ECC", @@ -462,6 +478,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::ApiGateway::Stage", @@ -755,6 +775,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::ApiGateway::RestApi", @@ -804,11 +828,15 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", }, - "lambdaapiDeployment8E95B5854e05b687211f19a449136200fabf5846": Object { + "lambdaapiDeployment8E95B585b22be554088fbcc2c63fc037a54e995d": Object { "DependsOn": Array [ "lambdaapiproxyANYA94E968A", "lambdaapiproxyB573C729", @@ -825,7 +853,7 @@ Object { "lambdaapiDeploymentStageprod9598BC2F": Object { "Properties": Object { "DeploymentId": Object { - "Ref": "lambdaapiDeployment8E95B5854e05b687211f19a449136200fabf5846", + "Ref": "lambdaapiDeployment8E95B585b22be554088fbcc2c63fc037a54e995d", }, "RestApiId": Object { "Ref": "lambdaapiC1812993", @@ -848,6 +876,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::ApiGateway::Stage", diff --git a/src/constructs/rds/instance.test.ts b/src/constructs/rds/instance.test.ts index f87ccdafa..2c571a335 100644 --- a/src/constructs/rds/instance.test.ts +++ b/src/constructs/rds/instance.test.ts @@ -3,8 +3,9 @@ import { SynthUtils } from "@aws-cdk/assert/lib/synth-utils"; import { Vpc } from "@aws-cdk/aws-ec2"; import { DatabaseInstanceEngine, ParameterGroup, PostgresEngineVersion } from "@aws-cdk/aws-rds"; import { Stack } 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 { TrackingTag } from "../../constants/library-info"; import { GuDatabaseInstance } from "./instance"; describe("The GuDatabaseInstance class", () => { @@ -100,6 +101,7 @@ describe("The GuDatabaseInstance class", () => { Ref: "Stage", }, }, + TrackingTag, ], }); }); diff --git a/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap b/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap index 0c8029d52..001181536 100644 --- a/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap +++ b/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap @@ -56,6 +56,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], "Timeout": 30, }, @@ -156,6 +160,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", diff --git a/src/patterns/__snapshots__/sns-lambda.test.ts.snap b/src/patterns/__snapshots__/sns-lambda.test.ts.snap index 7345fe97a..340dc1b0a 100644 --- a/src/patterns/__snapshots__/sns-lambda.test.ts.snap +++ b/src/patterns/__snapshots__/sns-lambda.test.ts.snap @@ -48,6 +48,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::SNS::Topic", @@ -89,6 +93,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], "Timeout": 30, }, @@ -205,6 +213,10 @@ Object { "Ref": "Stage", }, }, + Object { + "Key": "X-Gu-CDK-Version", + "Value": "0.31.0", + }, ], }, "Type": "AWS::IAM::Role", diff --git a/tsconfig.json b/tsconfig.json index cffaa79a3..c79d1cb45 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,8 @@ "inlineSources": true, "experimentalDecorators": true, "typeRoots": ["./node_modules/@types"], - "outDir": "lib" + "outDir": "lib", + "resolveJsonModule": true }, "include": ["src/**/*"], "exclude": ["node_modules", "test", "src/**/*.test.ts", "src/**/__snapshots__/**"]