Throw meaningful exception bad network config

In the unmanaged driver, create port will fail with a KeyError
if a network_id is used that isn't configured properly in
neutron.conf (ie - bridge isn't defined). Added checks and
meaningful error for users.

Change-Id: Ief9c2371100063ff679858abc5fdf7cc24ec9dda
Closes-Bug: #1611440
JIRA:NCP-2012
This commit is contained in:
Kyle Haley 2016-08-09 08:33:33 -07:00
parent 229843006f
commit c6f5207967
2 changed files with 16 additions and 2 deletions

View File

@ -12,6 +12,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
#
from neutron_lib import exceptions as n_exc
from oslo_log import log as logging
@ -58,8 +59,15 @@ class UnmanagedDriver(base.BaseDriver):
def create_port(self, context, network_id, port_id, **kwargs):
LOG.info("create_port %s %s %s" % (context.tenant_id, network_id,
port_id))
bridge_name = STRATEGY.get_network(network_id)["bridge"]
return {"uuid": port_id, "bridge": bridge_name}
network = STRATEGY.get_network(network_id)
if network and "bridge" in network:
return {"uuid": port_id, "bridge": network["bridge"]}
else:
raise n_exc.BadRequest(resource="ports",
msg=("Bridge is either not configured for"
" network or this network is not"
" defined in default_net_strategy:"
" %s" % network_id))
def update_port(self, context, port_id, **kwargs):
LOG.info("update_port %s %s" % (context.tenant_id, port_id))

View File

@ -18,6 +18,7 @@ import uuid
import mock
import netaddr
from neutron_lib import exceptions as n_exc
from quark.drivers import unmanaged
from quark import network_strategy
@ -65,6 +66,11 @@ class TestUnmanagedDriver(test_base.TestBase):
self.driver.create_port(context=self.context,
network_id="public_network", port_id=2)
def test_create_port_raises(self):
with self.assertRaises(n_exc.BadRequest):
self.driver.create_port(context=self.context,
network_id="bad_network", port_id=2)
def test_update_port(self):
self.driver.update_port(context=self.context,
network_id="public_network", port_id=2)