forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption-group.ts
162 lines (139 loc) · 4.12 KB
/
option-group.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
import * as ec2 from '@aws-cdk/aws-ec2';
import { Construct, IResource, Resource } from '@aws-cdk/core';
import { DatabaseInstanceEngine } from './instance';
import { CfnOptionGroup } from './rds.generated';
/**
* An option group
*/
export interface IOptionGroup extends IResource {
/**
* The name of the option group.
*
* @attribute
*/
readonly optionGroupName: string;
}
/**
* Configuration properties for an option.
*/
export interface OptionConfiguration {
/**
* The name of the option.
*/
readonly name: string;
/**
* The settings for the option.
*
* @default - no settings
*/
readonly settings?: { [name: string]: string };
/**
* The version for the option.
*
* @default - no version
*/
readonly version?: string;
/**
* The port number that this option uses. If `port` is specified then `vpc`
* must also be specified.
*
* @default - no port
*/
readonly port?: number;
/**
* The VPC where a security group should be created for this option. If `vpc`
* is specified then `port` must also be specified.
*
* @default - no VPC
*/
readonly vpc?: ec2.IVpc;
}
/**
* Construction properties for an OptionGroup.
*/
export interface OptionGroupProps {
/**
* The database engine that this option group is associated with.
*/
readonly engine: DatabaseInstanceEngine;
/**
* The major version number of the database engine that this option group
* is associated with.
*/
readonly majorEngineVersion: string;
/**
* A description of the option group.
*
* @default a CDK generated description
*/
readonly description?: string;
/**
* The configurations for this option group.
*/
readonly configurations: OptionConfiguration[];
}
/**
* An option group
*/
export class OptionGroup extends Resource implements IOptionGroup {
/**
* Import an existing option group.
*/
public static fromOptionGroupName(scope: Construct, id: string, optionGroupName: string): IOptionGroup {
class Import extends Resource {
public readonly optionGroupName = optionGroupName;
}
return new Import(scope, id);
}
/**
* The name of the option group.
*/
public readonly optionGroupName: string;
/**
* The connections object for the options.
*/
public readonly optionConnections: { [key: string]: ec2.Connections } = {};
constructor(scope: Construct, id: string, props: OptionGroupProps) {
super(scope, id);
const optionGroup = new CfnOptionGroup(this, 'Resource', {
engineName: props.engine.name,
majorEngineVersion: props.majorEngineVersion,
optionGroupDescription: props.description || `Option group for ${props.engine.name} ${props.majorEngineVersion}`,
optionConfigurations: this.renderConfigurations(props.configurations)
});
this.optionGroupName = optionGroup.ref;
}
/**
* Renders the option configurations specifications.
*/
private renderConfigurations(configurations: OptionConfiguration[]): CfnOptionGroup.OptionConfigurationProperty[] {
const configs: CfnOptionGroup.OptionConfigurationProperty[] = [];
for (const config of configurations) {
let configuration: CfnOptionGroup.OptionConfigurationProperty = {
optionName: config.name,
optionSettings: config.settings && Object.entries(config.settings).map(([name, value]) => ({ name, value })),
optionVersion: config.version
};
if (config.port) {
if (!config.vpc) {
throw new Error('`port` and `vpc` must be specified together.');
}
const securityGroup = new ec2.SecurityGroup(this, `SecurityGroup${config.name}`, {
description: `Security group for ${config.name} option`,
vpc: config.vpc
});
this.optionConnections[config.name] = new ec2.Connections({
securityGroups: [securityGroup],
defaultPort: ec2.Port.tcp(config.port)
});
configuration = {
...configuration,
port: config.port,
vpcSecurityGroupMemberships: [securityGroup.securityGroupId]
};
}
configs.push(configuration);
}
return configs;
}
}