From 5d627e226d402301404097537fdbfba49b38e3aa Mon Sep 17 00:00:00 2001 From: skanthed Date: Mon, 16 Dec 2024 10:36:14 -0500 Subject: [PATCH] Improve error handling for invalid network in esi trunk create --- esiclient/tests/unit/v1/test_trunk.py | 19 +++++++++++++++++++ esiclient/utils.py | 10 ++++++++++ esiclient/v1/trunk.py | 7 +++++++ 3 files changed, 36 insertions(+) diff --git a/esiclient/tests/unit/v1/test_trunk.py b/esiclient/tests/unit/v1/test_trunk.py index db7565b..cc978f9 100644 --- a/esiclient/tests/unit/v1/test_trunk.py +++ b/esiclient/tests/unit/v1/test_trunk.py @@ -223,6 +223,25 @@ def test_take_action(self, mock_create_trunk): ['network2', 'network3'] ) + def test_take_action_native_network_not_found(self): + self.app.client_manager.network.find_network.return_value = None + + arglist = ['trunk', '--native-network', 'nonexistent-network'] + verifylist = [] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.assertRaisesRegex( + exceptions.CommandError, + "ERROR: Native network 'nonexistent-network' could not be found. " + "Please check the name or ID.", + self.cmd.take_action, parsed_args + ) + + self.app.client_manager.network.find_network.assert_called_once_with( + "nonexistent-network" + ) + class TestDelete(base.TestCommand): diff --git a/esiclient/utils.py b/esiclient/utils.py index 1950c3d..a2409c3 100644 --- a/esiclient/utils.py +++ b/esiclient/utils.py @@ -12,6 +12,8 @@ import subprocess +from openstack import exceptions + def get_network_display_name(network): """Return Neutron network name with vlan, if any @@ -221,6 +223,14 @@ def create_trunk(neutron_client, trunk_name, network, tagged_networks=[]): sub_ports = [] for tagged_network_name in tagged_networks: tagged_network = neutron_client.find_network(tagged_network_name) + + if not tagged_network: + raise exceptions.ResourceNotFound( + "Tagged network '{}' could not be found.".format( + tagged_network_name + ) + ) + sub_port_name = get_port_name( tagged_network.name, prefix=trunk_name, suffix='sub-port') sub_port = neutron_client.create_port( diff --git a/esiclient/v1/trunk.py b/esiclient/v1/trunk.py index 3910641..05f9b7c 100644 --- a/esiclient/v1/trunk.py +++ b/esiclient/v1/trunk.py @@ -82,6 +82,13 @@ def take_action(self, parsed_args): trunk_name = parsed_args.name network = neutron_client.find_network(parsed_args.native_network) + + if network is None: + raise exceptions.CommandError( + "ERROR: Native network '{}' could not be found. Please check " + "the name or ID.".format(parsed_args.native_network) + ) + tagged_networks = parsed_args.tagged_networks trunk, trunk_port = utils.create_trunk(