filters out PLC_ROOT_USER as well
[plcapi.git] / PLC / Methods / GetPeerData.py
1 #
2 # Thierry Parmentelat - INRIA
3
4
5 import time
6
7 from PLC.Faults import *
8 from PLC.Method import Method
9 from PLC.Parameter import Parameter, Mixed
10 from PLC.Auth import Auth
11
12 from PLC.Peers import Peer, Peers
13
14 from PLC.Sites import Site, Sites
15 from PLC.Keys import Key, Keys
16 from PLC.Nodes import Node, Nodes
17 from PLC.Persons import Person, Persons
18 from PLC.Slices import Slice, Slices
19 from PLC.SliceAttributes import SliceAttributes
20
21 class GetPeerData(Method):
22     """
23     Returns lists of local objects that a peer should cache in its
24     database as foreign objects. Also returns the list of foreign
25     nodes in this database, for which the calling peer is
26     authoritative, to assist in synchronization of slivers.
27     
28     See the implementation of RefreshPeer for how this data is used.
29     """
30
31     roles = ['admin', 'peer']
32
33     accepts = [Auth()]
34
35     returns = {
36         'Sites': Parameter([dict], "List of local sites"),
37         'Keys': Parameter([dict], "List of local keys"),
38         'Nodes': Parameter([dict], "List of local nodes"),
39         'Persons': Parameter([dict], "List of local users"),
40         'Slices': Parameter([dict], "List of local slices"),
41         'db_time': Parameter(float, "(Debug) Database fetch time"),
42         }
43
44     def call (self, auth):
45         start = time.time()
46
47         # Filter out various secrets
48         node_fields = filter(lambda field: field not in \
49                              ['boot_nonce', 'key', 'session', 'root_person_ids'],
50                              Node.fields)
51
52         person_fields = filter(lambda field: field not in \
53                                ['password', 'verification_key', 'verification_expires'],
54                                Person.fields)
55
56         # XXX Optimize to return only those Persons, Keys, and Slices
57         # necessary for slice creation on the calling peer's nodes.
58
59         # filter out special person
60         persons = Persons(self.api, {'~email':[self.api.config.PLC_API_MAINTENANCE_USER,
61                                                self.api.config.PLC_ROOT_USER],
62                                      'peer_id': None}, person_fields)
63
64         # filter out system slices
65         system_slice_ids = SliceAttributes(self.api, {'name': 'system', 'value': '1'}).dict('slice_id')
66         slices = Slices(self.api, {'peer_id': None,
67                                    '~slice_id':system_slice_ids.keys()})
68         
69         result = {
70             'Sites': Sites(self.api, {'peer_id': None}),
71             'Keys': Keys(self.api, {'peer_id': None}),
72             'Nodes': Nodes(self.api, {'peer_id': None}, node_fields),
73             'Persons': persons,
74             'Slices': slices,
75             }
76
77         if isinstance(self.caller, Peer):
78             result['PeerNodes'] = Nodes(self.api, {'peer_id': self.caller['peer_id']})
79
80         result['db_time'] = time.time() - start
81
82         return result