Statistics
| Branch: | Tag: | Revision:

root / api / faults.py @ 6130effc

History | View | Annotate | Download (2.5 kB)

1 00b4f1be Faidon Liambotis
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8
2 00b4f1be Faidon Liambotis
#
3 00b4f1be Faidon Liambotis
# Copyright © 2010 Greek Research and Technology Network
4 00b4f1be Faidon Liambotis
#
5 00b4f1be Faidon Liambotis
6 00b4f1be Faidon Liambotis
from django.http import HttpResponse
7 00b4f1be Faidon Liambotis
from django.utils import simplejson
8 635cfd6e Faidon Liambotis
from piston.utils import HttpStatusCode
9 635cfd6e Faidon Liambotis
10 635cfd6e Faidon Liambotis
class Fault(HttpStatusCode):
11 635cfd6e Faidon Liambotis
    """Fault Exception"""
12 635cfd6e Faidon Liambotis
    pass
13 00b4f1be Faidon Liambotis
14 00b4f1be Faidon Liambotis
class _fault_factory(object):
15 00b4f1be Faidon Liambotis
    """
16 00b4f1be Faidon Liambotis
    Openstack API Faults factory
17 00b4f1be Faidon Liambotis
    """
18 00b4f1be Faidon Liambotis
19 00b4f1be Faidon Liambotis
    faults = {
20 00b4f1be Faidon Liambotis
        'serviceUnavailable': {
21 00b4f1be Faidon Liambotis
                'code': 503,
22 00b4f1be Faidon Liambotis
                'message': 'Service Unavailable',
23 00b4f1be Faidon Liambotis
            },
24 00b4f1be Faidon Liambotis
        'unauthorized': {
25 00b4f1be Faidon Liambotis
                'code': 401,
26 00b4f1be Faidon Liambotis
                'message': 'Unauthorized',
27 00b4f1be Faidon Liambotis
            },
28 00b4f1be Faidon Liambotis
        'badRequest': {
29 00b4f1be Faidon Liambotis
                'code': 400,
30 00b4f1be Faidon Liambotis
                'message': 'Bad request',
31 00b4f1be Faidon Liambotis
            },
32 00b4f1be Faidon Liambotis
        'overLimit': {
33 00b4f1be Faidon Liambotis
                'code': 413,
34 00b4f1be Faidon Liambotis
                'message': 'Overlimit',
35 00b4f1be Faidon Liambotis
            },
36 00b4f1be Faidon Liambotis
        'badMediaType': {
37 00b4f1be Faidon Liambotis
                'code': 415,
38 00b4f1be Faidon Liambotis
                'message': 'Bad media type',
39 00b4f1be Faidon Liambotis
            },
40 00b4f1be Faidon Liambotis
        'badMethod': {
41 00b4f1be Faidon Liambotis
                'code': 405,
42 00b4f1be Faidon Liambotis
                'message': 'Bad method',
43 00b4f1be Faidon Liambotis
            },
44 00b4f1be Faidon Liambotis
        'itemNotFound': {
45 00b4f1be Faidon Liambotis
                'code': 404,
46 00b4f1be Faidon Liambotis
                'message': 'Not Found',
47 00b4f1be Faidon Liambotis
            },
48 00b4f1be Faidon Liambotis
        'buildInProgress': {
49 00b4f1be Faidon Liambotis
                'code': 409,
50 00b4f1be Faidon Liambotis
                'message': 'Build in progress',
51 00b4f1be Faidon Liambotis
            },
52 00b4f1be Faidon Liambotis
        'serverCapacityUnavailable': {
53 00b4f1be Faidon Liambotis
                'code': 503,
54 00b4f1be Faidon Liambotis
                'message': 'Server capacity unavailable',
55 00b4f1be Faidon Liambotis
            },
56 00b4f1be Faidon Liambotis
        'backupOrResizeInProgress': {
57 00b4f1be Faidon Liambotis
                'code': 409,
58 00b4f1be Faidon Liambotis
                'message': 'Backup or resize in progress',
59 00b4f1be Faidon Liambotis
            },
60 00b4f1be Faidon Liambotis
        'resizeNotAllowed': {
61 00b4f1be Faidon Liambotis
                'code': 403,
62 00b4f1be Faidon Liambotis
                'message': 'Resize not allowed',
63 00b4f1be Faidon Liambotis
            },
64 00b4f1be Faidon Liambotis
        'notImplemented': {
65 00b4f1be Faidon Liambotis
                'code': 501,
66 00b4f1be Faidon Liambotis
                'message': 'Not Implemented',
67 00b4f1be Faidon Liambotis
            },
68 00b4f1be Faidon Liambotis
        }
69 00b4f1be Faidon Liambotis
70 00b4f1be Faidon Liambotis
    def __getattr__(self, attr):
71 00b4f1be Faidon Liambotis
        try:
72 00b4f1be Faidon Liambotis
            m = self.faults.get(attr)
73 00b4f1be Faidon Liambotis
        except TypeError:
74 00b4f1be Faidon Liambotis
            raise AttributeError(attr)
75 00b4f1be Faidon Liambotis
76 14a6a08f Faidon Liambotis
        # details are not supported for now
77 14a6a08f Faidon Liambotis
        m['details'] = ''
78 14a6a08f Faidon Liambotis
79 14a6a08f Faidon Liambotis
        # piston > 0.2.2 does the serialization for us, but be compatible
80 b9809f7c Faidon Liambotis
        # 'till the next version gets released. XXX: this doesn't do XML!
81 00b4f1be Faidon Liambotis
        message = simplejson.dumps({ attr: m }, ensure_ascii=False, indent=4)
82 00b4f1be Faidon Liambotis
        code = m['code']
83 635cfd6e Faidon Liambotis
        response = HttpResponse(message, status=code)
84 00b4f1be Faidon Liambotis
85 635cfd6e Faidon Liambotis
        return Fault(response)
86 00b4f1be Faidon Liambotis
87 00b4f1be Faidon Liambotis
88 00b4f1be Faidon Liambotis
fault = _fault_factory()
89 635cfd6e Faidon Liambotis
90 635cfd6e Faidon Liambotis
# these are in the 2xx range, hence not faults/exceptions
91 00b4f1be Faidon Liambotis
noContent = HttpResponse(status=204)
92 00b4f1be Faidon Liambotis
accepted = HttpResponse(status=202)
93 ed66d976 Faidon Liambotis
created = HttpResponse(status=201)