Repository: awslabs/aws-cfn-template-flip

Issue:
using clean produces invalid template
we are using `new_template = to_yaml( to_json( orig_template ) )` to cleanup the yaml we are producing with jinja2

`clean_up=False` we see
```
  PublicSubnetIds:
    Description: The public subnetids
    Condition: HasPublicSubnet1AZ1
    Value:
      Fn::Join:
        - ','
        -
          - Fn::If:
            - HasPublicSubnet1AZ1
            - !Ref PublicSubnet1AZ1
            - !Ref AWS::NoValue
          - Fn::If:
            - HasPublicSubnet1AZ2
            - !Ref PublicSubnet1AZ2
            - !Ref AWS::NoValue
          - Fn::If:
            - HasPublicSubnet1AZ3
            - !Ref PublicSubnet1AZ3
            - !Ref AWS::NoValue
    Export:
      Name: !Sub vpc-${Environment}-PublicSubnetIds
```
but with `clean_up=True` we get
```
  PublicSubnetIds:
    Description: The public subnetids
    Condition: HasPublicSubnet1AZ1
    Value: !Sub
      - ${Param1},${Param2},${Param3}
      - Param1: !If
          - HasPublicSubnet1AZ1
          - !Ref 'PublicSubnet1AZ1'
          - !Ref 'AWS::NoValue'
        Param2: !If
          - HasPublicSubnet1AZ2
          - !Ref 'PublicSubnet1AZ2'
          - !Ref 'AWS::NoValue'
        Param3: !If
          - HasPublicSubnet1AZ3
          - !Ref 'PublicSubnet1AZ3'
          - !Ref 'AWS::NoValue'
    Export:
      Name: !Sub 'vpc-${Environment}-PublicSubnetIds'
```
Which CloudFormation complains about .. suggestions ??

Comments:

Patch:
diff --git a/cfn_clean/__init__.py b/cfn_clean/__init__.py
index 782002b..3fdb6d4 100644
--- a/cfn_clean/__init__.py
+++ b/cfn_clean/__init__.py
@@ -53,6 +53,13 @@ def convert_join(value):
                 new_parts.append("${{{}}}".format(".".join(params)))
             else:
                 for key, val in args.items():
+                    # we want to bail if a conditional can evaluate to AWS::NoValue
+                    if isinstance(val, dict):
+                        if "Fn::If" in val and "AWS::NoValue" in val["Fn::If"]:
+                            return {
+                                "Fn::Join": value,
+                            }
+
                     if val == part:
                         param_name = key
                         break


Test patch:
diff --git a/tests/test_clean.py b/tests/test_clean.py
index 24e14a0..6fb9000 100644
--- a/tests/test_clean.py
+++ b/tests/test_clean.py
@@ -251,6 +251,84 @@ def test_deep_nested_join():
     assert expected == actual
 
 
+def test_gh_63_no_value():
+    """
+    Test that Joins with conditionals that can evaluate to AWS::NoValue
+    are not converted to Fn::Sub
+    """
+
+    source = {
+        "Fn::Join": [
+            ",",
+            [
+                {
+                    "Fn::If": [
+                        "Condition1",
+                        "True1",
+                        "AWS::NoValue"
+                    ]
+                },
+                {
+                    "Fn::If": [
+                        "Condition2",
+                        "True2",
+                        "False2"
+                    ]
+                }
+            ]
+        ]
+    }
+
+    assert source == clean(source)
+
+
+def test_gh_63_value():
+    """
+    Test that Joins with conditionals that cannot evaluate to AWS::NoValue
+    are converted to Fn::Sub
+    """
+
+    source = {
+        "Fn::Join": [
+            ",",
+            [
+                {
+                    "Fn::If": [
+                        "Condition1",
+                        "True1",
+                        "False1"
+                    ]
+                },
+                {
+                    "Fn::If": [
+                        "Condition2",
+                        "True2",
+                        "False2"
+                    ]
+                }
+            ]
+        ]
+    }
+
+    expected = ODict((
+        ("Fn::Sub", [
+            "${Param1},${Param2}",
+            ODict((
+                ("Param1", ODict((
+                    ("Fn::If", ["Condition1", "True1", "False1"]),
+                ))),
+                ("Param2", ODict((
+                    ("Fn::If", ["Condition2", "True2", "False2"]),
+                ))),
+            )),
+        ]),
+    ))
+
+    actual = clean(source)
+
+    assert actual == expected
+
+
 def test_misused_join():
     """
     Test that we don't break in the case that there is