Skip to content

Commit

Permalink
feat: Added configuration address cleaning function (#4248)
Browse files Browse the repository at this point in the history
* Added configuration address cleaning function

* Fixed spelling mistake

* Added additional test case
  • Loading branch information
lucashuy authored Sep 22, 2022
1 parent e33ad3e commit 2f7dcd5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
18 changes: 18 additions & 0 deletions samcli/hook_packages/terraform/hooks/prepare/resource_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
e.g. linking layers to functions
"""

import re
from typing import List


Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 2f7dcd5

Please sign in to comment.