summaryrefslogtreecommitdiff
path: root/oslo_vmware/exceptions.py
blob: 96b6c56e2ef82924ec4ef86eca9270dcd6a35846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Copyright (c) 2014 VMware, Inc.
# 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.

"""
Exception definitions.
"""

import logging

from oslo_vmware._i18n import _

LOG = logging.getLogger(__name__)

ALREADY_EXISTS = 'AlreadyExists'
CANNOT_DELETE_FILE = 'CannotDeleteFile'
DUPLICATE_NAME = 'DuplicateName'
FILE_ALREADY_EXISTS = 'FileAlreadyExists'
FILE_FAULT = 'FileFault'
FILE_LOCKED = 'FileLocked'
FILE_NOT_FOUND = 'FileNotFound'
INVALID_POWER_STATE = 'InvalidPowerState'
INVALID_PROPERTY = 'InvalidProperty'
NO_DISK_SPACE = 'NoDiskSpace'
NO_PERMISSION = 'NoPermission'
NOT_AUTHENTICATED = 'NotAuthenticated'
SECURITY_ERROR = "SecurityError"
MANAGED_OBJECT_NOT_FOUND = 'ManagedObjectNotFound'
TASK_IN_PROGRESS = 'TaskInProgress'
TOOLS_UNAVAILABLE = 'ToolsUnavailable'


