Skip to content

Commit c412da9

Browse files
committed
Enable creation of custom parameter group
1 parent c8701a4 commit c412da9

File tree

6 files changed

+111
-13
lines changed

6 files changed

+111
-13
lines changed

src/v2/components/database/builder.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
21
import * as pulumi from '@pulumi/pulumi';
2+
import * as aws from '@pulumi/aws';
33
import * as awsx from '@pulumi/awsx';
44
import { Database } from '.';
55

66
export namespace DatabaseBuilder {
7-
export type Config = Omit<Database.Args, 'vpc' | 'enableMonitoring' | 'snapshotIdentifier'>;
7+
export type Config = Omit<
8+
Database.Args,
9+
'vpc' | 'enableMonitoring' | 'snapshotIdentifier'
10+
>;
811
}
912

1013
export class DatabaseBuilder {
@@ -13,7 +16,8 @@ export class DatabaseBuilder {
1316
private _vpc?: Database.Args['vpc'];
1417
private _enableMonitoring?: Database.Args['enableMonitoring'];
1518
private _snapshotIdentifier?: Database.Args['snapshotIdentifier'];
16-
19+
private _parameterGroupArgs?: Database.Args['parameterGroupArgs'];
20+
1721
constructor(name: string) {
1822
this._name = name;
1923
}
@@ -34,19 +38,27 @@ export class DatabaseBuilder {
3438

3539
public withVpc(vpc: pulumi.Input<awsx.ec2.Vpc>): this {
3640
this._vpc = pulumi.output(vpc);
37-
41+
3842
return this;
3943
}
4044

4145
public withMonitoring(): this {
4246
this._enableMonitoring = true;
43-
47+
4448
return this;
4549
}
4650

4751
public withSnapshot(snapshotIdentifier: pulumi.Input<string>): this {
4852
this._snapshotIdentifier = snapshotIdentifier;
49-
53+
54+
return this;
55+
}
56+
57+
public withParameterGroup(
58+
parameterGroupArgs: pulumi.Input<aws.rds.ParameterGroupArgs>,
59+
): this {
60+
this._parameterGroupArgs = parameterGroupArgs;
61+
5062
return this;
5163
}
5264

@@ -63,17 +75,16 @@ export class DatabaseBuilder {
6375
);
6476
}
6577

66-
6778
return new Database(
6879
this._name,
6980
{
7081
...this._config,
7182
vpc: this._vpc,
7283
enableMonitoring: this._enableMonitoring,
7384
snapshotIdentifier: this._snapshotIdentifier,
85+
parameterGroupArgs: this._parameterGroupArgs,
7486
},
7587
opts,
7688
);
7789
}
78-
7990
}

