Repository: aws-quickstart/taskcat
Pull request #516 (2020-03-10 19:48:09)

Adding $[taskcat_current_region] magic parameter.
## Overview

This PR adds the `$[taskcat_current_region]` magic parameter. It returns the region passed into the ParamGenerator class. 

Brief description of what this PR does, and why it is needed (use case)?

## Testing/Steps taken to ensure quality

Unit tests. Live tests.


```
BucketRegion | us-east-2
-- | --
```


Patch:
diff --git a/CODEOWNERS b/CODEOWNERS
index ec96704..a14859a 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -1,4 +1,4 @@
 # Do not add any additional owners to this file, this is being used to ensure that
 # functional tests are run before merge, the taskcat-ci bot will auto-approve once
 # tests have passed
-*       @taskcat-ci @jaymccon @andrew-glenn @avattathil
+*       @taskcat-ci @jaymccon @andrew-glenn @tonynv
diff --git a/taskcat/_template_params.py b/taskcat/_template_params.py
index eb1581d..b88a024 100644
--- a/taskcat/_template_params.py
+++ b/taskcat/_template_params.py
@@ -31,6 +31,7 @@ class ParamGen:
         r"\$\[\w+_presignedurl],(.*?,){1,2}.*?$", re.IGNORECASE
     )
     RE_GETVAL = re.compile(r"(?<=._getval_)(\w+)(?=]$)", re.IGNORECASE)
+    RE_CURRENT_REGION = re.compile(r"\$\[taskcat_current_region]", re.IGNORECASE)
 
     def __init__(self, param_dict, bucket_name, region, boto_client, az_excludes=None):
         self.regxfind = CommonTools.regxfind
@@ -131,6 +132,11 @@ class ParamGen:
 
             # $[taskcat_genuuid]
             self._regex_replace_param_value(self.RE_GENUUID, self._gen_uuid())
+
+            # $[taskcat_current_region]
+            self._regex_replace_param_value(
+                self.RE_CURRENT_REGION, self._gen_current_region()
+            )
             self.results.update({self.param_name: self.param_value})
 
     def get_available_azs(self, count):
@@ -281,6 +287,9 @@ class ParamGen:
     def _gen_autobucket(self):
         return self.bucket_name
 
+    def _gen_current_region(self):
+        return self.region
+
     def _gen_password_wrapper(self, gen_regex, type_regex, count_regex):
         if gen_regex.search(self.param_value):
             passlen = int(self.regxfind(count_regex, self.param_value))


Test patch:
diff --git a/tests/test_template_params.py b/tests/test_template_params.py
index cf87adb..a762e43 100644
--- a/tests/test_template_params.py
+++ b/tests/test_template_params.py
@@ -180,6 +180,10 @@ class TestParamGen(unittest.TestCase):
             test_string="$[taskcat_autobucket]",
             test_pattern_attribute="RE_GENAUTOBUCKET",
         ),
+        rp_namedtup(
+            test_string="$[taskcat_current_region]",
+            test_pattern_attribute="RE_CURRENT_REGION",
+        ),
         rp_namedtup(
             test_string="https://s3.amazonaws.com/$[taskcat_autobucket]/myproject/",
             test_pattern_attribute="RE_GENAUTOBUCKET",
@@ -397,7 +401,7 @@ class TestParamGen(unittest.TestCase):
         pg.convert_to_str()
         self.assertEqual(pg.param_value, "1234")
 
-    def test_param_transform(self):
+    def test_param_transform(self):  # noqa: C901
         input_params = {
             "AvailabilityZones": "$[taskcat_genaz_3]",
             "ByteValue": "1",
@@ -411,6 +415,7 @@ class TestParamGen(unittest.TestCase):
             "SingleAZ": "$[taskcat_getsingleaz_2]",
             "StackName": "TestStack",
             "UUID": "$[taskcat_genuuid]",
+            "BucketRegion": "$[taskcat_current_region]",
         }
         bclient = MockClient
         bclient.logger = logger
@@ -445,10 +450,19 @@ class TestParamGen(unittest.TestCase):
             if not _found:
                 missed_regex_patterns.append(rp.test_pattern_attribute)
         self.assertEqual(missed_regex_patterns, [])
-        with self.subTest("SingleAZ transformed value must be us-east-1b"):
-            for _param_key, param_value in pg.results.items():
-                if _param_key == "SingleAZ":
-                    self.assertEqual(param_value, "us-east-1b")
+        subtests = [
+            ("SingleAZ transformed value must be us-east-1b", "SingleAZ", "us-east-1b"),
+            (
+                "CurrentRegion transformed must be us-east-1",
+                "CurrentRegion",
+                "us-east-1",
+            ),
+        ]
+        for testdata in subtests:
+            with self.subTest(testdata[0]):
+                for _param_key, param_value in pg.results.items():
+                    if _param_key == testdata[1]:
+                        self.assertEqual(param_value, testdata[2])
 
     def test_list_as_param_value(self):
         input_params = {