From 9c0e26d9bb326151246c9a2a75901e6dad57e7e5 Mon Sep 17 00:00:00 2001 From: Julian Michel Date: Mon, 13 Dec 2021 23:01:16 +0100 Subject: [PATCH 1/2] fix(rds): unable to use tokens as port in `DatabaseInstance` --- packages/@aws-cdk/aws-rds/lib/instance.ts | 2 +- .../@aws-cdk/aws-rds/test/instance.test.ts | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index 065f4c4fb947b..d50dd0e4f8004 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -746,7 +746,7 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData performanceInsightsRetentionPeriod: enablePerformanceInsights ? (props.performanceInsightRetention || PerformanceInsightRetention.DEFAULT) : undefined, - port: props.port?.toString(), + port: props.port ? Tokenization.stringifyNumber(props.port) : undefined, preferredBackupWindow: props.preferredBackupWindow, preferredMaintenanceWindow: props.preferredMaintenanceWindow, processorFeatures: props.processorFeatures && renderProcessorFeatures(props.processorFeatures), diff --git a/packages/@aws-cdk/aws-rds/test/instance.test.ts b/packages/@aws-cdk/aws-rds/test/instance.test.ts index fd6519b6fb6bc..51c4de837690b 100644 --- a/packages/@aws-cdk/aws-rds/test/instance.test.ts +++ b/packages/@aws-cdk/aws-rds/test/instance.test.ts @@ -1630,6 +1630,38 @@ describe('instance', () => { }, }); }); + + test('instance with port', () => { + // WHEN + new rds.DatabaseInstance(stack, 'Database', { + engine: rds.DatabaseInstanceEngine.MYSQL, + instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL), + vpc, + port: 3306, + }); + + // THEN + expect(stack).toHaveResourceLike('AWS::RDS::DBInstance', { + Port: '3306', + }); + }); + + test('instance with port from token', () => { + // WHEN + new rds.DatabaseInstance(stack, 'Database', { + engine: rds.DatabaseInstanceEngine.MYSQL, + instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL), + vpc, + port: cdk.Token.asNumber({ Ref: 'port' }), + }); + + // THEN + expect(stack).toHaveResourceLike('AWS::RDS::DBInstance', { + Port: { + Ref: 'port', + }, + }); + }); }); test.each([ From cb8853231a464906b882b44a70339af839a01051 Mon Sep 17 00:00:00 2001 From: Julian Michel Date: Tue, 14 Dec 2021 18:38:43 +0100 Subject: [PATCH 2/2] optimize logic / tests --- packages/@aws-cdk/aws-rds/lib/instance.ts | 2 +- packages/@aws-cdk/aws-rds/test/instance.test.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index d50dd0e4f8004..10467e0b19390 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -746,7 +746,7 @@ abstract class DatabaseInstanceNew extends DatabaseInstanceBase implements IData performanceInsightsRetentionPeriod: enablePerformanceInsights ? (props.performanceInsightRetention || PerformanceInsightRetention.DEFAULT) : undefined, - port: props.port ? Tokenization.stringifyNumber(props.port) : undefined, + port: props.port !== undefined ? Tokenization.stringifyNumber(props.port) : undefined, preferredBackupWindow: props.preferredBackupWindow, preferredMaintenanceWindow: props.preferredMaintenanceWindow, processorFeatures: props.processorFeatures && renderProcessorFeatures(props.processorFeatures), diff --git a/packages/@aws-cdk/aws-rds/test/instance.test.ts b/packages/@aws-cdk/aws-rds/test/instance.test.ts index 51c4de837690b..ae3972b7e9e19 100644 --- a/packages/@aws-cdk/aws-rds/test/instance.test.ts +++ b/packages/@aws-cdk/aws-rds/test/instance.test.ts @@ -1631,7 +1631,7 @@ describe('instance', () => { }); }); - test('instance with port', () => { + test('instance with port provided as a number', () => { // WHEN new rds.DatabaseInstance(stack, 'Database', { engine: rds.DatabaseInstanceEngine.MYSQL, @@ -1646,19 +1646,24 @@ describe('instance', () => { }); }); - test('instance with port from token', () => { + test('instance with port provided as a CloudFormation parameter', () => { + // GIVEN + const port = new cdk.CfnParameter(stack, 'Port', { + type: 'Number', + }).valueAsNumber; + // WHEN new rds.DatabaseInstance(stack, 'Database', { engine: rds.DatabaseInstanceEngine.MYSQL, instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL), vpc, - port: cdk.Token.asNumber({ Ref: 'port' }), + port, }); // THEN expect(stack).toHaveResourceLike('AWS::RDS::DBInstance', { Port: { - Ref: 'port', + Ref: 'Port', }, }); });