# NOTE(armando-migliaccio): in the base case this is invariant
return DEVICE_OWNER_ROUTER_INTF
- def _validate_interface_info(self, interface_info):
+ def _validate_interface_info(self, interface_info, for_removal=False):
port_id_specified = interface_info and 'port_id' in interface_info
subnet_id_specified = interface_info and 'subnet_id' in interface_info
if not (port_id_specified or subnet_id_specified):
msg = _("Either subnet_id or port_id must be specified")
raise n_exc.BadRequest(resource='router', msg=msg)
- if port_id_specified and subnet_id_specified:
- msg = _("Cannot specify both subnet-id and port-id")
- raise n_exc.BadRequest(resource='router', msg=msg)
+ if not for_removal:
+ if port_id_specified and subnet_id_specified:
+ msg = _("Cannot specify both subnet-id and port-id")
+ raise n_exc.BadRequest(resource='router', msg=msg)
return port_id_specified, subnet_id_specified
def _add_interface_by_port(self, context, router, port_id, owner):
if add_by_port:
port = self._add_interface_by_port(
context, router, interface_info['port_id'], device_owner)
- elif add_by_sub:
+ # add_by_subnet is not used here, because the validation logic of
+ # _validate_interface_info ensures that either of add_by_* is True.
+ else:
port = self._add_interface_by_subnet(
context, router, interface_info['subnet_id'], device_owner)
subnet_id=subnet_id)
def remove_router_interface(self, context, router_id, interface_info):
- if not interface_info:
- msg = _("Either subnet_id or port_id must be specified")
- raise n_exc.BadRequest(resource='router', msg=msg)
+ remove_by_port, remove_by_subnet = (
+ self._validate_interface_info(interface_info, for_removal=True)
+ )
port_id = interface_info.get('port_id')
subnet_id = interface_info.get('subnet_id')
device_owner = self._get_device_owner(context, router_id)
- if port_id:
+ if remove_by_port:
port, subnet = self._remove_interface_by_port(context, router_id,
port_id, subnet_id,
device_owner)
- elif subnet_id:
+ # remove_by_subnet is not used here, because the validation logic of
+ # _validate_interface_info ensures that at least one of remote_by_*
+ # is True.
+ else:
port, subnet = self._remove_interface_by_subnet(
context, router_id, subnet_id, device_owner)
return router_interface_info
def remove_router_interface(self, context, router_id, interface_info):
- if not interface_info:
- msg = _("Either subnet_id or port_id must be specified")
- raise n_exc.BadRequest(resource='router', msg=msg)
-
+ remove_by_port, remove_by_subnet = (
+ self._validate_interface_info(interface_info, for_removal=True)
+ )
port_id = interface_info.get('port_id')
subnet_id = interface_info.get('subnet_id')
router = self._get_router(context, router_id)
device_owner = self._get_device_owner(context, router)
- if port_id:
+ if remove_by_port:
port, subnet = self._remove_interface_by_port(
context, router_id, port_id, subnet_id, device_owner)
- elif subnet_id:
+ # remove_by_subnet is not used here, because the validation logic of
+ # _validate_interface_info ensures that at least one of remote_by_*
+ # is True.
+ else:
port, subnet = self._remove_interface_by_subnet(
context, router_id, subnet_id, device_owner)
interface_data = {}
if subnet_id:
interface_data.update({'subnet_id': subnet_id})
- if port_id and (action != 'add' or not subnet_id):
+ if port_id:
interface_data.update({'port_id': port_id})
req = self.new_action_request('routers', interface_data, router_id,
expected_code=exc.
HTTPBadRequest.code)
+ def test_router_add_interface_with_both_ids_returns_400(self):
+ with self.router() as r:
+ with self.subnet() as s:
+ with self.port(subnet=s) as p:
+ self._router_interface_action('add',
+ r['router']['id'],
+ s['subnet']['id'],
+ p['port']['id'],
+ expected_code=exc.
+ HTTPBadRequest.code)
+
def test_router_add_gateway_dup_subnet1_returns_400(self):
with self.router() as r:
with self.subnet() as s:
None,
p['port']['id'])
+ def test_router_remove_interface_nothing_returns_400(self):
+ with self.router() as r:
+ with self.subnet() as s:
+ with self.port(subnet=s) as p:
+ self._router_interface_action('add',
+ r['router']['id'],
+ None,
+ p['port']['id'])
+ self._router_interface_action('remove',
+ r['router']['id'],
+ None,
+ None,
+ exc.HTTPBadRequest.code)
+ #remove properly to clean-up
+ self._router_interface_action('remove',
+ r['router']['id'],
+ None,
+ p['port']['id'])
+
def test_router_remove_interface_returns_200(self):
with self.router() as r:
with self.port() as p:
p['port']['id'],
expected_body=body)
+ def test_router_remove_interface_with_both_ids_returns_200(self):
+ with self.router() as r:
+ with self.subnet() as s:
+ with self.port(subnet=s) as p:
+ self._router_interface_action('add',
+ r['router']['id'],
+ None,
+ p['port']['id'])
+ self._router_interface_action('remove',
+ r['router']['id'],
+ s['subnet']['id'],
+ p['port']['id'])
+
def test_router_remove_interface_wrong_port_returns_404(self):
with self.router() as r:
with self.subnet():