Statistics
| Branch: | Tag: | Revision:

root / api / helpers.py @ 5fb55fba

History | View | Annotate | Download (2.6 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
# XXX: most of the keys below are dummy
7
from synnefo.api.errors import Unauthorized
8

    
9
def instance_to_server(instance):
10
    server = {
11
            "id": instance["name"],
12
            "name": instance["name"],
13
            "hostId": instance["pnode"],
14
            "imageRef": 1,
15
            "flavorRef": 1,
16
            "addresses": {
17
                "public": [ ],
18
                "private": [ ],
19
                },
20
            "metadata": { }
21
    }
22
    if instance["status"] == "running":
23
        server["status"] = "ACTIVE"
24
    elif instance["status"] == "ADMIN_down":
25
        server["status"] = "SUSPENDED"
26
    else:
27
        server["status"] = "UNKNOWN"
28

    
29
    return server
30

    
31

    
32
def paginator(func):
33
    """
34
    A dummy paginator decorator that uses limit/offset query parameters to
35
    limit the result set of a view. The view must return a dict with a single
36
    key and an iterable for its value.
37

38
    This doesn't actually speed up the internal processing, but it's useful to
39
    easily provide compatibility for the API
40
    """
41
    def inner_func(self, request, *args, **kwargs):
42
        resp = func(self, request, *args, **kwargs)
43
        if 'limit' not in request.GET or 'offset' not in request.GET:
44
            return resp
45

    
46
        # handle structures such as { '
47
        if len(resp.keys()) != 1:
48
            return resp
49
        key = resp.keys()[0]
50
        full = resp.values()[0]
51

    
52
        try:
53
            limit = int(request.GET['limit'])
54
            offset = int(request.GET['offset'])
55
            if offset < 0:
56
                raise ValueError
57
            if limit < 0:
58
                raise ValueError
59
            limit = limit + offset
60
            partial = full[offset:limit]
61
            return { key: partial }
62
        except (ValueError, TypeError):
63
            return { key: [] }
64

    
65
    return inner_func
66

    
67
def authenticate(func):
68
    """
69
    Custom authentication filter supporting the OpenStack API protocol.
70

71
    All API methods are required to go through this. Temporarily implemented as
72
    a decorator until we find a way to apply it to all incoming requests.
73
    """
74
    def inner(self, request, *args, **kwargs):
75
        if 'X-Auth-Token' in request.META:
76
            return func(self, request, *args, **kwargs)
77

    
78
        #An authentication request
79
        if 'X-Auth-User' in request.META and 'X-Auth-Key' in request.META \
80
           and '/v1.0' == request.path and 'GET' == request.method:
81
            #Do authenticate or redirect
82
            return
83

    
84
        raise Unauthorized
85

    
86
    return inner