From f92b65e2a158f918d8f05132ed12a4bb85228997 Mon Sep 17 00:00:00 2001 From: Simon-TechForm <73996878+Simon-TechForm@users.noreply.github.com> Date: Fri, 19 Feb 2021 21:43:38 +0000 Subject: [PATCH] feat(aws-appsync): add databaseName to rdsDataSource (#12575) Fixes: #12572 BREAKING CHANGE: graphqlapi.addRdsDataSource now takes databaseName as its fourth argument ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-appsync/README.md | 2 +- .../@aws-cdk/aws-appsync/lib/data-source.ts | 7 ++++ .../aws-appsync/lib/graphqlapi-base.ts | 5 +++ .../aws-appsync/test/appsync-rds.test.ts | 32 +++++++++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-appsync/README.md b/packages/@aws-cdk/aws-appsync/README.md index 9c5777f022a22..1d217960b79ed 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({ 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', });