Skip to content

Commit 435e0a8

Browse files
author
Tolga Takır
authored
Change resource naming convention (#5)
1 parent d280831 commit 435e0a8

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ import * as thundra from '@thundra/cdk-rds-initializer';
132132
// Database Initializer
133133
//
134134
const userInitializer = new thundra.DatabaseUserInitializer(this, 'sample-database-user-initializer', {
135-
prefix: 'Sample',
136135
databaseAdminUserSecret: sampleDatabaseAdminUserSecret,
137136
databaseEngine: thundra.DatabaseEngine.MySQL,
138137
databaseUsers: [
@@ -151,7 +150,6 @@ import * as thundra from '@thundra/cdk-rds-initializer';
151150
userInitializer.node.addDependency(sampleDatabaseCluster);
152151

153152
const scriptRunner = new thundra.DatabaseScriptRunner(this, 'sample-database-script-runner', {
154-
prefix: 'Sample',
155153
databaseAdminUserSecret: sampleDatabaseAdminUserSecret,
156154
databaseEngine: thundra.DatabaseEngine.MySQL,
157155
script: 'select 1 from dual;'
@@ -180,7 +178,11 @@ export interface DatabaseInitializerProps {
180178
/**
181179
* Prefix to be used to name custom resources.
182180
*/
183-
readonly prefix: string;
181+
readonly prefix?: string;
182+
/**
183+
* Postfix to be used to name custom resources.
184+
*/
185+
readonly postfix?: string;
184186
/**
185187
* Secret to be used for database connection.
186188
* Secret must contain database information.
@@ -235,7 +237,11 @@ export interface DatabaseInitializerProps {
235237
/**
236238
* Prefix to be used to name custom resources.
237239
*/
238-
readonly prefix: string;
240+
readonly prefix?: string;
241+
/**
242+
* Postfix to be used to name custom resources.
243+
*/
244+
readonly postfix?: string;
239245
/**
240246
* Secret to be used for database connection.
241247
* Secret must contain database information.

src/database-initializer-props.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export interface DatabaseInitializerProps {
88
/**
99
* Prefix to be used to name custom resources.
1010
*/
11-
readonly prefix: string;
11+
readonly prefix?: string;
12+
/**
13+
* Postfix to be used to name custom resources.
14+
*/
15+
readonly postfix?: string;
1216
/**
1317
* Secret to be used for database connection.
1418
* Secret must contain database information.

src/database-script-runner.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,32 @@ export interface DatabaseScriptRunnerProps extends DatabaseInitializerProps {
2222
*
2323
* @example
2424
*
25-
* new DatabaseScriptRunner(this, 'SampleDatabaseScriptRunner', {
26-
* prefix: 'Sample',
25+
* new DatabaseScriptRunner(this, 'sample-database-script-runner', {
2726
* databaseAdminUserSecret: secret,
2827
* databaseEngine: DatabaseEngine.MySQL,
2928
* script: 'select 1 from dual;'
3029
* });
3130
*/
3231
export class DatabaseScriptRunner extends cdk.Resource {
33-
private readonly prefix: string;
32+
private readonly prefix?: string;
33+
private readonly postfix?: string;
3434
private readonly databaseAdminUserSecret: secretsmanager.ISecret;
3535
private readonly script: string;
3636
private readonly databaseEngine: DatabaseEngine;
3737

3838
public constructor(scope: cdk.Construct, id: string, props: DatabaseScriptRunnerProps) {
3939
super(scope, id);
40-
this.prefix = props.prefix;
40+
this.prefix = props.prefix || '';
41+
this.postfix = props.postfix || '';
4142
this.databaseAdminUserSecret = props.databaseAdminUserSecret;
4243
this.script = props.script;
4344
this.databaseEngine = props.databaseEngine;
4445

45-
const databaseScriptRunnerFunction = new lambda.Function(this, `${this.prefix}DatabaseScriptRunnerFunction`, {
46+
const databaseScriptRunnerFunction = new lambda.Function(this, `${this.prefix}database-script-runner-function${this.postfix}`, {
4647
vpc: props.vpc,
4748
vpcSubnets: props.vpcSubnets,
4849
securityGroups: props.securityGroups,
49-
functionName: `${this.prefix}DatabaseScriptRunner`,
50+
functionName: `${this.prefix}database-script-runner${this.postfix}`,
5051
code: lambda.Code.fromAsset(path.join(__dirname, "lambda")),
5152
handler: "index.databaseScriptRunnerHandler",
5253
runtime: lambda.Runtime.NODEJS_14_X,
@@ -67,11 +68,12 @@ export class DatabaseScriptRunner extends cdk.Resource {
6768
]
6869
});
6970

70-
const databaseScriptRunnerProvider = new cr.Provider(this, `${this.prefix}DatabaseScriptRunnerProvider`, {
71-
onEventHandler: databaseScriptRunnerFunction
71+
const databaseScriptRunnerProvider = new cr.Provider(this, `${this.prefix}database-script-runner-provider${this.postfix}`, {
72+
onEventHandler: databaseScriptRunnerFunction,
73+
providerFunctionName: `${this.prefix}database-script-runner-provider${this.postfix}`
7274
})
7375

74-
new cdk.CustomResource(this, `${this.prefix}DatabaseScriptRunnerResource`, {
76+
new cdk.CustomResource(this, `${this.prefix}database-script-runner-resource${this.postfix}`, {
7577
serviceToken: databaseScriptRunnerProvider.serviceToken,
7678
properties: {
7779
Region: cdk.Stack.of(this).region,
@@ -85,10 +87,6 @@ export class DatabaseScriptRunner extends cdk.Resource {
8587
protected validate(): string[] {
8688
const errors: string[] = [];
8789

88-
if (this.prefix == null || this.prefix.trim().length === 0) {
89-
errors.push('Prefix properties must not be empty.');
90-
}
91-
9290
if (this.script == null || this.script.trim().length === 0) {
9391
errors.push('Script properties must not be empty.');
9492
}

src/database-user-initializer.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ export interface DatabaseUserInitializerProps extends DatabaseInitializerProps {
3737
*
3838
* @example
3939
*
40-
* new DatabaseUserInitializer(this, 'SampleDatabaseUserInitializer', {
41-
* prefix: 'Sample',
40+
* new DatabaseUserInitializer(this, 'sample-database-user-initializer', {
4241
* databaseAdminUserSecret: secret,
4342
* databaseEngine: DatabaseEngine.MySQL,
4443
* databaseUsers: [
@@ -55,7 +54,8 @@ export interface DatabaseUserInitializerProps extends DatabaseInitializerProps {
5554
* });
5655
*/
5756
export class DatabaseUserInitializer extends cdk.Resource {
58-
private readonly prefix: string;
57+
private readonly prefix?: string;
58+
private readonly postfix?: string;
5959
private readonly databaseAdminUserSecret: secretsmanager.ISecret;
6060
private readonly databaseEngine: DatabaseEngine;
6161
private readonly databaseUsers: {
@@ -66,7 +66,8 @@ export class DatabaseUserInitializer extends cdk.Resource {
6666

6767
public constructor(scope: cdk.Construct, id: string, props: DatabaseUserInitializerProps) {
6868
super(scope, id);
69-
this.prefix = props.prefix
69+
this.prefix = props.prefix || '';
70+
this.postfix = props.postfix || '';
7071
this.databaseAdminUserSecret = props.databaseAdminUserSecret;
7172
this.databaseEngine = props.databaseEngine;
7273
this.databaseUsers = props.databaseUsers;
@@ -78,11 +79,11 @@ export class DatabaseUserInitializer extends cdk.Resource {
7879
}
7980
});
8081

81-
const databaseUserInitializerFunction = new lambda.Function(this, `${this.prefix}DatabaseUserInitializerFunction`, {
82+
const databaseUserInitializerFunction = new lambda.Function(this, `${this.prefix}database-user-initializer-function${this.postfix}`, {
8283
vpc: props.vpc,
8384
vpcSubnets: props.vpcSubnets,
8485
securityGroups: props.securityGroups,
85-
functionName: `${this.prefix}DatabaseUserInitializer`,
86+
functionName: `${this.prefix}database-user-initializer${this.postfix}`,
8687
code: lambda.Code.fromAsset(path.join(__dirname, "lambda")),
8788
handler: "index.databaseUserInitializerHandler",
8889
runtime: lambda.Runtime.NODEJS_14_X,
@@ -99,49 +100,47 @@ export class DatabaseUserInitializer extends cdk.Resource {
99100
})]
100101
});
101102

102-
const databaseUserInitializerProvider = new cr.Provider(this, `${this.prefix}DatabaseUserInitializerProvider`, {
103-
onEventHandler: databaseUserInitializerFunction
103+
const databaseUserInitializerProvider = new cr.Provider(this, `${this.prefix}database-user-initializer-provider${this.postfix}`, {
104+
onEventHandler: databaseUserInitializerFunction,
105+
providerFunctionName: `${this.prefix}database-user-initializer-provider${this.postfix}`,
104106
})
105107

106-
const paramater: {
108+
const parameter: {
107109
username: string;
108110
grants: DatabaseUserGrant[];
109111
secretName?: string;
110112
isIAMUser: boolean;
111113
}[] = []
112114
this.databaseUsers.forEach(user => {
113115
if (user.secret) {
114-
paramater.push({
116+
parameter.push({
115117
username: user.username,
116118
grants: user.grants,
117119
secretName: user.secret!.secretName,
118120
isIAMUser: false
119121
})
120122
} else {
121-
paramater.push({
123+
parameter.push({
122124
username: user.username,
123125
grants: user.grants,
124126
isIAMUser: true
125127
});
126128
}
127129
});
128130

129-
new cdk.CustomResource(this, `${this.prefix}DatabaseUserInitializerResource`, {
131+
new cdk.CustomResource(this, `${this.prefix}database-user-initializer-resource${this.postfix}`, {
130132
serviceToken: databaseUserInitializerProvider.serviceToken,
131133
properties: {
132134
Region: cdk.Stack.of(this).region,
133135
DatabaseAdminUserSecretName: this.databaseAdminUserSecret.secretName,
134-
DatabaseUsers: paramater,
136+
DatabaseUsers: parameter,
135137
DatabaseEngine: this.databaseEngine
136138
},
137139
});
138140
}
139141

140142
protected validate(): string[] {
141143
const errors: string[] = [];
142-
if (this.prefix == null || this.prefix.trim().length === 0) {
143-
errors.push('Prefix properties must not be empty.');
144-
}
145144

146145
if (this.databaseUsers == null || this.databaseUsers.length === 0) {
147146
errors.push('Database users properties must not be empty.');

0 commit comments

Comments
 (0)