Source code for watcher.common.exception

# -*- encoding: utf-8 -*-
# Copyright (c) 2015 b<>com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Watcher base exception handling.

Includes decorator for re-raising Watcher-type exceptions.

SHOULD include dedicated exception logging.

"""

import functools
import sys

from keystoneclient import exceptions as keystone_exceptions
from oslo_config import cfg
from oslo_log import log as logging
import six

from watcher._i18n import _, _LE

LOG = logging.getLogger(__name__)

exc_log_opts = [
    cfg.BoolOpt('fatal_exception_format_errors',
                default=False,
                help='Make exception message format errors fatal.'),
]

CONF = cfg.CONF
CONF.register_opts(exc_log_opts)


def wrap_keystone_exception(func):
    """Wrap keystone exceptions and throw Watcher specific exceptions."""
    @functools.wraps(func)
    def wrapped(*args, **kw):
        try:
            return func(*args, **kw)
        except keystone_exceptions.AuthorizationFailure:
            raise AuthorizationFailure(
                client=func.__name__, reason=sys.exc_info()[1])
        except keystone_exceptions.ClientException:
            raise AuthorizationFailure(
                client=func.__name__,
                reason=(_('Unexpected keystone client error occurred: %s')
                        % sys.exc_info()[1]))
    return wrapped


[docs]class WatcherException(Exception): """Base Watcher Exception To correctly use this class, inherit from it and define a 'msg_fmt' property. That msg_fmt will get printf'd with the keyword arguments provided to the constructor. """ msg_fmt = _("An unknown exception occurred") code = 500 headers = {} safe = False def __init__(self, message=None, **kwargs): self.kwargs = kwargs if 'code' not in self.kwargs: try: self.kwargs['code'] = self.code except AttributeError: pass if not message: try: message = self.msg_fmt % kwargs except Exception: # kwargs doesn't match a variable in msg_fmt # log the issue and the kwargs LOG.exception(_LE('Exception in string format operation')) for name, value in kwargs.items(): LOG.error(_LE("%(name)s: %(value)s"), {'name': name, 'value': value}) if CONF.fatal_exception_format_errors: raise else: # at least get the core msg_fmt out if something happened message = self.msg_fmt super(WatcherException, self).__init__(message) def __str__(self): """Encode to utf-8 then wsme api can consume it as well""" if not six.PY3: return six.text_type(self.args[0]).encode('utf-8') else: return self.args[0] def __unicode__(self): return six.text_type(self.args[0])
[docs] def format_message(self): if self.__class__.__name__.endswith('_Remote'): return self.args[0] else: return six.text_type(self)
[docs]class NotAuthorized(WatcherException): msg_fmt = _("Not authorized") code = 403
[docs]class PolicyNotAuthorized(NotAuthorized): msg_fmt = _("Policy doesn't allow %(action)s to be performed.")
[docs]class OperationNotPermitted(NotAuthorized): msg_fmt = _("Operation not permitted")
[docs]class Invalid(WatcherException): msg_fmt = _("Unacceptable parameters") code = 400
[docs]class ObjectNotFound(WatcherException): msg_fmt = _("The %(name)s %(id)s could not be found")
[docs]class Conflict(WatcherException): msg_fmt = _('Conflict') code = 409
[docs]class ResourceNotFound(ObjectNotFound): msg_fmt = _("The %(name)s resource %(id)s could not be found") code = 404
[docs]class InvalidIdentity(Invalid): msg_fmt = _("Expected a uuid or int but received %(identity)s")
[docs]class InvalidOperator(Invalid): msg_fmt = _("Filter operator is not valid: %(operator)s not " "in %(valid_operators)s")
[docs]class InvalidGoal(Invalid): msg_fmt = _("Goal %(goal)s is invalid")
[docs]class InvalidStrategy(Invalid): msg_fmt = _("Strategy %(strategy)s is invalid")
[docs]class InvalidUUID(Invalid): msg_fmt = _("Expected a uuid but received %(uuid)s")
[docs]class InvalidName(Invalid): msg_fmt = _("Expected a logical name but received %(name)s")
[docs]class InvalidUuidOrName(Invalid): msg_fmt = _("Expected a logical name or uuid but received %(name)s")
[docs]class GoalNotFound(ResourceNotFound): msg_fmt = _("Goal %(goal)s could not be found")
[docs]class GoalAlreadyExists(Conflict): msg_fmt = _("A goal with UUID %(uuid)s already exists")
[docs]class StrategyNotFound(ResourceNotFound): msg_fmt = _("Strategy %(strategy)s could not be found")
[docs]class StrategyAlreadyExists(Conflict): msg_fmt = _("A strategy with UUID %(uuid)s already exists")
[docs]class AuditTemplateNotFound(ResourceNotFound): msg_fmt = _("AuditTemplate %(audit_template)s could not be found")
[docs]class AuditTemplateAlreadyExists(Conflict): msg_fmt = _("An audit_template with UUID or name %(audit_template)s " "already exists")
[docs]class AuditTemplateReferenced(Invalid): msg_fmt = _("AuditTemplate %(audit_template)s is referenced by one or " "multiple audits")
[docs]class AuditTypeNotFound(Invalid): msg_fmt = _("Audit type %(audit_type)s could not be found")
[docs]class AuditParameterNotAllowed(Invalid): msg_fmt = _("Audit parameter %(parameter)s are not allowed")
[docs]class AuditNotFound(ResourceNotFound): msg_fmt = _("Audit %(audit)s could not be found")
[docs]class AuditAlreadyExists(Conflict): msg_fmt = _("An audit with UUID %(uuid)s already exists")
[docs]class AuditIntervalNotSpecified(Invalid): msg_fmt = _("Interval of audit must be specified for %(audit_type)s.")
[docs]class AuditIntervalNotAllowed(Invalid): msg_fmt = _("Interval of audit must not be set for %(audit_type)s.")
[docs]class AuditReferenced(Invalid): msg_fmt = _("Audit %(audit)s is referenced by one or multiple action " "plans")
[docs]class ActionPlanNotFound(ResourceNotFound): msg_fmt = _("ActionPlan %(action_plan)s could not be found")
[docs]class ActionPlanAlreadyExists(Conflict): msg_fmt = _("An action plan with UUID %(uuid)s already exists")
[docs]class ActionPlanReferenced(Invalid): msg_fmt = _("Action Plan %(action_plan)s is referenced by one or " "multiple actions")
[docs]class ActionNotFound(ResourceNotFound): msg_fmt = _("Action %(action)s could not be found")
[docs]class ActionAlreadyExists(Conflict): msg_fmt = _("An action with UUID %(uuid)s already exists")
[docs]class ActionReferenced(Invalid): msg_fmt = _("Action plan %(action_plan)s is referenced by one or " "multiple goals")
[docs]class ActionFilterCombinationProhibited(Invalid): msg_fmt = _("Filtering actions on both audit and action-plan is " "prohibited")
[docs]class EfficacyIndicatorNotFound(ResourceNotFound): msg_fmt = _("Efficacy indicator %(efficacy_indicator)s could not be found")
[docs]class EfficacyIndicatorAlreadyExists(Conflict): msg_fmt = _("An action with UUID %(uuid)s already exists")
[docs]class ScoringEngineAlreadyExists(Conflict): msg_fmt = _("A scoring engine with UUID %(uuid)s already exists")
[docs]class ScoringEngineNotFound(ResourceNotFound): msg_fmt = _("ScoringEngine %(scoring_engine)s could not be found")
[docs]class HTTPNotFound(ResourceNotFound): pass
[docs]class PatchError(Invalid): msg_fmt = _("Couldn't apply patch '%(patch)s'. Reason: %(reason)s") # decision engine
[docs]class WorkflowExecutionException(WatcherException): msg_fmt = _('Workflow execution error: %(error)s')
[docs]class IllegalArgumentException(WatcherException): msg_fmt = _('Illegal argument')
[docs]class NoSuchMetric(WatcherException): msg_fmt = _('No such metric')
[docs]class NoDataFound(WatcherException): msg_fmt = _('No rows were returned')
[docs]class AuthorizationFailure(WatcherException): msg_fmt = _('%(client)s connection failed. Reason: %(reason)s')
[docs]class KeystoneFailure(WatcherException): msg_fmt = _("Keystone API endpoint is missing")
[docs]class ClusterEmpty(WatcherException): msg_fmt = _("The list of compute node(s) in the cluster is empty")
[docs]class MetricCollectorNotDefined(WatcherException): msg_fmt = _("The metrics resource collector is not defined")
[docs]class ClusterDataModelCollectionError(WatcherException): msg_fmt = _("The cluster data model '%(cdm)s' could not be built")
[docs]class ClusterStateNotDefined(WatcherException): msg_fmt = _("The cluster state is not defined")
[docs]class CapacityNotDefined(WatcherException): msg_fmt = _("The capacity %(capacity)s is not defined for '%(resource)s'")
[docs]class NoAvailableStrategyForGoal(WatcherException): msg_fmt = _("No strategy could be found to achieve the '%(goal)s' goal.")
[docs]class InvalidIndicatorValue(WatcherException): msg_fmt = _("The indicator '%(name)s' with value '%(value)s' " "and spec type '%(spec_type)s' is invalid.")
[docs]class GlobalEfficacyComputationError(WatcherException): msg_fmt = _("Could not compute the global efficacy for the '%(goal)s' " "goal using the '%(strategy)s' strategy.")
[docs]class NoMetricValuesForInstance(WatcherException): msg_fmt = _("No values returned by %(resource_id)s for %(metric_name)s.")
[docs]class NoSuchMetricForHost(WatcherException): msg_fmt = _("No %(metric)s metric for %(host)s found.") # Model
[docs]class InstanceNotFound(WatcherException): msg_fmt = _("The instance '%(name)s' could not be found")
[docs]class ComputeNodeNotFound(WatcherException): msg_fmt = _("The compute node %(name)s could not be found")
[docs]class LoadingError(WatcherException): msg_fmt = _("Error loading plugin '%(name)s'")
[docs]class ReservedWord(WatcherException): msg_fmt = _("The identifier '%(name)s' is a reserved word")
[docs]class NotSoftDeletedStateError(WatcherException): msg_fmt = _("The %(name)s resource %(id)s is not soft deleted")
[docs]class NegativeLimitError(WatcherException): msg_fmt = _("Limit should be positive")

Project Source