Cleanup validation logic in _get_requested_networks

'networks' in the server request body is a list of dicts
that can take a fixed_ip, port id or network id. There are
semantic rules for the combinations of these which are checked
in the helper method _get_requested_networks. The network id
validation logic is a bit convoluted where it's placed though,
so this change cleans that up and moves it to it's own method.

Note the main difference in nesting logic. You can't request
a port and network on the same nic (dict entry in the networks
list). So the conditional logic is such that you either have a
port or a network in a single request. Before this change, the
network id validation was happening outside that conditional,
and checked a second time if port was requested before validating
the network id. Since we already have that condition, this
change moves the network id validation under the condition where
a port is not requested (so a network id must be).

There are no test changes since this is just cleaning up the code
and also shows that this doesn't change the overall results of
the validation.

Change-Id: I466f2273a4ce02279b942f7ada264a3da97dfe92
This commit is contained in:
Matt Riedemann 2016-05-15 12:20:16 -04:00
parent 9a05d38f48
commit 5657dc5006

View File

@ -446,6 +446,35 @@ class ServersController(wsgi.Controller):
req.cache_db_instance(instance)
return instance
@staticmethod
def _validate_network_id(net_id, network_uuids):
"""Validates that a requested network id.
This method performs two checks:
1. That the network id is in the proper uuid format.
2. That the network is not a duplicate when using nova-network.
:param net_id: The network id to validate.
:param network_uuids: A running list of requested network IDs that have
passed validation already.
:raises: webob.exc.HTTPBadRequest if validation fails
"""
if not uuidutils.is_uuid_like(net_id):
# NOTE(mriedem): Neutron would allow a network id with a br- prefix
# back in Folsom so continue to honor that.
# TODO(mriedem): Need to figure out if this is still a valid case.
br_uuid = net_id.split('-', 1)[-1]
if not uuidutils.is_uuid_like(br_uuid):
msg = _("Bad networks format: network uuid is "
"not in proper format (%s)") % net_id
raise exc.HTTPBadRequest(explanation=msg)
# duplicate networks are allowed only for neutron v2.0
if net_id in network_uuids and not utils.is_neutron():
expl = _("Duplicate networks (%s) are not allowed") % net_id
raise exc.HTTPBadRequest(explanation=expl)
def _get_requested_networks(self, requested_networks):
"""Create a list of requested networks from the networks attribute."""
networks = []
@ -474,24 +503,10 @@ class ServersController(wsgi.Controller):
raise exc.HTTPBadRequest(explanation=msg)
else:
request.network_id = network['uuid']
self._validate_network_id(
request.network_id, network_uuids)
network_uuids.append(request.network_id)
if (not request.port_id and
not uuidutils.is_uuid_like(request.network_id)):
br_uuid = request.network_id.split('-', 1)[-1]
if not uuidutils.is_uuid_like(br_uuid):
msg = _("Bad networks format: network uuid is "
"not in proper format "
"(%s)") % request.network_id
raise exc.HTTPBadRequest(explanation=msg)
# duplicate networks are allowed only for neutron v2.0
if (not utils.is_neutron() and request.network_id and
request.network_id in network_uuids):
expl = (_("Duplicate networks"
" (%s) are not allowed") %
request.network_id)
raise exc.HTTPBadRequest(explanation=expl)
network_uuids.append(request.network_id)
networks.append(request)
except KeyError as key:
expl = _('Bad network format: missing %s') % key