Handle ResourceNotFound when resource is not found

When creating, updating or deleting a resource which is not
existing, senlinclient desired HTTPNotFound in the current
implementation. But when server requests go to openstacksdk,
openstacksdk will convert all notfound exceptions into its
own exception type ResourceNotFound. So Senlin client should
handle this exception instead.
There is an exception when listing nodes of a cluster, ResourceNotFound
exception will be raised only when printing the output, so there is
no need to catch it when getting the nodes.

Change-Id: Ib80d749e62ed41ae07734a784d309e47c0d3fcca
This commit is contained in:
xu-haiwei
2016-02-05 16:27:00 +09:00
parent 100720d913
commit 18eac0d3c6
2 changed files with 45 additions and 60 deletions

View File

@@ -15,6 +15,7 @@ import mock
import six
import testtools
from openstack import exceptions as oexc
from senlinclient.common import exc
from senlinclient.common.i18n import _
from senlinclient.common import utils
@@ -95,12 +96,12 @@ class ShellTest(testtools.TestCase):
'format': 'json'
}
args = self._make_args(args)
ex = exc.HTTPNotFound
ex = oexc.ResourceNotFound
service.get_profile_type = mock.Mock(side_effect=ex)
ex = self.assertRaises(exc.CommandError,
sh.do_profile_type_show,
service, args)
self.assertEqual(_('Profile Type wrong_type not found.'),
self.assertEqual(_('Profile Type not found: wrong_type'),
six.text_type(ex))
@mock.patch.object(utils, 'print_list')
@@ -154,7 +155,7 @@ class ShellTest(testtools.TestCase):
def test_show_profile_not_found(self):
service = mock.Mock()
ex = exc.HTTPNotFound
ex = oexc.ResourceNotFound
service.get_profile.side_effect = ex
profile_id = 'wrong_id'
ex = self.assertRaises(exc.CommandError,
@@ -257,7 +258,7 @@ class ShellTest(testtools.TestCase):
args = copy.deepcopy(self.profile_args)
args = self._make_args(args)
args.id = 'FAKE_ID'
ex = exc.HTTPNotFound
ex = oexc.ResourceNotFound
service.get_profile.side_effect = ex
ex = self.assertRaises(exc.CommandError,
sh.do_profile_update,
@@ -278,13 +279,12 @@ class ShellTest(testtools.TestCase):
args = {'id': ['profile1', 'profile2']}
args = self._make_args(args)
sh.do_profile_delete(service, args)
ex = Exception()
service.delete_profile.side_effect = ex
service.delete_profile.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(exc.CommandError,
sh.do_profile_delete,
service, args)
self.assertEqual(_('Failed to delete some of the specified '
'profile(s).'), six.text_type(ex))
msg = _("Failed to delete some of the specified profile(s).")
self.assertEqual(msg, six.text_type(ex))
service.delete_profile.assert_called_with('profile2', False)
@mock.patch.object(utils, 'print_list')
@@ -326,10 +326,10 @@ class ShellTest(testtools.TestCase):
args = {'type_name': 'BAD'}
args = self._make_args(args)
service.get_policy_type.side_effect = exc.HTTPNotFound
service.get_policy_type.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(exc.CommandError,
sh.do_policy_type_show, service, args)
msg = _('Policy type BAD not found.')
msg = _('Policy type not found: BAD')
self.assertEqual(msg, six.text_type(ex))
@mock.patch.object(utils, 'print_list')
@@ -400,7 +400,7 @@ class ShellTest(testtools.TestCase):
receiver_id = 'wrong_id'
receiver.id = receiver_id
service.get_receiver.side_effect = exc.HTTPNotFound
service.get_receiver.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(exc.CommandError,
sh._show_receiver, service, receiver_id)
self.assertEqual(_('Receiver not found: wrong_id'), six.text_type(ex))
@@ -452,10 +452,10 @@ class ShellTest(testtools.TestCase):
args = {'id': ['receiver_id']}
args = self._make_args(args)
service.delete_receiver.side_effect = exc.HTTPNotFound
service.delete_receiver.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(exc.CommandError,
sh.do_receiver_delete, service, args)
msg = _('Failed to delete some of the specified receiver(s).')
msg = _("Failed to delete some of the specified receiver(s).")
self.assertEqual(msg, six.text_type(ex))
@mock.patch.object(utils, 'print_list')
@@ -508,7 +508,7 @@ class ShellTest(testtools.TestCase):
formatters=formatters)
# policy not found
ex = exc.HTTPNotFound
ex = oexc.ResourceNotFound
service.get_policy.side_effect = ex
ex = self.assertRaises(exc.CommandError,
sh._show_policy,
@@ -580,10 +580,10 @@ class ShellTest(testtools.TestCase):
args = {'id': ['policy_id']}
args = self._make_args(args)
service.delete_policy.side_effect = exc.HTTPNotFound
service.delete_policy.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(exc.CommandError,
sh.do_policy_delete, service, args)
msg = _('Failed to delete some of the specified policy(s).')
msg = _("Failed to delete some of the specified policy(s).")
self.assertEqual(msg, six.text_type(ex))
@mock.patch.object(utils, 'print_list')
@@ -668,7 +668,7 @@ class ShellTest(testtools.TestCase):
args = {'id': ['cluster_id']}
args = self._make_args(args)
service.delete_cluster.side_effect = exc.HTTPNotFound
service.delete_cluster.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(exc.CommandError,
sh.do_cluster_delete, service, args)
msg = _('Failed to delete some of the specified clusters.')
@@ -734,13 +734,6 @@ class ShellTest(testtools.TestCase):
formatters=formatters,
sortby_index=5)
# node not found
service.nodes.side_effect = exc.HTTPNotFound
ex = self.assertRaises(exc.CommandError,
sh.do_cluster_node_list, service, args)
msg = _('No node matching criteria is found')
self.assertEqual(msg, six.text_type(ex))
def test_do_cluster_node_add(self):
service = mock.Mock()
args = {
@@ -1130,7 +1123,7 @@ class ShellTest(testtools.TestCase):
def test_do_node_delete_not_found(self):
service = mock.Mock()
ex = exc.HTTPNotFound
ex = oexc.ResourceNotFound
service.delete_node.side_effect = ex
args = self._make_args({'id': ['node1']})
@@ -1217,7 +1210,7 @@ class ShellTest(testtools.TestCase):
args = self._make_args({'id': 'FAKE'})
# event not found
ex = exc.CommandError
service.get_event.side_effect = exc.HTTPNotFound
service.get_event.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(ex,
sh.do_event_show,
service, args)
@@ -1276,9 +1269,9 @@ class ShellTest(testtools.TestCase):
service = mock.Mock()
args = self._make_args({'id': 'fake_id'})
service.get_action.side_effect = exc.HTTPNotFound
service.get_action.side_effect = oexc.ResourceNotFound
ex = self.assertRaises(exc.CommandError,
sh.do_action_show,
service, args)
msg = _('Action fake_id is not found')
msg = _('Action not found: fake_id')
self.assertEqual(msg, six.text_type(ex))

View File

@@ -12,6 +12,7 @@
import logging
from openstack import exceptions as sdk_exc
from senlinclient.common import exc
from senlinclient.common.i18n import _
from senlinclient.common import utils
@@ -57,9 +58,9 @@ def do_profile_type_show(service, args):
"""Get the details about a profile type."""
try:
res = service.get_profile_type(args.type_name)
except exc.HTTPNotFound:
except sdk_exc.ResourceNotFound:
raise exc.CommandError(
_('Profile Type %s not found.') % args.type_name)
_('Profile Type not found: %s') % args.type_name)
pt = res.to_dict()
@@ -111,7 +112,7 @@ def do_profile_list(service, args=None):
def _show_profile(service, profile_id):
try:
profile = service.get_profile(profile_id)
except exc.HTTPNotFound:
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Profile not found: %s') % profile_id)
formatters = {
@@ -189,7 +190,7 @@ def do_profile_update(service, args):
# Find the profile first, we need its id
try:
profile = service.get_profile(args.id)
except exc.HTTPNotFound:
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Profile not found: %s') % args.id)
service.update_profile(profile.id, **params)
_show_profile(service, profile.id)
@@ -232,9 +233,8 @@ def do_policy_type_show(service, args):
"""Get the details about a policy type."""
try:
res = service.get_policy_type(args.type_name)
except exc.HTTPNotFound:
raise exc.CommandError(
_('Policy type %s not found.') % args.type_name)
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Policy type not found: %s') % args.type_name)
pt = res.to_dict()
if args.format:
@@ -284,7 +284,7 @@ def do_policy_list(service, args=None):
def _show_policy(service, policy_id):
try:
policy = service.get_policy(policy_id)
except exc.HTTPNotFound:
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Policy not found: %s') % policy_id)
formatters = {
@@ -342,7 +342,7 @@ def do_policy_delete(service, args):
for pid in args.id:
try:
service.delete_policy(pid, False)
except exc.HTTPNotFound as ex:
except Exception as ex:
failure_count += 1
print(ex)
if failure_count > 0:
@@ -401,8 +401,8 @@ def do_cluster_list(service, args=None):
def _show_cluster(service, cluster_id):
try:
cluster = service.get_cluster(cluster_id)
except exc.HTTPNotFound:
raise exc.CommandError(_('Cluster %s is not found') % cluster_id)
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Cluster not found: %s') % cluster_id)
formatters = {
'metadata': utils.json_formatter,
@@ -456,13 +456,12 @@ def do_cluster_delete(service, args):
for cid in args.id:
try:
service.delete_cluster(cid, False)
except exc.HTTPNotFound as ex:
except Exception as ex:
failure_count += 1
print(ex)
if failure_count > 0:
msg = _('Failed to delete some of the specified clusters.')
raise exc.CommandError(msg)
print('Request accepted')
@@ -524,12 +523,7 @@ def do_cluster_node_list(service, args):
if args.filters:
queries.update(utils.format_parameters(args.filters))
try:
nodes = service.nodes(**queries)
except exc.HTTPNotFound:
msg = _('No node matching criteria is found')
raise exc.CommandError(msg)
nodes = service.nodes(**queries)
if not args.full_id:
formatters = {
'id': lambda x: x.id[:8],
@@ -833,9 +827,8 @@ def _show_node(service, node_id, show_details=False):
args = {'show_details': True} if show_details else None
try:
node = service.get_node(node_id, args=args)
except exc.HTTPNotFound:
msg = _('Node %s is not found') % node_id
raise exc.CommandError(msg)
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Node not found: %s') % node_id)
formatters = {
'metadata': utils.json_formatter,
@@ -894,9 +887,9 @@ def do_node_delete(service, args):
for nid in args.id:
try:
service.delete_node(nid, False)
except exc.HTTPNotFound:
except Exception as ex:
failure_count += 1
print('Node id "%s" not found' % nid)
print(ex)
if failure_count > 0:
msg = _('Failed to delete some of the specified nodes.')
raise exc.CommandError(msg)
@@ -921,7 +914,7 @@ def do_node_update(service, args):
# Find the node first, we need its UUID
try:
node = service.get_node(args.id)
except exc.HTTPNotFound:
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Node not found: %s') % args.id)
attrs = {
@@ -986,7 +979,7 @@ def do_receiver_list(service, args=None):
def _show_receiver(service, receiver_id):
try:
receiver = service.get_receiver(receiver_id)
except exc.HTTPNotFound:
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Receiver not found: %s') % receiver_id)
formatters = {
@@ -1041,7 +1034,7 @@ def do_receiver_delete(service, args):
for wid in args.id:
try:
service.delete_receiver(wid, False)
except exc.HTTPNotFound as ex:
except Exception as ex:
failure_count += 1
print(ex)
if failure_count > 0:
@@ -1104,8 +1097,8 @@ def do_event_show(service, args):
"""Describe the event."""
try:
event = service.get_event(args.id)
except exc.HTTPNotFound as ex:
raise exc.CommandError(str(ex))
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_("Event not found: %s") % args.id)
utils.print_dict(event.to_dict())
@@ -1172,9 +1165,8 @@ def do_action_show(service, args):
"""Show detailed info about the specified action."""
try:
action = service.get_action(args.id)
except exc.HTTPNotFound:
msg = _('Action %(id)s is not found') % {'id': args.id}
raise exc.CommandError(msg)
except sdk_exc.ResourceNotFound:
raise exc.CommandError(_('Action not found: %s') % args.id)
formatters = {
'inputs': utils.json_formatter,