From 16e72c2bb187a71922eb2c7af90d81a25b161070 Mon Sep 17 00:00:00 2001 From: Simon-TechForm <73996878+Simon-TechForm@users.noreply.github.com> Date: Mon, 18 Jan 2021 15:02:09 +0100 Subject: [PATCH 1/2] feat(aws-appsync): add databaseName to rdsDataSource Fixes: #12572 BREAKING CHANGE: graphqlapi.addRdsDataSource now takes databaseName as its fourth argument --- .../@aws-cdk/aws-appsync/lib/data-source.ts | 7 ++++ .../aws-appsync/lib/graphqlapi-base.ts | 5 +++ .../aws-appsync/test/appsync-rds.test.ts | 32 +++++++++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/lib/data-source.ts b/packages/@aws-cdk/aws-appsync/lib/data-source.ts index 21646e8573193..00895965a23a3 100644 --- a/packages/@aws-cdk/aws-appsync/lib/data-source.ts +++ b/packages/@aws-cdk/aws-appsync/lib/data-source.ts @@ -306,6 +306,12 @@ export interface RdsDataSourceProps extends BackedDataSourceProps { * The secret containing the credentials for the database */ readonly secretStore: ISecret; + /** + * The name of the database to use within the cluster + * + * @default - None + */ + readonly databaseName?: string; } /** @@ -327,6 +333,7 @@ export class RdsDataSource extends BackedDataSource { }, }), awsSecretStoreArn: props.secretStore.secretArn, + databaseName: props.databaseName, }, relationalDatabaseSourceType: 'RDS_HTTP_ENDPOINT', }, diff --git a/packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts b/packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts index 3b037101e97ee..4e28e337fd1c5 100644 --- a/packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts +++ b/packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts @@ -99,12 +99,14 @@ export interface IGraphqlApi extends IResource { * @param id The data source's id * @param databaseCluster The database cluster to interact with this data source * @param secretStore The secret store that contains the username and password for the database cluster + * @param databaseName The optional name of the database to use within the cluster * @param options The optional configuration for this data source */ addRdsDataSource( id: string, databaseCluster: IDatabaseCluster, secretStore: ISecret, + databaseName?: string, options?: DataSourceOptions ): RdsDataSource; @@ -206,12 +208,14 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi { * @param id The data source's id * @param databaseCluster The database cluster to interact with this data source * @param secretStore The secret store that contains the username and password for the database cluster + * @param databaseName The optional name of the database to use within the cluster * @param options The optional configuration for this data source */ public addRdsDataSource( id: string, databaseCluster: IDatabaseCluster, secretStore: ISecret, + databaseName?: string, options?: DataSourceOptions, ): RdsDataSource { return new RdsDataSource(this, id, { @@ -220,6 +224,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi { description: options?.description, databaseCluster, secretStore, + databaseName, }); } diff --git a/packages/@aws-cdk/aws-appsync/test/appsync-rds.test.ts b/packages/@aws-cdk/aws-appsync/test/appsync-rds.test.ts index 97cea819de8f3..12c2f5d91cf8c 100644 --- a/packages/@aws-cdk/aws-appsync/test/appsync-rds.test.ts +++ b/packages/@aws-cdk/aws-appsync/test/appsync-rds.test.ts @@ -123,6 +123,34 @@ describe('Rds Data Source configuration', () => { }); }); + test('databaseName saved to RdsHttpEndpointConfig', () => { + // WHEN + const testDatabaseName = 'testDatabaseName'; + api.addRdsDataSource('ds', cluster, secret, testDatabaseName); + + // THEN + expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', { + Type: 'RELATIONAL_DATABASE', + RelationalDatabaseConfig: { + RdsHttpEndpointConfig: { + AwsRegion: { Ref: 'AWS::Region' }, + AwsSecretStoreArn: { Ref: 'AuroraSecret41E6E877' }, + DbClusterIdentifier: { + 'Fn::Join': ['', ['arn:', + { Ref: 'AWS::Partition' }, + ':rds:', + { Ref: 'AWS::Region' }, + ':', + { Ref: 'AWS::AccountId' }, + ':cluster:', + { Ref: 'AuroraCluster23D869C0' }]], + }, + DatabaseName: testDatabaseName, + }, + }, + }); + }); + test('default configuration produces name identical to the id', () => { // WHEN api.addRdsDataSource('ds', cluster, secret); @@ -136,7 +164,7 @@ describe('Rds Data Source configuration', () => { test('appsync configures name correctly', () => { // WHEN - api.addRdsDataSource('ds', cluster, secret, { + api.addRdsDataSource('ds', cluster, secret, undefined, { name: 'custom', }); @@ -149,7 +177,7 @@ describe('Rds Data Source configuration', () => { test('appsync configures name and description correctly', () => { // WHEN - api.addRdsDataSource('ds', cluster, secret, { + api.addRdsDataSource('ds', cluster, secret, undefined, { name: 'custom', description: 'custom description', }); From c1e79d3541bb1c4f0a19468ac4ce746c0ea99d1e Mon Sep 17 00:00:00 2001 From: Simon-TechForm <73996878+Simon-TechForm@users.noreply.github.com> Date: Mon, 18 Jan 2021 15:14:55 +0100 Subject: [PATCH 2/2] feat(aws-appsync): update readme References: #12572 --- packages/@aws-cdk/aws-appsync/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-appsync/README.md b/packages/@aws-cdk/aws-appsync/README.md index 61c4e797f5bb4..5d2ca26d7149a 100644 --- a/packages/@aws-cdk/aws-appsync/README.md +++ b/packages/@aws-cdk/aws-appsync/README.md @@ -116,7 +116,7 @@ const cluster = new rds.DatabaseCluster(stack, 'AuroraCluster', { }); // Build a data source for AppSync to access the database. -const rdsDS = api.addRdsDataSource('rds', 'The rds data source', cluster, secret); +const rdsDS = api.addRdsDataSource('rds', cluster, secret, 'demos'); // Set up a resolver for an RDS query. rdsDS.createResolver({