From 5c1e28e26bb7937f389d2e6d7fd34d4e0842d11a Mon Sep 17 00:00:00 2001 From: Ramon Date: Fri, 20 Dec 2024 11:52:22 +0100 Subject: [PATCH] wip - fixing conditional tags in resources --- pycfmodel/resolver.py | 2 ++ tests/test_generic.py | 26 +++++++++++++++++ tests/test_resolver.py | 65 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/pycfmodel/resolver.py b/pycfmodel/resolver.py index 0a3dc52..90486ef 100644 --- a/pycfmodel/resolver.py +++ b/pycfmodel/resolver.py @@ -88,6 +88,8 @@ def resolve_ref(function_body, params: Dict, mappings: Dict[str, Dict], conditio resolved_ref = resolve(function_body, params, mappings, conditions) if resolved_ref in params: return params[resolved_ref] + # elif resolved_ref == AWS_NOVALUE: #todo - pending to resolve ref to no value? + # return "" else: logger.warning(f"Using `UNDEFINED_PARAM_{resolved_ref}` for {resolved_ref}. Original value wasn't available.") return f"UNDEFINED_PARAM_{resolved_ref}" diff --git a/tests/test_generic.py b/tests/test_generic.py index 8163ade..37e02b0 100644 --- a/tests/test_generic.py +++ b/tests/test_generic.py @@ -21,6 +21,32 @@ def test_recursive(): # FunctionDict ({"Fn::ImportValue": "abc"}, FunctionDict(**{"Fn::ImportValue": "abc"})), ([{"Fn::ImportValue": "abc"}], [FunctionDict(**{"Fn::ImportValue": "abc"})]), + ( + {"Fn::If": ["MyCustomCondition", {"Key": "mysecretkey", "Value": "standard"}, {"Ref": "AWS::NoValue"}]}, + FunctionDict( + **{ + "Fn::If": [ + "MyCustomCondition", + {"Key": "mysecretkey", "Value": "standard"}, + {"Ref": "AWS::NoValue"}, + ] + } + ), + ), + ( + [{"Fn::If": ["MyCustomCondition", {"Key": "mysecretkey", "Value": "standard"}, {"Ref": "AWS::NoValue"}]}], + [ + FunctionDict( + **{ + "Fn::If": [ + "MyCustomCondition", + {"Key": "mysecretkey", "Value": "standard"}, + {"Ref": "AWS::NoValue"}, + ] + } + ) + ], + ), # Properties ({"Key": "test", "Value": "potatoe"}, Tag(Key="test", Value="potatoe")), ([{"Key": "test", "Value": "potatoe"}], [Tag(Key="test", Value="potatoe")]), diff --git a/tests/test_resolver.py b/tests/test_resolver.py index 20d23d1..7f30b60 100644 --- a/tests/test_resolver.py +++ b/tests/test_resolver.py @@ -119,7 +119,41 @@ def test_split(function, expected_output): @pytest.mark.parametrize( - "function, expected_output", [({"Fn::If": ["A", "a", "b"]}, "a"), ({"Fn::If": ["B", "a", "b"]}, "b")] + "function, expected_output", + [ + ({"Fn::If": ["A", "a", "b"]}, "a"), + ({"Fn::If": ["B", "a", "b"]}, "b"), + ( + { + "Fn::If": [ + "A", + {"Key": "SecondTag", "Value": "true"}, + {"Key": "SecondTag", "Value": "false"}, + ] + }, + {"Key": "SecondTag", "Value": "true"}, + ), + ( + { + "Fn::If": [ + "A", + {"Key": "SecondTag", "Value": "true"}, + {"Ref": "AWS::NoValue"}, + ] + }, + {"Key": "SecondTag", "Value": "true"}, + ), + ( + { + "Fn::If": [ + "B", + {"Key": "SecondTag", "Value": "true"}, + {"Ref": "AWS::NoValue"}, + ] + }, + "UNDEFINED_PARAM_AWS::NoValue", + ), + ], ) def test_if(function, expected_output): parameters = {} @@ -818,3 +852,32 @@ def test_resolve_find_in_map_for_bool_values_in_map(params, expected_resolved_va result = resolve_find_in_map(function_body=function_body, params=params, mappings=mappings, conditions={}) assert result == expected_resolved_value + + +def test_resolve_tags_on_no_value_with_condition(): + template = { + "AWSTemplateFormatVersion": "2010-09-09", + "Description": "TEST DESCRIPTION", + "Conditions": {"MyCustomCondition": {"Fn::Equals": [{"Ref": "AWS::AccountId"}, "123456789012"]}}, + "Resources": { + "PublicS3Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "BucketName": "test", + "Tags": [ + { + "Fn::If": [ + "MyCustomCondition", + {"Key": "mysecretkey", "Value": "standard"}, + {"Ref": "AWS::NoValue"}, + ] + } + ], + }, + } + }, + } + + model = parse(template).resolve() + resource = model.Resources["PublicS3Bucket"] + assert isinstance(resource, GenericResource)