2011-02-25 02:00:13 -08:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
2013-12-06 10:47:41 +10:30
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2011-02-24 17:59:42 -04:00
|
|
|
"""
|
2011-02-24 13:54:10 -04:00
|
|
|
Exception definitions.
|
|
|
|
"""
|
|
|
|
|
2011-08-03 17:41:33 -04:00
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class UnsupportedVersion(Exception):
|
|
|
|
"""Indicates that the user is trying to use an unsupported
|
|
|
|
version of the API.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-12-22 10:31:29 +08:00
|
|
|
class UnsupportedConsoleType(Exception):
|
|
|
|
"""Indicates that the user is trying to use an unsupported
|
|
|
|
console type when retrieving console urls of servers.
|
|
|
|
"""
|
|
|
|
def __init__(self, console_type):
|
|
|
|
self.message = 'Unsupported console_type "%s"' % console_type
|
|
|
|
|
|
|
|
|
2016-02-10 16:05:48 +02:00
|
|
|
class UnsupportedAttribute(AttributeError):
|
|
|
|
"""Indicates that the user is trying to transmit the argument to a method,
|
|
|
|
which is not supported by selected version.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, argument_name, start_version, end_version=None):
|
|
|
|
if end_version:
|
|
|
|
self.message = (
|
|
|
|
"'%(name)s' argument is only allowed for microversions "
|
|
|
|
"%(start)s - %(end)s." % {"name": argument_name,
|
|
|
|
"start": start_version,
|
|
|
|
"end": end_version})
|
|
|
|
else:
|
|
|
|
self.message = (
|
|
|
|
"'%(name)s' argument is only allowed since microversion "
|
|
|
|
"%(start)s." % {"name": argument_name, "start": start_version})
|
2017-12-14 18:20:45 -05:00
|
|
|
super(UnsupportedAttribute, self).__init__(self.message)
|
2016-02-10 16:05:48 +02:00
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class CommandError(Exception):
|
|
|
|
pass
|
2011-08-03 17:41:33 -04:00
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class AuthorizationFailure(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NoUniqueMatch(Exception):
|
|
|
|
pass
|
2012-04-09 20:32:37 +00:00
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class NoTokenLookupException(Exception):
|
2011-09-09 06:33:38 -07:00
|
|
|
"""This form of authentication does not support looking up
|
2013-12-12 03:55:50 +00:00
|
|
|
endpoints from an existing token.
|
|
|
|
"""
|
2011-09-09 06:33:38 -07:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class EndpointNotFound(Exception):
|
|
|
|
"""Could not find Service or Region in Service Catalog."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class AmbiguousEndpoints(Exception):
|
|
|
|
"""Found more than one matching endpoint in Service Catalog."""
|
|
|
|
def __init__(self, endpoints=None):
|
|
|
|
self.endpoints = endpoints
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "AmbiguousEndpoints: %s" % repr(self.endpoints)
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionRefused(Exception):
|
|
|
|
"""
|
|
|
|
Connection refused: the server refused the connection.
|
|
|
|
"""
|
|
|
|
def __init__(self, response=None):
|
|
|
|
self.response = response
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "ConnectionRefused: %s" % repr(self.response)
|
|
|
|
|
|
|
|
|
Make _poll_for_status more user-friendly
NOTE: _poll_for_status is a private method, which is used only in shell
module, so we can modify it without backward compatibility.
_poll_for_status is used for various resources with different interfaces.
In case of snapshotting, it works with Image resources, which doesn't have
"fault" attribute in "error" state. To prevent AttributeError, we should take
this into account.
Also, an exception raised by _poll_for_status(InstanceInErrorState) should
not be hardcoded for one type of resource, so this exception is renamed to
ResourceInErrorState. An exception InstanceInDeletedState, which is also
can be raised by _poll_for_status, is not modified, since I don't know cases
when it can be used with resources other than Server.
Change-Id: Ie0ee96999376cbf608caa1cf8521dbef5c939b52
Closes-Bug: #1538073
2016-01-26 12:39:52 +02:00
|
|
|
class ResourceInErrorState(Exception):
|
|
|
|
"""Resource is in the error state."""
|
|
|
|
|
|
|
|
def __init__(self, obj):
|
|
|
|
msg = "`%s` resource is in the error state" % obj.__class__.__name__
|
|
|
|
fault_msg = getattr(obj, "fault", {}).get("message")
|
|
|
|
if fault_msg:
|
|
|
|
msg += "due to '%s'" % fault_msg
|
|
|
|
self.message = "%s." % msg
|
2014-03-14 15:32:10 -04:00
|
|
|
|
|
|
|
|
2015-04-02 19:28:02 +03:00
|
|
|
class VersionNotFoundForAPIMethod(Exception):
|
|
|
|
msg_fmt = "API version '%(vers)s' is not supported on '%(method)s' method."
|
|
|
|
|
|
|
|
def __init__(self, version, method):
|
|
|
|
self.version = version
|
|
|
|
self.method = method
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.msg_fmt % {"vers": self.version, "method": self.method}
|
|
|
|
|
|
|
|
|
2015-07-01 15:26:10 +08:00
|
|
|
class InstanceInDeletedState(Exception):
|
|
|
|
"""Instance is in the deleted state."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class ClientException(Exception):
|
|
|
|
"""
|
|
|
|
The base exception class for all exceptions this library raises.
|
|
|
|
"""
|
|
|
|
message = 'Unknown Error'
|
|
|
|
|
|
|
|
def __init__(self, code, message=None, details=None, request_id=None,
|
|
|
|
url=None, method=None):
|
|
|
|
self.code = code
|
|
|
|
self.message = message or self.__class__.message
|
|
|
|
self.details = details
|
|
|
|
self.request_id = request_id
|
|
|
|
self.url = url
|
|
|
|
self.method = method
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
formatted_string = "%s (HTTP %s)" % (self.message, self.code)
|
|
|
|
if self.request_id:
|
|
|
|
formatted_string += " (Request-ID: %s)" % self.request_id
|
|
|
|
|
|
|
|
return formatted_string
|
|
|
|
|
|
|
|
|
2014-09-18 13:07:17 +02:00
|
|
|
class RetryAfterException(ClientException):
|
|
|
|
"""
|
|
|
|
The base exception class for ClientExceptions that use Retry-After header.
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
self.retry_after = int(kwargs.pop('retry_after'))
|
|
|
|
except (KeyError, ValueError):
|
|
|
|
self.retry_after = 0
|
|
|
|
|
|
|
|
super(RetryAfterException, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class BadRequest(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 400 - Bad request: you sent some malformed data.
|
|
|
|
"""
|
|
|
|
http_status = 400
|
|
|
|
message = "Bad request"
|
|
|
|
|
|
|
|
|
|
|
|
class Unauthorized(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 401 - Unauthorized: bad credentials.
|
|
|
|
"""
|
|
|
|
http_status = 401
|
|
|
|
message = "Unauthorized"
|
|
|
|
|
|
|
|
|
|
|
|
class Forbidden(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 403 - Forbidden: your credentials don't give you access to this
|
|
|
|
resource.
|
|
|
|
"""
|
|
|
|
http_status = 403
|
|
|
|
message = "Forbidden"
|
|
|
|
|
|
|
|
|
|
|
|
class NotFound(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 404 - Not found
|
|
|
|
"""
|
|
|
|
http_status = 404
|
|
|
|
message = "Not found"
|
|
|
|
|
|
|
|
|
|
|
|
class MethodNotAllowed(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 405 - Method Not Allowed
|
|
|
|
"""
|
|
|
|
http_status = 405
|
|
|
|
message = "Method Not Allowed"
|
|
|
|
|
|
|
|
|
2015-04-02 16:37:59 +03:00
|
|
|
class NotAcceptable(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 406 - Not Acceptable
|
|
|
|
"""
|
|
|
|
http_status = 406
|
|
|
|
message = "Not Acceptable"
|
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
class Conflict(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 409 - Conflict
|
|
|
|
"""
|
|
|
|
http_status = 409
|
|
|
|
message = "Conflict"
|
|
|
|
|
|
|
|
|
2014-09-18 13:07:17 +02:00
|
|
|
class OverLimit(RetryAfterException):
|
2014-07-11 13:31:14 -04:00
|
|
|
"""
|
|
|
|
HTTP 413 - Over limit: you're over the API limits for this time period.
|
|
|
|
"""
|
|
|
|
http_status = 413
|
|
|
|
message = "Over limit"
|
|
|
|
|
|
|
|
|
2014-09-18 13:07:17 +02:00
|
|
|
class RateLimit(RetryAfterException):
|
2013-07-16 09:35:42 +00:00
|
|
|
"""
|
|
|
|
HTTP 429 - Rate limit: you've sent too many requests for this time period.
|
|
|
|
"""
|
|
|
|
http_status = 429
|
|
|
|
message = "Rate limit"
|
|
|
|
|
|
|
|
|
2014-07-11 13:31:14 -04:00
|
|
|
# NotImplemented is a python keyword.
|
|
|
|
class HTTPNotImplemented(ClientException):
|
|
|
|
"""
|
|
|
|
HTTP 501 - Not Implemented: the server does not support this operation.
|
|
|
|
"""
|
|
|
|
http_status = 501
|
|
|
|
message = "Not Implemented"
|
|
|
|
|
|
|
|
|
|
|
|
# In Python 2.4 Exception is old-style and thus doesn't have a __subclasses__()
|
|
|
|
# so we can do this:
|
|
|
|
# _code_map = dict((c.http_status, c)
|
|
|
|
# for c in ClientException.__subclasses__())
|
|
|
|
#
|
|
|
|
# Instead, we have to hardcode it:
|
|
|
|
_error_classes = [BadRequest, Unauthorized, Forbidden, NotFound,
|
2015-04-02 16:37:59 +03:00
|
|
|
MethodNotAllowed, NotAcceptable, Conflict, OverLimit,
|
|
|
|
RateLimit, HTTPNotImplemented]
|
2014-07-11 13:31:14 -04:00
|
|
|
_code_map = dict((c.http_status, c) for c in _error_classes)
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
|
2014-04-08 11:40:41 +10:00
|
|
|
class InvalidUsage(RuntimeError):
|
|
|
|
"""This function call is invalid in the way you are using this client.
|
|
|
|
|
2015-12-10 13:49:14 -05:00
|
|
|
Due to the transition to using keystoneauth some function calls are no
|
2014-04-08 11:40:41 +10:00
|
|
|
longer available. You should make a similar call to the session object
|
|
|
|
instead.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-01-23 11:14:41 -06:00
|
|
|
def from_response(response, body, url, method=None):
|
2011-01-25 14:01:22 -06:00
|
|
|
"""
|
2014-07-11 13:29:34 -04:00
|
|
|
Return an instance of an ClientException or subclass
|
2015-12-30 14:59:29 +08:00
|
|
|
based on a requests response.
|
2011-01-25 14:01:22 -06:00
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
2012-12-18 14:05:29 -06:00
|
|
|
resp, body = requests.request(...)
|
|
|
|
if resp.status_code != 200:
|
|
|
|
raise exception_from_response(resp, rest.text)
|
2011-01-25 14:01:22 -06:00
|
|
|
"""
|
2014-09-18 13:07:17 +02:00
|
|
|
cls = _code_map.get(response.status_code, ClientException)
|
|
|
|
|
2013-04-29 11:33:32 -07:00
|
|
|
kwargs = {
|
2014-07-11 13:31:14 -04:00
|
|
|
'code': response.status_code,
|
2013-04-29 11:33:32 -07:00
|
|
|
'method': method,
|
|
|
|
'url': url,
|
|
|
|
'request_id': None,
|
|
|
|
}
|
|
|
|
|
2012-12-18 14:05:29 -06:00
|
|
|
if response.headers:
|
2013-04-29 11:33:32 -07:00
|
|
|
kwargs['request_id'] = response.headers.get('x-compute-request-id')
|
|
|
|
|
2014-09-18 13:07:17 +02:00
|
|
|
if (issubclass(cls, RetryAfterException) and
|
|
|
|
'retry-after' in response.headers):
|
2013-04-29 11:34:24 -07:00
|
|
|
kwargs['retry_after'] = response.headers.get('retry-after')
|
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
if body:
|
|
|
|
message = "n/a"
|
|
|
|
details = "n/a"
|
2013-04-29 11:33:32 -07:00
|
|
|
|
2011-01-25 14:01:22 -06:00
|
|
|
if hasattr(body, 'keys'):
|
2016-03-18 19:17:47 -04:00
|
|
|
# NOTE(mriedem): WebOb<1.6.0 will return a nested dict structure
|
|
|
|
# where the error keys to the message/details/code. WebOb>=1.6.0
|
|
|
|
# returns just a response body as a single dict, not nested,
|
|
|
|
# so we have to handle both cases (since we can't trust what we're
|
|
|
|
# given with content_type: application/json either way.
|
|
|
|
if 'message' in body:
|
|
|
|
# WebOb 1.6.0 case
|
|
|
|
message = body.get('message')
|
|
|
|
details = body.get('details')
|
|
|
|
else:
|
|
|
|
# WebOb<1.6.0 where we assume there is a single error message
|
|
|
|
# key to the body that has the message and details.
|
|
|
|
error = body[list(body)[0]]
|
|
|
|
message = error.get('message')
|
|
|
|
details = error.get('details')
|
2013-04-29 11:33:32 -07:00
|
|
|
|
|
|
|
kwargs['message'] = message
|
|
|
|
kwargs['details'] = details
|
|
|
|
|
|
|
|
return cls(**kwargs)
|
2014-06-25 07:53:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ResourceNotFound(Exception):
|
|
|
|
"""Error in getting the resource."""
|
|
|
|
pass
|