@@ -68,6 +68,8 @@ export class CfnInclude extends core.CfnElement {
68
68
private readonly resources : { [ logicalId : string ] : core . CfnResource } = { } ;
69
69
private readonly parameters : { [ logicalId : string ] : core . CfnParameter } = { } ;
70
70
private readonly parametersToReplace : { [ parameterName : string ] : any } ;
71
+ private readonly mappingsScope : core . Construct ;
72
+ private readonly mappings : { [ mappingName : string ] : core . CfnMapping } = { } ;
71
73
private readonly outputs : { [ logicalId : string ] : core . CfnOutput } = { } ;
72
74
private readonly nestedStacks : { [ logicalId : string ] : IncludedNestedStack } = { } ;
73
75
private readonly nestedStacksToInclude : { [ name : string ] : CfnIncludeProps } ;
@@ -92,6 +94,12 @@ export class CfnInclude extends core.CfnElement {
92
94
}
93
95
}
94
96
97
+ // instantiate the Mappings
98
+ this . mappingsScope = new core . Construct ( this , '$Mappings' ) ;
99
+ for ( const mappingName of Object . keys ( this . template . Mappings || { } ) ) {
100
+ this . createMapping ( mappingName ) ;
101
+ }
102
+
95
103
// instantiate all parameters
96
104
for ( const logicalId of Object . keys ( this . template . Parameters || { } ) ) {
97
105
this . createParameter ( logicalId ) ;
@@ -104,12 +112,10 @@ export class CfnInclude extends core.CfnElement {
104
112
}
105
113
106
114
this . nestedStacksToInclude = props . nestedStacks || { } ;
107
-
108
115
// instantiate all resources as CDK L1 objects
109
116
for ( const logicalId of Object . keys ( this . template . Resources || { } ) ) {
110
117
this . getOrCreateResource ( logicalId ) ;
111
118
}
112
-
113
119
// verify that all nestedStacks have been instantiated
114
120
for ( const nestedStackId of Object . keys ( props . nestedStacks || { } ) ) {
115
121
if ( ! ( nestedStackId in this . resources ) ) {
@@ -118,7 +124,6 @@ export class CfnInclude extends core.CfnElement {
118
124
}
119
125
120
126
const outputScope = new core . Construct ( this , '$Ouputs' ) ;
121
-
122
127
for ( const logicalId of Object . keys ( this . template . Outputs || { } ) ) {
123
128
this . createOutput ( logicalId , outputScope ) ;
124
129
}
@@ -168,7 +173,7 @@ export class CfnInclude extends core.CfnElement {
168
173
169
174
/**
170
175
* Returns the CfnParameter object from the 'Parameters'
171
- * section of the included template
176
+ * section of the included template.
172
177
* Any modifications performed on that object will be reflected in the resulting CDK template.
173
178
*
174
179
* If a Parameter with the given name is not present in the template,
@@ -184,9 +189,26 @@ export class CfnInclude extends core.CfnElement {
184
189
return ret ;
185
190
}
186
191
192
+ /**
193
+ * Returns the CfnMapping object from the 'Mappings' section of the included template.
194
+ * Any modifications performed on that object will be reflected in the resulting CDK template.
195
+ *
196
+ * If a Mapping with the given name is not present in the template,
197
+ * an exception will be thrown.
198
+ *
199
+ * @param mappingName the name of the Mapping in the template to retrieve
200
+ */
201
+ public getMapping ( mappingName : string ) : core . CfnMapping {
202
+ const ret = this . mappings [ mappingName ] ;
203
+ if ( ! ret ) {
204
+ throw new Error ( `Mapping with name '${ mappingName } ' was not found in the template` ) ;
205
+ }
206
+ return ret ;
207
+ }
208
+
187
209
/**
188
210
* Returns the CfnOutput object from the 'Outputs'
189
- * section of the included template
211
+ * section of the included template.
190
212
* Any modifications performed on that object will be reflected in the resulting CDK template.
191
213
*
192
214
* If an Output with the given name is not present in the template,
@@ -205,7 +227,9 @@ export class CfnInclude extends core.CfnElement {
205
227
/**
206
228
* Returns the NestedStack with name logicalId.
207
229
* For a nested stack to be returned by this method, it must be specified in the {@link CfnIncludeProps.nestedStacks}
208
- * @param logicalId the ID of the stack to retrieve, as it appears in the template.
230
+ * property.
231
+ *
232
+ * @param logicalId the ID of the stack to retrieve, as it appears in the template
209
233
*/
210
234
public getNestedStack ( logicalId : string ) : IncludedNestedStack {
211
235
if ( ! this . nestedStacks [ logicalId ] ) {
@@ -236,6 +260,9 @@ export class CfnInclude extends core.CfnElement {
236
260
findCondition ( conditionName : string ) : core . CfnCondition | undefined {
237
261
return self . conditions [ conditionName ] ;
238
262
} ,
263
+ findMapping ( mappingName ) : core . CfnMapping | undefined {
264
+ return self . mappings [ mappingName ] ;
265
+ } ,
239
266
} ;
240
267
const cfnParser = new cfn_parse . CfnParser ( {
241
268
finder,
@@ -244,6 +271,7 @@ export class CfnInclude extends core.CfnElement {
244
271
245
272
switch ( section ) {
246
273
case 'Conditions' :
274
+ case 'Mappings' :
247
275
case 'Resources' :
248
276
case 'Parameters' :
249
277
case 'Outputs' :
@@ -257,6 +285,22 @@ export class CfnInclude extends core.CfnElement {
257
285
return ret ;
258
286
}
259
287
288
+ private createMapping ( mappingName : string ) : void {
289
+ const cfnParser = new cfn_parse . CfnParser ( {
290
+ finder : {
291
+ findCondition ( ) { throw new Error ( 'Referring to Conditions in Mapping definitions is not allowed' ) ; } ,
292
+ findMapping ( ) { throw new Error ( 'Referring to other Mappings in Mapping definitions is not allowed' ) ; } ,
293
+ findRefTarget ( ) { throw new Error ( 'Using Ref expressions in Mapping definitions is not allowed' ) ; } ,
294
+ findResource ( ) { throw new Error ( 'Using GetAtt expressions in Mapping definitions is not allowed' ) ; } ,
295
+ } ,
296
+ } ) ;
297
+ const cfnMapping = new core . CfnMapping ( this . mappingsScope , mappingName , {
298
+ mapping : cfnParser . parseValue ( this . template . Mappings [ mappingName ] ) ,
299
+ } ) ;
300
+ this . mappings [ mappingName ] = cfnMapping ;
301
+ cfnMapping . overrideLogicalId ( mappingName ) ;
302
+ }
303
+
260
304
private createParameter ( logicalId : string ) : void {
261
305
if ( logicalId in this . parametersToReplace ) {
262
306
return ;
@@ -267,6 +311,7 @@ export class CfnInclude extends core.CfnElement {
267
311
findResource ( ) { throw new Error ( 'Using GetAtt expressions in Parameter definitions is not allowed' ) ; } ,
268
312
findRefTarget ( ) { throw new Error ( 'Using Ref expressions in Parameter definitions is not allowed' ) ; } ,
269
313
findCondition ( ) { throw new Error ( 'Referring to Conditions in Parameter definitions is not allowed' ) ; } ,
314
+ findMapping ( ) { throw new Error ( 'Referring to Mappings in Parameter definitions is not allowed' ) ; } ,
270
315
} ,
271
316
} ) . parseValue ( this . template . Parameters [ logicalId ] ) ;
272
317
const cfnParameter = new core . CfnParameter ( this , logicalId , {
@@ -300,6 +345,9 @@ export class CfnInclude extends core.CfnElement {
300
345
findCondition ( ) : undefined {
301
346
return undefined ;
302
347
} ,
348
+ findMapping ( mappingName ) : core . CfnMapping | undefined {
349
+ return self . mappings [ mappingName ] ;
350
+ } ,
303
351
} ,
304
352
parameters : this . parametersToReplace ,
305
353
} ) . parseValue ( this . template . Outputs [ logicalId ] ) ;
@@ -341,6 +389,7 @@ export class CfnInclude extends core.CfnElement {
341
389
? self . getOrCreateCondition ( cName )
342
390
: undefined ;
343
391
} ,
392
+ findMapping ( ) { throw new Error ( 'Using FindInMap in Condition definitions is not allowed' ) ; } ,
344
393
} ,
345
394
context : cfn_parse . CfnParsingContext . CONDITIONS ,
346
395
parameters : this . parametersToReplace ,
@@ -381,6 +430,10 @@ export class CfnInclude extends core.CfnElement {
381
430
return self . conditions [ conditionName ] ;
382
431
} ,
383
432
433
+ findMapping ( mappingName ) : core . CfnMapping | undefined {
434
+ return self . mappings [ mappingName ] ;
435
+ } ,
436
+
384
437
findResource ( lId : string ) : core . CfnResource | undefined {
385
438
if ( ! ( lId in ( self . template . Resources || { } ) ) ) {
386
439
return undefined ;
0 commit comments