# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    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.
"""Glance exception subclasses"""
import six
import six.moves.urllib.parse as urlparse
from glance.i18n import _
_FATAL_EXCEPTION_FORMAT_ERRORS = False
[docs]class RedirectException(Exception):
    def __init__(self, url):
        self.url = urlparse.urlparse(url) 
[docs]class GlanceException(Exception):
    """
    Base Glance Exception
    To correctly use this class, inherit from it and define
    a 'message' property. That message will get printf'd
    with the keyword arguments provided to the constructor.
    """
    message = _("An unknown exception occurred")
    def __init__(self, message=None, *args, **kwargs):
        if not message:
            message = self.message
        try:
            if kwargs:
                message = message % kwargs
        except Exception:
            if _FATAL_EXCEPTION_FORMAT_ERRORS:
                raise
            else:
                # at least get the core message out if something happened
                pass
        self.msg = message
        super(GlanceException, self).__init__(message)
    def __unicode__(self):
        # NOTE(flwang): By default, self.msg is an instance of Message, which
        # can't be converted by str(). Based on the definition of
        # __unicode__, it should return unicode always.
        return six.text_type(self.msg) 
[docs]class MissingCredentialError(GlanceException):
    message = _("Missing required credential: %(required)s") 
[docs]class BadAuthStrategy(GlanceException):
    message = _("Incorrect auth strategy, expected \"%(expected)s\" but "
                "received \"%(received)s\"") 
[docs]class NotFound(GlanceException):
    message = _("An object with the specified identifier was not found.") 
[docs]class BadStoreUri(GlanceException):
    message = _("The Store URI was malformed.") 
[docs]class Duplicate(GlanceException):
    message = _("An object with the same identifier already exists.") 
[docs]class Conflict(GlanceException):
    message = _("An object with the same identifier is currently being "
                "operated on.") 
[docs]class StorageQuotaFull(GlanceException):
    message = _("The size of the data %(image_size)s will exceed the limit. "
                "%(remaining)s bytes remaining.") 
[docs]class AuthBadRequest(GlanceException):
    message = _("Connect error/bad request to Auth service at URL %(url)s.") 
[docs]class AuthUrlNotFound(GlanceException):
    message = _("Auth service at URL %(url)s not found.") 
[docs]class AuthorizationFailure(GlanceException):
    message = _("Authorization failed.") 
[docs]class NotAuthenticated(GlanceException):
    message = _("You are not authenticated.") 
[docs]class UploadException(GlanceException):
    message = _('Image upload problem: %s') 
[docs]class Forbidden(GlanceException):
    message = _("You are not authorized to complete %(action)s action.") 
[docs]class ForbiddenPublicImage(Forbidden):
    message = _("You are not authorized to complete this action.") 
[docs]class ProtectedImageDelete(Forbidden):
    message = _("Image %(image_id)s is protected and cannot be deleted.") 
[docs]class Invalid(GlanceException):
    message = _("Data supplied was not valid.") 
[docs]class InvalidSortKey(Invalid):
    message = _("Sort key supplied was not valid.") 
[docs]class InvalidSortDir(Invalid):
    message = _("Sort direction supplied was not valid.") 
[docs]class InvalidPropertyProtectionConfiguration(Invalid):
    message = _("Invalid configuration in property protection file.") 
[docs]class InvalidSwiftStoreConfiguration(Invalid):
    message = _("Invalid configuration in glance-swift conf file.") 
[docs]class InvalidFilterOperatorValue(Invalid):
    message = _("Unable to filter using the specified operator.") 
[docs]class InvalidFilterRangeValue(Invalid):
    message = _("Unable to filter using the specified range.") 
[docs]class InvalidOptionValue(Invalid):
    message = _("Invalid value for option %(option)s: %(value)s") 
[docs]class ReadonlyProperty(Forbidden):
    message = _("Attribute '%(property)s' is read-only.") 
[docs]class ReservedProperty(Forbidden):
    message = _("Attribute '%(property)s' is reserved.") 
[docs]class AuthorizationRedirect(GlanceException):
    message = _("Redirecting to %(uri)s for authorization.") 
[docs]class ClientConnectionError(GlanceException):
    message = _("There was an error connecting to a server") 
[docs]class ClientConfigurationError(GlanceException):
    message = _("There was an error configuring the client.") 
[docs]class MultipleChoices(GlanceException):
    message = _("The request returned a 302 Multiple Choices. This generally "
                "means that you have not included a version indicator in a "
                "request URI.\n\nThe body of response returned:\n%(body)s") 
