2
2
3
3
from six import string_types
4
4
5
+
5
6
class Action (object ):
6
7
"""
7
8
Base class for intrinsic function actions. Each intrinsic function must subclass this,
@@ -45,14 +46,15 @@ def can_handle(self, input_dict):
45
46
"""
46
47
47
48
return input_dict is not None \
48
- and isinstance (input_dict , dict ) \
49
- and len (input_dict ) == 1 \
50
- and self .intrinsic_name in input_dict
49
+ and isinstance (input_dict , dict ) \
50
+ and len (input_dict ) == 1 \
51
+ and self .intrinsic_name in input_dict
51
52
52
53
@classmethod
53
54
def _parse_resource_reference (cls , ref_value ):
54
55
"""
55
- Splits a resource reference of structure "LogicalId.Property" and returns the "LogicalId" and "Property" separately.
56
+ Splits a resource reference of structure "LogicalId.Property" and returns the "LogicalId" and "Property"
57
+ separately.
56
58
57
59
:param string ref_value: Input reference value which *may* contain the structure "LogicalId.Property"
58
60
:return string, string: Returns two values - logical_id, property. If the input does not contain the structure,
@@ -162,6 +164,7 @@ def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs):
162
164
self .intrinsic_name : resolved_value
163
165
}
164
166
167
+
165
168
class SubAction (Action ):
166
169
intrinsic_name = "Fn::Sub"
167
170
@@ -189,7 +192,6 @@ def do_replacement(full_ref, prop_name):
189
192
190
193
return self ._handle_sub_action (input_dict , do_replacement )
191
194
192
-
193
195
def resolve_resource_refs (self , input_dict , supported_resource_refs ):
194
196
"""
195
197
Resolves reference to some property of a resource. Inside string to be substituted, there could be either a
@@ -249,7 +251,6 @@ def do_replacement(full_ref, ref_value):
249
251
250
252
return self ._handle_sub_action (input_dict , do_replacement )
251
253
252
-
253
254
def resolve_resource_id_refs (self , input_dict , supported_resource_id_refs ):
254
255
"""
255
256
Resolves reference to some property of a resource. Inside string to be substituted, there could be either a
@@ -306,14 +307,13 @@ def do_replacement(full_ref, ref_value):
306
307
307
308
return self ._handle_sub_action (input_dict , do_replacement )
308
309
309
-
310
310
def _handle_sub_action (self , input_dict , handler ):
311
311
"""
312
312
Handles resolving replacements in the Sub action based on the handler that is passed as an input.
313
313
314
314
:param input_dict: Dictionary to be resolved
315
- :param supported_values: One of several different objects that contain the supported values that need to be changed.
316
- See each method above for specifics on these objects.
315
+ :param supported_values: One of several different objects that contain the supported values that
316
+ need to be changed. See each method above for specifics on these objects.
317
317
:param handler: handler that is specific to each implementation.
318
318
:return: Resolved value of the Sub dictionary
319
319
"""
@@ -327,7 +327,6 @@ def _handle_sub_action(self, input_dict, handler):
327
327
328
328
return input_dict
329
329
330
-
331
330
def _handle_sub_value (self , sub_value , handler_method ):
332
331
"""
333
332
Generic method to handle value to Fn::Sub key. We are interested in parsing the ${} syntaxes inside
@@ -365,15 +364,15 @@ def handler_method(full_ref, ref_value):
365
364
366
365
:param string text: Input text
367
366
:param handler_method: Method to be called to handle each occurrence of ${blah} reference structure.
368
- First parameter to this method is the full reference structure Ex: ${LogicalId.Property}. Second parameter is just the
369
- value of the reference such as "LogicalId.Property"
367
+ First parameter to this method is the full reference structure Ex: ${LogicalId.Property}.
368
+ Second parameter is just the value of the reference such as "LogicalId.Property"
370
369
371
370
:return string: Text with all reference structures replaced as necessary
372
371
"""
373
372
374
373
# RegExp to find pattern "${logicalId.property}" and return the word inside bracket
375
374
logical_id_regex = '[A-Za-z0-9\.]+'
376
- ref_pattern = re .compile (r'\$\{(' + logical_id_regex + ')\}' )
375
+ ref_pattern = re .compile (r'\$\{(' + logical_id_regex + ')\}' )
377
376
378
377
# Find all the pattern, and call the handler to decide how to substitute them.
379
378
# Do the substitution and return the final text
@@ -383,14 +382,14 @@ def handler_method(full_ref, ref_value):
383
382
lambda match : handler_method (match .group (0 ), match .group (1 )),
384
383
text )
385
384
385
+
386
386
class GetAttAction (Action ):
387
387
intrinsic_name = "Fn::GetAtt"
388
388
389
389
def resolve_parameter_refs (self , input_dict , parameters ):
390
390
# Parameters can never be referenced within GetAtt value
391
391
return input_dict
392
392
393
-
394
393
def resolve_resource_refs (self , input_dict , supported_resource_refs ):
395
394
"""
396
395
Resolve resource references within a GetAtt dict.
@@ -441,12 +440,11 @@ def resolve_resource_refs(self, input_dict, supported_resource_refs):
441
440
splits = value_str .split (self ._resource_ref_separator )
442
441
logical_id = splits [0 ]
443
442
property = splits [1 ]
444
- remaining = splits [2 :] # if any
443
+ remaining = splits [2 :] # if any
445
444
446
445
resolved_value = supported_resource_refs .get (logical_id , property )
447
446
return self ._get_resolved_dictionary (input_dict , key , resolved_value , remaining )
448
447
449
-
450
448
def resolve_resource_id_refs (self , input_dict , supported_resource_id_refs ):
451
449
"""
452
450
Resolve resource references within a GetAtt dict.
@@ -485,12 +483,11 @@ def resolve_resource_id_refs(self, input_dict, supported_resource_id_refs):
485
483
value_str = self ._resource_ref_separator .join (value )
486
484
splits = value_str .split (self ._resource_ref_separator )
487
485
logical_id = splits [0 ]
488
- remaining = splits [1 :] # if any
486
+ remaining = splits [1 :] # if any
489
487
490
488
resolved_value = supported_resource_id_refs .get (logical_id )
491
489
return self ._get_resolved_dictionary (input_dict , key , resolved_value , remaining )
492
490
493
-
494
491
def _get_resolved_dictionary (self , input_dict , key , resolved_value , remaining ):
495
492
"""
496
493
Resolves the function and returns the updated dictionary
@@ -505,4 +502,4 @@ def _get_resolved_dictionary(self, input_dict, key, resolved_value, remaining):
505
502
# This is the new value of Fn::GetAtt
506
503
input_dict [key ] = [resolved_value ] + remaining
507
504
508
- return input_dict
505
+ return input_dict
0 commit comments