CF: Raise ValidationError on bad resource id (#4186)

This commit is contained in:
Timothy Klopotoski 2021-08-18 13:41:02 -04:00 committed by GitHub
parent bac013c15a
commit 8743f81e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -230,11 +230,18 @@ class CloudFormationResponse(BaseResponse):
stack = self.cloudformation_backend.get_stack(stack_name) stack = self.cloudformation_backend.get_stack(stack_name)
logical_resource_id = self._get_param("LogicalResourceId") logical_resource_id = self._get_param("LogicalResourceId")
resource = None
for stack_resource in stack.stack_resources: for stack_resource in stack.stack_resources:
if stack_resource.logical_resource_id == logical_resource_id: if stack_resource.logical_resource_id == logical_resource_id:
resource = stack_resource resource = stack_resource
break break
if not resource:
message = "Resource {0} does not exist for stack {1}".format(
logical_resource_id, stack_name
)
raise ValidationError(stack_name, message)
template = self.response_template(DESCRIBE_STACK_RESOURCE_RESPONSE_TEMPLATE) template = self.response_template(DESCRIBE_STACK_RESOURCE_RESPONSE_TEMPLATE)
return template.render(stack=stack, resource=resource) return template.render(stack=stack, resource=resource)

View File

@ -6,7 +6,7 @@ from datetime import datetime, timedelta
import pytz import pytz
import boto3 import boto3
from botocore.exceptions import ClientError from botocore.exceptions import ClientError, ValidationError
import sure # noqa import sure # noqa
import pytest import pytest
@ -1139,6 +1139,37 @@ def test_describe_stack_pagination():
assert "NextToken" not in resp2.keys() assert "NextToken" not in resp2.keys()
@mock_cloudformation
def test_describe_stack_resource():
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
cf_conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)
stack = cf_conn.describe_stacks(StackName="test_stack")["Stacks"][0]
response = cf_conn.describe_stack_resource(
StackName=stack["StackName"], LogicalResourceId="EC2Instance1"
)
resource = response["StackResourceDetail"]
resource["LogicalResourceId"].should.equal("EC2Instance1")
resource["ResourceStatus"].should.equal("CREATE_COMPLETE")
resource["ResourceType"].should.equal("AWS::EC2::Instance")
resource["StackId"].should.equal(stack["StackId"])
@mock_cloudformation
def test_describe_stack_resource_when_resource_does_not_exist():
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
cf_conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)
stack = cf_conn.describe_stacks(StackName="test_stack")["Stacks"][0]
with pytest.raises(ClientError, match="does not exist for stack"):
cf_conn.describe_stack_resource(
StackName=stack["StackName"], LogicalResourceId="DoesNotExist"
)
@mock_cloudformation @mock_cloudformation
def test_describe_stack_resources(): def test_describe_stack_resources():
cf_conn = boto3.client("cloudformation", region_name="us-east-1") cf_conn = boto3.client("cloudformation", region_name="us-east-1")