Skip to content

Commit

Permalink
feat: Return early during linking if no destination resources are fou…
Browse files Browse the repository at this point in the history
…nd (aws#5220)

* Returns during linking if no destination resources are found

* Updated comment to correctly reflect state

* Cleaned extra word

---------

Co-authored-by: Mohamed Elasmar <[email protected]>
  • Loading branch information
2 people authored and Leonardo Gama committed Jun 22, 2023
1 parent e74caef commit 5446146
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions samcli/hook_packages/terraform/hooks/prepare/resource_linking.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ def _link_using_terraform_config(self, source_tf_resource: TFResource, cfn_resou
)
if not dest_resources:
LOG.debug(
"There are destination resources defined for for the source resource %s",
"There are no destination resources defined for the source resource %s, skipping linking.",
source_tf_resource.full_address,
)

return

for cfn_resource in cfn_resources:
self._resource_pair.cfn_resource_update_call_back_function(cfn_resource, dest_resources) # type: ignore

Expand Down Expand Up @@ -287,7 +290,12 @@ def _link_using_linking_fields(self, cfn_resource: Dict) -> None:
else ExistingResourceReference(value)
for value in values
]
LOG.debug("The value of the source resource linking field after mapping $s", dest_resources)

if not dest_resources:
LOG.debug("Skipping linking call back, no destination resources discovered.")
return

LOG.debug("The value of the source resource linking field after mapping %s", dest_resources)
self._resource_pair.cfn_resource_update_call_back_function(cfn_resource, dest_resources) # type: ignore

def _process_resolved_resources(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,21 @@ def setUp(self) -> None:
linking_exceptions=self.linker_exceptions,
)

def test_applied_empty_destination_skip_call_back(self):
resource_linker = ResourceLinker(self.sample_resource_linking_pair)
resource_linker._link_using_linking_fields({"Properties": {"Layers": []}})

self.sample_resource_linking_pair.cfn_resource_update_call_back_function.assert_not_called()

@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking._resolve_resource_attribute")
@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking.ResourceLinker._process_resolved_resources")
def test_config_empty_destination_skip_call_back(self, proccess_resolved_res_mock, resolve_resource_attr_mock):
resource_linker = ResourceLinker(self.sample_resource_linking_pair)
proccess_resolved_res_mock.return_value = []
resource_linker._link_using_terraform_config(Mock(), Mock())

self.sample_resource_linking_pair.cfn_resource_update_call_back_function.assert_not_called()

def test_handle_linking_mix_of_applied_and_non_applied_resources(self):
cfn_resource_depend_on_applied_resources = {
"Type": "AWS::Lambda::Function",
Expand Down

0 comments on commit 5446146

Please sign in to comment.