Skip to content

Commit

Permalink
Improve error handling for invalid network in esi trunk create
Browse files Browse the repository at this point in the history
  • Loading branch information
skanthed committed Dec 16, 2024
1 parent a812654 commit 5d627e2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions esiclient/tests/unit/v1/test_trunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
10 changes: 10 additions & 0 deletions esiclient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import subprocess

from openstack import exceptions


def get_network_display_name(network):
"""Return Neutron network name with vlan, if any
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions esiclient/v1/trunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 5d627e2

Please sign in to comment.