Skip to content

Commit

Permalink
refactor(rds): Make instanceResourceId prop optional
Browse files Browse the repository at this point in the history
To avoid a breaking change, make `instanceResourceId` an optional prop.
  • Loading branch information
akash1810 committed Apr 20, 2023
1 parent 73d879a commit ce630ec
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-backup/test/selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ test('fromRdsDatabaseInstance', () => {
const existingInstance = rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'Existing', {
instanceEndpointAddress: 'address',
instanceIdentifier: 'existing-instance',
instanceResourceId: 'resource-id',
port: 3306,
securityGroups: [],
});
Expand Down
16 changes: 10 additions & 6 deletions packages/aws-cdk-lib/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface IDatabaseInstance extends IResource, ec2.IConnectable, secretsm
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbinstance.html#aws-resource-rds-dbinstance-return-values
*/
readonly instanceResourceId: string;
readonly instanceResourceId?: string;

/**
* The instance endpoint.
Expand Down Expand Up @@ -112,7 +112,7 @@ export interface DatabaseInstanceAttributes {
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-rds-dbinstance.html#aws-resource-rds-dbinstance-return-values
*/
readonly instanceResourceId: string;
readonly instanceResourceId?: string;

/**
* The security groups of the instance.
Expand Down Expand Up @@ -156,7 +156,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
public abstract readonly instanceIdentifier: string;
public abstract readonly dbInstanceEndpointAddress: string;
public abstract readonly dbInstanceEndpointPort: string;
public abstract readonly instanceResourceId: string;
public abstract readonly instanceResourceId?: string;
public abstract readonly instanceEndpoint: Endpoint;
// only required because of JSII bug: https://github.com/aws/jsii/issues/2040
public abstract readonly engine?: IInstanceEngine;
Expand All @@ -182,6 +182,10 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
throw new Error('Cannot grant connect when IAM authentication is disabled');
}

if (!this.instanceResourceId) {
throw new Error('For imported Database Instances, instanceResourceId is required to grantConnect()');
}

if (!dbUser) {
throw new Error('For imported Database Instances, the dbUser is required to grantConnect()');
}
Expand Down Expand Up @@ -1126,7 +1130,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas
public readonly instanceIdentifier: string;
public readonly dbInstanceEndpointAddress: string;
public readonly dbInstanceEndpointPort: string;
public readonly instanceResourceId: string;
public readonly instanceResourceId?: string;
public readonly instanceEndpoint: Endpoint;
public readonly secret?: secretsmanager.ISecret;

Expand Down Expand Up @@ -1195,7 +1199,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme
public readonly instanceIdentifier: string;
public readonly dbInstanceEndpointAddress: string;
public readonly dbInstanceEndpointPort: string;
public readonly instanceResourceId: string;
public readonly instanceResourceId?: string;
public readonly instanceEndpoint: Endpoint;
public readonly secret?: secretsmanager.ISecret;

Expand Down Expand Up @@ -1285,7 +1289,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements
public readonly instanceIdentifier: string;
public readonly dbInstanceEndpointAddress: string;
public readonly dbInstanceEndpointPort: string;
public readonly instanceResourceId: string;
public readonly instanceResourceId?: string;
public readonly instanceEndpoint: Endpoint;
public readonly engine?: IInstanceEngine = undefined;
protected readonly instanceType: ec2.InstanceType;
Expand Down
2 changes: 0 additions & 2 deletions packages/aws-cdk-lib/aws-rds/test/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ describe('instance', () => {
instanceIdentifier: '',
securityGroups: [],
instanceEndpointAddress: '',
instanceResourceId: '',
port: databasePort,
});

Expand Down Expand Up @@ -686,7 +685,6 @@ describe('instance', () => {
const instance = rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'Database', {
instanceEndpointAddress: 'address',
instanceIdentifier: 'identifier',
instanceResourceId: 'resource-id',
port: 3306,
securityGroups: [ec2.SecurityGroup.fromSecurityGroupId(stack, 'SG', 'sg-123456789', {
allowAllOutbound: false,
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-rds/test/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ describe('proxy', () => {
const importedInstance = rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'Cluster', {
instanceIdentifier: 'my-instance',
instanceEndpointAddress: 'instance-address',
instanceResourceId: 'resource-id',
port: 5432,
securityGroups: [],
engine: rds.DatabaseInstanceEngine.mariaDb({
Expand Down

0 comments on commit ce630ec

Please sign in to comment.