Skip to content

Commit 5f6d20c

Browse files
authored
fix(docdb): make most attributes of DatabaseClusterAttributes optional (#19625)
Fixes #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*
1 parent ad16386 commit 5f6d20c

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

packages/@aws-cdk/aws-docdb/lib/cluster-ref.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ export interface IDatabaseCluster extends IResource, IConnectable, ISecretAttach
4646
export interface DatabaseClusterAttributes {
4747
/**
4848
* The database port
49+
* @default - none
4950
*/
50-
readonly port: number;
51+
readonly port?: number;
5152

5253
/**
5354
* The security group of the database cluster
55+
* @default - no security groups
5456
*/
55-
readonly securityGroup: ISecurityGroup;
57+
readonly securityGroup?: ISecurityGroup;
5658

5759
/**
5860
* Identifier for the cluster
@@ -61,22 +63,26 @@ export interface DatabaseClusterAttributes {
6163

6264
/**
6365
* Identifier for the instances
66+
* @default - no instance identifiers
6467
*/
65-
readonly instanceIdentifiers: string[];
68+
readonly instanceIdentifiers?: string[];
6669
// Actual underlying type: DBInstanceId[], but we have to type it more loosely for Java's benefit.
6770

6871
/**
6972
* Cluster endpoint address
73+
* @default - no cluster endpoint address
7074
*/
71-
readonly clusterEndpointAddress: string;
75+
readonly clusterEndpointAddress?: string;
7276

7377
/**
7478
* Reader endpoint address
79+
* @default - no reader endpoint address
7580
*/
76-
readonly readerEndpointAddress: string;
81+
readonly readerEndpointAddress?: string;
7782

7883
/**
7984
* Endpoint addresses of individual instances
85+
* @default - no instance endpoint addresses
8086
*/
81-
readonly instanceEndpointAddresses: string[];
87+
readonly instanceEndpointAddresses?: string[];
8288
}

packages/@aws-cdk/aws-docdb/lib/cluster.ts

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,55 @@ export class DatabaseCluster extends DatabaseClusterBase {
257257
*/
258258
public static fromDatabaseClusterAttributes(scope: Construct, id: string, attrs: DatabaseClusterAttributes): IDatabaseCluster {
259259
class Import extends DatabaseClusterBase implements IDatabaseCluster {
260-
public readonly defaultPort = ec2.Port.tcp(attrs.port);
260+
public readonly defaultPort = typeof attrs.port !== 'undefined' ? ec2.Port.tcp(attrs.port) : undefined;
261261
public readonly connections = new ec2.Connections({
262-
securityGroups: [attrs.securityGroup],
262+
securityGroups: attrs.securityGroup ? [attrs.securityGroup] : undefined,
263263
defaultPort: this.defaultPort,
264264
});
265265
public readonly clusterIdentifier = attrs.clusterIdentifier;
266-
public readonly instanceIdentifiers = attrs.instanceIdentifiers;
267-
public readonly clusterEndpoint = new Endpoint(attrs.clusterEndpointAddress, attrs.port);
268-
public readonly clusterReadEndpoint = new Endpoint(attrs.readerEndpointAddress, attrs.port);
269-
public readonly instanceEndpoints = attrs.instanceEndpointAddresses.map(a => new Endpoint(a, attrs.port));
270-
public readonly securityGroupId = attrs.securityGroup.securityGroupId;
266+
private readonly _instanceIdentifiers = attrs.instanceIdentifiers;
267+
private readonly _clusterEndpoint = attrs.clusterEndpointAddress && typeof attrs.port !== 'undefined' ?
268+
new Endpoint(attrs.clusterEndpointAddress, attrs.port) : undefined;
269+
private readonly _clusterReadEndpoint = attrs.readerEndpointAddress && typeof attrs.port !== 'undefined' ?
270+
new Endpoint(attrs.readerEndpointAddress, attrs.port) : undefined;
271+
private readonly _instanceEndpoints = attrs.instanceEndpointAddresses && typeof attrs.port !== 'undefined' ?
272+
attrs.instanceEndpointAddresses.map(addr => new Endpoint(addr, attrs.port!)) : undefined;
273+
private readonly _securityGroupId = attrs.securityGroup?.securityGroupId;
274+
275+
public get instanceIdentifiers(): string[] {
276+
if (!this._instanceIdentifiers) {
277+
throw new Error('Cannot access `instanceIdentifiers` of an imported cluster without provided instanceIdentifiers');
278+
}
279+
return this._instanceIdentifiers;
280+
}
281+
282+
public get clusterEndpoint(): Endpoint {
283+
if (!this._clusterEndpoint) {
284+
throw new Error('Cannot access `clusterEndpoint` of an imported cluster without an endpoint address and port');
285+
}
286+
return this._clusterEndpoint;
287+
}
288+
289+
public get clusterReadEndpoint(): Endpoint {
290+
if (!this._clusterReadEndpoint) {
291+
throw new Error('Cannot access `clusterReadEndpoint` of an imported cluster without a readerEndpointAddress and port');
292+
}
293+
return this._clusterReadEndpoint;
294+
}
295+
296+
public get instanceEndpoints(): Endpoint[] {
297+
if (!this._instanceEndpoints) {
298+
throw new Error('Cannot access `instanceEndpoints` of an imported cluster without instanceEndpointAddresses and port');
299+
}
300+
return this._instanceEndpoints;
301+
}
302+
303+
public get securityGroupId(): string {
304+
if (!this._securityGroupId) {
305+
throw new Error('Cannot access `securityGroupId` of an imported cluster without securityGroupId');
306+
}
307+
return this._securityGroupId;
308+
}
271309
}
272310

273311
return new Import(scope, id);

packages/@aws-cdk/aws-docdb/test/cluster.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,21 @@ describe('DatabaseCluster', () => {
518518
});
519519
});
520520

521+
test('minimal imported cluster throws on accessing attributes for unprovided parameters', () => {
522+
const stack = testStack();
523+
524+
const cluster = DatabaseCluster.fromDatabaseClusterAttributes(stack, 'Database', {
525+
clusterIdentifier: 'identifier',
526+
});
527+
528+
expect(cluster.clusterIdentifier).toEqual('identifier');
529+
expect(() => cluster.clusterEndpoint).toThrow(/Cannot access `clusterEndpoint` of an imported cluster/);
530+
expect(() => cluster.clusterReadEndpoint).toThrow(/Cannot access `clusterReadEndpoint` of an imported cluster/);
531+
expect(() => cluster.instanceIdentifiers).toThrow(/Cannot access `instanceIdentifiers` of an imported cluster/);
532+
expect(() => cluster.instanceEndpoints).toThrow(/Cannot access `instanceEndpoints` of an imported cluster/);
533+
expect(() => cluster.securityGroupId).toThrow(/Cannot access `securityGroupId` of an imported cluster/);
534+
});
535+
521536
test('backup retention period respected', () => {
522537
// GIVEN
523538
const stack = testStack();

0 commit comments

Comments
 (0)