Statistics
| Branch: | Tag: | Revision:

root / api / faults.py @ 00b4f1be

History | View | Annotate | Download (2.2 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

    
9
class _fault_factory(object):
10
    """
11
    Openstack API Faults factory
12
    """
13

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

    
65
    def __getattr__(self, attr):
66
        try:
67
            m = self.faults.get(attr)
68
        except TypeError:
69
            raise AttributeError(attr)
70

    
71
        # XXX: piston > 0.2.2 does the serialization for us, but be compatible
72
        message = simplejson.dumps({ attr: m }, ensure_ascii=False, indent=4)
73
        code = m['code']
74

    
75
        return HttpResponse(message, status=code)
76

    
77

    
78
fault = _fault_factory()
79
noContent = HttpResponse(status=204)
80
accepted = HttpResponse(status=202)