Skip to content

CFNv2: Implement get_template #12865

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from datetime import datetime, timezone
from typing import NotRequired, Optional, TypedDict

Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(
self.account_id = account_id
self.region_name = region_name
self.template = template
self.template_original = copy.deepcopy(self.template)
self.template_body = template_body
self.status = StackStatus.CREATE_IN_PROGRESS
self.status_reason = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json
import logging
from collections import defaultdict
from datetime import datetime, timezone
Expand All @@ -24,6 +25,7 @@
DisableRollback,
ExecuteChangeSetOutput,
ExecutionStatus,
GetTemplateOutput,
GetTemplateSummaryInput,
GetTemplateSummaryOutput,
IncludePropertyValues,
Expand All @@ -39,6 +41,7 @@
StackName,
StackNameOrId,
StackStatus,
TemplateStage,
UpdateStackInput,
UpdateStackOutput,
)
Expand Down Expand Up @@ -589,6 +592,49 @@ def describe_stack_events(
raise StackNotFoundError(stack_name)
return DescribeStackEventsOutput(StackEvents=stack.events)

@handler("GetTemplate")
def get_template(
self,
context: RequestContext,
stack_name: StackName = None,
change_set_name: ChangeSetNameOrId = None,
template_stage: TemplateStage = None,
**kwargs,
) -> GetTemplateOutput:
state = get_cloudformation_store(context.account_id, context.region)
if change_set_name:
change_set = find_change_set_v2(state, change_set_name, stack_name=stack_name)
stack = change_set.stack
elif stack_name:
stack = find_stack_v2(state, stack_name)
else:
raise StackNotFoundError(stack_name)

if template_stage == TemplateStage.Processed and "Transform" in stack.template_body:
copy_template = copy.deepcopy(stack.template_original)
for key in [
"ChangeSetName",
"StackName",
"StackId",
"Transform",
"Conditions",
"Mappings",
]:
copy_template.pop(key, None)
for key in ["Parameters", "Outputs"]:
if key in copy_template and not copy_template[key]:
copy_template.pop(key)
for resource in copy_template.get("Resources", {}).values():
resource.pop("LogicalResourceId", None)
template_body = json.dumps(copy_template)
else:
template_body = stack.template_body

return GetTemplateOutput(
TemplateBody=template_body,
StagesAvailable=[TemplateStage.Original, TemplateStage.Processed],
)

@handler("GetTemplateSummary", expand=False)
def get_template_summary(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def test_stack_name_creation(self, deploy_cfn_template, snapshot, aws_client):

snapshot.match("stack_response", e.value.response)

@pytest.mark.skip(reason="CFNV2:Other")
@markers.aws.validated
@pytest.mark.parametrize("fileformat", ["yaml", "json"])
def test_get_template_using_create_stack(self, snapshot, fileformat, aws_client):
Expand Down Expand Up @@ -141,7 +140,6 @@ def test_get_template_using_create_stack(self, snapshot, fileformat, aws_client)
)
snapshot.match("template_processed", template_processed)

@pytest.mark.skip(reason="CFNV2:Other")
@markers.aws.validated
@pytest.mark.parametrize("fileformat", ["yaml", "json"])
def test_get_template_using_changesets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ def test_update_with_rollback_configuration(deploy_cfn_template, aws_client):
aws_client.cloudwatch.delete_alarms(AlarmNames=["HighResourceUsage"])


@pytest.mark.skip(reason="CFNV2:UpdateStack")
@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(["$..Stacks..ChangeSetId"])
def test_diff_after_update(deploy_cfn_template, aws_client, snapshot):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def test_macro_deployment(
snapshot.match("stack_outputs", stack_with_macro.outputs)
snapshot.match("stack_resource_descriptions", description)

@pytest.mark.skip("CFNV2:GetTemplate")
@pytest.mark.skip("CFNV2:Macros")
@markers.aws.validated
@markers.snapshot.skip_snapshot_verify(
paths=[
Expand Down
Loading