Merge pull request #239 from joekiller/enhance/describe_stacks_delete_stacks_stack_status

Enhance DescribeStacks. Keep track of deleted stacks. Stack status.
This commit is contained in:
Steve Pulec 2014-10-23 07:51:02 -04:00
commit e67ef8f038
3 changed files with 52 additions and 8 deletions

View File

@ -12,6 +12,7 @@ class FakeStack(object):
self.stack_id = stack_id
self.name = name
self.template = template
self.status = 'CREATE_COMPLETE'
template_dict = json.loads(self.template)
self.description = template_dict.get('Description')
@ -35,6 +36,7 @@ class CloudFormationBackend(BaseBackend):
def __init__(self):
self.stacks = {}
self.deleted_stacks = {}
def create_stack(self, name, template):
stack_id = generate_stack_id(name)
@ -42,10 +44,16 @@ class CloudFormationBackend(BaseBackend):
self.stacks[stack_id] = new_stack
return new_stack
def describe_stacks(self, names):
def describe_stacks(self, name_or_stack_id):
stacks = self.stacks.values()
if names:
return [stack for stack in stacks if stack.name in names]
if name_or_stack_id:
for stack in stacks:
if stack.name == name_or_stack_id or stack.stack_id == name_or_stack_id:
return [stack]
deleted_stacks = self.deleted_stacks.values()
for stack in deleted_stacks:
if stack.stack_id == name_or_stack_id:
return [stack]
else:
return stacks
@ -68,6 +76,9 @@ class CloudFormationBackend(BaseBackend):
def delete_stack(self, name_or_stack_id):
if name_or_stack_id in self.stacks:
# Delete by stack id
stack = self.stacks.pop(name_or_stack_id, None)
stack.status = 'DELETE_COMPLETE'
self.deleted_stacks[stack.stack_id] = stack
return self.stacks.pop(name_or_stack_id, None)
else:
# Delete by stack name

View File

@ -27,8 +27,10 @@ class CloudFormationResponse(BaseResponse):
return json.dumps(stack_body)
def describe_stacks(self):
names = [value[0] for key, value in self.querystring.items() if "StackName" in key]
stacks = cloudformation_backend.describe_stacks(names)
stack_name_or_id = None
if self._get_param('StackName'):
stack_name_or_id = self.querystring.get('StackName')[0]
stacks = cloudformation_backend.describe_stacks(stack_name_or_id)
template = Template(DESCRIBE_STACKS_TEMPLATE)
return template.render(stacks=stacks)
@ -86,7 +88,7 @@ DESCRIBE_STACKS_TEMPLATE = """<DescribeStacksResult>
<StackName>{{ stack.name }}</StackName>
<StackId>{{ stack.stack_id }}</StackId>
<CreationTime>2010-07-27T22:28:28Z</CreationTime>
<StackStatus>CREATE_COMPLETE</StackStatus>
<StackStatus>{{ stack.status }}</StackStatus>
<DisableRollback>false</DisableRollback>
<Outputs>
{% for output in stack.stack_outputs %}
@ -108,7 +110,7 @@ LIST_STACKS_RESPONSE = """<ListStacksResponse>
{% for stack in stacks %}
<member>
<StackId>{{ stack.id }}</StackId>
<StackStatus>CREATE_IN_PROGRESS</StackStatus>
<StackStatus>{{ stack.status }}</StackStatus>
<StackName>{{ stack.name }}</StackName>
<CreationTime>2011-05-23T15:47:44Z</CreationTime>
<TemplateDescription>{{ stack.description }}</TemplateDescription>
@ -129,7 +131,7 @@ LIST_STACKS_RESOURCES_RESPONSE = """<DescribeStackResourcesResult>
<PhysicalResourceId>{{ resource.physical_resource_id }}</PhysicalResourceId>
<ResourceType>{{ resource.type }}</ResourceType>
<Timestamp>2010-07-27T22:27:28Z</Timestamp>
<ResourceStatus>CREATE_COMPLETE</ResourceStatus>
<ResourceStatus>{{ stack.status }}</ResourceStatus>
</member>
{% endfor %}
</StackResources>

View File

@ -47,6 +47,37 @@ def test_describe_stack_by_name():
stack.stack_name.should.equal('test_stack')
@mock_cloudformation
def test_describe_stack_by_stack_id():
conn = boto.connect_cloudformation()
conn.create_stack(
"test_stack",
template_body=dummy_template_json,
)
stack = conn.describe_stacks("test_stack")[0]
stack_by_id = conn.describe_stacks(stack.stack_id)[0]
stack_by_id.stack_id.should.equal(stack.stack_id)
stack_by_id.stack_name.should.equal("test_stack")
@mock_cloudformation
def test_describe_deleted_stack():
conn = boto.connect_cloudformation()
conn.create_stack(
"test_stack",
template_body=dummy_template_json,
)
stack = conn.describe_stacks("test_stack")[0]
stack_id = stack.stack_id
conn.delete_stack(stack.stack_id)
stack_by_id = conn.describe_stacks(stack_id)[0]
stack_by_id.stack_id.should.equal(stack.stack_id)
stack_by_id.stack_name.should.equal("test_stack")
stack_by_id.stack_status.should.equal("DELETE_COMPLETE")
@mock_cloudformation
def test_get_template_by_name():
conn = boto.connect_cloudformation()