Fixes poor error message when deleting networks

This patch fixes the poor error message when deleting networks when
the network is being used by an active instance. The old error
message upon network deletion used to show the network UUID, which
was a little inconvenient for the end-user. So, with the help of
this patch, any error in deleting the network will show the
network name instead of network UUID.

Change-Id: I38a8a0260860383eee9d3e3b28cc4bc3ec65b255
Closes-bug: #1279485
This commit is contained in:
nikunj2512 2014-10-06 15:32:02 +05:30
parent 6f0f18334b
commit 0d085cd422
2 changed files with 39 additions and 29 deletions

View File

@ -59,22 +59,24 @@ class DeleteNetwork(policy.PolicyTargetMixin, CheckNetworkEditable,
policy_rules = (("network", "delete_network"),)
def delete(self, request, network_id):
network_name = network_id
try:
# Retrieve existing subnets belonging to the network.
subnets = api.neutron.subnet_list(request, network_id=network_id)
LOG.debug('Network %s has subnets: %s' %
(network_id, [s.id for s in subnets]))
for s in subnets:
api.neutron.subnet_delete(request, s.id)
LOG.debug('Deleted subnet %s' % s.id)
# Retrieve the network list.
network = api.neutron.network_get(request, network_id,
expand_subnet=False)
network_name = network.name
LOG.debug('Network %(network_id)s has subnets: %(subnets)s',
{'network_id': network_id, 'subnets': network.subnets})
for subnet_id in network.subnets:
api.neutron.subnet_delete(request, subnet_id)
LOG.debug('Deleted subnet %s', subnet_id)
api.neutron.network_delete(request, network_id)
LOG.debug('Deleted network %s successfully' % network_id)
LOG.debug('Deleted network %s successfully', network_id)
except Exception:
msg = _('Failed to delete network %s') % network_id
LOG.info(msg)
msg = _('Failed to delete network %s')
LOG.info(msg, network_id)
redirect = reverse("horizon:project:networks:index")
exceptions.handle(request, msg, redirect=redirect)
exceptions.handle(request, msg % network_name, redirect=redirect)
class CreateNetwork(tables.LinkAction):

View File

@ -710,11 +710,16 @@ class NetworkTests(test.TestCase):
self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.neutron: ('network_list',
'subnet_list',
@test.create_stubs({api.neutron: ('network_get',
'network_list',
'network_delete')})
def test_delete_network_no_subnet(self):
network = self.networks.first()
network.subnets = []
api.neutron.network_get(IsA(http.HttpRequest),
network.id,
expand_subnet=False)\
.AndReturn(network)
api.neutron.network_list(IsA(http.HttpRequest),
tenant_id=network.tenant_id,
shared=False)\
@ -722,33 +727,33 @@ class NetworkTests(test.TestCase):
api.neutron.network_list(IsA(http.HttpRequest),
shared=True)\
.AndReturn([])
api.neutron.subnet_list(IsA(http.HttpRequest), network_id=network.id)\
.AndReturn([])
api.neutron.network_delete(IsA(http.HttpRequest), network.id)
self.mox.ReplayAll()
form_data = {'action': 'networks__delete__%s' % network.id}
res = self.client.post(INDEX_URL, form_data)
self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.neutron: ('network_list',
'subnet_list',
@test.create_stubs({api.neutron: ('network_get',
'network_list',
'network_delete',
'subnet_delete')})
def test_delete_network_with_subnet(self):
network = self.networks.first()
subnet = self.subnets.first()
network.subnets = [subnet.id for subnet in network.subnets]
subnet_id = network.subnets[0]
api.neutron.network_get(IsA(http.HttpRequest),
network.id,
expand_subnet=False)\
.AndReturn(network)
api.neutron.network_list(IsA(http.HttpRequest),
tenant_id=network.tenant_id,
shared=False)\
.AndReturn([network])
api.neutron.network_list(IsA(http.HttpRequest), shared=True)\
.AndReturn([])
api.neutron.subnet_list(IsA(http.HttpRequest), network_id=network.id)\
.AndReturn([subnet])
api.neutron.subnet_delete(IsA(http.HttpRequest), subnet.id)
api.neutron.subnet_delete(IsA(http.HttpRequest), subnet_id)
api.neutron.network_delete(IsA(http.HttpRequest), network.id)
self.mox.ReplayAll()
@ -758,13 +763,18 @@ class NetworkTests(test.TestCase):
self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.neutron: ('network_list',
'subnet_list',
@test.create_stubs({api.neutron: ('network_get',
'network_list',
'network_delete',
'subnet_delete')})
def test_delete_network_exception(self):
network = self.networks.first()
subnet = self.subnets.first()
network.subnets = [subnet.id for subnet in network.subnets]
subnet_id = network.subnets[0]
api.neutron.network_get(IsA(http.HttpRequest),
network.id,
expand_subnet=False)\
.AndReturn(network)
api.neutron.network_list(IsA(http.HttpRequest),
tenant_id=network.tenant_id,
shared=False)\
@ -772,9 +782,7 @@ class NetworkTests(test.TestCase):
api.neutron.network_list(IsA(http.HttpRequest),
shared=True)\
.AndReturn([])
api.neutron.subnet_list(IsA(http.HttpRequest), network_id=network.id)\
.AndReturn([subnet])
api.neutron.subnet_delete(IsA(http.HttpRequest), subnet.id)
api.neutron.subnet_delete(IsA(http.HttpRequest), subnet_id)
api.neutron.network_delete(IsA(http.HttpRequest), network.id)\
.AndRaise(self.exceptions.neutron)