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

fix: Update GuDatabaseInstance with revised logicalId logic #433

Merged
merged 1 commit into from
Apr 13, 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
52 changes: 7 additions & 45 deletions src/constructs/rds/instance.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import "@aws-cdk/assert/jest";
import { SynthUtils } from "@aws-cdk/assert/lib/synth-utils";
import "../../utils/test/jest";
import { Vpc } from "@aws-cdk/aws-ec2";
import { DatabaseInstanceEngine, ParameterGroup, PostgresEngineVersion } from "@aws-cdk/aws-rds";
import { Stack } from "@aws-cdk/core";
import { TrackingTag } from "../../constants/library-info";
import { alphabeticalTags, simpleGuStackForTesting } from "../../utils/test";
import type { SynthedStack } from "../../utils/test";
import { GuDatabaseInstance } from "./instance";

describe("The GuDatabaseInstance class", () => {
Expand Down Expand Up @@ -107,70 +106,33 @@ describe("The GuDatabaseInstance class", () => {
});
});

it("overrides the id if the prop is true", () => {
const stack = simpleGuStackForTesting();
new GuDatabaseInstance(stack, "DatabaseInstance", {
vpc,
overrideId: true,
instanceType: "t3.small",
engine: DatabaseInstanceEngine.postgres({
version: PostgresEngineVersion.VER_11_8,
}),
app: "testing",
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Resources)).toContain("DatabaseInstance");
});

it("does not override the id if the prop is false", () => {
const stack = simpleGuStackForTesting();
new GuDatabaseInstance(stack, "DatabaseInstance", {
vpc,
instanceType: "t3.small",
engine: DatabaseInstanceEngine.postgres({
version: PostgresEngineVersion.VER_11_8,
}),
app: "testing",
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Resources)).not.toContain("DatabaseInstance");
});

it("overrides the id if the stack migrated value is true", () => {
it("overrides the logicalId when existingLogicalId is set in a migrating stack", () => {
const stack = simpleGuStackForTesting({ migratedFromCloudFormation: true });
new GuDatabaseInstance(stack, "DatabaseInstance", {
vpc,
existingLogicalId: "MyDb",
instanceType: "t3.small",
engine: DatabaseInstanceEngine.postgres({
version: PostgresEngineVersion.VER_11_8,
}),
app: "testing",
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Resources)).toContain("DatabaseInstance");
expect(stack).toHaveResourceOfTypeAndLogicalId("AWS::RDS::DBInstance", "MyDb");
});

it("does not override the id if the stack migrated value is true but the override id value is false", () => {
const stack = simpleGuStackForTesting({ migratedFromCloudFormation: true });
test("auto-generates the logicalId by default", () => {
const stack = simpleGuStackForTesting();
new GuDatabaseInstance(stack, "DatabaseInstance", {
vpc,
overrideId: false,
instanceType: "t3.small",
engine: DatabaseInstanceEngine.postgres({
version: PostgresEngineVersion.VER_11_8,
}),
app: "testing",
});

const json = SynthUtils.toCloudFormation(stack) as SynthedStack;

expect(Object.keys(json.Resources)).not.toContain("DatabaseInstance");
expect(stack).toHaveResourceOfTypeAndLogicalId("AWS::RDS::DBInstance", /^DatabaseInstance.+$/);
});

test("sets the deletion protection value to true by default", () => {
Expand Down
16 changes: 8 additions & 8 deletions src/constructs/rds/instance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { InstanceType } from "@aws-cdk/aws-ec2";
import type { CfnDBInstance, DatabaseInstanceProps, IParameterGroup } from "@aws-cdk/aws-rds";
import type { DatabaseInstanceProps, IParameterGroup } from "@aws-cdk/aws-rds";
import { DatabaseInstance, ParameterGroup } from "@aws-cdk/aws-rds";
import { Fn } from "@aws-cdk/core";
import { GuStatefulMigratableConstruct } from "../../utils/mixin";
import type { GuStack } from "../core";
import { AppIdentity } from "../core/identity";
import type { GuMigratingResource } from "../core/migrating";

export interface GuDatabaseInstanceProps extends Omit<DatabaseInstanceProps, "instanceType">, AppIdentity {
overrideId?: boolean;
export interface GuDatabaseInstanceProps
extends Omit<DatabaseInstanceProps, "instanceType">,
AppIdentity,
GuMigratingResource {
instanceType: string;
parameters?: Record<string, string>;
}

export class GuDatabaseInstance extends DatabaseInstance {
export class GuDatabaseInstance extends GuStatefulMigratableConstruct(DatabaseInstance) {
constructor(scope: GuStack, id: string, props: GuDatabaseInstanceProps) {
// CDK just wants "t3.micro" format, whereas
// some CFN yaml might have the older "db.t3.micro" with the "db." prefix
Expand All @@ -35,10 +39,6 @@ export class GuDatabaseInstance extends DatabaseInstance {
...(parameterGroup && { parameterGroup }),
});

if (props.overrideId || (scope.migratedFromCloudFormation && props.overrideId !== false)) {
(this.node.defaultChild as CfnDBInstance).overrideLogicalId(id);
}

parameterGroup && AppIdentity.taggedConstruct(props, parameterGroup);
AppIdentity.taggedConstruct(props, this);
}
Expand Down