2014-01-28 17:02:25 -08:00
|
|
|
# Copyright 2011 VMware, Inc
|
2012-01-10 17:37:11 -08:00
|
|
|
# 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.
|
|
|
|
|
2015-12-09 06:22:03 +09:00
|
|
|
from oslo_utils import encodeutils
|
2015-12-09 06:07:50 +09:00
|
|
|
import six
|
|
|
|
|
2015-11-28 09:16:42 +09:00
|
|
|
from neutronclient._i18n import _
|
2013-01-19 21:39:40 +08:00
|
|
|
|
2012-01-10 17:37:11 -08:00
|
|
|
"""
|
2013-07-02 18:44:42 -04:00
|
|
|
Neutron base exception handling.
|
2014-03-22 19:50:08 +09:00
|
|
|
|
|
|
|
Exceptions are classified into three categories:
|
|
|
|
* Exceptions corresponding to exceptions from neutron server:
|
|
|
|
This type of exceptions should inherit one of exceptions
|
|
|
|
in HTTP_EXCEPTION_MAP.
|
|
|
|
* Exceptions from client library:
|
|
|
|
This type of exceptions should inherit NeutronClientException.
|
|
|
|
* Exceptions from CLI code:
|
|
|
|
This type of exceptions should inherit NeutronCLIError.
|
2012-01-10 17:37:11 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2015-12-09 06:22:03 +09:00
|
|
|
# NOTE: This method is defined here to avoid
|
|
|
|
# an import loop between common.utils and this module.
|
|
|
|
def _safe_decode_dict(kwargs):
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
kwargs[k] = encodeutils.safe_decode(v)
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2015-12-09 06:07:50 +09:00
|
|
|
@six.python_2_unicode_compatible
|
2013-07-02 18:44:42 -04:00
|
|
|
class NeutronException(Exception):
|
2014-08-21 15:45:10 +03:00
|
|
|
"""Base Neutron Exception.
|
2012-01-10 17:37:11 -08:00
|
|
|
|
|
|
|
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.")
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
def __init__(self, message=None, **kwargs):
|
|
|
|
if message:
|
|
|
|
self.message = message
|
2012-01-10 17:37:11 -08:00
|
|
|
try:
|
2015-12-09 06:22:03 +09:00
|
|
|
self._error_string = self.message % _safe_decode_dict(kwargs)
|
2012-01-10 17:37:11 -08:00
|
|
|
except Exception:
|
|
|
|
# at least get the core message out if something happened
|
|
|
|
self._error_string = self.message
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self._error_string
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class NeutronClientException(NeutronException):
|
|
|
|
"""Base exception which exceptions from Neutron are mapped into.
|
2012-01-10 17:37:11 -08:00
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
NOTE: on the client side, we use different exception types in order
|
|
|
|
to allow client library users to handle server exceptions in try...except
|
|
|
|
blocks. The actual error message is the one generated on the server side.
|
|
|
|
"""
|
2012-01-10 17:37:11 -08:00
|
|
|
|
2014-06-06 17:32:08 +10:00
|
|
|
status_code = 0
|
2016-02-24 19:28:28 +09:00
|
|
|
req_ids_msg = _("Neutron server returns request_ids: %s")
|
|
|
|
request_ids = []
|
2014-06-06 17:32:08 +10:00
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
def __init__(self, message=None, **kwargs):
|
2016-02-24 19:28:28 +09:00
|
|
|
self.request_ids = kwargs.get('request_ids')
|
2014-03-22 19:50:08 +09:00
|
|
|
if 'status_code' in kwargs:
|
|
|
|
self.status_code = kwargs['status_code']
|
2016-02-24 19:28:28 +09:00
|
|
|
if self.request_ids:
|
|
|
|
req_ids_msg = self.req_ids_msg % self.request_ids
|
|
|
|
if message:
|
2016-03-04 13:48:15 +05:30
|
|
|
message = _('%(msg)s\n%(id)s') % {'msg': message,
|
|
|
|
'id': req_ids_msg}
|
2016-02-24 19:28:28 +09:00
|
|
|
else:
|
|
|
|
message = req_ids_msg
|
2014-03-22 19:50:08 +09:00
|
|
|
super(NeutronClientException, self).__init__(message, **kwargs)
|
2012-01-10 17:37:11 -08:00
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
# Base exceptions from Neutron
|
2012-01-10 17:37:11 -08:00
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class BadRequest(NeutronClientException):
|
|
|
|
status_code = 400
|
2012-01-10 17:37:11 -08:00
|
|
|
|
2012-02-13 14:38:17 +00:00
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class Unauthorized(NeutronClientException):
|
|
|
|
status_code = 401
|
|
|
|
message = _("Unauthorized: bad credentials.")
|
2012-01-10 17:37:11 -08:00
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class Forbidden(NeutronClientException):
|
|
|
|
status_code = 403
|
|
|
|
message = _("Forbidden: your credentials don't give you access to this "
|
|
|
|
"resource.")
|
|
|
|
|
|
|
|
|
|
|
|
class NotFound(NeutronClientException):
|
|
|
|
status_code = 404
|
|
|
|
|
|
|
|
|
|
|
|
class Conflict(NeutronClientException):
|
|
|
|
status_code = 409
|
|
|
|
|
|
|
|
|
|
|
|
class InternalServerError(NeutronClientException):
|
|
|
|
status_code = 500
|
2012-02-02 15:01:49 +00:00
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class ServiceUnavailable(NeutronClientException):
|
|
|
|
status_code = 503
|
|
|
|
|
|
|
|
|
|
|
|
HTTP_EXCEPTION_MAP = {
|
|
|
|
400: BadRequest,
|
|
|
|
401: Unauthorized,
|
|
|
|
403: Forbidden,
|
|
|
|
404: NotFound,
|
|
|
|
409: Conflict,
|
|
|
|
500: InternalServerError,
|
|
|
|
503: ServiceUnavailable,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Exceptions mapped to Neutron server exceptions
|
|
|
|
# These are defined if a user of client library needs specific exception.
|
|
|
|
# Exception name should be <Neutron Exception Name> + 'Client'
|
|
|
|
# e.g., NetworkNotFound -> NetworkNotFoundClient
|
|
|
|
|
|
|
|
class NetworkNotFoundClient(NotFound):
|
2012-01-10 17:37:11 -08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class PortNotFoundClient(NotFound):
|
2012-01-10 17:37:11 -08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class StateInvalidClient(BadRequest):
|
2012-01-10 17:37:11 -08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class NetworkInUseClient(Conflict):
|
2013-11-04 14:05:09 -08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class PortInUseClient(Conflict):
|
2012-01-10 17:37:11 -08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class IpAddressInUseClient(Conflict):
|
2013-09-27 17:04:44 +09:30
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-09-16 16:09:21 +08:00
|
|
|
class InvalidIpForNetworkClient(BadRequest):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-03-06 17:20:06 +08:00
|
|
|
class InvalidIpForSubnetClient(BadRequest):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-04-22 14:20:02 +08:00
|
|
|
class OverQuotaClient(Conflict):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class IpAddressGenerationFailureClient(Conflict):
|
|
|
|
pass
|
2012-05-18 09:02:29 +08:00
|
|
|
|
|
|
|
|
2014-07-23 09:27:46 -07:00
|
|
|
class MacAddressInUseClient(Conflict):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-07-22 17:27:32 -06:00
|
|
|
class HostNotCompatibleWithFixedIpsClient(Conflict):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class ExternalIpAddressExhaustedClient(BadRequest):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Exceptions from client library
|
|
|
|
|
|
|
|
class NoAuthURLProvided(Unauthorized):
|
|
|
|
message = _("auth_url was not provided to the Neutron client")
|
2012-05-18 09:02:29 +08:00
|
|
|
|
|
|
|
|
2013-07-02 18:44:42 -04:00
|
|
|
class EndpointNotFound(NeutronClientException):
|
2013-02-14 12:04:26 +00:00
|
|
|
message = _("Could not find Service or Region in Service Catalog.")
|
2012-02-13 14:38:17 +00:00
|
|
|
|
|
|
|
|
2013-07-02 18:44:42 -04:00
|
|
|
class EndpointTypeNotFound(NeutronClientException):
|
2014-03-22 19:50:08 +09:00
|
|
|
message = _("Could not find endpoint type %(type_)s in Service Catalog.")
|
2013-04-30 15:20:10 -06:00
|
|
|
|
|
|
|
|
2013-07-02 18:44:42 -04:00
|
|
|
class AmbiguousEndpoints(NeutronClientException):
|
2014-03-22 19:50:08 +09:00
|
|
|
message = _("Found more than one matching endpoint in Service Catalog: "
|
|
|
|
"%(matching_endpoints)")
|
2012-01-10 17:37:11 -08:00
|
|
|
|
|
|
|
|
2013-07-02 18:44:42 -04:00
|
|
|
class RequestURITooLong(NeutronClientException):
|
2013-05-03 19:46:11 +02:00
|
|
|
"""Raised when a request fails with HTTP error 414."""
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.excess = kwargs.get('excess', 0)
|
|
|
|
super(RequestURITooLong, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
|
2013-07-02 18:44:42 -04:00
|
|
|
class ConnectionFailed(NeutronClientException):
|
|
|
|
message = _("Connection to neutron failed: %(reason)s")
|
2012-03-02 15:44:51 -08:00
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class SslCertificateValidationError(NeutronClientException):
|
|
|
|
message = _("SSL certificate validation has failed: %(reason)s")
|
2012-01-10 17:37:11 -08:00
|
|
|
|
2013-10-23 16:41:45 -05:00
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class MalformedResponseBody(NeutronClientException):
|
|
|
|
message = _("Malformed response body: %(reason)s")
|
2013-10-23 16:41:45 -05:00
|
|
|
|
2012-02-13 14:38:17 +00:00
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class InvalidContentType(NeutronClientException):
|
|
|
|
message = _("Invalid content type %(content_type)s.")
|
2012-02-13 14:38:17 +00:00
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
# Command line exceptions
|
2012-02-13 14:38:17 +00:00
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class NeutronCLIError(NeutronException):
|
|
|
|
"""Exception raised when command line parsing fails."""
|
2012-02-13 14:38:17 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class CommandError(NeutronCLIError):
|
|
|
|
pass
|
2012-05-18 09:02:29 +08:00
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class UnsupportedVersion(NeutronCLIError):
|
2015-12-03 15:40:14 +09:00
|
|
|
"""Indicates usage of an unsupported API version
|
|
|
|
|
|
|
|
Indicates that the user is trying to use an unsupported version of
|
2014-08-21 15:45:10 +03:00
|
|
|
the API.
|
2013-04-05 23:20:45 +00:00
|
|
|
"""
|
2012-05-18 09:02:29 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-03-22 19:50:08 +09:00
|
|
|
class NeutronClientNoUniqueMatch(NeutronCLIError):
|
2013-07-11 10:54:59 -07:00
|
|
|
message = _("Multiple %(resource)s matches found for name '%(name)s',"
|
|
|
|
" use an ID to be more specific.")
|