Statistics
| Branch: | Tag: | Revision:

root / api / faults.py @ b9809f7c

History | View | Annotate | Download (2.5 kB)

1
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8
2
#
3
# Copyright © 2010 Greek Research and Technology Network
4
#
5

    
6
from django.http import HttpResponse
7
from django.utils import simplejson
8
from piston.utils import HttpStatusCode
9

    
10
class Fault(HttpStatusCode):
11
    """Fault Exception"""
12
    pass
13

    
14
class _fault_factory(object):
15
    """
16
    Openstack API Faults factory
17
    """
18

    
19
    faults = {
20
        'serviceUnavailable': {
21
                'code': 503,
22
                'message': 'Service Unavailable',
23
            },
24
        'unauthorized': {
25
                'code': 401,
26
                'message': 'Unauthorized',
27
            },
28
        'badRequest': {
29
                'code': 400,
30
                'message': 'Bad request',
31
            },
32
        'overLimit': {
33
                'code': 413,
34
                'message': 'Overlimit',
35
            },
36
        'badMediaType': {
37
                'code': 415,
38
                'message': 'Bad media type',
39
            },
40
        'badMethod': {
41
                'code': 405,
42
                'message': 'Bad method',
43
            },
44
        'itemNotFound': {
45
                'code': 404,
46
                'message': 'Not Found',
47
            },
48
        'buildInProgress': {
49
                'code': 409,
50
                'message': 'Build in progress',
51
            },
52
        'serverCapacityUnavailable': {
53
                'code': 503,
54
                'message': 'Server capacity unavailable',
55
            },
56
        'backupOrResizeInProgress': {
57
                'code': 409,
58
                'message': 'Backup or resize in progress',
59
            },
60
        'resizeNotAllowed': {
61
                'code': 403,
62
                'message': 'Resize not allowed',
63
            },
64
        'notImplemented': {
65
                'code': 501,
66
                'message': 'Not Implemented',
67
            },
68
        }
69

    
70
    def __getattr__(self, attr):
71
        try:
72
            m = self.faults.get(attr)
73
        except TypeError:
74
            raise AttributeError(attr)
75

    
76
        # details are not supported for now
77
        m['details'] = ''
78

    
79
        # piston > 0.2.2 does the serialization for us, but be compatible
80
        # 'till the next version gets released. XXX: this doesn't do XML!
81
        message = simplejson.dumps({ attr: m }, ensure_ascii=False, indent=4)
82
        code = m['code']
83
        response = HttpResponse(message, status=code)
84

    
85
        return Fault(response)
86

    
87

    
88
fault = _fault_factory()
89

    
90
# these are in the 2xx range, hence not faults/exceptions
91
noContent = HttpResponse(status=204)
92
accepted = HttpResponse(status=202)
93
created = HttpResponse(status=201)