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