network.common.NetworkAndComputeShowOne: catch HttpException
Problem: if a user issue the cmd 'openstack floating ip create public' and has already maxed his quota for FIP, OSC exits with a not so useful message: >jordan@jordan-XPS13-9333:~ $ openstack floating ip create public >HttpException: Conflict This patches catch the HttpException earlier and prints a more verbose message: > jordan@jordan-XPS13-9333:~ $ openstack floating ip create public > Error while executing command: Quota exceeded for resources: ['floatingip'] Change-Id: I7c87524d871d230d92f007c32e06439b34c7194a
This commit is contained in:
		@@ -14,6 +14,7 @@
 | 
			
		||||
import abc
 | 
			
		||||
import logging
 | 
			
		||||
 | 
			
		||||
import openstack.exceptions
 | 
			
		||||
from osc_lib.command import command
 | 
			
		||||
from osc_lib import exceptions
 | 
			
		||||
import six
 | 
			
		||||
@@ -181,12 +182,16 @@ class NetworkAndComputeShowOne(command.ShowOne):
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    def take_action(self, parsed_args):
 | 
			
		||||
        try:
 | 
			
		||||
            if self.app.client_manager.is_network_endpoint_enabled():
 | 
			
		||||
            return self.take_action_network(self.app.client_manager.network,
 | 
			
		||||
                                            parsed_args)
 | 
			
		||||
                return self.take_action_network(
 | 
			
		||||
                    self.app.client_manager.network, parsed_args)
 | 
			
		||||
            else:
 | 
			
		||||
            return self.take_action_compute(self.app.client_manager.compute,
 | 
			
		||||
                                            parsed_args)
 | 
			
		||||
                return self.take_action_compute(
 | 
			
		||||
                    self.app.client_manager.compute, parsed_args)
 | 
			
		||||
        except openstack.exceptions.HttpException as exc:
 | 
			
		||||
            msg = _("Error while executing command: %s") % exc.message
 | 
			
		||||
            raise exceptions.CommandError(msg)
 | 
			
		||||
 | 
			
		||||
    def get_parser(self, prog_name):
 | 
			
		||||
        LOG.debug('get_parser(%s)', prog_name)
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,8 @@
 | 
			
		||||
import argparse
 | 
			
		||||
import mock
 | 
			
		||||
 | 
			
		||||
import openstack
 | 
			
		||||
from openstackclient.common import exceptions
 | 
			
		||||
from openstackclient.network import common
 | 
			
		||||
from openstackclient.tests.unit import utils
 | 
			
		||||
 | 
			
		||||
@@ -172,3 +174,15 @@ class TestNetworkAndComputeShowOne(TestNetworkAndCompute):
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        super(TestNetworkAndComputeShowOne, self).setUp()
 | 
			
		||||
        self.cmd = FakeNetworkAndComputeShowOne(self.app, self.namespace)
 | 
			
		||||
 | 
			
		||||
    def test_take_action_with_http_exception(self):
 | 
			
		||||
        with mock.patch.object(self.cmd, 'take_action_network') as m_action:
 | 
			
		||||
            m_action.side_effect = openstack.exceptions.HttpException("bar")
 | 
			
		||||
            self.assertRaisesRegex(exceptions.CommandError, "bar",
 | 
			
		||||
                                   self.cmd.take_action, mock.Mock())
 | 
			
		||||
 | 
			
		||||
        self.app.client_manager.network_endpoint_enabled = False
 | 
			
		||||
        with mock.patch.object(self.cmd, 'take_action_compute') as m_action:
 | 
			
		||||
            m_action.side_effect = openstack.exceptions.HttpException("bar")
 | 
			
		||||
            self.assertRaisesRegex(exceptions.CommandError, "bar",
 | 
			
		||||
                                   self.cmd.take_action, mock.Mock())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user