Skip to content

Commit

Permalink
Fix AWS::EC2::VPC resource delete failure
Browse files Browse the repository at this point in the history
Check resource_id is not None before deleting the resource.

Change-Id: Ib8e8de5badc54c79cee248a9fcc86ebc50397b00
Closes-bug: #1365924
  • Loading branch information
huangtianhua committed Sep 10, 2014
1 parent 35d7465 commit d870865
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
11 changes: 7 additions & 4 deletions heat/engine/resources/vpc.py
Expand Up @@ -78,9 +78,8 @@ def handle_create(self):
router_props = {'name': self.physical_resource_name()}

net = client.create_network({'network': net_props})['network']
client.create_router({'router': router_props})['router']

self.resource_id_set(net['id'])
client.create_router({'router': router_props})['router']

@staticmethod
def network_for_vpc(client, network_id):
Expand Down Expand Up @@ -109,10 +108,14 @@ def check_create_complete(self, *args):
return neutron.NeutronResource.is_built(router)

def handle_delete(self):
if self.resource_id is None:
return
client = self.neutron()
router = self.router_for_vpc(client, self.resource_id)

try:
client.delete_router(router['id'])
router = self.router_for_vpc(client, self.resource_id)
if router:
client.delete_router(router['id'])
except Exception as ex:
self.client_plugin().ignore_not_found(ex)

Expand Down
19 changes: 19 additions & 0 deletions heat/tests/test_vpc.py
Expand Up @@ -346,6 +346,13 @@ class VPCTest(VPCTestBase):
Properties: {CidrBlock: '10.0.0.0/16'}
'''

def mock_create_network_failed(self):
self.vpc_name = utils.PhysName('test_stack', 'the_vpc')
neutronclient.Client.create_network(
{
'network': {'name': self.vpc_name}
}).AndRaise(NeutronClientException())

def test_vpc(self):
self.mock_create_network()
self.mock_delete_network()
Expand All @@ -358,6 +365,18 @@ def test_vpc(self):
scheduler.TaskRunner(vpc.delete)()
self.m.VerifyAll()

def test_vpc_delete_successful_if_created_failed(self):
self.mock_create_network_failed()
self.m.ReplayAll()

t = template_format.parse(self.test_template)
stack = self.parse_stack(t)
scheduler.TaskRunner(stack.create)()
self.assertEqual(stack.state, (stack.CREATE, stack.FAILED))
scheduler.TaskRunner(stack.delete)()

self.m.VerifyAll()


class SubnetTest(VPCTestBase):

Expand Down

0 comments on commit d870865

Please sign in to comment.