Merge pull request #3072 from bblommers/cloudformation-check-name-in-use
CloudFormation - Check stack name in use
This commit is contained in:
commit
09c061e8a8
@ -50,6 +50,12 @@ class CloudFormationResponse(BaseResponse):
|
|||||||
for item in self._get_list_prefix("Tags.member")
|
for item in self._get_list_prefix("Tags.member")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.stack_name_exists(new_stack_name=stack_name):
|
||||||
|
template = self.response_template(
|
||||||
|
CREATE_STACK_NAME_EXISTS_RESPONSE_TEMPLATE
|
||||||
|
)
|
||||||
|
return 400, {"status": 400}, template.render(name=stack_name)
|
||||||
|
|
||||||
# Hack dict-comprehension
|
# Hack dict-comprehension
|
||||||
parameters = dict(
|
parameters = dict(
|
||||||
[
|
[
|
||||||
@ -82,6 +88,12 @@ class CloudFormationResponse(BaseResponse):
|
|||||||
template = self.response_template(CREATE_STACK_RESPONSE_TEMPLATE)
|
template = self.response_template(CREATE_STACK_RESPONSE_TEMPLATE)
|
||||||
return template.render(stack=stack)
|
return template.render(stack=stack)
|
||||||
|
|
||||||
|
def stack_name_exists(self, new_stack_name):
|
||||||
|
for stack in self.cloudformation_backend.stacks.values():
|
||||||
|
if stack.name == new_stack_name:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
@amzn_request_id
|
@amzn_request_id
|
||||||
def create_change_set(self):
|
def create_change_set(self):
|
||||||
stack_name = self._get_param("StackName")
|
stack_name = self._get_param("StackName")
|
||||||
@ -564,6 +576,15 @@ CREATE_STACK_RESPONSE_TEMPLATE = """<CreateStackResponse>
|
|||||||
</CreateStackResponse>
|
</CreateStackResponse>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
CREATE_STACK_NAME_EXISTS_RESPONSE_TEMPLATE = """<ErrorResponse xmlns="http://cloudformation.amazonaws.com/doc/2010-05-15/">
|
||||||
|
<Error>
|
||||||
|
<Type>Sender</Type>
|
||||||
|
<Code>AlreadyExistsException</Code>
|
||||||
|
<Message>Stack [{{ name }}] already exists</Message>
|
||||||
|
</Error>
|
||||||
|
<RequestId>950ff8d7-812a-44b3-bb0c-9b271b954104</RequestId>
|
||||||
|
</ErrorResponse>"""
|
||||||
|
|
||||||
UPDATE_STACK_RESPONSE_TEMPLATE = """<UpdateStackResponse xmlns="http://cloudformation.amazonaws.com/doc/2010-05-15/">
|
UPDATE_STACK_RESPONSE_TEMPLATE = """<UpdateStackResponse xmlns="http://cloudformation.amazonaws.com/doc/2010-05-15/">
|
||||||
<UpdateStackResult>
|
<UpdateStackResult>
|
||||||
<StackId>{{ stack.stack_id }}</StackId>
|
<StackId>{{ stack.stack_id }}</StackId>
|
||||||
|
@ -98,12 +98,12 @@ def test_create_stack_hosted_zone_by_id():
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
conn.create_stack(
|
conn.create_stack(
|
||||||
"test_stack", template_body=json.dumps(dummy_template), parameters={}.items()
|
"test_stack1", template_body=json.dumps(dummy_template), parameters={}.items()
|
||||||
)
|
)
|
||||||
r53_conn = boto.connect_route53()
|
r53_conn = boto.connect_route53()
|
||||||
zone_id = r53_conn.get_zones()[0].id
|
zone_id = r53_conn.get_zones()[0].id
|
||||||
conn.create_stack(
|
conn.create_stack(
|
||||||
"test_stack",
|
"test_stack2",
|
||||||
template_body=json.dumps(dummy_template2),
|
template_body=json.dumps(dummy_template2),
|
||||||
parameters={"ZoneId": zone_id}.items(),
|
parameters={"ZoneId": zone_id}.items(),
|
||||||
)
|
)
|
||||||
|
@ -919,7 +919,9 @@ def test_execute_change_set_w_name():
|
|||||||
def test_describe_stack_pagination():
|
def test_describe_stack_pagination():
|
||||||
conn = boto3.client("cloudformation", region_name="us-east-1")
|
conn = boto3.client("cloudformation", region_name="us-east-1")
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
conn.create_stack(StackName="test_stack", TemplateBody=dummy_template_json)
|
conn.create_stack(
|
||||||
|
StackName="test_stack_{}".format(i), TemplateBody=dummy_template_json
|
||||||
|
)
|
||||||
|
|
||||||
resp = conn.describe_stacks()
|
resp = conn.describe_stacks()
|
||||||
stacks = resp["Stacks"]
|
stacks = resp["Stacks"]
|
||||||
@ -1211,7 +1213,8 @@ def test_list_exports_with_token():
|
|||||||
# Add index to ensure name is unique
|
# Add index to ensure name is unique
|
||||||
dummy_output_template["Outputs"]["StackVPC"]["Export"]["Name"] += str(i)
|
dummy_output_template["Outputs"]["StackVPC"]["Export"]["Name"] += str(i)
|
||||||
cf.create_stack(
|
cf.create_stack(
|
||||||
StackName="test_stack", TemplateBody=json.dumps(dummy_output_template)
|
StackName="test_stack_{}".format(i),
|
||||||
|
TemplateBody=json.dumps(dummy_output_template),
|
||||||
)
|
)
|
||||||
exports = cf.list_exports()
|
exports = cf.list_exports()
|
||||||
exports["Exports"].should.have.length_of(100)
|
exports["Exports"].should.have.length_of(100)
|
||||||
@ -1273,3 +1276,16 @@ def test_non_json_redrive_policy():
|
|||||||
|
|
||||||
stack.Resource("MainQueue").resource_status.should.equal("CREATE_COMPLETE")
|
stack.Resource("MainQueue").resource_status.should.equal("CREATE_COMPLETE")
|
||||||
stack.Resource("DeadLetterQueue").resource_status.should.equal("CREATE_COMPLETE")
|
stack.Resource("DeadLetterQueue").resource_status.should.equal("CREATE_COMPLETE")
|
||||||
|
|
||||||
|
|
||||||
|
@mock_cloudformation
|
||||||
|
def test_boto3_create_duplicate_stack():
|
||||||
|
cf_conn = boto3.client("cloudformation", region_name="us-east-1")
|
||||||
|
cf_conn.create_stack(
|
||||||
|
StackName="test_stack", TemplateBody=dummy_template_json,
|
||||||
|
)
|
||||||
|
|
||||||
|
with assert_raises(ClientError):
|
||||||
|
cf_conn.create_stack(
|
||||||
|
StackName="test_stack", TemplateBody=dummy_template_json,
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user