Skip to content

Commit

Permalink
fix: Add AppIdentity to GuClassicLoadBalancer
Browse files Browse the repository at this point in the history
Adding `AppIdentity` to `GuClassicLoadBalancer` to support a multi-app stack.

This means the auto-generated logicalId gets suffixed with the app string
and the resource also gets the App tag.

This isn't considered a breaking change because `GuClassicLoadBalancer`
only gets used when migrating a stack and not for new stacks.

That is, we can expect `existingLogicalId` will always get set,
which will prevent the logicalId from being auto-generated.
  • Loading branch information
akash1810 committed Apr 15, 2021
1 parent d9f9653 commit 88d2666
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
46 changes: 42 additions & 4 deletions src/constructs/loadbalancing/elb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import "../../utils/test/jest";
import { SynthUtils } from "@aws-cdk/assert/lib/synth-utils";
import { Vpc } from "@aws-cdk/aws-ec2";
import { Stack } from "@aws-cdk/core";
import { simpleGuStackForTesting } from "../../utils/test";
import { TrackingTag } from "../../constants/library-info";
import { alphabeticalTags, simpleGuStackForTesting } from "../../utils/test";
import type { SynthedStack } from "../../utils/test";
import type { AppIdentity } from "../core/identity";
import { GuClassicLoadBalancer, GuHttpsClassicLoadBalancer } from "./elb";

describe("The GuClassicLoadBalancer class", () => {
Expand All @@ -15,26 +17,54 @@ describe("The GuClassicLoadBalancer class", () => {
privateSubnetIds: [""],
});

const app: AppIdentity = { app: "testing" };

test("overrides the logicalId when existingLogicalId is set in a migrating stack", () => {
const stack = simpleGuStackForTesting({ migratedFromCloudFormation: true });
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", { vpc, existingLogicalId: "MyCLB" });
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", { ...app, vpc, existingLogicalId: "MyCLB" });

expect(stack).toHaveResourceOfTypeAndLogicalId("AWS::ElasticLoadBalancing::LoadBalancer", "MyCLB");
});

test("auto-generates the logicalId by default", () => {
const stack = simpleGuStackForTesting();
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", { vpc });
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", { ...app, vpc });

expect(stack).toHaveResourceOfTypeAndLogicalId(
"AWS::ElasticLoadBalancing::LoadBalancer",
/^ClassicLoadBalancer.+$/
/^ClassicLoadBalancerTesting.+$/
);
});

test("applies the App tag", () => {
const stack = simpleGuStackForTesting();
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", { ...app, vpc });

expect(stack).toHaveResource("AWS::ElasticLoadBalancing::LoadBalancer", {
Tags: alphabeticalTags([
{
Key: "App",
Value: "testing",
},
{
Key: "Stack",
Value: "test-stack",
},
{
Key: "Stage",
Value: {
Ref: "Stage",
},
},
TrackingTag,
]),
});
});

test("overrides any properties as required", () => {
const stack = simpleGuStackForTesting();
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", {
...app,
vpc,
propertiesToOverride: {
AccessLoggingPolicy: {
Expand All @@ -55,6 +85,7 @@ describe("The GuClassicLoadBalancer class", () => {
test("uses default health check properties", () => {
const stack = simpleGuStackForTesting();
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", {
...app,
vpc,
});

Expand All @@ -72,6 +103,7 @@ describe("The GuClassicLoadBalancer class", () => {
test("merges any health check properties provided", () => {
const stack = simpleGuStackForTesting();
new GuClassicLoadBalancer(stack, "ClassicLoadBalancer", {
...app,
vpc,
healthCheck: {
path: "/test",
Expand All @@ -98,9 +130,12 @@ describe("The GuHttpsClassicLoadBalancer class", () => {
privateSubnetIds: [""],
});

const app: AppIdentity = { app: "testing" };

test("uses default listener values", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
...app,
vpc,
});

Expand All @@ -122,6 +157,7 @@ describe("The GuHttpsClassicLoadBalancer class", () => {
test("adds the CertificateARN parameter if no value provided", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
...app,
vpc,
});

Expand All @@ -138,6 +174,7 @@ describe("The GuHttpsClassicLoadBalancer class", () => {
test("uses the certificate id provided", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
...app,
vpc,
listener: {
sslCertificateId: "certificateId",
Expand All @@ -164,6 +201,7 @@ describe("The GuHttpsClassicLoadBalancer class", () => {
test("merges any listener values provided", () => {
const stack = simpleGuStackForTesting();
new GuHttpsClassicLoadBalancer(stack, "HttpsClassicLoadBalancer", {
...app,
vpc,
listener: {
internalPort: 3000,
Expand Down
6 changes: 4 additions & 2 deletions src/constructs/loadbalancing/elb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { Duration } from "@aws-cdk/core";
import { GuStatefulMigratableConstruct } from "../../utils/mixin";
import type { GuStack } from "../core";
import { GuArnParameter } from "../core";
import { AppIdentity } from "../core/identity";
import type { GuMigratingResource } from "../core/migrating";

interface GuClassicLoadBalancerProps extends Omit<LoadBalancerProps, "healthCheck">, GuMigratingResource {
interface GuClassicLoadBalancerProps extends Omit<LoadBalancerProps, "healthCheck">, GuMigratingResource, AppIdentity {
propertiesToOverride?: Record<string, unknown>;
healthCheck?: Partial<HealthCheck>;
}
Expand All @@ -33,7 +34,8 @@ export class GuClassicLoadBalancer extends GuStatefulMigratableConstruct(LoadBal
healthCheck: { ...GuClassicLoadBalancer.DefaultHealthCheck, ...props.healthCheck },
};

super(scope, id, mergedProps);
super(scope, AppIdentity.suffixText({ app: props.app }, id), mergedProps);
AppIdentity.taggedConstruct({ app: props.app }, this);

const cfnLb = this.node.defaultChild as CfnLoadBalancer;

Expand Down

0 comments on commit 88d2666

Please sign in to comment.