Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added configuration address cleaning function #4248

Merged
merged 6 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)