filter the subnets which already in the router

When we already add the subnet to router,
if we click 'Add Interface' again, we can see
the subnet also can select, but if we real to
add the subnet, it report a error. We can hidden
the subnet if it already add to the router.

Closes-bug:#1417892

Change-Id: I485608d267b9b235c1e83a990e330dd1ca98f17e
This commit is contained in:
tinytmy 2015-02-09 21:37:22 +08:00
parent 8ae838c836
commit ca596187a8
2 changed files with 26 additions and 11 deletions

View File

@ -48,14 +48,20 @@ class AddInterface(forms.SelfHandlingForm):
def populate_subnet_id_choices(self, request):
tenant_id = self.request.user.tenant_id
networks = []
router_subnet_ids = []
router_id = request.REQUEST.get('router_id',
self.initial.get('router_id'))
try:
networks = api.neutron.network_list_for_tenant(request, tenant_id)
if router_id:
ports = api.neutron.port_list(request, device_id=router_id)
router_subnet_ids = [fixed_ip["subnet_id"] for port in ports
for fixed_ip in port.fixed_ips]
except Exception as e:
msg = _('Failed to get network list %s') % e
LOG.info(msg)
messages.error(request, msg)
router_id = request.REQUEST.get('router_id',
self.initial.get('router_id'))
if router_id:
redirect = reverse(self.failure_url, args=[router_id])
else:
@ -69,7 +75,8 @@ class AddInterface(forms.SelfHandlingForm):
choices += [(subnet.id,
'%s%s (%s)' % (net_name, subnet.cidr,
subnet.name or subnet.id))
for subnet in n['subnets']]
for subnet in n['subnets']
if subnet.id not in router_subnet_ids]
if choices:
choices.insert(0, ("", _("Select Subnet")))
else:

View File

@ -524,6 +524,8 @@ class RouterActionTests(RouterMixin, test.TestCase):
# mock APIs used to show router detail
api.neutron.router_get(IsA(http.HttpRequest), router.id)\
.AndReturn(router)
api.neutron.port_list(IsA(http.HttpRequest), device_id=router.id)\
.AndReturn([])
self._mock_network_list(router['tenant_id'])
self.mox.ReplayAll()
@ -542,13 +544,15 @@ class RouterActionTests(RouterMixin, test.TestCase):
@test.create_stubs({api.neutron: ('router_get',
'router_add_interface',
'port_get',
'network_list')})
'network_list',
'port_list')})
def test_router_addinterface(self):
self._test_router_addinterface()
@test.create_stubs({api.neutron: ('router_get',
'router_add_interface',
'network_list')})
'network_list',
'port_list')})
def test_router_addinterface_exception(self):
self._test_router_addinterface(raise_error=True)
@ -590,29 +594,33 @@ class RouterActionTests(RouterMixin, test.TestCase):
@test.create_stubs({api.neutron: ('router_add_interface', 'subnet_get',
'port_create',
'router_get', 'network_list')})
'router_get', 'network_list',
'port_list')})
def test_router_addinterface_ip_addr(self):
self._test_router_addinterface_ip_addr()
@test.create_stubs({api.neutron: ('subnet_get',
'router_get', 'network_list')})
@test.create_stubs({api.neutron: ('subnet_get', 'router_get',
'network_list', 'port_list')})
def test_router_addinterface_ip_addr_exception_subnet_get(self):
self._test_router_addinterface_ip_addr(errors=['subnet_get'])
@test.create_stubs({api.neutron: ('subnet_get', 'port_create',
'router_get', 'network_list')})
'router_get', 'network_list',
'port_list')})
def test_router_addinterface_ip_addr_exception_port_create(self):
self._test_router_addinterface_ip_addr(errors=['port_create'])
@test.create_stubs({api.neutron: ('router_add_interface', 'subnet_get',
'port_create', 'port_delete',
'router_get', 'network_list')})
'router_get', 'network_list',
'port_list')})
def test_router_addinterface_ip_addr_exception_add_interface(self):
self._test_router_addinterface_ip_addr(errors=['add_interface'])
@test.create_stubs({api.neutron: ('router_add_interface', 'subnet_get',
'port_create', 'port_delete',
'router_get', 'network_list')})
'router_get', 'network_list',
'port_list')})
def test_router_addinterface_ip_addr_exception_port_delete(self):
self._test_router_addinterface_ip_addr(errors=['add_interface',
'port_delete'])