Repository: aws-cloudformation/rain
Pull request #133 (2023-05-12 18:39:00)

Add Rules to ordering
Issue #, if available:
No Issue

Description of changes:
We currently have Rules in our CloudFormation template. It looks like, since this isn't included in the ordering, it automatically gets pushed at the bottom of the file. 

It would be nice to see Rules being consistently placed in the same position in the template.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.


Patch:
diff --git a/cft/format/sort.go b/cft/format/sort.go
index 423511e8..16675200 100644
--- a/cft/format/sort.go
+++ b/cft/format/sort.go
@@ -15,6 +15,7 @@ var orders = ordering{
 		"Description",
 		"Metadata",
 		"Parameters",
+		"Rules",
 		"Mappings",
 		"Conditions",
 		"Transform",


Test patch:
diff --git a/cft/format/format_test.go b/cft/format/format_test.go
index c1b98ccc..46ab0daf 100644
--- a/cft/format/format_test.go
+++ b/cft/format/format_test.go
@@ -53,6 +53,16 @@ Resources:
             apt-get update
 
             apt-get upgrade -y
+Rules:
+  Rule1:
+    RuleCondition: !Equals
+      - !Ref Environment
+      - test
+    Assertions:
+      - Assert:
+          Fn::Contains:
+            - - a1.medium
+            - !Ref InstanceType
 Parameters:
   Name:
     Type: String
@@ -62,6 +72,17 @@ const expectedYaml = `Parameters:
   Name:
     Type: String
 
+Rules:
+  Rule1:
+    RuleCondition: !Equals
+      - !Ref Environment
+      - test
+    Assertions:
+      - Assert:
+          Fn::Contains:
+            - - a1.medium
+            - !Ref InstanceType
+
 # Multiline comment
 # starting at indent 0
 Resources:
@@ -143,6 +164,17 @@ Resources:
 
             apt-get upgrade -y
 
+Rules:
+  Rule1:
+    RuleCondition: !Equals
+      - !Ref Environment
+      - test
+    Assertions:
+      - Assert:
+          Fn::Contains:
+            - - a1.medium
+            - !Ref InstanceType
+
 Parameters:
   Name:
     Type: String
@@ -154,6 +186,32 @@ const expectedJson = `{
             "Type": "String"
         }
     },
+    "Rules": {
+        "Rule1": {
+            "RuleCondition": {
+                "Fn::Equals": [
+                    {
+                        "Ref": "Environment"
+                    },
+                    "test"
+                ]
+            },
+            "Assertions": [
+                {
+                    "Assert": {
+                        "Fn::Contains": [
+                            [
+                                "a1.medium"
+                            ],
+                            {
+                                "Ref": "InstanceType"
+                            }
+                        ]
+                    }
+                }
+            ]
+        }
+    },
     "Resources": {
         "Bucket2": {
             "Type": "AWS::S3::Bucket",
@@ -280,6 +338,32 @@ const expectedUnsortedJson = `{
             }
         }
     },
+    "Rules": {
+        "Rule1": {
+            "RuleCondition": {
+                "Fn::Equals": [
+                    {
+                        "Ref": "Environment"
+                    },
+                    "test"
+                ]
+            },
+            "Assertions": [
+                {
+                    "Assert": {
+                        "Fn::Contains": [
+                            [
+                                "a1.medium"
+                            ],
+                            {
+                                "Ref": "InstanceType"
+                            }
+                        ]
+                    }
+                }
+            ]
+        }
+    },
     "Parameters": {
         "Name": {
             "Type": "String"