From 7ffd69640bc4313fa1480c60e2ce897cf406607a Mon Sep 17 00:00:00 2001 From: Hyeonsoo David Lee Date: Sat, 25 Jul 2020 14:10:00 +0900 Subject: [PATCH] fix(rds): Add `engine` to `IDatabaseInstance`, `IClusterInstance` fixes #9195 related to #8686 BREAKING CHANGE: Constructs for imported resources take additional parameters in props - **rds**: `DatabaseCluster.fromDatabaseClusterAttributes` takes `engine: IClusterEngine` - **rds**: `DatabaseInstanceBase.fromDatabaseInstanceAttributes` takes `engine: IInstanceEngine` --- packages/@aws-cdk/aws-rds/README.md | 14 ++++++++++++++ packages/@aws-cdk/aws-rds/lib/cluster-ref.ts | 11 +++++++++++ packages/@aws-cdk/aws-rds/lib/cluster.ts | 11 +++++++++++ packages/@aws-cdk/aws-rds/lib/instance.ts | 18 ++++++++++++++++++ packages/@aws-cdk/aws-rds/lib/proxy.ts | 12 ++++++------ packages/@aws-cdk/aws-rds/test/test.cluster.ts | 1 + .../@aws-cdk/aws-rds/test/test.instance.ts | 1 + 7 files changed, 62 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-rds/README.md b/packages/@aws-cdk/aws-rds/README.md index b6ba9cb161b71..7c0f255a804bb 100644 --- a/packages/@aws-cdk/aws-rds/README.md +++ b/packages/@aws-cdk/aws-rds/README.md @@ -112,6 +112,20 @@ const instance = new rds.DatabaseInstance(this, 'Instance', { }); ``` +To import an already existing instance: + +```ts +rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'Database', { + engine: rds.DatabaseInstanceEngine.mysql({ version: rds.MysqlEngineVersion.VER_5_7_30 }), + instanceEndpointAddress: 'address', + instanceIdentifier: 'identifier', + port: 3306, + securityGroups: [ec2.SecurityGroup.fromSecurityGroupId(stack, 'SG', 'sg-123456789', { + allowAllOutbound: false, + })], +}); +``` + Use `DatabaseInstanceFromSnapshot` and `DatabaseInstanceReadReplica` to create an instance from snapshot or a source database respectively: diff --git a/packages/@aws-cdk/aws-rds/lib/cluster-ref.ts b/packages/@aws-cdk/aws-rds/lib/cluster-ref.ts index 0db0389b9cb67..6abfe3dbd3f82 100644 --- a/packages/@aws-cdk/aws-rds/lib/cluster-ref.ts +++ b/packages/@aws-cdk/aws-rds/lib/cluster-ref.ts @@ -1,6 +1,7 @@ import * as ec2 from '@aws-cdk/aws-ec2'; import * as secretsmanager from '@aws-cdk/aws-secretsmanager'; import { IResource } from '@aws-cdk/core'; +import { IClusterEngine } from './cluster-engine'; import { Endpoint } from './endpoint'; import { DatabaseProxy, DatabaseProxyOptions } from './proxy'; @@ -8,6 +9,11 @@ import { DatabaseProxy, DatabaseProxyOptions } from './proxy'; * Create a clustered database with a given number of instances. */ export interface IDatabaseCluster extends IResource, ec2.IConnectable, secretsmanager.ISecretAttachmentTarget { + /** + * The database engine + */ + readonly engine: IClusterEngine; + /** * Identifier of the cluster */ @@ -45,6 +51,11 @@ export interface IDatabaseCluster extends IResource, ec2.IConnectable, secretsma * Properties that describe an existing cluster instance */ export interface DatabaseClusterAttributes { + /** + * The database engine + */ + readonly engine: IClusterEngine; + /** * The database port */ diff --git a/packages/@aws-cdk/aws-rds/lib/cluster.ts b/packages/@aws-cdk/aws-rds/lib/cluster.ts index 2318fb1d9762e..1bd7756ce122e 100644 --- a/packages/@aws-cdk/aws-rds/lib/cluster.ts +++ b/packages/@aws-cdk/aws-rds/lib/cluster.ts @@ -205,6 +205,10 @@ export interface DatabaseClusterProps { * A new or imported clustered database. */ abstract class DatabaseClusterBase extends Resource implements IDatabaseCluster { + /** + * The database engine + */ + public abstract readonly engine: IClusterEngine; /** * Identifier of the cluster */ @@ -271,6 +275,7 @@ export class DatabaseCluster extends DatabaseClusterBase { securityGroups: attrs.securityGroups, defaultPort: this.defaultPort, }); + public readonly engine = attrs.engine; public readonly clusterIdentifier = attrs.clusterIdentifier; public readonly instanceIdentifiers: string[] = []; public readonly clusterEndpoint = new Endpoint(attrs.clusterEndpointAddress, attrs.port); @@ -281,6 +286,11 @@ export class DatabaseCluster extends DatabaseClusterBase { return new Import(scope, id); } + /** + * The database engine + */ + public readonly engine: IClusterEngine; + /** * Identifier of the cluster */ @@ -528,6 +538,7 @@ export class DatabaseCluster extends DatabaseClusterBase { const defaultPort = ec2.Port.tcp(this.clusterEndpoint.port); this.connections = new ec2.Connections({ securityGroups, defaultPort }); + this.engine = props.engine; } /** diff --git a/packages/@aws-cdk/aws-rds/lib/instance.ts b/packages/@aws-cdk/aws-rds/lib/instance.ts index 0890b26b705d2..056584f8357e0 100644 --- a/packages/@aws-cdk/aws-rds/lib/instance.ts +++ b/packages/@aws-cdk/aws-rds/lib/instance.ts @@ -19,6 +19,11 @@ import { CfnDBInstance, CfnDBInstanceProps, CfnDBSubnetGroup } from './rds.gener * A database instance */ export interface IDatabaseInstance extends IResource, ec2.IConnectable, secretsmanager.ISecretAttachmentTarget { + /** + * The database engine. + */ + readonly engine: IInstanceEngine; + /** * The instance identifier. */ @@ -64,6 +69,11 @@ export interface IDatabaseInstance extends IResource, ec2.IConnectable, secretsm * Properties that describe an existing instance */ export interface DatabaseInstanceAttributes { + /** + * The database engine. + */ + readonly engine: IInstanceEngine; + /** * The instance identifier. */ @@ -99,6 +109,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase securityGroups: attrs.securityGroups, defaultPort: this.defaultPort, }); + public readonly engine = attrs.engine; public readonly instanceIdentifier = attrs.instanceIdentifier; public readonly dbInstanceEndpointAddress = attrs.instanceEndpointAddress; public readonly dbInstanceEndpointPort = attrs.port.toString(); @@ -108,6 +119,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase return new Import(scope, id); } + public abstract readonly engine: IInstanceEngine; public abstract readonly instanceIdentifier: string; public abstract readonly dbInstanceEndpointAddress: string; public abstract readonly dbInstanceEndpointPort: string; @@ -776,6 +788,7 @@ export interface DatabaseInstanceProps extends DatabaseInstanceSourceProps { * @resource AWS::RDS::DBInstance */ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabaseInstance { + public readonly engine: IInstanceEngine; public readonly instanceIdentifier: string; public readonly dbInstanceEndpointAddress: string; public readonly dbInstanceEndpointPort: string; @@ -804,6 +817,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas storageEncrypted: props.storageEncryptionKey ? true : props.storageEncrypted, }); + this.engine = props.engine; this.instanceIdentifier = instance.ref; this.dbInstanceEndpointAddress = instance.attrEndpointAddress; this.dbInstanceEndpointPort = instance.attrEndpointPort; @@ -862,6 +876,7 @@ export interface DatabaseInstanceFromSnapshotProps extends DatabaseInstanceSourc * @resource AWS::RDS::DBInstance */ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource implements IDatabaseInstance { + public readonly engine: IInstanceEngine; public readonly instanceIdentifier: string; public readonly dbInstanceEndpointAddress: string; public readonly dbInstanceEndpointPort: string; @@ -900,6 +915,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme : props.masterUserPassword && props.masterUserPassword.toString(), }); + this.engine = props.engine; this.instanceIdentifier = instance.ref; this.dbInstanceEndpointAddress = instance.attrEndpointAddress; this.dbInstanceEndpointPort = instance.attrEndpointPort; @@ -957,6 +973,7 @@ export interface DatabaseInstanceReadReplicaProps extends DatabaseInstanceNewPro * @resource AWS::RDS::DBInstance */ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements IDatabaseInstance { + public readonly engine: IInstanceEngine; public readonly instanceIdentifier: string; public readonly dbInstanceEndpointAddress: string; public readonly dbInstanceEndpointPort: string; @@ -974,6 +991,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements storageEncrypted: props.storageEncryptionKey ? true : props.storageEncrypted, }); + this.engine = props.sourceDatabaseInstance.engine; this.instanceType = props.instanceType; this.instanceIdentifier = instance.ref; this.dbInstanceEndpointAddress = instance.attrEndpointAddress; diff --git a/packages/@aws-cdk/aws-rds/lib/proxy.ts b/packages/@aws-cdk/aws-rds/lib/proxy.ts index eff37a0b4307c..caa3f2dcf17aa 100644 --- a/packages/@aws-cdk/aws-rds/lib/proxy.ts +++ b/packages/@aws-cdk/aws-rds/lib/proxy.ts @@ -4,7 +4,7 @@ import * as secretsmanager from '@aws-cdk/aws-secretsmanager'; import * as cdk from '@aws-cdk/core'; import { IDatabaseCluster } from './cluster-ref'; import { IDatabaseInstance } from './instance'; -import { CfnDBCluster, CfnDBInstance, CfnDBProxy, CfnDBProxyTargetGroup } from './rds.generated'; +import { CfnDBProxy, CfnDBProxyTargetGroup } from './rds.generated'; /** * SessionPinningFilter @@ -65,17 +65,17 @@ export class ProxyTarget { * Bind this target to the specified database proxy. */ public bind(_: DatabaseProxy): ProxyTargetConfig { - let engine: string | undefined; + let engineType: string | undefined; if (this.dbCluster && this.dbInstance) { throw new Error('Proxy cannot target both database cluster and database instance.'); } else if (this.dbCluster) { - engine = (this.dbCluster.node.defaultChild as CfnDBCluster).engine; + engineType = this.dbCluster.engine.engineType; } else if (this.dbInstance) { - engine = (this.dbInstance.node.defaultChild as CfnDBInstance).engine; + engineType = this.dbInstance.engine.engineType; } let engineFamily; - switch (engine) { + switch (engineType) { case 'aurora': case 'aurora-mysql': case 'mysql': @@ -86,7 +86,7 @@ export class ProxyTarget { engineFamily = 'POSTGRESQL'; break; default: - throw new Error(`Unsupported engine type - ${engine}`); + throw new Error(`Unsupported engine type - ${engineType}`); } return { diff --git a/packages/@aws-cdk/aws-rds/test/test.cluster.ts b/packages/@aws-cdk/aws-rds/test/test.cluster.ts index 85d2a6d2b75d4..16ce218684045 100644 --- a/packages/@aws-cdk/aws-rds/test/test.cluster.ts +++ b/packages/@aws-cdk/aws-rds/test/test.cluster.ts @@ -379,6 +379,7 @@ export = { const stack = testStack(); const cluster = DatabaseCluster.fromDatabaseClusterAttributes(stack, 'Database', { + engine: DatabaseClusterEngine.auroraMysql({ version: AuroraMysqlEngineVersion.VER_2_08_1 }), clusterEndpointAddress: 'addr', clusterIdentifier: 'identifier', instanceEndpointAddresses: ['addr'], diff --git a/packages/@aws-cdk/aws-rds/test/test.instance.ts b/packages/@aws-cdk/aws-rds/test/test.instance.ts index 39136b7bc45a6..426fa2b79f836 100644 --- a/packages/@aws-cdk/aws-rds/test/test.instance.ts +++ b/packages/@aws-cdk/aws-rds/test/test.instance.ts @@ -549,6 +549,7 @@ export = { const stack = new cdk.Stack(); const instance = rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'Database', { + engine: rds.DatabaseInstanceEngine.mysql({ version: rds.MysqlEngineVersion.VER_5_7_30 }), instanceEndpointAddress: 'address', instanceIdentifier: 'identifier', port: 3306,