Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for invalid network in esi trunk create #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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