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

fix: copy resource metadata before normalizing the information in it #5904

Merged
merged 4 commits into from
Sep 14, 2023
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
8 changes: 4 additions & 4 deletions samcli/lib/samlib/resource_metadata_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import re
from copy import deepcopy
from pathlib import Path
from typing import Dict

Expand Down Expand Up @@ -61,10 +62,8 @@ def normalize(template_dict, normalize_parameters=False):
resources = template_dict.get(RESOURCES_KEY, {})

for logical_id, resource in resources.items():
resource_metadata = resource.get(METADATA_KEY)
if resource_metadata is None:
resource_metadata = {}
resource[METADATA_KEY] = resource_metadata
# copy metadata to another variable, change its values and assign it back in the end
resource_metadata = deepcopy(resource.get(METADATA_KEY)) or {}

is_normalized = resource_metadata.get(SAM_IS_NORMALIZED, False)
if not is_normalized:
Expand Down Expand Up @@ -97,6 +96,7 @@ def normalize(template_dict, normalize_parameters=False):
resource_metadata,
{SAM_RESOURCE_ID_KEY: ResourceMetadataNormalizer.get_resource_id(resource, logical_id)},
)
resource[METADATA_KEY] = resource_metadata

# This is a work around to allow the customer to use sam deploy or package commands without the need to provide
# values for the CDK auto generated asset parameters. The suggested solution is to let CDK add some metadata to
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/lib/samlib/test_resource_metadata_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import TestCase

from samcli.lib.samlib.resource_metadata_normalizer import ResourceMetadataNormalizer
from samcli.yamlhelper import yaml_parse


class TestResourceMetadataNormalizer(TestCase):
Expand Down Expand Up @@ -456,6 +457,50 @@ def test_skip_normalizing_already_normalized_resource(self):
self.assertEqual("new path", template_data["Resources"]["Function1"]["Properties"]["Code"])
self.assertEqual("Function1", template_data["Resources"]["Function1"]["Metadata"]["SamResourceId"])

def test_with_referenced_metadata(self):
input_template = """
Resources:
FirstFunction:
Type: AWS::Serverless::Function
Metadata: &GoFunctionMetadata
BuildMethod: go1.x
BuildProperties:
TrimGoPath: true
Properties:
CodeUri: dummy
Handler: bootstrap
Runtime: provided.al2
SecondFunction:
Type: AWS::Serverless::Function
Metadata: *GoFunctionMetadata
Properties:
CodeUri: dummy
Handler: bootstrap
Runtime: provided.al2
ThirdFunction:
Type: AWS::Serverless::Function
Metadata: *GoFunctionMetadata
Properties:
CodeUri: dummy
Handler: bootstrap
Runtime: provided.al2
"""
template_dict = yaml_parse(input_template)
ResourceMetadataNormalizer.normalize(template_dict)

self.assertEqual(
template_dict.get("Resources", {}).get("FirstFunction", {}).get("Metadata", {}).get("SamResourceId"),
"FirstFunction",
)
self.assertEqual(
template_dict.get("Resources", {}).get("SecondFunction", {}).get("Metadata", {}).get("SamResourceId"),
"SecondFunction",
)
self.assertEqual(
template_dict.get("Resources", {}).get("ThirdFunction", {}).get("Metadata", {}).get("SamResourceId"),
"ThirdFunction",
)


class TestResourceMetadataNormalizerGetResourceId(TestCase):
@parameterized.expand(
Expand Down
Loading