From 32dfc672d58dffb489a564767a343f0b946d0379 Mon Sep 17 00:00:00 2001 From: Toby Buckley <74737385+tobuck-aws@users.noreply.github.com> Date: Fri, 8 Mar 2024 08:17:15 -0800 Subject: [PATCH] feat: Add CloudFormation telemetry (#10) * feat: Add CloudFormation telemtry * Added tests * assert => expect * expect => expect().toStrictEqual() --- src/control-plane/control-plane.ts | 4 +++- src/core-app-plane/core-app-plane.ts | 4 +++- src/utils/index.ts | 1 + src/utils/utils.ts | 11 +++++++++++ test/control-plane.test.ts | 18 +++++++++++++++++- 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/utils/utils.ts diff --git a/src/control-plane/control-plane.ts b/src/control-plane/control-plane.ts index 7cf8b814..99166c45 100644 --- a/src/control-plane/control-plane.ts +++ b/src/control-plane/control-plane.ts @@ -13,7 +13,7 @@ import { Services } from './services'; import { Tables } from './tables'; import { TenantConfigService } from './tenant-config/tenant-config-service'; import { DestroyPolicySetter } from '../cdk-aspect/destroy-policy-setter'; -import { EventManager } from '../utils'; +import { EventManager, setTemplateDesc } from '../utils'; export interface ControlPlaneProps { readonly applicationPlaneEventSource: string; @@ -33,6 +33,8 @@ export class ControlPlane extends Construct { constructor(scope: Construct, id: string, props: ControlPlaneProps) { super(scope, id); + setTemplateDesc(this, 'SaaS Builder Toolkit - CoreApplicationPlane (uksb-1tupboc57)'); + cdk.Aspects.of(this).add(new DestroyPolicySetter()); const messaging = new Messaging(this, 'messaging-stack'); diff --git a/src/core-app-plane/core-app-plane.ts b/src/core-app-plane/core-app-plane.ts index 4209ad24..05bcf9e7 100644 --- a/src/core-app-plane/core-app-plane.ts +++ b/src/core-app-plane/core-app-plane.ts @@ -8,7 +8,7 @@ import { Construct } from 'constructs'; import { BashJobOrchestrator } from './bash-job-orchestrator'; import { BashJobRunner } from './bash-job-runner'; import { DestroyPolicySetter } from '../cdk-aspect/destroy-policy-setter'; -import { EventManager } from '../utils'; +import { EventManager, setTemplateDesc } from '../utils'; /** * Provides metadata for outgoing events. @@ -133,6 +133,8 @@ export interface CoreApplicationPlaneProps { export class CoreApplicationPlane extends Construct { constructor(scope: Construct, id: string, props: CoreApplicationPlaneProps) { super(scope, id); + setTemplateDesc(this, 'SaaS Builder Toolkit - CoreApplicationPlane (uksb-1tupboc57)'); + cdk.Aspects.of(this).add(new DestroyPolicySetter()); const eventBus = EventBus.fromEventBusArn(this, 'eventBus', props.eventBusArn); diff --git a/src/utils/index.ts b/src/utils/index.ts index e01dc866..ce845086 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,3 +2,4 @@ // SPDX-License-Identifier: Apache-2.0 export * from './event-manager'; +export * from './utils'; diff --git a/src/utils/utils.ts b/src/utils/utils.ts new file mode 100644 index 00000000..c7695c40 --- /dev/null +++ b/src/utils/utils.ts @@ -0,0 +1,11 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { Stack } from 'aws-cdk-lib'; +import { Construct } from 'constructs'; + +export const setTemplateDesc = (construct: Construct, sbtDesc: string) => { + const stackDesc = Stack.of(construct).templateOptions.description; + let description = stackDesc === undefined ? sbtDesc : stackDesc + ' - ' + sbtDesc; + Stack.of(construct).templateOptions.description = description; +}; diff --git a/test/control-plane.test.ts b/test/control-plane.test.ts index a9607397..6747a70c 100644 --- a/test/control-plane.test.ts +++ b/test/control-plane.test.ts @@ -59,7 +59,7 @@ describe('No unsuppressed cdk-nag Warnings or Errors', () => { }); }); -describe('ControlPlane', () => { +describe('ControlPlane without Description', () => { const app = new cdk.App(); interface TestStackProps extends cdk.StackProps { systemAdminEmail: string; @@ -113,4 +113,20 @@ describe('ControlPlane', () => { expect(targetsCapture.asArray()).toHaveLength(1); } while (targetsCapture.next()); }); + + it('should have a fixed template description, when the containing stack does not have description', () => { + const actual = controlPlaneTestStack.templateOptions.description; + const expected = 'SaaS Builder Toolkit - CoreApplicationPlane (uksb-1tupboc57)'; + expect(actual).toStrictEqual(expected); + }); + + it('should have a concatenated template description, when the containing stack has an existing desc', () => { + const stackWithDescription = new TestStack(app, 'stackWithDescription', { + systemAdminEmail: 'test@example.com', + description: 'ABC', + }); + const actual = stackWithDescription.templateOptions.description; + const expected = 'ABC - SaaS Builder Toolkit - CoreApplicationPlane (uksb-1tupboc57)'; + expect(expected).toStrictEqual(actual); + }); });