/
cfn-parameter.ts
379 lines (333 loc) · 10.2 KB
/
cfn-parameter.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { Construct } from 'constructs';
import { CfnElement } from './cfn-element';
import { CfnReference } from './private/cfn-reference';
import { IResolvable, IResolveContext } from './resolvable';
import { Token } from './token';
import { ResolutionTypeHint } from './type-hints';
export interface CfnParameterProps {
/**
* The data type for the parameter (DataType).
*
* @default String
*/
readonly type?: string;
/**
* A value of the appropriate type for the template to use if no value is specified
* when a stack is created. If you define constraints for the parameter, you must specify
* a value that adheres to those constraints.
*
* @default - No default value for parameter.
*/
readonly default?: any;
/**
* A regular expression that represents the patterns to allow for String types.
*
* @default - No constraints on patterns allowed for parameter.
*/
readonly allowedPattern?: string;
/**
* An array containing the list of values allowed for the parameter.
*
* @default - No constraints on values allowed for parameter.
*/
readonly allowedValues?: string[];
/**
* A string that explains a constraint when the constraint is violated.
* For example, without a constraint description, a parameter that has an allowed
* pattern of [A-Za-z0-9]+ displays the following error message when the user specifies
* an invalid value:
*
* @default - No description with customized error message when user specifies invalid values.
*/
readonly constraintDescription?: string;
/**
* A string of up to 4000 characters that describes the parameter.
*
* @default - No description for the parameter.
*/
readonly description?: string;
/**
* An integer value that determines the largest number of characters you want to allow for String types.
*
* @default - None.
*/
readonly maxLength?: number;
/**
* A numeric value that determines the largest numeric value you want to allow for Number types.
*
* @default - None.
*/
readonly maxValue?: number;
/**
* An integer value that determines the smallest number of characters you want to allow for String types.
*
* @default - None.
*/
readonly minLength?: number;
/**
* A numeric value that determines the smallest numeric value you want to allow for Number types.
*
* @default - None.
*/
readonly minValue?: number;
/**
* Whether to mask the parameter value when anyone makes a call that describes the stack.
* If you set the value to ``true``, the parameter value is masked with asterisks (``*****``).
*
* @default - Parameter values are not masked.
*/
readonly noEcho?: boolean;
}
/**
* A CloudFormation parameter.
*
* Use the optional Parameters section to customize your templates.
* Parameters enable you to input custom values to your template each time you create or
* update a stack.
*/
export class CfnParameter extends CfnElement {
private _type: string;
private _default?: any;
private _allowedPattern?: string;
private _allowedValues?: string[];
private _constraintDescription?: string;
private _description?: string;
private _maxLength?: number;
private _maxValue?: number;
private _minLength?: number;
private _minValue?: number;
private _noEcho?: boolean;
private typeHint: ResolutionTypeHint;
/**
* Creates a parameter construct.
* Note that the name (logical ID) of the parameter will derive from it's `coname` and location
* within the stack. Therefore, it is recommended that parameters are defined at the stack level.
*
* @param scope The parent construct.
* @param props The parameter properties.
*/
constructor(scope: Construct, id: string, props: CfnParameterProps = {}) {
super(scope, id);
this._type = props.type || 'String';
this._default = props.default;
this._allowedPattern = props.allowedPattern;
this._allowedValues = props.allowedValues;
this._constraintDescription = props.constraintDescription;
this._description = props.description;
this._maxLength = props.maxLength;
this._maxValue = props.maxValue;
this._minLength = props.minLength;
this._minValue = props.minValue;
this._noEcho = props.noEcho;
this.typeHint = typeToTypeHint(this._type);
}
/**
* The data type for the parameter (DataType).
*
* @default String
*/
public get type(): string {
return this._type;
}
public set type(type: string) {
this._type = type;
this.typeHint = typeToTypeHint(this._type);
}
/**
* A value of the appropriate type for the template to use if no value is specified
* when a stack is created. If you define constraints for the parameter, you must specify
* a value that adheres to those constraints.
*
* @default - No default value for parameter.
*/
public get default(): any {
return this._default;
}
public set default(value: any) {
this._default = value;
}
/**
* A regular expression that represents the patterns to allow for String types.
*
* @default - No constraints on patterns allowed for parameter.
*/
public get allowedPattern(): string | undefined {
return this._allowedPattern;
}
public set allowedPattern(pattern: string | undefined) {
this._allowedPattern = pattern;
}
/**
* An array containing the list of values allowed for the parameter.
*
* @default - No constraints on values allowed for parameter.
*/
public get allowedValues(): string[] | undefined {
return this._allowedValues;
}
public set allowedValues(values: string[] | undefined) {
this._allowedValues = values;
}
/**
* A string that explains a constraint when the constraint is violated.
* For example, without a constraint description, a parameter that has an allowed
* pattern of [A-Za-z0-9]+ displays the following error message when the user specifies
* an invalid value:
*
* @default - No description with customized error message when user specifies invalid values.
*/
public get constraintDescription(): string | undefined {
return this._constraintDescription;
}
public set constraintDescription(desc: string | undefined) {
this._constraintDescription = desc;
}
/**
* A string of up to 4000 characters that describes the parameter.
*
* @default - No description for the parameter.
*/
public get description(): string | undefined {
return this._description;
}
public set description(desc: string | undefined) {
this._description = desc;
}
/**
* An integer value that determines the largest number of characters you want to allow for String types.
*
* @default - None.
*/
public get maxLength(): number | undefined {
return this._maxLength;
}
public set maxLength(len: number | undefined) {
this._maxLength = len;
}
/**
* An integer value that determines the smallest number of characters you want to allow for String types.
*
* @default - None.
*/
public get minLength(): number | undefined {
return this._minLength;
}
public set minLength(len: number | undefined) {
this._minLength = len;
}
/**
* A numeric value that determines the largest numeric value you want to allow for Number types.
*
* @default - None.
*/
public get maxValue(): number | undefined {
return this._maxValue;
}
public set maxValue(len: number | undefined) {
this._maxValue = len;
}
/**
* A numeric value that determines the smallest numeric value you want to allow for Number types.
*
* @default - None.
*/
public get minValue(): number | undefined {
return this._minValue;
}
public set minValue(len: number | undefined) {
this._minValue = len;
}
/**
* Indicates if this parameter is configured with "NoEcho" enabled.
*/
public get noEcho(): boolean {
return !!this._noEcho;
}
public set noEcho(echo: boolean) {
this._noEcho = echo;
}
/**
* The parameter value as a Token
*/
public get value(): IResolvable {
return CfnReference.for(this, 'Ref', undefined, this.typeHint);
}
/**
* The parameter value, if it represents a string.
*/
public get valueAsString(): string {
if (!isStringType(this.type) && !isNumberType(this.type)) {
throw new Error(`Parameter type (${this.type}) is not a string or number type`);
}
return Token.asString(this.value);
}
/**
* The parameter value, if it represents a string list.
*/
public get valueAsList(): string[] {
if (!isListType(this.type)) {
throw new Error(`Parameter type (${this.type}) is not a string list type`);
}
return Token.asList(this.value);
}
/**
* The parameter value, if it represents a number.
*/
public get valueAsNumber(): number {
if (!isNumberType(this.type)) {
throw new Error(`Parameter type (${this.type}) is not a number type`);
}
return Token.asNumber(this.value);
}
/**
* @internal
*/
public _toCloudFormation(): object {
return {
Parameters: {
[this.logicalId]: {
Type: this.type,
Default: this.default,
AllowedPattern: this.allowedPattern,
AllowedValues: this.allowedValues,
ConstraintDescription: this.constraintDescription,
Description: this.description,
MaxLength: this.maxLength,
MaxValue: this.maxValue,
MinLength: this.minLength,
MinValue: this.minValue,
NoEcho: this._noEcho,
},
},
};
}
public resolve(_context: IResolveContext): any {
return this.value;
}
}
/**
* Whether the given parameter type looks like a list type
*/
function isListType(type: string) {
return type.indexOf('List<') >= 0 || type.indexOf('CommaDelimitedList') >= 0;
}
/**
* Whether the given parameter type looks like a number type
*/
function isNumberType(type: string) {
return type === 'Number';
}
/**
* Whether the given parameter type looks like a string type
*/
function isStringType(type: string) {
return !isListType(type) && !isNumberType(type);
}
function typeToTypeHint(type: string): ResolutionTypeHint {
if (isListType(type)) {
return ResolutionTypeHint.STRING_LIST;
} else if (isNumberType(type)) {
return ResolutionTypeHint.NUMBER;
}
return ResolutionTypeHint.STRING;
}