Skip to content

Commit

Permalink
feat(aws-appsync): add databaseName to rdsDataSource (#12575)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
monholm committed Feb 19, 2021
1 parent 96728aa commit f92b65e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-appsync/README.md
Expand Up @@ -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({
Expand Down
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-appsync/lib/data-source.ts
Expand Up @@ -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;
}

/**
Expand All @@ -327,6 +333,7 @@ export class RdsDataSource extends BackedDataSource {
},
}),
awsSecretStoreArn: props.secretStore.secretArn,
databaseName: props.databaseName,
},
relationalDatabaseSourceType: 'RDS_HTTP_ENDPOINT',
},
Expand Down
5 changes: 5 additions & 0 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts
Expand Up @@ -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;

Expand Down Expand Up @@ -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, {
Expand All @@ -220,6 +224,7 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
description: options?.description,
databaseCluster,
secretStore,
databaseName,
});
}

Expand Down
32 changes: 30 additions & 2 deletions packages/@aws-cdk/aws-appsync/test/appsync-rds.test.ts
Expand Up @@ -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);
Expand All @@ -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',
});

Expand All @@ -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',
});
Expand Down

0 comments on commit f92b65e

Please sign in to comment.