- remove unnecessary slice attribute imports
[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
20 class GetPeerData(Method):
21     """
22     Returns lists of local objects that a peer should cache in its
23     database as foreign objects. Also returns the list of foreign
24     nodes in this database, for which the calling peer is
25     authoritative, to assist in synchronization of slivers.
26     
27     See the implementation of RefreshPeer for how this data is used.
28     """
29
30     roles = ['admin', 'peer']
31
32     accepts = [Auth()]
33
34     returns = {
35         'Sites': Parameter([dict], "List of local sites"),
36         'Keys': Parameter([dict], "List of local keys"),
37         'Nodes': Parameter([dict], "List of local nodes"),
38         'Persons': Parameter([dict], "List of local users"),
39         'Slices': Parameter([dict], "List of local slices"),
40         'db_time': Parameter(float, "(Debug) Database fetch time"),
41         }
42
43     def call (self, auth):
44         start = time.time()
45
46         # Filter out various secrets
47         node_fields = filter(lambda field: field not in \
48                              ['boot_nonce', 'key', 'session', 'root_person_ids'],
49                              Node.fields)
50
51         person_fields = filter(lambda field: field not in \
52                                ['password', 'verification_key', 'verification_expires'],
53                                Person.fields)
54
55         # XXX Optimize to return only those Persons, Keys, and Slices
56         # necessary for slice creation on the calling peer's nodes.
57         result = {
58             'Sites': Sites(self.api, {'peer_id': None}),
59             'Keys': Keys(self.api, {'peer_id': None}),
60             'Nodes': Nodes(self.api, {'peer_id': None}, node_fields),
61             'Persons': Persons(self.api, {'peer_id': None}, person_fields),
62             'Slices': Slices(self.api, {'peer_id': None}),
63             }
64
65         if isinstance(self.caller, Peer):
66             result['PeerNodes'] = Nodes(self.api, {'peer_id': self.caller['peer_id']})
67
68         result['db_time'] = time.time() - start
69
70         return result