-
Notifications
You must be signed in to change notification settings - Fork 4k
/
fn.ts
688 lines (630 loc) · 27.7 KB
/
fn.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
import { ICfnConditionExpression } from './cfn-condition';
import { minimalCloudFormationJoin } from './cloudformation-lang';
import { Intrinsic } from './private/intrinsic';
import { IResolvable, IResolveContext } from './resolvable';
import { captureStackTrace } from './stack-trace';
import { Token } from './token';
// tslint:disable:max-line-length
/**
* CloudFormation intrinsic functions.
* http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html
*/
export class Fn {
/**
* The ``Fn::GetAtt`` intrinsic function returns the value of an attribute
* from a resource in the template.
* @param logicalNameOfResource The logical name (also called logical ID) of
* the resource that contains the attribute that you want.
* @param attributeName The name of the resource-specific attribute whose
* value you want. See the resource's reference page for details about the
* attributes available for that resource type.
* @returns a CloudFormationToken object
*/
public static getAtt(logicalNameOfResource: string, attributeName: string): Token {
return new FnGetAtt(logicalNameOfResource, attributeName);
}
/**
* The intrinsic function ``Fn::Join`` appends a set of values into a single
* value, separated by the specified delimiter. If a delimiter is the empty
* string, the set of values are concatenated with no delimiter.
* @param delimiter The value you want to occur between fragments. The
* delimiter will occur between fragments only. It will not terminate the
* final value.
* @param listOfValues The list of values you want combined.
* @returns a token represented as a string
*/
public static join(delimiter: string, listOfValues: string[]): string {
if (listOfValues.length === 0) {
throw new Error(`FnJoin requires at least one value to be provided`);
}
return new FnJoin(delimiter, listOfValues).toString();
}
/**
* To split a string into a list of string values so that you can select an element from the
* resulting string list, use the ``Fn::Split`` intrinsic function. Specify the location of splits
* with a delimiter, such as , (a comma). After you split a string, use the ``Fn::Select`` function
* to pick a specific element.
* @param delimiter A string value that determines where the source string is divided.
* @param source The string value that you want to split.
* @returns a token represented as a string array
*/
public static split(delimiter: string, source: string): string[] {
// short-circut if source is not a token
if (!Token.isUnresolved(source)) {
return source.split(delimiter);
}
return Token.asList(new FnSplit(delimiter, source));
}
/**
* The intrinsic function ``Fn::Select`` returns a single object from a list of objects by index.
* @param index The index of the object to retrieve. This must be a value from zero to N-1, where N represents the number of elements in the array.
* @param array The list of objects to select from. This list must not be null, nor can it have null entries.
* @returns a token represented as a string
*/
public static select(index: number, array: string[]): string {
if (!Token.isUnresolved(array)) {
return array[index];
}
return new FnSelect(index, array).toString();
}
/**
* The intrinsic function ``Fn::Sub`` substitutes variables in an input string
* with values that you specify. In your templates, you can use this function
* to construct commands or outputs that include values that aren't available
* until you create or update a stack.
* @param body A string with variables that AWS CloudFormation substitutes
* with their associated values at runtime. Write variables as ${MyVarName}.
* Variables can be template parameter names, resource logical IDs, resource
* attributes, or a variable in a key-value map. If you specify only template
* parameter names, resource logical IDs, and resource attributes, don't
* specify a key-value map.
* @param variables The name of a variable that you included in the String
* parameter. The value that AWS CloudFormation substitutes for the associated
* variable name at runtime.
* @returns a token represented as a string
*/
public static sub(body: string, variables?: { [key: string]: string }): string {
return new FnSub(body, variables).toString();
}
/**
* The intrinsic function ``Fn::Base64`` returns the Base64 representation of
* the input string. This function is typically used to pass encoded data to
* Amazon EC2 instances by way of the UserData property.
* @param data The string value you want to convert to Base64.
* @returns a token represented as a string
*/
public static base64(data: string): string {
return new FnBase64(data).toString();
}
/**
* The intrinsic function ``Fn::Cidr`` returns the specified Cidr address block.
* @param ipBlock The user-specified default Cidr address block.
* @param count The number of subnets' Cidr block wanted. Count can be 1 to 256.
* @param sizeMask The digit covered in the subnet.
* @returns a token represented as a string
*/
public static cidr(ipBlock: string, count: number, sizeMask?: string): string[] {
return Token.asList(new FnCidr(ipBlock, count, sizeMask));
}
/**
* The intrinsic function ``Fn::GetAZs`` returns an array that lists
* Availability Zones for a specified region. Because customers have access to
* different Availability Zones, the intrinsic function ``Fn::GetAZs`` enables
* template authors to write templates that adapt to the calling user's
* access. That way you don't have to hard-code a full list of Availability
* Zones for a specified region.
* @param region The name of the region for which you want to get the
* Availability Zones. You can use the AWS::Region pseudo parameter to specify
* the region in which the stack is created. Specifying an empty string is
* equivalent to specifying AWS::Region.
* @returns a token represented as a string array
*/
public static getAZs(region?: string): string[] {
return Token.asList(new FnGetAZs(region));
}
/**
* The intrinsic function ``Fn::ImportValue`` returns the value of an output
* exported by another stack. You typically use this function to create
* cross-stack references. In the following example template snippets, Stack A
* exports VPC security group values and Stack B imports them.
* @param sharedValueToImport The stack output value that you want to import.
* @returns a token represented as a string
*/
public static importValue(sharedValueToImport: string): string {
return new FnImportValue(sharedValueToImport).toString();
}
/**
* The intrinsic function ``Fn::FindInMap`` returns the value corresponding to
* keys in a two-level map that is declared in the Mappings section.
* @returns a token represented as a string
*/
public static findInMap(mapName: string, topLevelKey: string, secondLevelKey: string): string {
return new FnFindInMap(mapName, topLevelKey, secondLevelKey).toString();
}
/**
* Returns true if all the specified conditions evaluate to true, or returns
* false if any one of the conditions evaluates to false. ``Fn::And`` acts as
* an AND operator. The minimum number of conditions that you can include is
* 2, and the maximum is 10.
* @param conditions conditions to AND
* @returns an FnCondition token
*/
public static conditionAnd(...conditions: ICfnConditionExpression[]): ICfnConditionExpression {
return new FnAnd(...conditions);
}
/**
* Compares if two values are equal. Returns true if the two values are equal
* or false if they aren't.
* @param lhs A value of any type that you want to compare.
* @param rhs A value of any type that you want to compare.
* @returns an FnCondition token
*/
public static conditionEquals(lhs: any, rhs: any): ICfnConditionExpression {
return new FnEquals(lhs, rhs);
}
/**
* Returns one value if the specified condition evaluates to true and another
* value if the specified condition evaluates to false. Currently, AWS
* CloudFormation supports the ``Fn::If`` intrinsic function in the metadata
* attribute, update policy attribute, and property values in the Resources
* section and Outputs sections of a template. You can use the AWS::NoValue
* pseudo parameter as a return value to remove the corresponding property.
* @param conditionId A reference to a condition in the Conditions section. Use
* the condition's name to reference it.
* @param valueIfTrue A value to be returned if the specified condition
* evaluates to true.
* @param valueIfFalse A value to be returned if the specified condition
* evaluates to false.
* @returns an FnCondition token
*/
public static conditionIf(conditionId: string, valueIfTrue: any, valueIfFalse: any): ICfnConditionExpression {
return new FnIf(conditionId, valueIfTrue, valueIfFalse);
}
/**
* Returns true for a condition that evaluates to false or returns false for a
* condition that evaluates to true. ``Fn::Not`` acts as a NOT operator.
* @param condition A condition such as ``Fn::Equals`` that evaluates to true
* or false.
* @returns an FnCondition token
*/
public static conditionNot(condition: ICfnConditionExpression): ICfnConditionExpression {
return new FnNot(condition);
}
/**
* Returns true if any one of the specified conditions evaluate to true, or
* returns false if all of the conditions evaluates to false. ``Fn::Or`` acts
* as an OR operator. The minimum number of conditions that you can include is
* 2, and the maximum is 10.
* @param conditions conditions that evaluates to true or false.
* @returns an FnCondition token
*/
public static conditionOr(...conditions: ICfnConditionExpression[]): ICfnConditionExpression {
return new FnOr(...conditions);
}
/**
* Returns true if a specified string matches at least one value in a list of
* strings.
* @param listOfStrings A list of strings, such as "A", "B", "C".
* @param value A string, such as "A", that you want to compare against a list of strings.
* @returns an FnCondition token
*/
public static conditionContains(listOfStrings: string[], value: string): ICfnConditionExpression {
return new FnContains(listOfStrings, value);
}
/**
* Returns true if a specified string matches all values in a list.
* @param listOfStrings A list of strings, such as "A", "B", "C".
* @param value A string, such as "A", that you want to compare against a list
* of strings.
* @returns an FnCondition token
*/
public conditionEachMemberEquals(listOfStrings: string[], value: string): ICfnConditionExpression {
return new FnEachMemberEquals(listOfStrings, value);
}
/**
* Returns true if each member in a list of strings matches at least one value
* in a second list of strings.
* @param stringsToCheck A list of strings, such as "A", "B", "C". AWS
* CloudFormation checks whether each member in the strings_to_check parameter
* is in the strings_to_match parameter.
* @param stringsToMatch A list of strings, such as "A", "B", "C". Each member
* in the strings_to_match parameter is compared against the members of the
* strings_to_check parameter.
* @returns an FnCondition token
*/
public conditionEachMemberIn(stringsToCheck: string[], stringsToMatch: string): ICfnConditionExpression {
return new FnEachMemberIn(stringsToCheck, stringsToMatch);
}
/**
* Returns all values for a specified parameter type.
* @param parameterType An AWS-specific parameter type, such as
* AWS::EC2::SecurityGroup::Id or AWS::EC2::VPC::Id. For more information, see
* Parameters in the AWS CloudFormation User Guide.
* @returns a token represented as a string array
*/
public refAll(parameterType: string): string[] {
return Token.asList(new FnRefAll(parameterType));
}
/**
* Returns an attribute value or list of values for a specific parameter and
* attribute.
* @param parameterOrLogicalId The name of a parameter for which you want to
* retrieve attribute values. The parameter must be declared in the Parameters
* section of the template.
* @param attribute The name of an attribute from which you want to retrieve a
* value.
* @returns a token represented as a string
*/
public valueOf(parameterOrLogicalId: string, attribute: string): string {
return new FnValueOf(parameterOrLogicalId, attribute).toString();
}
/**
* Returns a list of all attribute values for a given parameter type and
* attribute.
* @param parameterType An AWS-specific parameter type, such as
* AWS::EC2::SecurityGroup::Id or AWS::EC2::VPC::Id. For more information, see
* Parameters in the AWS CloudFormation User Guide.
* @param attribute The name of an attribute from which you want to retrieve a
* value. For more information about attributes, see Supported Attributes.
* @returns a token represented as a string array
*/
public valueOfAll(parameterType: string, attribute: string): string[] {
return Token.asList(new FnValueOfAll(parameterType, attribute));
}
}
/**
* Base class for tokens that represent CloudFormation intrinsic functions.
*/
class FnBase extends Intrinsic {
constructor(name: string, value: any) {
super({ [name]: value });
}
}
/**
* The intrinsic function ``Fn::FindInMap`` returns the value corresponding to keys in a two-level
* map that is declared in the Mappings section.
*/
class FnFindInMap extends FnBase {
/**
* Creates an ``Fn::FindInMap`` function.
* @param mapName The logical name of a mapping declared in the Mappings section that contains the keys and values.
* @param topLevelKey The top-level key name. Its value is a list of key-value pairs.
* @param secondLevelKey The second-level key name, which is set to one of the keys from the list assigned to TopLevelKey.
*/
constructor(mapName: string, topLevelKey: any, secondLevelKey: any) {
super('Fn::FindInMap', [ mapName, topLevelKey, secondLevelKey ]);
}
}
/**
* The ``Fn::GetAtt`` intrinsic function returns the value of an attribute from a resource in the template.
*/
class FnGetAtt extends FnBase {
/**
* Creates a ``Fn::GetAtt`` function.
* @param logicalNameOfResource The logical name (also called logical ID) of the resource that contains the attribute that you want.
* @param attributeName The name of the resource-specific attribute whose value you want. See the resource's reference page for details about the attributes available for that resource type.
*/
constructor(logicalNameOfResource: string, attributeName: string) {
super('Fn::GetAtt', [ logicalNameOfResource, attributeName ]);
}
}
/**
* The intrinsic function ``Fn::GetAZs`` returns an array that lists Availability Zones for a
* specified region. Because customers have access to different Availability Zones, the intrinsic
* function ``Fn::GetAZs`` enables template authors to write templates that adapt to the calling
* user's access. That way you don't have to hard-code a full list of Availability Zones for a
* specified region.
*/
class FnGetAZs extends FnBase {
/**
* Creates an ``Fn::GetAZs`` function.
* @param region The name of the region for which you want to get the Availability Zones.
* You can use the AWS::Region pseudo parameter to specify the region in
* which the stack is created. Specifying an empty string is equivalent to
* specifying AWS::Region.
*/
constructor(region?: string) {
super('Fn::GetAZs', region || '');
}
}
/**
* The intrinsic function ``Fn::ImportValue`` returns the value of an output exported by another stack.
* You typically use this function to create cross-stack references. In the following example
* template snippets, Stack A exports VPC security group values and Stack B imports them.
*/
class FnImportValue extends FnBase {
/**
* Creates an ``Fn::ImportValue`` function.
* @param sharedValueToImport The stack output value that you want to import.
*/
constructor(sharedValueToImport: string) {
super('Fn::ImportValue', sharedValueToImport);
}
}
/**
* The intrinsic function ``Fn::Select`` returns a single object from a list of objects by index.
*/
class FnSelect extends FnBase {
/**
* Creates an ``Fn::Select`` function.
* @param index The index of the object to retrieve. This must be a value from zero to N-1, where N represents the number of elements in the array.
* @param array The list of objects to select from. This list must not be null, nor can it have null entries.
*/
constructor(index: number, array: any) {
super('Fn::Select', [ index, array ]);
}
}
/**
* To split a string into a list of string values so that you can select an element from the
* resulting string list, use the ``Fn::Split`` intrinsic function. Specify the location of splits
* with a delimiter, such as , (a comma). After you split a string, use the ``Fn::Select`` function
* to pick a specific element.
*/
class FnSplit extends FnBase {
/**
* Create an ``Fn::Split`` function.
* @param delimiter A string value that determines where the source string is divided.
* @param source The string value that you want to split.
*/
constructor(delimiter: string, source: any) {
super('Fn::Split', [ delimiter, source ]);
}
}
/**
* The intrinsic function ``Fn::Sub`` substitutes variables in an input string with values that
* you specify. In your templates, you can use this function to construct commands or outputs
* that include values that aren't available until you create or update a stack.
*/
class FnSub extends FnBase {
/**
* Creates an ``Fn::Sub`` function.
* @param body A string with variables that AWS CloudFormation substitutes with their
* associated values at runtime. Write variables as ${MyVarName}. Variables
* can be template parameter names, resource logical IDs, resource attributes,
* or a variable in a key-value map. If you specify only template parameter names,
* resource logical IDs, and resource attributes, don't specify a key-value map.
* @param variables The name of a variable that you included in the String parameter.
* The value that AWS CloudFormation substitutes for the associated variable name at runtime.
*/
constructor(body: string, variables?: { [key: string]: any }) {
super('Fn::Sub', variables ? [body, variables] : body);
}
}
/**
* The intrinsic function ``Fn::Base64`` returns the Base64 representation of the input string.
* This function is typically used to pass encoded data to Amazon EC2 instances by way of
* the UserData property.
*/
class FnBase64 extends FnBase {
/**
* Creates an ``Fn::Base64`` function.
* @param data The string value you want to convert to Base64.
*/
constructor(data: any) {
super('Fn::Base64', data);
}
}
/**
* The intrinsic function ``Fn::Cidr`` returns the specified Cidr address block.
*/
class FnCidr extends FnBase {
/**
* Creates an ``Fn::Cidr`` function.
* @param ipBlock The user-specified default Cidr address block.
* @param count The number of subnets' Cidr block wanted. Count can be 1 to 256.
* @param sizeMask The digit covered in the subnet.
*/
constructor(ipBlock: any, count: any, sizeMask?: any) {
if (count < 1 || count > 256) {
throw new Error(`Fn::Cidr's count attribute must be betwen 1 and 256, ${count} was provided.`);
}
super('Fn::Cidr', [ipBlock, count, sizeMask]);
}
}
class FnConditionBase extends Intrinsic implements ICfnConditionExpression {
constructor(type: string, value: any) {
super({ [type]: value });
}
}
/**
* Returns true if all the specified conditions evaluate to true, or returns false if any one
* of the conditions evaluates to false. ``Fn::And`` acts as an AND operator. The minimum number of
* conditions that you can include is 2, and the maximum is 10.
*/
class FnAnd extends FnConditionBase {
constructor(...condition: ICfnConditionExpression[]) {
super('Fn::And', condition);
}
}
/**
* Compares if two values are equal. Returns true if the two values are equal or false
* if they aren't.
*/
class FnEquals extends FnConditionBase {
/**
* Creates an ``Fn::Equals`` condition function.
* @param lhs A value of any type that you want to compare.
* @param rhs A value of any type that you want to compare.
*/
constructor(lhs: any, rhs: any) {
super('Fn::Equals', [ lhs, rhs ]);
}
}
/**
* Returns one value if the specified condition evaluates to true and another value if the
* specified condition evaluates to false. Currently, AWS CloudFormation supports the ``Fn::If``
* intrinsic function in the metadata attribute, update policy attribute, and property values
* in the Resources section and Outputs sections of a template. You can use the AWS::NoValue
* pseudo parameter as a return value to remove the corresponding property.
*/
class FnIf extends FnConditionBase {
/**
* Creates an ``Fn::If`` condition function.
* @param condition A reference to a condition in the Conditions section. Use the condition's name to reference it.
* @param valueIfTrue A value to be returned if the specified condition evaluates to true.
* @param valueIfFalse A value to be returned if the specified condition evaluates to false.
*/
constructor(condition: string, valueIfTrue: any, valueIfFalse: any) {
super('Fn::If', [ condition, valueIfTrue, valueIfFalse ]);
}
}
/**
* Returns true for a condition that evaluates to false or returns false for a condition that evaluates to true.
* ``Fn::Not`` acts as a NOT operator.
*/
class FnNot extends FnConditionBase {
/**
* Creates an ``Fn::Not`` condition function.
* @param condition A condition such as ``Fn::Equals`` that evaluates to true or false.
*/
constructor(condition: ICfnConditionExpression) {
super('Fn::Not', [ condition ]);
}
}
/**
* Returns true if any one of the specified conditions evaluate to true, or returns false if
* all of the conditions evaluates to false. ``Fn::Or`` acts as an OR operator. The minimum number
* of conditions that you can include is 2, and the maximum is 10.
*/
class FnOr extends FnConditionBase {
/**
* Creates an ``Fn::Or`` condition function.
* @param condition A condition that evaluates to true or false.
*/
constructor(...condition: ICfnConditionExpression[]) {
super('Fn::Or', condition);
}
}
/**
* Returns true if a specified string matches at least one value in a list of strings.
*/
class FnContains extends FnConditionBase {
/**
* Creates an ``Fn::Contains`` function.
* @param listOfStrings A list of strings, such as "A", "B", "C".
* @param value A string, such as "A", that you want to compare against a list of strings.
*/
constructor(listOfStrings: any, value: string) {
super('Fn::Contains', [ listOfStrings, value ]);
}
}
/**
* Returns true if a specified string matches all values in a list.
*/
class FnEachMemberEquals extends FnConditionBase {
/**
* Creates an ``Fn::EachMemberEquals`` function.
* @param listOfStrings A list of strings, such as "A", "B", "C".
* @param value A string, such as "A", that you want to compare against a list of strings.
*/
constructor(listOfStrings: any, value: string) {
super('Fn::EachMemberEquals', [ listOfStrings, value ]);
}
}
/**
* Returns true if each member in a list of strings matches at least one value in a second
* list of strings.
*/
class FnEachMemberIn extends FnConditionBase {
/**
* Creates an ``Fn::EachMemberIn`` function.
* @param stringsToCheck A list of strings, such as "A", "B", "C". AWS CloudFormation checks whether each member in the strings_to_check parameter is in the strings_to_match parameter.
* @param stringsToMatch A list of strings, such as "A", "B", "C". Each member in the strings_to_match parameter is compared against the members of the strings_to_check parameter.
*/
constructor(stringsToCheck: any, stringsToMatch: any) {
super('Fn::EachMemberIn', [ [stringsToCheck], stringsToMatch ]);
}
}
/**
* Returns all values for a specified parameter type.
*/
class FnRefAll extends FnBase {
/**
* Creates an ``Fn::RefAll`` function.
* @param parameterType An AWS-specific parameter type, such as AWS::EC2::SecurityGroup::Id or
* AWS::EC2::VPC::Id. For more information, see Parameters in the AWS
* CloudFormation User Guide.
*/
constructor(parameterType: string) {
super('Fn::RefAll', parameterType);
}
}
/**
* Returns an attribute value or list of values for a specific parameter and attribute.
*/
class FnValueOf extends FnBase {
/**
* Creates an ``Fn::ValueOf`` function.
* @param parameterOrLogicalId The name of a parameter for which you want to retrieve attribute values. The parameter must be declared in the Parameters section of the template.
* @param attribute The name of an attribute from which you want to retrieve a value.
*/
constructor(parameterOrLogicalId: string, attribute: string) {
super('Fn::ValueOf', [ parameterOrLogicalId, attribute ]);
}
}
/**
* Returns a list of all attribute values for a given parameter type and attribute.
*/
class FnValueOfAll extends FnBase {
/**
* Creates an ``Fn::ValueOfAll`` function.
* @param parameterType An AWS-specific parameter type, such as AWS::EC2::SecurityGroup::Id or AWS::EC2::VPC::Id. For more information, see Parameters in the AWS CloudFormation User Guide.
* @param attribute The name of an attribute from which you want to retrieve a value. For more information about attributes, see Supported Attributes.
*/
constructor(parameterType: string, attribute: string) {
super('Fn::ValueOfAll', [ parameterType, attribute ]);
}
}
/**
* The intrinsic function ``Fn::Join`` appends a set of values into a single value, separated by
* the specified delimiter. If a delimiter is the empty string, the set of values are concatenated
* with no delimiter.
*/
class FnJoin implements IResolvable {
public readonly creationStack: string[];
private readonly delimiter: string;
private readonly listOfValues: any[];
// Cache for the result of resolveValues() - since it otherwise would be computed several times
private _resolvedValues?: any[];
/**
* Creates an ``Fn::Join`` function.
* @param delimiter The value you want to occur between fragments. The delimiter will occur between fragments only.
* It will not terminate the final value.
* @param listOfValues The list of values you want combined.
*/
constructor(delimiter: string, listOfValues: any[]) {
if (listOfValues.length === 0) {
throw new Error(`FnJoin requires at least one value to be provided`);
}
this.delimiter = delimiter;
this.listOfValues = listOfValues;
this.creationStack = captureStackTrace();
}
public resolve(context: IResolveContext): any {
if (Token.isUnresolved(this.listOfValues)) {
// This is a list token, don't try to do smart things with it.
return { 'Fn::Join': [ this.delimiter, this.listOfValues ] };
}
const resolved = this.resolveValues(context);
if (resolved.length === 1) {
return resolved[0];
}
return { 'Fn::Join': [ this.delimiter, resolved ] };
}
public toString() {
return Token.asString(this, { displayHint: 'Fn::Join' });
}
public toJSON() {
return `<Fn::Join>`;
}
/**
* Optimization: if an Fn::Join is nested in another one and they share the same delimiter, then flatten it up. Also,
* if two concatenated elements are literal strings (not tokens), then pre-concatenate them with the delimiter, to
* generate shorter output.
*/
private resolveValues(context: IResolveContext) {
if (this._resolvedValues) { return this._resolvedValues; }
const resolvedValues = this.listOfValues.map(context.resolve);
return this._resolvedValues = minimalCloudFormationJoin(this.delimiter, resolvedValues);
}
}