Skip to content

Commit 5c1e28e

Browse files
committed
wip - fixing conditional tags in resources
1 parent ade6cbe commit 5c1e28e

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

pycfmodel/resolver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def resolve_ref(function_body, params: Dict, mappings: Dict[str, Dict], conditio
8888
resolved_ref = resolve(function_body, params, mappings, conditions)
8989
if resolved_ref in params:
9090
return params[resolved_ref]
91+
# elif resolved_ref == AWS_NOVALUE: #todo - pending to resolve ref to no value?
92+
# return ""
9193
else:
9294
logger.warning(f"Using `UNDEFINED_PARAM_{resolved_ref}` for {resolved_ref}. Original value wasn't available.")
9395
return f"UNDEFINED_PARAM_{resolved_ref}"

tests/test_generic.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ def test_recursive():
2121
# FunctionDict
2222
({"Fn::ImportValue": "abc"}, FunctionDict(**{"Fn::ImportValue": "abc"})),
2323
([{"Fn::ImportValue": "abc"}], [FunctionDict(**{"Fn::ImportValue": "abc"})]),
24+
(
25+
{"Fn::If": ["MyCustomCondition", {"Key": "mysecretkey", "Value": "standard"}, {"Ref": "AWS::NoValue"}]},
26+
FunctionDict(
27+
**{
28+
"Fn::If": [
29+
"MyCustomCondition",
30+
{"Key": "mysecretkey", "Value": "standard"},
31+
{"Ref": "AWS::NoValue"},
32+
]
33+
}
34+
),
35+
),
36+
(
37+
[{"Fn::If": ["MyCustomCondition", {"Key": "mysecretkey", "Value": "standard"}, {"Ref": "AWS::NoValue"}]}],
38+
[
39+
FunctionDict(
40+
**{
41+
"Fn::If": [
42+
"MyCustomCondition",
43+
{"Key": "mysecretkey", "Value": "standard"},
44+
{"Ref": "AWS::NoValue"},
45+
]
46+
}
47+
)
48+
],
49+
),
2450
# Properties
2551
({"Key": "test", "Value": "potatoe"}, Tag(Key="test", Value="potatoe")),
2652
([{"Key": "test", "Value": "potatoe"}], [Tag(Key="test", Value="potatoe")]),

tests/test_resolver.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,41 @@ def test_split(function, expected_output):
119119

120120

121121
@pytest.mark.parametrize(
122-
"function, expected_output", [({"Fn::If": ["A", "a", "b"]}, "a"), ({"Fn::If": ["B", "a", "b"]}, "b")]
122+
"function, expected_output",
123+
[
124+
({"Fn::If": ["A", "a", "b"]}, "a"),
125+
({"Fn::If": ["B", "a", "b"]}, "b"),
126+
(
127+
{
128+
"Fn::If": [
129+
"A",
130+
{"Key": "SecondTag", "Value": "true"},
131+
{"Key": "SecondTag", "Value": "false"},
132+
]
133+
},
134+
{"Key": "SecondTag", "Value": "true"},
135+
),
136+
(
137+
{
138+
"Fn::If": [
139+
"A",
140+
{"Key": "SecondTag", "Value": "true"},
141+
{"Ref": "AWS::NoValue"},
142+
]
143+
},
144+
{"Key": "SecondTag", "Value": "true"},
145+
),
146+
(
147+
{
148+
"Fn::If": [
149+
"B",
150+
{"Key": "SecondTag", "Value": "true"},
151+
{"Ref": "AWS::NoValue"},
152+
]
153+
},
154+
"UNDEFINED_PARAM_AWS::NoValue",
155+
),
156+
],
123157
)
124158
def test_if(function, expected_output):
125159
parameters = {}
@@ -818,3 +852,32 @@ def test_resolve_find_in_map_for_bool_values_in_map(params, expected_resolved_va
818852

819853
result = resolve_find_in_map(function_body=function_body, params=params, mappings=mappings, conditions={})
820854
assert result == expected_resolved_value
855+
856+
857+
def test_resolve_tags_on_no_value_with_condition():
858+
template = {
859+
"AWSTemplateFormatVersion": "2010-09-09",
860+
"Description": "TEST DESCRIPTION",
861+
"Conditions": {"MyCustomCondition": {"Fn::Equals": [{"Ref": "AWS::AccountId"}, "123456789012"]}},
862+
"Resources": {
863+
"PublicS3Bucket": {
864+
"Type": "AWS::S3::Bucket",
865+
"Properties": {
866+
"BucketName": "test",
867+
"Tags": [
868+
{
869+
"Fn::If": [
870+
"MyCustomCondition",
871+
{"Key": "mysecretkey", "Value": "standard"},
872+
{"Ref": "AWS::NoValue"},
873+
]
874+
}
875+
],
876+
},
877+
}
878+
},
879+
}
880+
881+
model = parse(template).resolve()
882+
resource = model.Resources["PublicS3Bucket"]
883+
assert isinstance(resource, GenericResource)

0 commit comments

Comments
 (0)