Skip to content
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

feat(aws-appsync): add databaseName to rdsDataSource #12575

Merged
merged 4 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-appsync/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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