From 2f7dcd51aa244666157feab0a1d70dddc43089f2 Mon Sep 17 00:00:00 2001 From: Lucas <12496191+lucashuy@users.noreply.github.com> Date: Thu, 22 Sep 2022 09:25:32 -0700 Subject: [PATCH] feat: Added configuration address cleaning function (#4248) * Added configuration address cleaning function * Fixed spelling mistake * Added additional test case --- .../hooks/prepare/resource_linking.py | 18 +++++++++++ .../hooks/prepare/test_resource_linking.py | 32 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py b/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py index 729f4e4834..338ed8e4c0 100644 --- a/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py +++ b/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py @@ -3,6 +3,7 @@ e.g. linking layers to functions """ +import re from typing import List @@ -37,3 +38,20 @@ def _clean_references_list(references: List[str]) -> List[str]: if not cleaned_references[-1].startswith(copied_references[i]): cleaned_references.append(copied_references[i]) return cleaned_references + + +def _get_configuration_address(address: str) -> str: + """ + Cleans all addresses of indices and returns a clean address + + Parameters + ========== + address : str + The address to clean + + Returns + ======= + str + The address clean of indices + """ + return re.sub(r"\[[^\[\]]*\]", "", address) diff --git a/tests/unit/hook_packages/terraform/hooks/prepare/test_resource_linking.py b/tests/unit/hook_packages/terraform/hooks/prepare/test_resource_linking.py index 3f418d591e..c11a72b253 100644 --- a/tests/unit/hook_packages/terraform/hooks/prepare/test_resource_linking.py +++ b/tests/unit/hook_packages/terraform/hooks/prepare/test_resource_linking.py @@ -3,7 +3,10 @@ from parameterized import parameterized -from samcli.hook_packages.terraform.hooks.prepare.resource_linking import _clean_references_list +from samcli.hook_packages.terraform.hooks.prepare.resource_linking import ( + _clean_references_list, + _get_configuration_address, +) class TestResourceLinking(TestCase): @@ -65,3 +68,30 @@ def test_ensure_original_references_not_mutated(self): cleaned_references_list = _clean_references_list(references) self.assertEqual(references, original_references) self.assertNotEqual(references, cleaned_references_list) + + @parameterized.expand( + [ + ( + "module.get_language_function.aws_lambda_function.this[0]", + "module.get_language_function.aws_lambda_function.this", + ), + ( + "module.get_language_function.aws_lambda_function.this[1]", + "module.get_language_function.aws_lambda_function.this", + ), + ("module.functions[0].aws_lambda_function.this[0]", "module.functions.aws_lambda_function.this"), + ("module.functions[1].aws_lambda_function.this[1]", "module.functions.aws_lambda_function.this"), + ( + 'module.functions["get_function"].aws_lambda_function.this[0]', + "module.functions.aws_lambda_function.this", + ), + ( + "module.functions.aws_lambda_function.this", + "module.functions.aws_lambda_function.this", + ), + ] + ) + def test_get_configation_address(self, input_addr, expected_addr): + cleaned_addr = _get_configuration_address(input_addr) + + self.assertEqual(cleaned_addr, expected_addr)