Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - fixing conditional tags in resources #155

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pycfmodel/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
26 changes: 26 additions & 0 deletions tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]),
Expand Down
65 changes: 64 additions & 1 deletion tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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)
Loading