[docs]class LimitExceeded(GlanceException):
    message = _("The request returned a 413 Request Entity Too Large. This "
                "generally means that rate limiting or a quota threshold was "
                "breached.\n\nThe response body:\n%(body)s")
    def __init__(self, *args, **kwargs):
        self.retry_after = (int(kwargs['retry']) if kwargs.get('retry')
                            else None)
        super(LimitExceeded, self).__init__(*args, **kwargs) 
[docs]class ServiceUnavailable(GlanceException):
    message = _("The request returned 503 Service Unavailable. This "
                "generally occurs on service overload or other transient "
                "outage.")
    def __init__(self, *args, **kwargs):
        self.retry_after = (int(kwargs['retry']) if kwargs.get('retry')
                            else None)
        super(ServiceUnavailable, self).__init__(*args, **kwargs) 
[docs]class ServerError(GlanceException):
    message = _("The request returned 500 Internal Server Error.") 
[docs]class UnexpectedStatus(GlanceException):
    message = _("The request returned an unexpected status: %(status)s."
                "\n\nThe response body:\n%(body)s") 
[docs]class InvalidContentType(GlanceException):
    message = _("Invalid content type %(content_type)s") 
[docs]class BadRegistryConnectionConfiguration(GlanceException):
    message = _("Registry was not configured correctly on API server. "
                "Reason: %(reason)s") 
[docs]class BadDriverConfiguration(GlanceException):
    message = _("Driver %(driver_name)s could not be configured correctly. "
                "Reason: %(reason)s") 
[docs]class MaxRedirectsExceeded(GlanceException):
    message = _("Maximum redirects (%(redirects)s) was exceeded.") 
[docs]class InvalidRedirect(GlanceException):
    message = _("Received invalid HTTP redirect.") 
[docs]class NoServiceEndpoint(GlanceException):
    message = _("Response from Keystone does not contain a Glance endpoint.") 
[docs]class RegionAmbiguity(GlanceException):
    message = _("Multiple 'image' service matches for region %(region)s. This "
                "generally means that a region is required and you have not "
                "supplied one.") 
[docs]class WorkerCreationFailure(GlanceException):
    message = _("Server worker creation failed: %(reason)s.") 
[docs]class SchemaLoadError(GlanceException):
    message = _("Unable to load schema: %(reason)s") 
[docs]class InvalidObject(GlanceException):
    message = _("Provided object does not match schema "
                "'%(schema)s': %(reason)s") 
[docs]class ImageSizeLimitExceeded(GlanceException):
    message = _("The provided image is too large.") 
[docs]class ImageMemberLimitExceeded(LimitExceeded):
    message = _("The limit has been exceeded on the number of allowed image "
                "members for this image. Attempted: %(attempted)s, "
                "Maximum: %(maximum)s") 
[docs]class ImagePropertyLimitExceeded(LimitExceeded):
    message = _("The limit has been exceeded on the number of allowed image "
                "properties. Attempted: %(attempted)s, Maximum: %(maximum)s") 
[docs]class ImageTagLimitExceeded(LimitExceeded):
    message = _("The limit has been exceeded on the number of allowed image "
                "tags. Attempted: %(attempted)s, Maximum: %(maximum)s") 
[docs]class ImageLocationLimitExceeded(LimitExceeded):
    message = _("The limit has been exceeded on the number of allowed image "
                "locations. Attempted: %(attempted)s, Maximum: %(maximum)s") 
[docs]class SIGHUPInterrupt(GlanceException):
    message = _("System SIGHUP signal received.") 
[docs]class RPCError(GlanceException):
    message = _("%(cls)s exception was raised in the last rpc call: %(val)s") 
[docs]class TaskException(GlanceException):
    message = _("An unknown task exception occurred") 
[docs]class BadTaskConfiguration(GlanceException):
    message = _("Task was not configured properly") 
[docs]class ImageNotFound(NotFound):
    message = _("Image with the given id %(image_id)s was not found") 
[docs]class TaskNotFound(TaskException, NotFound):
    message = _("Task with the given id %(task_id)s was not found") 
[docs]class InvalidTaskStatus(TaskException, Invalid):
    message = _("Provided status of task is unsupported: %(status)s") 
[docs]class InvalidTaskType(TaskException, Invalid):
    message = _("Provided type of task is unsupported: %(type)s") 
