-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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: Clean Terraform References List #4252
Merged
mildaniel
merged 7 commits into
aws:develop-terraform-hooks
from
mildaniel:clean-references-list
Sep 21, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a06adda
Function for cleaning layer reference list
mildaniel afcd552
Merge branch 'develop-terraform-hooks' of https://github.com/aws/aws-…
mildaniel 6b9569f
Merge from upstream
mildaniel 16ad352
Merge branch 'develop-terraform-hooks' into clean-references-list
mildaniel fcef01f
Add test case for for each
mildaniel a66c722
Don't mutate existing reference list
mildaniel 12e96f0
Merge branch 'develop-terraform-hooks' into clean-references-list
mildaniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
samcli/hook_packages/terraform/hooks/prepare/layer_linking.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Use Terraform plan to link resources together | ||
e.g. linking layers to functions | ||
""" | ||
|
||
from typing import List | ||
|
||
|
||
def _clean_references_list(references: List[str]) -> List[str]: | ||
""" | ||
Return a new copy of the complete references list. | ||
|
||
e.g. given a list of references like | ||
[ | ||
'aws_lambda_layer_version.layer1[0].arn', | ||
'aws_lambda_layer_version.layer1[0]', | ||
'aws_lambda_layer_version.layer1', | ||
] | ||
We want only the first complete reference ('aws_lambda_layer_version.layer1[0].arn') | ||
|
||
Parameters | ||
---------- | ||
references: List[str] | ||
A list of reference strings | ||
|
||
Returns | ||
------- | ||
List[str] | ||
A copy of a cleaned list of reference strings | ||
""" | ||
cleaned_references = [] | ||
copied_references = sorted(references, reverse=True) | ||
if not references: | ||
return [] | ||
cleaned_references.append(copied_references[0]) | ||
for i in range(1, len(copied_references)): | ||
if not cleaned_references[-1].startswith(copied_references[i]): | ||
cleaned_references.append(copied_references[i]) | ||
return cleaned_references |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from copy import deepcopy | ||
from unittest import TestCase | ||
|
||
from parameterized import parameterized | ||
|
||
from samcli.hook_packages.terraform.hooks.prepare.layer_linking import _clean_references_list | ||
|
||
|
||
class TestLayerLinking(TestCase): | ||
@parameterized.expand( | ||
[ | ||
([], []), | ||
(["aws_lambda_layer_version.layer1[0].arn"], ["aws_lambda_layer_version.layer1[0].arn"]), | ||
(["aws_lambda_layer_version.layer1[0]"], ["aws_lambda_layer_version.layer1[0]"]), | ||
(["aws_lambda_layer_version.layer1"], ["aws_lambda_layer_version.layer1"]), | ||
( | ||
[ | ||
"aws_lambda_layer_version.layer1[0].arn", | ||
"aws_lambda_layer_version.layer1[0]", | ||
"aws_lambda_layer_version.layer1", | ||
], | ||
["aws_lambda_layer_version.layer1[0].arn"], | ||
), | ||
( | ||
[ | ||
"aws_lambda_layer_version.layer1[0].arn", | ||
"aws_lambda_layer_version.layer1[0]", | ||
"aws_lambda_layer_version.layer1", | ||
"module.const_layer1.layer_arn", | ||
"module.const_layer1", | ||
"module.const_layer2.layer_arn", | ||
"module.const_layer2", | ||
], | ||
[ | ||
"module.const_layer2.layer_arn", | ||
"module.const_layer1.layer_arn", | ||
"aws_lambda_layer_version.layer1[0].arn", | ||
], | ||
), | ||
( | ||
[ | ||
'aws_lambda_layer_version.layer1["key1"].arn', | ||
'aws_lambda_layer_version.layer1["key1"]', | ||
"aws_lambda_layer_version.layer1", | ||
], | ||
['aws_lambda_layer_version.layer1["key1"].arn'], | ||
), | ||
] | ||
) | ||
def test_clean_references_list(self, references, expected): | ||
cleaned_references = _clean_references_list(references) | ||
self.assertEqual(cleaned_references, expected) | ||
|
||
def test_ensure_original_references_not_mutated(self): | ||
references = [ | ||
"aws_lambda_layer_version.layer1[0].arn", | ||
"aws_lambda_layer_version.layer1[0]", | ||
"aws_lambda_layer_version.layer1", | ||
"module.const_layer1.layer_arn", | ||
"module.const_layer1", | ||
"module.const_layer2.layer_arn", | ||
"module.const_layer2", | ||
] | ||
original_references = deepcopy(references) | ||
cleaned_references_list = _clean_references_list(references) | ||
self.assertEqual(references, original_references) | ||
self.assertNotEqual(references, cleaned_references_list) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a case that contain an instance for a block defined with for_each. https://www.terraform.io/language/meta-arguments/for_each#referring-to-instances
something like
([aws_lambda_layer_version.layer1["key1"].arn, aws_lambda_layer_version.layer1["key1"], aws_lambda_layer_version.layer1)