Statistics
| Branch: | Tag: | Revision:

root / api / helpers.py @ 6130effc

History | View | Annotate | Download (1.8 kB)

1 253f0c82 Faidon Liambotis
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8
2 253f0c82 Faidon Liambotis
#
3 253f0c82 Faidon Liambotis
# Copyright © 2010 Greek Research and Technology Network
4 253f0c82 Faidon Liambotis
#
5 253f0c82 Faidon Liambotis
6 253f0c82 Faidon Liambotis
# XXX: most of the keys below are dummy
7 253f0c82 Faidon Liambotis
def instance_to_server(instance):
8 253f0c82 Faidon Liambotis
    server = {
9 253f0c82 Faidon Liambotis
            "id": instance["name"],
10 253f0c82 Faidon Liambotis
            "name": instance["name"],
11 253f0c82 Faidon Liambotis
            "hostId": instance["pnode"],
12 253f0c82 Faidon Liambotis
            "imageId": 1,
13 253f0c82 Faidon Liambotis
            "flavorId": 1,
14 253f0c82 Faidon Liambotis
            "addresses": {
15 253f0c82 Faidon Liambotis
                "public": [ ],
16 253f0c82 Faidon Liambotis
                "private": [ ],
17 253f0c82 Faidon Liambotis
                },
18 253f0c82 Faidon Liambotis
            "metadata": { }
19 253f0c82 Faidon Liambotis
    }
20 253f0c82 Faidon Liambotis
    if instance["status"] == "running":
21 253f0c82 Faidon Liambotis
        server["status"] = "ACTIVE"
22 253f0c82 Faidon Liambotis
    elif instance["status"] == "ADMIN_down":
23 253f0c82 Faidon Liambotis
        server["status"] = "SUSPENDED"
24 253f0c82 Faidon Liambotis
    else:
25 253f0c82 Faidon Liambotis
        server["status"] = "UNKNOWN"
26 253f0c82 Faidon Liambotis
27 253f0c82 Faidon Liambotis
    return server
28 2c089b77 Faidon Liambotis
29 2c089b77 Faidon Liambotis
30 2c089b77 Faidon Liambotis
def paginator(func):
31 2c089b77 Faidon Liambotis
    """
32 2c089b77 Faidon Liambotis
    A dummy paginator decorator that uses limit/offset query parameters to
33 2c089b77 Faidon Liambotis
    limit the result set of a view. The view must return a dict with a single
34 2c089b77 Faidon Liambotis
    key and an iterable for its value.
35 2c089b77 Faidon Liambotis

36 2c089b77 Faidon Liambotis
    This doesn't actually speed up the internal processing, but it's useful to
37 2c089b77 Faidon Liambotis
    easily provide compatibility for the API
38 2c089b77 Faidon Liambotis
    """
39 2c089b77 Faidon Liambotis
    def inner_func(self, request, *args, **kwargs):
40 2c089b77 Faidon Liambotis
        resp = func(self, request, *args, **kwargs)
41 2c089b77 Faidon Liambotis
        if 'limit' not in request.GET or 'offset' not in request.GET:
42 2c089b77 Faidon Liambotis
            return resp
43 2c089b77 Faidon Liambotis
44 2c089b77 Faidon Liambotis
        # handle structures such as { '
45 2c089b77 Faidon Liambotis
        if len(resp.keys()) != 1:
46 2c089b77 Faidon Liambotis
            return resp
47 2c089b77 Faidon Liambotis
        key = resp.keys()[0]
48 2c089b77 Faidon Liambotis
        full = resp.values()[0]
49 2c089b77 Faidon Liambotis
50 2c089b77 Faidon Liambotis
        try:
51 2c089b77 Faidon Liambotis
            limit = int(request.GET['limit'])
52 2c089b77 Faidon Liambotis
            offset = int(request.GET['offset'])
53 2c089b77 Faidon Liambotis
            if offset < 0:
54 2c089b77 Faidon Liambotis
                raise ValueError
55 2c089b77 Faidon Liambotis
            if limit < 0:
56 2c089b77 Faidon Liambotis
                raise ValueError
57 2c089b77 Faidon Liambotis
            limit = limit + offset
58 2c089b77 Faidon Liambotis
            partial = full[offset:limit]
59 2c089b77 Faidon Liambotis
            return { key: partial }
60 2c089b77 Faidon Liambotis
        except (ValueError, TypeError):
61 2c089b77 Faidon Liambotis
            return { key: [] }
62 2c089b77 Faidon Liambotis
63 2c089b77 Faidon Liambotis
    return inner_func