Move Resource exceptions to common module (3)

It is convenient to have all exceptions in exception module.
Also it is reduces namespace cluttering of resource module and decreases
the number of dependencies in other modules (we do not need to import resource
in some cases for now).
ResourceInError exception is moved in this patch.

Change-Id: I122c032468efeb97d165af4ae372bc35b60a11a2
This commit is contained in:
Oleksii Chuprykov 2015-09-02 17:32:53 +03:00
parent 68a73b513c
commit 6d41bcc1d3
20 changed files with 74 additions and 80 deletions

View File

@ -476,8 +476,8 @@ class DockerContainer(resource.Resource):
exit_status = status.get('ExitCode') exit_status = status.get('ExitCode')
if exit_status is not None and exit_status != 0: if exit_status is not None and exit_status != 0:
logs = self.get_client().logs(self.resource_id) logs = self.get_client().logs(self.resource_id)
raise resource.ResourceInError(resource_status=self.FAILED, raise exception.ResourceInError(resource_status=self.FAILED,
status_reason=logs) status_reason=logs)
return status['Running'] return status['Running']
def handle_delete(self): def handle_delete(self):

View File

@ -127,7 +127,7 @@ class DockerContainerTest(common.HeatTestCase):
res_def = mock.Mock(spec=rsrc_defn.ResourceDefinition) res_def = mock.Mock(spec=rsrc_defn.ResourceDefinition)
docker_res = docker_container.DockerContainer("test", res_def, docker_res = docker_container.DockerContainer("test", res_def,
mock_stack) mock_stack)
exc = self.assertRaises(resource.ResourceInError, exc = self.assertRaises(exception.ResourceInError,
docker_res.check_create_complete, docker_res.check_create_complete,
'foo') 'foo')
self.assertIn("Container startup failed", six.text_type(exc)) self.assertIn("Container startup failed", six.text_type(exc))

View File

@ -405,6 +405,15 @@ class ResourceUnknownStatus(HeatException):
result=result, status_reason=status_reason, **kwargs) result=result, status_reason=status_reason, **kwargs)
class ResourceInError(HeatException):
msg_fmt = _('Went to status %(resource_status)s '
'due to "%(status_reason)s"')
def __init__(self, status_reason=_('Unknown'), **kwargs):
super(ResourceInError, self).__init__(status_reason=status_reason,
**kwargs)
class HTTPExceptionDisguise(Exception): class HTTPExceptionDisguise(Exception):
"""Disguises HTTP exceptions so they can be handled by the webob fault """Disguises HTTP exceptions so they can be handled by the webob fault
application in the wsgi pipeline. application in the wsgi pipeline.

View File

@ -34,7 +34,6 @@ from heat.common.i18n import _LI
from heat.common.i18n import _LW from heat.common.i18n import _LW
from heat.engine.clients import client_plugin from heat.engine.clients import client_plugin
from heat.engine import constraints from heat.engine import constraints
from heat.engine import resource
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -215,7 +214,7 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
return True return True
elif status == 'ERROR': elif status == 'ERROR':
fault = getattr(server, 'fault', {}) fault = getattr(server, 'fault', {})
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=status, resource_status=status,
status_reason=_("Message: %(message)s, Code: %(code)s") % { status_reason=_("Message: %(message)s, Code: %(code)s") % {
'message': fault.get('message', _('Unknown')), 'message': fault.get('message', _('Unknown')),
@ -406,8 +405,8 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers
"%(message)s") % dict(name=server.name, "%(message)s") % dict(name=server.name,
code=code, code=code,
message=message) message=message)
raise resource.ResourceInError(resource_status=status, raise exception.ResourceInError(resource_status=status,
status_reason=errmsg) status_reason=errmsg)
return False return False
def rename(self, server, name): def rename(self, server, name):

View File