src/v2/components/database/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ export namespace Database {
6262
*/
6363
autoMinorVersionUpgrade?: pulumi.Input<boolean>;
6464
/**
65-
* The name of custom aws.rds.ParameterGroup. Setting this param will apply custom
65+
* The name of existing custom aws.rds.ParameterGroup. Setting this param will apply custom
6666
* DB parameters to this instance.
6767
*/
6868
parameterGroupName?: pulumi.Input<string>;
69+
/**
70+
* Arguments for creation of new custom aws.rds.ParameterGroup.
71+
*/
72+
parameterGroupArgs?: pulumi.Input<aws.rds.ParameterGroupArgs>;
6973
/**
7074
* Set this to `{ enabled: true }` to enable low-downtime updates using RDS Blue/Green deployments.
7175
* Defaults to `{ enabled: false }`.
@@ -115,6 +119,7 @@ export class Database extends pulumi.ComponentResource {
115119
password: Password;
116120
encryptedSnapshotCopy?: aws.rds.SnapshotCopy;
117121
monitoringRole?: aws.iam.Role;
122+
parameterGroup?: pulumi.Output<aws.rds.ParameterGroup>;
118123

119124
constructor(
120125
name: string,
@@ -129,6 +134,7 @@ export class Database extends pulumi.ComponentResource {
129134
const {
130135
enableMonitoring,
131136
snapshotIdentifier,
137+
parameterGroupArgs,
132138
} = argsWithDefaults;
133139

134140
const vpc = pulumi.output(argsWithDefaults.vpc);
@@ -148,6 +154,9 @@ export class Database extends pulumi.ComponentResource {
148154
this.encryptedSnapshotCopy =
149155
this.createEncryptedSnapshotCopy(snapshotIdentifier);
150156
}
157+
if (parameterGroupArgs) {
158+
this.parameterGroup = this.createParameterGroup(parameterGroupArgs);
159+
}
151160
this.instance = this.createDatabaseInstance(args);
152161

153162
this.registerOutputs();
@@ -248,6 +257,18 @@ export class Database extends pulumi.ComponentResource {
248257
return encryptedSnapshotCopy;
249258
}
250259

260+
private createParameterGroup(
261+
parameterGroupArgs: pulumi.Input<aws.rds.ParameterGroupArgs>
262+
) {
263+
return pulumi.output(parameterGroupArgs).apply(args => {
264+
return new aws.rds.ParameterGroup(
265+
`${this.name}-parameter-group`,
266+
args,
267+
{ parent: this }
268+
);
269+
})
270+
}
271+
251272
private createDatabaseInstance(args: Database.Args) {
252273
const argsWithDefaults = Object.assign({}, defaults, args);
253274
const stack = pulumi.getStack();
@@ -262,6 +283,10 @@ export class Database extends pulumi.ComponentResource {
262283
}
263284
: {};
264285

286+
const parameterGroupName = this.parameterGroup
287+
? this.parameterGroup.name
288+
: argsWithDefaults.parameterGroupName;
289+
265290
const instance = new aws.rds.Instance(
266291
`${this.name}-rds`,
267292
{
@@ -288,7 +313,7 @@ export class Database extends pulumi.ComponentResource {
288313
backupWindow: '06:00-06:30',
289314
backupRetentionPeriod: 14,
290315
caCertIdentifier: 'rds-ca-rsa2048-g1',
291-
parameterGroupName: argsWithDefaults.parameterGroupName,
316+
parameterGroupName,
292317
allowMajorVersionUpgrade: argsWithDefaults.allowMajorVersionUpgrade,
293318
blueGreenUpdate: argsWithDefaults.blueGreenUpdate,
294319
snapshotIdentifier:

tests/build/index.tst.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ describe('Build output', () => {
314314
it('should have withSnapshot method', () => {
315315
expect(builder.withSnapshot).type.toBeCallableWith('snapshot-id');
316316
});
317+
318+
it('should have withParameterGroup method', () => {
319+
expect(builder.withParameterGroup).type.toBeCallableWith({
320+
family: 'custom-family'
321+
});
322+
});
317323
});
318324
});
319325
});

tests/database/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { DatabaseTestContext } from './test-context';
1717
import { IAMClient } from '@aws-sdk/client-iam';
1818
import { InlineProgramArgs } from '@pulumi/pulumi/automation';
1919
import { testDbWithMonitoring } from './monitoring.test';
20+
import { testDbWithParameterGroup } from './parameter-group.test';
2021

2122
const programArgs: InlineProgramArgs = {
2223
stackName: 'dev',
@@ -181,4 +182,5 @@ describe('Database component deployment', () => {
181182
});
182183

183184
describe('With monitoring', () => testDbWithMonitoring(ctx));
185+
describe('With parameter group', () => testDbWithParameterGroup(ctx));
184186
});

tests/database/infrastructure/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const database = new studion.DatabaseBuilder(config.instanceName)
1313
.build();
1414

1515
const dbWithMonitoring = new studion.DatabaseBuilder(
16-
`${config.instanceName}-w-monitoring`,
17-
)
16+
`${config.instanceName}-w-monitoring`,
17+
)
1818
.configure(config.dbName, config.username, {
1919
password: config.password,
2020
applyImmediately: config.applyImmediately,
@@ -24,4 +24,18 @@ const dbWithMonitoring = new studion.DatabaseBuilder(
2424
.withMonitoring()
2525
.build();
2626

27-
export { vpc, database, dbWithMonitoring };
27+
const dbWithParameterGroup = new studion.DatabaseBuilder(
28+
`${config.instanceName}-w-param-group`,
29+
)
30+
.configure(config.dbName, config.username, {
31+
password: config.password,
32+
applyImmediately: config.applyImmediately,
33+
skipFinalSnapshot: config.skipFinalSnapshot,
34+
})
35+
.withVpc(vpc.vpc)
36+
.withParameterGroup({
37+
family: 'postgres17'
38+
})
39+
.build();
40+
41+
export { vpc, database, dbWithMonitoring, dbWithParameterGroup };
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as assert from 'node:assert';
2+
import { DatabaseTestContext } from './test-context';
3+
import { DescribeDBParameterGroupsCommand } from '@aws-sdk/client-rds';
4+
import { it } from 'node:test';
5+
6+
export function testDbWithParameterGroup(ctx: DatabaseTestContext) {
7+
it('should properly configure parameter group', () => {
8+
const dbWithParameterGroup = ctx.outputs.dbWithParameterGroup.value;
9+
10+
assert.ok(
11+
dbWithParameterGroup.parameterGroup,
12+
'Parameter group should exist',
13+
);
14+
assert.strictEqual(
15+
dbWithParameterGroup.instance.parameterGroupName,
16+
dbWithParameterGroup.parameterGroup.name,
17+
'Parameter group name should be set correctly',
18+
);
19+
});
20+
21+
it('should create parameter group with correct family', async () => {
22+
const dbWithParameterGroup = ctx.outputs.dbWithParameterGroup.value;
23+
const parameterGroupName = dbWithParameterGroup.parameterGroup.name;
24+
25+
const command = new DescribeDBParameterGroupsCommand({
26+
DBParameterGroupName: parameterGroupName,
27+
});
28+
const { DBParameterGroups } = await ctx.clients.rds.send(command);
29+
assert.ok(
30+
DBParameterGroups && DBParameterGroups.length > 0,
31+
'DB parameter group should exist',
32+
);
33+
const [parameterGroup] = DBParameterGroups;
34+
assert.strictEqual(
35+
parameterGroup.DBParameterGroupFamily,
36+
'postgres17',
37+
'DB Parameter group family should be set correctly',
38+
);
39+
});
40+
}

0 commit comments

Comments
 (0)