class VMwareDriverException(Exception):
    """Base oslo.vmware 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.")

    def __str__(self):
        return self.description

    def __init__(self, message=None, details=None, **kwargs):

        if message is not None and isinstance(message, list):
            # we need this to protect against developers using
            # this method like VimFaultException
            raise ValueError(_("exception message must not be a list"))

        if details is not None and not isinstance(details, dict):
            raise ValueError(_("details must be a dict"))

        self.kwargs = kwargs
        self.details = details
        self.cause = None

        if not message:
            try:
                message = self.msg_fmt % kwargs
            except Exception:
                # kwargs doesn't match a variable in the message
                # log the issue and the kwargs
                LOG.exception('Exception in string format operation')
                for name, value in kwargs.items():
                    LOG.error("%(name)s: %(value)s",
                              {'name': name, 'value': value})
                # at least get the core message out if something happened
                message = self.msg_fmt

        self.message = message
        super(VMwareDriverException, self).__init__(message)

    @property
    def msg(self):
        return self.message

    @property
    def description(self):
        # NOTE(jecarey): self.msg and self.cause may be i18n objects
        # that do not support str or concatenation, but can be used
        # as replacement text.
        descr = str(self.msg)
        if self.cause:
            descr += '\nCause: ' + str(self.cause)
        return descr


class VimException(VMwareDriverException):
    """The base exception class for all VIM related exceptions."""

    def __init__(self, message=None, cause=None, details=None, **kwargs):
        super(VimException, self).__init__(message, details, **kwargs)
        self.cause = cause


class VimSessionOverLoadException(VMwareDriverException):
    """Thrown when there is an API call overload at the VMware server."""

    def __init__(self, message, cause=None):
        super(VimSessionOverLoadException, self).__init__(message)
        self.cause = cause


class VimConnectionException(VMwareDriverException):
    """Thrown when there is a connection problem."""

    def __init__(self, message, cause=None):
        super(VimConnectionException, self).__init__(message)
        self.cause = cause


class VimAttributeException(VMwareDriverException):
    """Thrown when a particular attribute cannot be found."""

    def __init__(self, message, cause=None):
        super(VimAttributeException, self).__init__(message)
        self.cause = cause


class VimFaultException(VimException):
    """Exception thrown when there are unrecognized VIM faults."""

    def __init__(self, fault_list, message, cause=None, details=None):
        super(VimFaultException, self).__init__(message, cause, details)
        if not isinstance(fault_list, list):
            raise ValueError(_("fault_list must be a list"))
        self.fault_list = fault_list

    @property
    def description(self):
        descr = VimException.description.fget(self)
        if self.fault_list:
            # fault_list doesn't contain non-ASCII chars, we can use str()
            descr += '\nFaults: ' + str(self.fault_list)
        if self.details:
            # details may contain non-ASCII values
            details = '{%s}' % ', '.join(["'%s': '%s'" % (k, v) for k, v in
                                          self.details.items()])
            descr += '\nDetails: ' + details
        return descr


class ImageTransferException(VMwareDriverException):
    """Thrown when there is an error during image transfer."""

    def __init__(self, message, cause=None):
        super(ImageTransferException, self).__init__(message)
        self.cause = cause


def _print_deprecation_warning(clazz):
    LOG.warning("Exception %s is deprecated, it will be removed in the "
                "next release.", clazz.__name__)


class VMwareDriverConfigurationException(VMwareDriverException):
    """Base class for all configuration exceptions.
    """
    msg_fmt = _("VMware Driver configuration fault.")

    def __init__(self, message=None, details=None, **kwargs):
        super(VMwareDriverConfigurationException, self).__init__(
            message, details, **kwargs)
        _print_deprecation_warning(self.__class__)


class UseLinkedCloneConfigurationFault(VMwareDriverConfigurationException):
    msg_fmt = _("No default value for use_linked_clone found.")


class MissingParameter(VMwareDriverException):
    msg_fmt = _("Missing parameter : %(param)s")

    def __init__(self, message=None, details=None, **kwargs):
        super(MissingParameter, self).__init__(message, details, **kwargs)
        _print_deprecation_warning(self.__class__)


class AlreadyExistsException(VimException):
    msg_fmt = _("Resource already exists.")
    code = 409


class CannotDeleteFileException(VimException):
    msg_fmt = _("Cannot delete file.")
    code = 403


class FileAlreadyExistsException(VimException):
    msg_fmt = _("File already exists.")
    code = 409


class FileFaultException(VimException):
    msg_fmt = _("File fault.")
    code = 409


class FileLockedException(VimException):
    msg_fmt = _("File locked.")
    code = 403


class FileNotFoundException(VimException):
    msg_fmt = _("File not found.")
    code = 404


class InvalidPowerStateException(VimException):
    msg_fmt = _("Invalid power state.")
    code = 409


class InvalidPropertyException(VimException):
    msg_fmt = _("Invalid property.")
    code = 400


class NoPermissionException(VimException):
    msg_fmt = _("No Permission.")
    code = 403


class NotAuthenticatedException(VimException):
    msg_fmt = _("Not Authenticated.")
    code = 403


class TaskInProgress(VimException):
    msg_fmt = _("Entity has another operation in process.")


class DuplicateName(VimException):
    msg_fmt = _("Duplicate name.")


class NoDiskSpaceException(VimException):
    msg_fmt = _("Insufficient disk space.")


class ToolsUnavailableException(VimException):
    msg_fmt = _("VMware Tools is not running.")


class ManagedObjectNotFoundException(VimException):
    msg_fmt = _("Managed object not found.")
    code = 404


# Populate the fault registry with the exceptions that have
# special treatment.
_fault_classes_registry = {
    ALREADY_EXISTS: AlreadyExistsException,
    CANNOT_DELETE_FILE: CannotDeleteFileException,
    DUPLICATE_NAME: DuplicateName,
    FILE_ALREADY_EXISTS: FileAlreadyExistsException,
    FILE_FAULT: FileFaultException,
    FILE_LOCKED: FileLockedException,
    FILE_NOT_FOUND: FileNotFoundException,
    INVALID_POWER_STATE: InvalidPowerStateException,
    INVALID_PROPERTY: InvalidPropertyException,
    MANAGED_OBJECT_NOT_FOUND: ManagedObjectNotFoundException,
    NO_DISK_SPACE: NoDiskSpaceException,
    NO_PERMISSION: NoPermissionException,
    NOT_AUTHENTICATED: NotAuthenticatedException,
    TASK_IN_PROGRESS: TaskInProgress,
    TOOLS_UNAVAILABLE: ToolsUnavailableException,
}


def get_fault_class(name):
    """Get a named subclass of VimException."""
    name = str(name)
    fault_class = _fault_classes_registry.get(name)
    if not fault_class:
        LOG.debug('Fault %s not matched.', name)
    return fault_class


def translate_fault(localized_method_fault, excep_msg=None):
    """Produce proper VimException subclass object,

    The exception is based on a vmodl.LocalizedMethodFault.

    :param excep_msg: Message to set to the exception. Defaults to
                      localizedMessage of the fault.
    """
    try:
        if not excep_msg:
            excep_msg = str(localized_method_fault.localizedMessage)
        name = localized_method_fault.fault.__class__.__name__
        fault_class = get_fault_class(name)
        if fault_class:
            ex = fault_class(excep_msg)
        else:
            ex = VimFaultException([name], excep_msg)
    except Exception as e:
        LOG.debug("Unexpected exception thrown (%s) while translating"
                  " fault (%s) with message: %s.",
                  e, localized_method_fault, excep_msg)
        ex = VimException(message=excep_msg, cause=e)

    return ex


def register_fault_class(name, exception):
    fault_class = _fault_classes_registry.get(name)
    if not issubclass(exception, VimException):
        raise TypeError(_("exception should be a subclass of "
                          "VimException"))
    if fault_class:
        LOG.debug('Overriding exception for %s', name)
    _fault_classes_registry[name] = exception