@ -61,15 +61,6 @@ class NoActionRequired(Exception):
pass pass
class ResourceInError(exception.HeatException):
msg_fmt = _('Went to status %(resource_status)s '
'due to "%(status_reason)s"')
def __init__(self, status_reason=_('Unknown'), **kwargs):
super(ResourceInError, self).__init__(status_reason=status_reason,
**kwargs)
class UpdateInProgress(Exception): class UpdateInProgress(Exception):
def __init__(self, resource_name='Unknown'): def __init__(self, resource_name='Unknown'):
msg = _("The resource %s is already being updated.") % resource_name msg = _("The resource %s is already being updated.") % resource_name
@ -775,7 +766,7 @@ class Resource(object):
else: else:
action = self.CREATE action = self.CREATE
except exception.ResourceFailure as failure: except exception.ResourceFailure as failure:
if not isinstance(failure.exc, ResourceInError): if not isinstance(failure.exc, exception.ResourceInError):
raise failure raise failure
count[action] += 1 count[action] += 1

View File

@ -251,7 +251,7 @@ class RemoteStack(resource.Resource):
elif stack.status == self.COMPLETE: elif stack.status == self.COMPLETE:
return True return True
elif stack.status == self.FAILED: elif stack.status == self.FAILED:
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=stack.stack_status, resource_status=stack.stack_status,
status_reason=stack.stack_status_reason) status_reason=stack.stack_status_reason)
else: else:

View File

@ -249,12 +249,12 @@ class ManilaShare(resource.Resource):
'Error during applying access rules to share "{0}". ' 'Error during applying access rules to share "{0}". '
'The root cause of the problem is the following: {1}.' 'The root cause of the problem is the following: {1}.'
).format(self.resource_id, ex.message) ).format(self.resource_id, ex.message)
raise resource.ResourceInError(status_reason=reason) raise exception.ResourceInError(status_reason=reason)
elif share_status == self.STATUS_ERROR: elif share_status == self.STATUS_ERROR:
reason = _('Error during creation of share "{0}"').format( reason = _('Error during creation of share "{0}"').format(
self.resource_id) self.resource_id)
raise resource.ResourceInError(status_reason=reason, raise exception.ResourceInError(status_reason=reason,
resource_status=share_status) resource_status=share_status)
else: else:
reason = _('Unknown share_status during creation of share "{0}"' reason = _('Unknown share_status during creation of share "{0}"'
).format(self.resource_id) ).format(self.resource_id)
@ -276,7 +276,7 @@ class ManilaShare(resource.Resource):
return False return False
elif share.status in (self.STATUS_ERROR, elif share.status in (self.STATUS_ERROR,
self.STATUS_ERROR_DELETING): self.STATUS_ERROR_DELETING):
raise resource.ResourceInError( raise exception.ResourceInError(
status_reason=_( status_reason=_(
'Error during deleting share "{0}".' 'Error during deleting share "{0}".'
).format(self.resource_id), ).format(self.resource_id),

View File

@ -473,14 +473,14 @@ class Pool(neutron.NeutronResource):
if vip_status == 'ACTIVE': if vip_status == 'ACTIVE':
return True return True
if vip_status == 'ERROR': if vip_status == 'ERROR':
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=vip_status, resource_status=vip_status,
status_reason=_('error in vip')) status_reason=_('error in vip'))
raise exception.ResourceUnknownStatus( raise exception.ResourceUnknownStatus(
resource_status=vip_status, resource_status=vip_status,
result=_('Pool creation failed due to vip')) result=_('Pool creation failed due to vip'))
elif status == 'ERROR': elif status == 'ERROR':
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=status, resource_status=status,
status_reason=_('error in pool')) status_reason=_('error in pool'))
else: else:

View File

