-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
appsync-rds.test.ts
260 lines (239 loc) · 7.78 KB
/
appsync-rds.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import '@aws-cdk/assert/jest';
import * as path from 'path';
import { Vpc, SecurityGroup, SubnetType, InstanceType, InstanceClass, InstanceSize } from '@aws-cdk/aws-ec2';
import { DatabaseSecret, DatabaseCluster, DatabaseClusterEngine, AuroraMysqlEngineVersion } from '@aws-cdk/aws-rds';
import * as cdk from '@aws-cdk/core';
import * as appsync from '../lib';
// GLOBAL GIVEN
let stack: cdk.Stack;
let api: appsync.GraphqlApi;
beforeEach(() => {
stack = new cdk.Stack();
api = new appsync.GraphqlApi(stack, 'baseApi', {
name: 'api',
schema: new appsync.Schema({
filePath: path.join(__dirname, 'appsync.test.graphql'),
}),
});
});
describe('Rds Data Source configuration', () => {
// GIVEN
let secret: DatabaseSecret;
let cluster: DatabaseCluster;
beforeEach(() => {
const vpc = new Vpc(stack, 'Vpc', { maxAzs: 2 });
const securityGroup = new SecurityGroup(stack, 'AuroraSecurityGroup', {
vpc,
allowAllOutbound: true,
});
secret = new DatabaseSecret(stack, 'AuroraSecret', {
username: 'clusteradmin',
});
cluster = new DatabaseCluster(stack, 'AuroraCluster', {
engine: DatabaseClusterEngine.auroraMysql({ version: AuroraMysqlEngineVersion.VER_2_07_1 }),
credentials: { username: 'clusteradmin' },
clusterIdentifier: 'db-endpoint-test',
instanceProps: {
instanceType: InstanceType.of(InstanceClass.BURSTABLE2, InstanceSize.SMALL),
vpcSubnets: { subnetType: SubnetType.PRIVATE },
vpc,
securityGroups: [securityGroup],
},
defaultDatabaseName: 'Animals',
});
});
test('appsync creates correct policy', () => {
// WHEN
api.addRdsDataSource('ds', cluster, secret);
// THEN
expect(stack).toHaveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Version: '2012-10-17',
Statement: [{
Action: [
'secretsmanager:GetSecretValue',
'secretsmanager:DescribeSecret',
],
Effect: 'Allow',
Resource: { Ref: 'AuroraSecret41E6E877' },
},
{
Action: [
'rds-data:DeleteItems',
'rds-data:ExecuteSql',
'rds-data:ExecuteStatement',
'rds-data:GetItems',
'rds-data:InsertItems',
'rds-data:UpdateItems',
],
Effect: 'Allow',
Resource: [{
'Fn::Join': ['', ['arn:',
{ Ref: 'AWS::Partition' },
':rds:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':cluster:',
{ Ref: 'AuroraCluster23D869C0' }]],
},
{
'Fn::Join': ['', ['arn:',
{ Ref: 'AWS::Partition' },
':rds:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':cluster:',
{ Ref: 'AuroraCluster23D869C0' },
':*']],
}],
}],
},
});
});
test('rds cluster arn saved to RdsHttpEndpointConfig', () => {
// WHEN
api.addRdsDataSource('ds', cluster, secret);
// 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' }]],
},
},
},
});
});
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);
// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'RELATIONAL_DATABASE',
Name: 'ds',
});
});
test('appsync configures name correctly', () => {
// WHEN
api.addRdsDataSource('ds', cluster, secret, undefined, {
name: 'custom',
});
// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'RELATIONAL_DATABASE',
Name: 'custom',
});
});
test('appsync configures name and description correctly', () => {
// WHEN
api.addRdsDataSource('ds', cluster, secret, undefined, {
name: 'custom',
description: 'custom description',
});
// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'RELATIONAL_DATABASE',
Name: 'custom',
Description: 'custom description',
});
});
test('appsync errors when creating multiple rds data sources with no configuration', () => {
// WHEN
const when = () => {
api.addRdsDataSource('ds', cluster, secret);
api.addRdsDataSource('ds', cluster, secret);
};
// THEN
expect(when).toThrow('There is already a Construct with name \'ds\' in GraphqlApi [baseApi]');
});
});
describe('adding rds data source from imported api', () => {
// GIVEN
let secret: DatabaseSecret;
let cluster: DatabaseCluster;
beforeEach(() => {
const vpc = new Vpc(stack, 'Vpc', { maxAzs: 2 });
const securityGroup = new SecurityGroup(stack, 'AuroraSecurityGroup', {
vpc,
allowAllOutbound: true,
});
secret = new DatabaseSecret(stack, 'AuroraSecret', {
username: 'clusteradmin',
});
cluster = new DatabaseCluster(stack, 'AuroraCluster', {
engine: DatabaseClusterEngine.auroraMysql({ version: AuroraMysqlEngineVersion.VER_2_07_1 }),
credentials: { username: 'clusteradmin' },
clusterIdentifier: 'db-endpoint-test',
instanceProps: {
instanceType: InstanceType.of(InstanceClass.BURSTABLE2, InstanceSize.SMALL),
vpcSubnets: { subnetType: SubnetType.PRIVATE },
vpc,
securityGroups: [securityGroup],
},
defaultDatabaseName: 'Animals',
});
});
test('imported api can add RdsDbDataSource from id', () => {
// WHEN
const importedApi = appsync.GraphqlApi.fromGraphqlApiAttributes(stack, 'importedApi', {
graphqlApiId: api.apiId,
});
importedApi.addRdsDataSource('ds', cluster, secret);
// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'RELATIONAL_DATABASE',
ApiId: { 'Fn::GetAtt': ['baseApiCDA4D43A', 'ApiId'] },
});
});
test('imported api can add RdsDataSource from attributes', () => {
// WHEN
const importedApi = appsync.GraphqlApi.fromGraphqlApiAttributes(stack, 'importedApi', {
graphqlApiId: api.apiId,
graphqlApiArn: api.arn,
});
importedApi.addRdsDataSource('ds', cluster, secret);
// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'RELATIONAL_DATABASE',
ApiId: { 'Fn::GetAtt': ['baseApiCDA4D43A', 'ApiId'] },
});
});
});