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(rds): do not allow aurora engines when using DatabaseInstance #5367

Merged
merged 6 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ abstract class DatabaseInstanceSource extends DatabaseInstanceNew implements IDa
constructor(scope: Construct, id: string, props: DatabaseInstanceSourceProps) {
super(scope, id, props);

if (/aurora/.test(props.engine.name)) {
Copy link
Contributor

@skinny85 skinny85 Dec 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I love this test...

How about a method on Engine, something like get allowedOutsideCluster(): boolean (name TBD of course)? Then, this code could be:

    if (!props.engine.allowedOutsideCluster) {
      throw new Error(`${props.engine.name} instances can only be created inside a cluster. Please use the DatabaseCluster class instead`);
    }

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually just a public readonly isDatabaseInstanceEngine = true; in DatabaseInstanceEngine is enough no? Because then the typing system will not allow to use a DatabaseInstanceCluster for a DatabaseInstance and we don't need to check anymore?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. This works:

export class DatabaseInstanceEngine extends DatabaseClusterEngine {
  // ...

  /** To make it a compile-time error to pass a DatabaseClusterEngine where a DatabaseInstanceEngine is expected. */
  public readonly isDatabaseInstanceEngine = true;
}

Having it be a compile-time error is even better than adding runtime validation.

throw new Error('Aurora instances can only be created inside a cluster. Please use `DatabaseCluster`.');
}

this.secretRotationApplication = props.engine.secretRotationApplication;

this.sourceCfnProps = {
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-rds/test/test.instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,22 @@ export = {
}
}));

test.done();
},

'throws with aurora engines'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');

// THEN
test.throws(() => new rds.DatabaseInstance(stack, 'Instance', {
engine: rds.DatabaseInstanceEngine.AURORA_POSTGRESQL,
instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.SMALL),
masterUsername: 'admin',
vpc,
}), /Aurora instances can only be created inside a cluster. Please use `DatabaseCluster`./);

test.done();
}
};