@ -111,7 +111,7 @@ class NeutronResource(resource.Resource):
if status in ('ACTIVE', 'DOWN'): if status in ('ACTIVE', 'DOWN'):
return True return True
elif status == 'ERROR': elif status == 'ERROR':
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=status) resource_status=status)
else: else:
raise exception.ResourceUnknownStatus( raise exception.ResourceUnknownStatus(

View File

@ -175,7 +175,7 @@ class SaharaCluster(resource.Resource):
def check_create_complete(self, cluster_id): def check_create_complete(self, cluster_id):
cluster = self.client().clusters.get(cluster_id) cluster = self.client().clusters.get(cluster_id)
if cluster.status == self.CLUSTER_ERROR: if cluster.status == self.CLUSTER_ERROR:
raise resource.ResourceInError(resource_status=cluster.status) raise exception.ResourceInError(resource_status=cluster.status)
if cluster.status != self.CLUSTER_ACTIVE: if cluster.status != self.CLUSTER_ACTIVE:
return False return False
@ -196,7 +196,7 @@ class SaharaCluster(resource.Resource):
return True return True
else: else:
if cluster.status == self.CLUSTER_ERROR: if cluster.status == self.CLUSTER_ERROR:
raise resource.ResourceInError(resource_status=cluster.status) raise exception.ResourceInError(resource_status=cluster.status)
return False return False

View File

@ -388,7 +388,7 @@ class OSDBInstance(resource.Resource):
if instance is None: if instance is None:
return False return False
if instance.status in self.BAD_STATUSES: if instance.status in self.BAD_STATUSES:
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=instance.status, resource_status=instance.status,
status_reason=self.TROVE_STATUS_REASON.get(instance.status, status_reason=self.TROVE_STATUS_REASON.get(instance.status,
_("Unknown"))) _("Unknown")))

View File

@ -13,6 +13,7 @@
from oslo_log import log as logging from oslo_log import log as logging
from heat.common import exception
from heat.common.i18n import _ from heat.common.i18n import _
from heat.common.i18n import _LI from heat.common.i18n import _LI
from heat.common.i18n import _LW from heat.common.i18n import _LW
@ -183,7 +184,7 @@ class TroveCluster(resource.Resource):
for instance in cluster.instances: for instance in cluster.instances:
if instance['status'] in self.BAD_STATUSES: if instance['status'] in self.BAD_STATUSES:
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=instance['status'], resource_status=instance['status'],
status_reason=self.TROVE_STATUS_REASON.get( status_reason=self.TROVE_STATUS_REASON.get(
instance['status'], _("Unknown"))) instance['status'], _("Unknown")))

View File