[docs]class InvalidTaskStatusTransition(TaskException, Invalid):
    message = _("Status transition from %(cur_status)s to"
                " %(new_status)s is not allowed") 
[docs]class ImportTaskError(TaskException, Invalid):
    message = _("An import task exception occurred") 
[docs]class DuplicateLocation(Duplicate):
    message = _("The location %(location)s already exists") 
[docs]class InvalidParameterValue(Invalid):
    message = _("Invalid value '%(value)s' for parameter '%(param)s': "
                "%(extra_msg)s") 
[docs]class InvalidImageStatusTransition(Invalid):
    message = _("Image status transition from %(cur_status)s to"
                " %(new_status)s is not allowed") 
[docs]class InvalidVersion(Invalid):
    message = _("Version is invalid: %(reason)s") 
[docs]class InvalidArtifactTypePropertyDefinition(Invalid):
    message = _("Invalid property definition") 
[docs]class InvalidArtifactTypeDefinition(Invalid):
    message = _("Invalid type definition") 
[docs]class InvalidArtifactPropertyValue(Invalid):
    message = _("Property '%(name)s' may not have value '%(val)s': %(msg)s")
    def __init__(self, message=None, *args, **kwargs):
        super(InvalidArtifactPropertyValue, self).__init__(message, *args,
                                                           **kwargs)
        self.name = kwargs.get('name')
        self.value = kwargs.get('val') 
[docs]class ArtifactNotFound(NotFound):
    message = _("Artifact with id=%(id)s was not found") 
[docs]class ArtifactForbidden(Forbidden):
    message = _("Artifact with id=%(id)s is not accessible") 
[docs]class ArtifactDuplicateNameTypeVersion(Duplicate):
    message = _("Artifact with the specified type, name and version"
                " already exists") 
[docs]class InvalidArtifactStateTransition(Invalid):
    message = _("Artifact cannot change state from %(source)s to %(target)s") 
[docs]class ArtifactDuplicateDirectDependency(Duplicate):
    message = _("Artifact with the specified type, name and version"
                " already has the direct dependency=%(dep)s") 
[docs]class ArtifactDuplicateTransitiveDependency(Duplicate):
    message = _("Artifact with the specified type, name and version"
                " already has the transitive dependency=%(dep)s") 
[docs]class ArtifactCircularDependency(Invalid):
    message = _("Artifact with a circular dependency can not be created") 
[docs]class ArtifactUnsupportedPropertyOperator(Invalid):
    message = _("Operator %(op)s is not supported") 
[docs]class ArtifactUnsupportedShowLevel(Invalid):
    message = _("Show level %(shl)s is not supported in this operation") 
[docs]class ArtifactPropertyValueNotFound(NotFound):
    message = _("Property's %(prop)s value has not been found") 
[docs]class ArtifactInvalidProperty(Invalid):
    message = _("Artifact has no property %(prop)s") 
[docs]class ArtifactInvalidPropertyParameter(Invalid):
    message = _("Cannot use this parameter with the operator %(op)s") 
[docs]class ArtifactLoadError(GlanceException):
    message = _("Cannot load artifact '%(name)s'") 
[docs]class ArtifactNonMatchingTypeName(ArtifactLoadError):
    message = _("Plugin name '%(plugin)s' should match "
                "artifact typename '%(name)s'") 
[docs]class ArtifactPluginNotFound(NotFound):
    message = _("No plugin for '%(name)s' has been loaded") 
[docs]class UnknownArtifactType(NotFound):
    message = _("Artifact type with name '%(name)s' and version '%(version)s' "
                "is not known") 
[docs]class ArtifactInvalidStateTransition(Invalid):
    message = _("Artifact state cannot be changed from %(curr)s to %(to)s") 
[docs]class JsonPatchException(GlanceException):
    message = _("Invalid jsonpatch request") 
[docs]class InvalidJsonPatchBody(JsonPatchException):
    message = _("The provided body %(body)s is invalid "
                "under given schema: %(schema)s") 
[docs]class InvalidJsonPatchPath(JsonPatchException):
    message = _("The provided path '%(path)s' is invalid: %(explanation)s")
    def __init__(self, message=None, *args, **kwargs):
        self.explanation = kwargs.get("explanation")
        super(InvalidJsonPatchPath, self).__init__(message, *args, **kwargs) 
[docs]class InvalidDataMigrationScript(GlanceException):
    message = _("Invalid data migration script '%(script)s'. A valid data "
                "migration script must implement functions 'has_migrations' "
                "and 'migrate'.")