- be a little more verbose in the returns doc
[plcapi.git] / PLC / Methods / RefreshPeer.py
1 #
2 # Thierry Parmentelat - INRIA
3
4
5 from PLC.Faults import *
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Auth import Auth
9
10 from PLC.Peers import Peer, Peers
11 from PLC.Persons import Person, Persons
12
13 from PLC.Cache import Cache
14
15 class RefreshPeer(Method):
16     """
17     First queries a remote PLC for its name and updates the local peers table
18
19     Then proceeds with fetching objects on the remote PLC, and updates the
20     local database accordingly
21
22     It requires the remote peer to be aware of our own name (as configured in PLC_NAME)
23
24     Returns a dict containing
25     (*) 'peername' :   the peer's name
26     (*) 'new_xxx':     the number of new objects from that peer - may be negative
27     (*) 'timers':      various stats on performance for optimization
28
29     """
30     
31     roles = ['admin']
32     
33     accepts = [ Auth(),
34                 Mixed(Peer.fields['peer_id'],
35                       Peer.fields['peername']),
36                 ]
37     
38     returns = {
39         'new_sites': Parameter([dict], "List of new sites"),
40         'new_keys': Parameter([dict], "List of new keys"),
41         'new_nodes': Parameter([dict], "List of new nodes"),
42         'new_persons': Parameter([dict], "List of new users"),
43         'new_slice_attribute_types': Parameter([dict], "List of new slice attribute types"),
44         'new_slices': Parameter([dict], "List of new slices"),
45         'new_slice_attributes': Parameter([dict], "List of new slice attributes"),
46         'timers': Parameter(dict, "(Debug) Timing information"),
47         }
48
49     def call (self, auth, peer_id_or_peername):
50         peers = Peers(self.api, [peer_id_or_peername])
51         if not peers:
52             raise PLCInvalidArgument, "No such peer '%s'" % unicode(peer_id_or_peername)
53         peer = peers[0]
54
55         # Connect to peer API
56         peer.connect()
57
58         # Update peer name
59         peername = peer.GetPeerName()
60         if peer['peername'] != peername:
61             peer['peername'] = peername
62             peer.sync()
63
64         cache = Cache(self.api, peer['peer_id'], peer)
65         result = cache.refresh_peer()
66
67         # Add peer name to result set
68         result['peername'] = peername
69
70         return result