@ -51,7 +51,7 @@ class BaseVolume(resource.Resource):
if vol.status in self._volume_creating_status: if vol.status in self._volume_creating_status:
return False return False
if vol.status == 'error': if vol.status == 'error':
raise resource.ResourceInError( raise exception.ResourceInError(
resource_status=vol.status) resource_status=vol.status)
else: else:
raise exception.ResourceUnknownStatus( raise exception.ResourceUnknownStatus(

View File

@ -28,7 +28,6 @@ from heat.engine.clients.os import neutron
from heat.engine.clients.os import nova from heat.engine.clients.os import nova
from heat.engine.clients import progress from heat.engine.clients import progress
from heat.engine import environment from heat.engine import environment
from heat.engine import resource
from heat.engine.resources.aws.ec2 import instance as instances from heat.engine.resources.aws.ec2 import instance as instances
from heat.engine.resources import scheduler_hints as sh from heat.engine.resources import scheduler_hints as sh
from heat.engine import scheduler from heat.engine import scheduler
@ -549,7 +548,7 @@ class InstancesTest(common.HeatTestCase):
self.fc.servers.get(instance.resource_id).AndReturn(return_server) self.fc.servers.get(instance.resource_id).AndReturn(return_server)
self.m.ReplayAll() self.m.ReplayAll()
e = self.assertRaises(resource.ResourceInError, e = self.assertRaises(exception.ResourceInError,
instance.check_create_complete, instance.check_create_complete,
(creator, None)) (creator, None))
self.assertEqual( self.assertEqual(
@ -572,7 +571,7 @@ class InstancesTest(common.HeatTestCase):
self.m.ReplayAll() self.m.ReplayAll()
e = self.assertRaises( e = self.assertRaises(
resource.ResourceInError, instance.check_create_complete, exception.ResourceInError, instance.check_create_complete,
(creator, None)) (creator, None))
self.assertEqual( self.assertEqual(
'Went to status ERROR due to "Message: Unknown, Code: Unknown"', 'Went to status ERROR due to "Message: Unknown, Code: Unknown"',

View File

@ -24,7 +24,6 @@ import six
from heat.common import exception from heat.common import exception
from heat.engine.clients.os import nova from heat.engine.clients.os import nova
from heat.engine import resource
from heat.tests import common from heat.tests import common
from heat.tests.nova import fakes as fakes_nova from heat.tests.nova import fakes as fakes_nova
from heat.tests import utils from heat.tests import utils
@ -267,7 +266,7 @@ class NovaClientPluginCheckActiveTests(NovaClientPluginTestCase):
e_raise=False)), e_raise=False)),
('error', dict( ('error', dict(
status='ERROR', status='ERROR',
e_raise=resource.ResourceInError)), e_raise=exception.ResourceInError)),
('unknown', dict( ('unknown', dict(
status='VIKINGS!', status='VIKINGS!',
e_raise=exception.ResourceUnknownStatus)) e_raise=exception.ResourceUnknownStatus))

View File

@ -18,7 +18,6 @@ import six
from heat.common import exception from heat.common import exception
from heat.common import template_format from heat.common import template_format
from heat.engine import resource
from heat.engine.resources.openstack.manila import share as mshare from heat.engine.resources.openstack.manila import share as mshare
from heat.engine import rsrc_defn from heat.engine import rsrc_defn
from heat.engine import scheduler from heat.engine import scheduler
@ -127,7 +126,7 @@ class ManilaShareTest(common.HeatTestCase):
def test_share_create_fail(self): def test_share_create_fail(self):
share = self._init_share("stack_share_create_fail") share = self._init_share("stack_share_create_fail")
share.client().shares.get.return_value = self.failed_share share.client().shares.get.return_value = self.failed_share
exc = self.assertRaises(resource.ResourceInError, exc = self.assertRaises(exception.ResourceInError,
share.check_create_complete, share.check_create_complete,
self.failed_share) self.failed_share)
self.assertIn("Error during creation", six.text_type(exc)) self.assertIn("Error during creation", six.text_type(exc))

View File

@ -20,7 +20,6 @@ from heat.common import exception
from heat.engine.clients.os import neutron from heat.engine.clients.os import neutron
from heat.engine.hot import functions from heat.engine.hot import functions
from heat.engine import properties from heat.engine import properties
from heat.engine import resource
from heat.engine.resources.openstack.neutron import net from heat.engine.resources.openstack.neutron import net
from heat.engine.resources.openstack.neutron import neutron as nr from heat.engine.resources.openstack.neutron import neutron as nr
from heat.engine.resources.openstack.neutron import subnet from heat.engine.resources.openstack.neutron import subnet
@ -91,7 +90,7 @@ class NeutronTest(common.HeatTestCase):
self.assertTrue(nr.NeutronResource.is_built({'status': 'DOWN'})) self.assertTrue(nr.NeutronResource.is_built({'status': 'DOWN'}))
self.assertFalse(nr.NeutronResource.is_built({'status': 'BUILD'})) self.assertFalse(nr.NeutronResource.is_built({'status': 'BUILD'}))
e = self.assertRaises( e = self.assertRaises(
resource.ResourceInError, exception.ResourceInError,
nr.NeutronResource.is_built, {'status': 'ERROR'}) nr.NeutronResource.is_built, {'status': 'ERROR'})
self.assertEqual( self.assertEqual(
'Went to status ERROR due to "Unknown"', 'Went to status ERROR due to "Unknown"',

View File

@ -30,7 +30,6 @@ from heat.engine.clients.os import nova
from heat.engine.clients.os import swift from heat.engine.clients.os import swift
from heat.engine.clients.os import zaqar from heat.engine.clients.os import zaqar
from heat.engine import environment from heat.engine import environment
from heat.engine import resource
from heat.engine.resources.openstack.nova import server as servers from heat.engine.resources.openstack.nova import server as servers
from heat.engine.resources import scheduler_hints as sh from heat.engine.resources import scheduler_hints as sh
from heat.engine import scheduler from heat.engine import scheduler
@ -536,7 +535,7 @@ class ServersTest(common.HeatTestCase):
self.fc.servers.get(server.resource_id).AndReturn(return_server) self.fc.servers.get(server.resource_id).AndReturn(return_server)
self.m.ReplayAll() self.m.ReplayAll()
e = self.assertRaises(resource.ResourceInError, e = self.assertRaises(exception.ResourceInError,
server.check_create_complete, server.check_create_complete,
server.resource_id) server.resource_id)
self.assertEqual( self.assertEqual(

View File

@ -22,7 +22,6 @@ from heat.common import template_format
from heat.engine.clients.os import neutron from heat.engine.clients.os import neutron
from heat.engine.clients.os import nova from heat.engine.clients.os import nova
from heat.engine.clients.os import trove from heat.engine.clients.os import trove
from heat.engine import resource
from heat.engine.resources.openstack.trove import os_database from heat.engine.resources.openstack.trove import os_database
from heat.engine import rsrc_defn from heat.engine import rsrc_defn
from heat.engine import scheduler from heat.engine import scheduler
@ -195,7 +194,7 @@ class OSDBInstanceTest(common.HeatTestCase):
trove_mock.instances.get.return_value = mock_input trove_mock.instances.get.return_value = mock_input
error_string = ('Went to status ERROR due to "The last operation for ' error_string = ('Went to status ERROR due to "The last operation for '
'the database instance failed due to an error."') 'the database instance failed due to an error."')
exc = self.assertRaises(resource.ResourceInError, exc = self.assertRaises(exception.ResourceInError,
osdb_res.check_create_complete, osdb_res.check_create_complete,
mock_input) mock_input)
self.assertIn(error_string, six.text_type(exc)) self.assertIn(error_string, six.text_type(exc))
@ -208,7 +207,7 @@ class OSDBInstanceTest(common.HeatTestCase):
'datastore. If a database instance is in the FAILED ' 'datastore. If a database instance is in the FAILED '
'state, it should be deleted and a new one should ' 'state, it should be deleted and a new one should '
'be created."') 'be created."')
exc = self.assertRaises(resource.ResourceInError, exc = self.assertRaises(exception.ResourceInError,
osdb_res.check_create_complete, osdb_res.check_create_complete,
mock_input) mock_input)
self.assertIn(error_string, six.text_type(exc)) self.assertIn(error_string, six.text_type(exc))
@ -220,7 +219,7 @@ class OSDBInstanceTest(common.HeatTestCase):
mock_input.status = 'ERROR' mock_input.status = 'ERROR'
error_string = ('Went to status ERROR due to "Unknown"') error_string = ('Went to status ERROR due to "Unknown"')
trove_mock.instances.get.return_value = mock_input trove_mock.instances.get.return_value = mock_input
exc = self.assertRaises(resource.ResourceInError, exc = self.assertRaises(exception.ResourceInError,
osdb_res.check_create_complete, osdb_res.check_create_complete,
mock_input) mock_input)
self.assertIn(error_string, six.text_type(exc)) self.assertIn(error_string, six.text_type(exc))

View File

@ -682,11 +682,11 @@ class ResourceTest(common.HeatTestCase):
# first attempt to create fails # first attempt to create fails
generic_rsrc.ResourceWithProps.handle_create().AndRaise( generic_rsrc.ResourceWithProps.handle_create().AndRaise(
resource.ResourceInError(resource_name='test_resource', exception.ResourceInError(resource_name='test_resource',
resource_status='ERROR', resource_status='ERROR',
resource_type='GenericResourceType', resource_type='GenericResourceType',
resource_action='CREATE', resource_action='CREATE',
status_reason='just because')) status_reason='just because'))
# delete error resource from first attempt # delete error resource from first attempt
generic_rsrc.ResourceWithProps.handle_delete().AndReturn(None) generic_rsrc.ResourceWithProps.handle_delete().AndReturn(None)
@ -711,11 +711,11 @@ class ResourceTest(common.HeatTestCase):
# attempt to create fails # attempt to create fails
generic_rsrc.ResourceWithProps.handle_create().AndRaise( generic_rsrc.ResourceWithProps.handle_create().AndRaise(
resource.ResourceInError(resource_name='test_resource', exception.ResourceInError(resource_name='test_resource',
resource_status='ERROR', resource_status='ERROR',
resource_type='GenericResourceType', resource_type='GenericResourceType',
resource_action='CREATE', resource_action='CREATE',
status_reason='just because')) status_reason='just because'))
self.m.ReplayAll() self.m.ReplayAll()
estr = ('ResourceInError: resources.test_resource: ' estr = ('ResourceInError: resources.test_resource: '
@ -738,26 +738,26 @@ class ResourceTest(common.HeatTestCase):
# first attempt to create fails # first attempt to create fails
generic_rsrc.ResourceWithProps.handle_create().AndRaise( generic_rsrc.ResourceWithProps.handle_create().AndRaise(
resource.ResourceInError(resource_name='test_resource', exception.ResourceInError(resource_name='test_resource',
resource_status='ERROR', resource_status='ERROR',
resource_type='GenericResourceType', resource_type='GenericResourceType',
resource_action='CREATE', resource_action='CREATE',
status_reason='just because')) status_reason='just because'))
# first attempt to delete fails # first attempt to delete fails
generic_rsrc.ResourceWithProps.handle_delete().AndRaise( generic_rsrc.ResourceWithProps.handle_delete().AndRaise(
resource.ResourceInError(resource_name='test_resource', exception.ResourceInError(resource_name='test_resource',
resource_status='ERROR', resource_status='ERROR',
resource_type='GenericResourceType', resource_type='GenericResourceType',
resource_action='DELETE', resource_action='DELETE',
status_reason='delete failed')) status_reason='delete failed'))
# second attempt to delete fails # second attempt to delete fails
timeutils.retry_backoff_delay(1, jitter_max=2.0).AndReturn(0.01) timeutils.retry_backoff_delay(1, jitter_max=2.0).AndReturn(0.01)
generic_rsrc.ResourceWithProps.handle_delete().AndRaise( generic_rsrc.ResourceWithProps.handle_delete().AndRaise(
resource.ResourceInError(resource_name='test_resource', exception.ResourceInError(resource_name='test_resource',
resource_status='ERROR', resource_status='ERROR',
resource_type='GenericResourceType', resource_type='GenericResourceType',
resource_action='DELETE', resource_action='DELETE',
status_reason='delete failed again')) status_reason='delete failed again'))
# third attempt to delete succeeds # third attempt to delete succeeds
timeutils.retry_backoff_delay(2, jitter_max=2.0).AndReturn(0.01) timeutils.retry_backoff_delay(2, jitter_max=2.0).AndReturn(0.01)
@ -783,22 +783,22 @@ class ResourceTest(common.HeatTestCase):
# first attempt to create fails # first attempt to create fails
generic_rsrc.ResourceWithProps.handle_create().AndRaise( generic_rsrc.ResourceWithProps.handle_create().AndRaise(
resource.ResourceInError(resource_name='test_resource', exception.ResourceInError(resource_name='test_resource',
resource_status='ERROR', resource_status='ERROR',
resource_type='GenericResourceType', resource_type='GenericResourceType',
resource_action='CREATE', resource_action='CREATE',
status_reason='just because')) status_reason='just because'))
# delete error resource from first attempt # delete error resource from first attempt
generic_rsrc.ResourceWithProps.handle_delete().AndReturn(None) generic_rsrc.ResourceWithProps.handle_delete().AndReturn(None)
# second attempt to create fails # second attempt to create fails
timeutils.retry_backoff_delay(1, jitter_max=2.0).AndReturn(0.01) timeutils.retry_backoff_delay(1, jitter_max=2.0).AndReturn(0.01)
generic_rsrc.ResourceWithProps.handle_create().AndRaise( generic_rsrc.ResourceWithProps.handle_create().AndRaise(
resource.ResourceInError(resource_name='test_resource', exception.ResourceInError(resource_name='test_resource',
resource_status='ERROR', resource_status='ERROR',
resource_type='GenericResourceType', resource_type='GenericResourceType',
resource_action='CREATE', resource_action='CREATE',
status_reason='just because')) status_reason='just because'))
# delete error resource from second attempt # delete error resource from second attempt
generic_rsrc.ResourceWithProps.handle_delete().AndReturn(None) generic_rsrc.ResourceWithProps.handle_delete().AndReturn(None)