~asomya/+junk/quantum-dev

« back to all changes in this revision

Viewing changes to quantum/api/faults.py

* Merged changes from Salvatore's branch - quantum-api-workinprogress
* Removed spurious methods from quantum_base_plugin class.
* Updated the sample plugins to be compliant with the new QuantumBase class.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 Citrix Systems.
 
4
# All Rights Reserved.
 
5
#
 
6
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
7
#    not use this file except in compliance with the License. You may obtain
 
8
#    a copy of the License at
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
#    Unless required by applicable law or agreed to in writing, software
 
13
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
 
 
19
import webob.dec
 
20
import webob.exc
 
21
 
 
22
from quantum.api import api_common as common
 
23
from quantum.common import wsgi
 
24
 
 
25
 
 
26
class Fault(webob.exc.HTTPException):
 
27
    """Error codes for API faults"""
 
28
 
 
29
    _fault_names = {
 
30
            400: "malformedRequest",
 
31
            401: "unauthorized",
 
32
            420: "networkNotFound",
 
33
            421: "networkInUse",
 
34
            430: "portNotFound",
 
35
            431: "requestedStateInvalid",
 
36
            432: "portInUse",
 
37
            440: "alreadyAttached",
 
38
            470: "serviceUnavailable",
 
39
            471: "pluginFault"
 
40
    }
 
41
 
 
42
    def __init__(self, exception):
 
43
        """Create a Fault for the given webob.exc.exception."""
 
44
        self.wrapped_exc = exception
 
45
 
 
46
    @webob.dec.wsgify(RequestClass=wsgi.Request)
 
47
    def __call__(self, req):
 
48
        """Generate a WSGI response based on the exception passed to ctor."""
 
49
        # Replace the body with fault details.
 
50
        code = self.wrapped_exc.status_int
 
51
        fault_name = self._fault_names.get(code, "quantumServiceFault")
 
52
        fault_data = {
 
53
            fault_name: {
 
54
                'code': code,
 
55
                'message': self.wrapped_exc.explanation, 
 
56
                'detail': self.wrapped_exc.detail}}
 
57
        # 'code' is an attribute on the fault tag itself
 
58
        metadata = {'application/xml': {'attributes': {fault_name: 'code'}}}
 
59
        default_xmlns = common.XML_NS_V10
 
60
        serializer = wsgi.Serializer(metadata, default_xmlns)
 
61
        content_type = req.best_match_content_type()
 
62
        self.wrapped_exc.body = serializer.serialize(fault_data, content_type)
 
63
        self.wrapped_exc.content_type = content_type
 
64
        return self.wrapped_exc
 
65
 
 
66
 
 
67
class NetworkNotFound(webob.exc.HTTPClientError):
 
68
    """
 
69
    subclass of :class:`~HTTPClientError`
 
70
 
 
71
    This indicates that the server did not find the network specified
 
72
    in the HTTP request
 
73
 
 
74
    code: 420, title: Network not Found
 
75
    """
 
76
    code = 420
 
77
    title = 'Network not Found'
 
78
    explanation = ('Unable to find a network with the specified identifier.')
 
79
 
 
80
 
 
81
class NetworkInUse(webob.exc.HTTPClientError):
 
82
    """
 
83
    subclass of :class:`~HTTPClientError`
 
84
 
 
85
    This indicates that the server could not delete the network as there is
 
86
    at least an attachment plugged into its ports
 
87
 
 
88
    code: 421, title: Network In Use
 
89
    """
 
90
    code = 421
 
91
    title = 'Network in Use'
 
92
    explanation = ('Unable to remove the network: attachments still plugged.')
 
93
 
 
94
 
 
95
class PortNotFound(webob.exc.HTTPClientError):
 
96
    """
 
97
    subclass of :class:`~HTTPClientError`
 
98
 
 
99
    This indicates that the server did not find the port specified
 
100
    in the HTTP request for a given network
 
101
 
 
102
    code: 430, title: Port not Found
 
103
    """
 
104
    code = 430
 
105
    title = 'Port not Found'
 
106
    explanation = ('Unable to find a port with the specified identifier.')
 
107
 
 
108
 
 
109
class RequestedStateInvalid(webob.exc.HTTPClientError):
 
110
    """
 
111
    subclass of :class:`~HTTPClientError`
 
112
 
 
113
    This indicates that the server could not update the port state to
 
114
    to the request value
 
115
 
 
116
    code: 431, title: Requested State Invalid
 
117
    """
 
118
    code = 431
 
119
    title = 'Requested State Invalid'
 
120
    explanation = ('Unable to update port state with specified value.')
 
121
 
 
122
 
 
123
class PortInUse(webob.exc.HTTPClientError):
 
124
    """
 
125
    subclass of :class:`~HTTPClientError`
 
126
 
 
127
    This indicates that the server could not remove o port or attach
 
128
    a resource to it because there is an attachment plugged into the port
 
129
 
 
130
    code: 432, title: PortInUse
 
131
    """
 
132
    code = 432
 
133
    title = 'Port in Use'
 
134
    explanation = ('A resource is currently attached to the logical port')
 
135
 
 
136
 
 
137
class AlreadyAttached(webob.exc.HTTPClientError):
 
138
    """
 
139
    subclass of :class:`~HTTPClientError`
 
140
 
 
141
    This indicates that the server refused an attempt to re-attach a resource
 
142
    already attached to the network
 
143
 
 
144
    code: 440, title: AlreadyAttached
 
145
    """
 
146
    code = 440
 
147
    title = 'Already Attached'
 
148
    explanation = ('The resource is already attached to another port')