make GetLeases reachable with anon auth
[plcapi.git] / PLC / Methods / GetLeases.py
1 # Thierry Parmentelat -- INRIA
2
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Filter import Filter
6 from PLC.Auth import Auth
7 from PLC.Leases import Lease, Leases, LeaseFilter
8
9 class GetLeases(Method):
10     """
11     Returns an array of structs containing details about leases. If
12     lease_filter is specified and is an array of lease identifiers or
13     lease names, or a struct of lease attributes, only leases matching
14     the filter will be returned. If return_fields is specified, only the
15     specified details will be returned.
16
17     All leases are exposed to all users.
18
19     In addition to the usual filter capabilities, the following are supported:
20      * GetLeases ({ 'alive' : '2010-02-20 20:00' , <regular_filter_fields...> })
21        returns the leases that are active at that point in time
22      * GetLeases ({ 'alive' : ('2010-02-20 20:00' , '2010-02-20 21:00' ) , ... })
23        ditto for a time range
24
25     This is implemented in the LeaseFilter class; negation actually is supported
26     through the usual '~alive' form, although maybe not really useful.
27
28     """
29
30     roles = ['admin', 'pi', 'user', 'node', 'anonymous']
31
32     accepts = [
33         Auth(),
34         Mixed(Lease.fields['lease_id'],
35               [Lease.fields['lease_id']],
36               LeaseFilter(Lease.fields)),
37         Parameter([str], "List of fields to return", nullok = True)
38         ]
39
40     returns = [Lease.fields]
41
42     def call(self, auth, lease_filter = None, return_fields = None):
43
44         # Must query at least lease_id (see below)
45         if return_fields is not None and 'lease_id' not in return_fields:
46             return_fields.append('lease_id')
47             added_fields = True
48         else:
49             added_fields = False
50
51         leases = Leases(self.api, lease_filter, return_fields)
52
53         # Remove lease_id if not specified
54         if added_fields:
55             for lease in leases:
56                 if 'lease_id' in lease:
57                     del lease['lease_id']
58
59         return leases