-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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(docdb): make most attributes of DatabaseClusterAttributes optional #19625
fix(docdb): make most attributes of DatabaseClusterAttributes optional #19625
Conversation
Title does not follow the guidelines of Conventional Commits. Please adjust title before merge. |
0f0fc84
to
1b9ee78
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great @Synohara! 2 minor comments.
/** | ||
* Represents an imported database cluster. | ||
*/ | ||
class ImportedDatabaseCluster extends DatabaseClusterBase implements IDatabaseCluster { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity - why did you move this class from where it was defined?
Let's maybe leave it where it is - this way, the diff will be significantly smaller?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks all for the lighting fast response!
I have made changes based on the following code.
aws-cdk/packages/@aws-cdk/aws-rds/lib/cluster.ts
Lines 427 to 487 in 4071d9b
/** | |
* Represents an imported database cluster. | |
*/ | |
class ImportedDatabaseCluster extends DatabaseClusterBase implements IDatabaseCluster { | |
public readonly clusterIdentifier: string; | |
public readonly connections: ec2.Connections; | |
public readonly engine?: IClusterEngine; | |
private readonly _clusterEndpoint?: Endpoint; | |
private readonly _clusterReadEndpoint?: Endpoint; | |
private readonly _instanceIdentifiers?: string[]; | |
private readonly _instanceEndpoints?: Endpoint[]; | |
constructor(scope: Construct, id: string, attrs: DatabaseClusterAttributes) { | |
super(scope, id); | |
this.clusterIdentifier = attrs.clusterIdentifier; | |
const defaultPort = attrs.port ? ec2.Port.tcp(attrs.port) : undefined; | |
this.connections = new ec2.Connections({ | |
securityGroups: attrs.securityGroups, | |
defaultPort, | |
}); | |
this.engine = attrs.engine; | |
this._clusterEndpoint = (attrs.clusterEndpointAddress && attrs.port) ? new Endpoint(attrs.clusterEndpointAddress, attrs.port) : undefined; | |
this._clusterReadEndpoint = (attrs.readerEndpointAddress && attrs.port) ? new Endpoint(attrs.readerEndpointAddress, attrs.port) : undefined; | |
this._instanceIdentifiers = attrs.instanceIdentifiers; | |
this._instanceEndpoints = (attrs.instanceEndpointAddresses && attrs.port) | |
? attrs.instanceEndpointAddresses.map(addr => new Endpoint(addr, attrs.port!)) | |
: undefined; | |
} | |
public get clusterEndpoint() { | |
if (!this._clusterEndpoint) { | |
throw new Error('Cannot access `clusterEndpoint` of an imported cluster without an endpoint address and port'); | |
} | |
return this._clusterEndpoint; | |
} | |
public get clusterReadEndpoint() { | |
if (!this._clusterReadEndpoint) { | |
throw new Error('Cannot access `clusterReadEndpoint` of an imported cluster without a readerEndpointAddress and port'); | |
} | |
return this._clusterReadEndpoint; | |
} | |
public get instanceIdentifiers() { | |
if (!this._instanceIdentifiers) { | |
throw new Error('Cannot access `instanceIdentifiers` of an imported cluster without provided instanceIdentifiers'); | |
} | |
return this._instanceIdentifiers; | |
} | |
public get instanceEndpoints() { | |
if (!this._instanceEndpoints) { | |
throw new Error('Cannot access `instanceEndpoints` of an imported cluster without instanceEndpointAddresses and port'); | |
} | |
return this._instanceEndpoints; | |
} | |
} |
but I agree with your suggestion. So I left it as is and made fix.
test('minimal imported cluster throws on accessing attributes for unprovided parameters', () => { | ||
const stack = testStack(); | ||
|
||
const cluster = DatabaseCluster.fromDatabaseClusterAttributes(stack, 'Database', { | ||
clusterIdentifier: 'identifier', | ||
}); | ||
|
||
expect(() => cluster.clusterEndpoint).toThrow(/Cannot access `clusterEndpoint` of an imported cluster/); | ||
expect(() => cluster.clusterReadEndpoint).toThrow(/Cannot access `clusterReadEndpoint` of an imported cluster/); | ||
expect(() => cluster.instanceIdentifiers).toThrow(/Cannot access `instanceIdentifiers` of an imported cluster/); | ||
expect(() => cluster.instanceEndpoints).toThrow(/Cannot access `instanceEndpoints` of an imported cluster/); | ||
expect(() => cluster.securityGroupId).toThrow(/Cannot access `securityGroupId` of an imported cluster/); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's maybe merge these 2 into one test?
1b9ee78
to
981e56b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great @Synohara! A few last stylistic changes before we merge this in.
private readonly _instanceEndpoints?: Endpoint[]; | ||
private readonly _securityGroupId?: string; | ||
|
||
constructor(_scope: Construct, _id: string, _attrs: DatabaseClusterAttributes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this constructor? Can't we just assign the fields like _clusterEndpoint
and _clusterReadEndpoint
directly, like we do for clusterIdentifier
and defaultPort
?
this._clusterEndpoint = (_attrs.clusterEndpointAddress && _attrs.port) ? new Endpoint(_attrs.clusterEndpointAddress, _attrs.port) : undefined; | ||
this._clusterReadEndpoint = (_attrs.readerEndpointAddress && _attrs.port) | ||
? new Endpoint(_attrs.readerEndpointAddress, _attrs.port) | ||
: undefined; | ||
this._instanceIdentifiers = _attrs.instanceIdentifiers; | ||
this._instanceEndpoints = (_attrs.instanceEndpointAddresses && _attrs.port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need the parenthesis in the ternary operator, let's get rid of them.
@@ -257,20 +257,70 @@ export class DatabaseCluster extends DatabaseClusterBase { | |||
*/ | |||
public static fromDatabaseClusterAttributes(scope: Construct, id: string, attrs: DatabaseClusterAttributes): IDatabaseCluster { | |||
class Import extends DatabaseClusterBase implements IDatabaseCluster { | |||
public readonly defaultPort = ec2.Port.tcp(attrs.port); | |||
public readonly clusterIdentifier = attrs.clusterIdentifier; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep the fields in the same order they are now? This way, the diff will be easier to to read.
@@ -257,20 +257,70 @@ export class DatabaseCluster extends DatabaseClusterBase { | |||
*/ | |||
public static fromDatabaseClusterAttributes(scope: Construct, id: string, attrs: DatabaseClusterAttributes): IDatabaseCluster { | |||
class Import extends DatabaseClusterBase implements IDatabaseCluster { | |||
public readonly defaultPort = ec2.Port.tcp(attrs.port); | |||
public readonly clusterIdentifier = attrs.clusterIdentifier; | |||
public readonly defaultPort = attrs.port ? ec2.Port.tcp(attrs.port) : undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember that 0
is falsy in JavaScript/TypeScript. You probably want to compare against undefined
here.
public readonly connections = new ec2.Connections({ | ||
securityGroups: [attrs.securityGroup], | ||
securityGroups: !!attrs.securityGroup ? [attrs.securityGroup] : undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the double !!
here, all of a sudden? Removing it will not change this code.
return this._instanceEndpoints; | ||
} | ||
|
||
public get securityGroupId() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you specify the return types for all of these getters?
8157fe3
to
1ace74d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks almost perfect @Synohara!
public readonly clusterReadEndpoint = new Endpoint(attrs.readerEndpointAddress, attrs.port); | ||
public readonly instanceEndpoints = attrs.instanceEndpointAddresses.map(a => new Endpoint(a, attrs.port)); | ||
public readonly securityGroupId = attrs.securityGroup.securityGroupId; | ||
private readonly _instanceIdentifiers?= attrs.instanceIdentifiers; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the ?=
? I think this should just be a =
. Also, missing space:
private readonly _instanceIdentifiers?= attrs.instanceIdentifiers; | |
private readonly _instanceIdentifiers = attrs.instanceIdentifiers; |
Same comment in the other fields added in the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for suggestion! I fixed the use of optional properties.
Co-authored-by: sandship <sandship.blancard@gmail.com> Co-authored-by: yoshitaka KOITABASHI <Yoshiitaka@users.noreply.github.com>
directory assign fields add return types in getter
99f66fc
to
dcccdc2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution @Synohara!
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
aws#19625) Fixes aws#14492 - Test Result ``` ❯ yarn test yarn run v1.22.17 $ cdk-test PASS test/endpoint.test.js (17.557 s) PASS test/parameter-group.test.js (17.601 s) PASS test/instance.test.js (18.16 s) PASS test/cluster.test.js (19.233 s) =============================== Coverage summary =============================== Statements : 98.76% ( 160/162 ) Branches : 99.02% ( 102/103 ) Functions : 100% ( 27/27 ) Lines : 98.75% ( 158/160 ) ================================================================================ Test Suites: 4 passed, 4 total Tests: 51 passed, 51 total Snapshots: 0 total Time: 19.946 s Ran all test suites. Verifying integ.cluster-rotation.lit.js against integ.cluster-rotation.lit.expected.json ... OK. Verifying integ.cluster.js against integ.cluster.expected.json ... OK. Tests successful. Total time (26.2s) | /Users/yoshitaka.koitabashi/Desktop/OSS/tmp/aws-cdk/node_modules/jest/bin/jest.js (23.8s) | cdk-integ-assert (2.4s) ✨ Done in 26.72s. ``` ---- ### All Submissions: * [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Fixes #14492
All Submissions:
Adding new Unconventional Dependencies:
New Features
cdk-integ
to deploy the infrastructure and generate the snapshot (i.e.cdk-integ
without--dry-run
)?By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license