iteration 4 & last:
[plcapi.git] / PLC / Methods / RefreshPeer.py
1 #
2 # Thierry Parmentelat - INRIA
3
4
5 import xmlrpclib
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 from PLC.Persons import Person, Persons
14
15
16 class RefreshPeer(Method):
17     """
18     Query a peer PLC for its list of nodes, and refreshes
19     the local database accordingly
20     
21     Returns a dict containing
22     (*) 'plcname' :   the peer name
23     (*) 'new_nodes' : the number of new nodes from that peer - may be negative
24     (*) 'new_slices': the number of new slices from that peer - may be negative
25     """
26     
27     roles = ['admin']
28     
29     accepts = [ Auth(),
30                 Parameter (int, "Peer id") ]
31     
32     returns = Parameter(dict, "plcname, new_nodes, new_slices")
33
34     def call (self, auth, peer_id):
35         
36         ### retrieve peer info
37         peers = Peers (self.api,[peer_id])
38         try:
39             peer=peers[0]
40         except:
41             raise PLCInvalidArgument,'no such peer_id:%d'%peer_id
42         
43         ### retrieve account info
44         person_id = peer['person_id']
45         persons = Persons (self.api,[person_id])
46         try:
47             person = persons[0]
48         except:
49             raise PLCInvalidArgument,'no such person_id:%d'%person_id
50         
51         ### build up foreign auth
52         auth={ 'Username': person['email'],
53                'AuthMethod' : 'password',
54                'AuthString' : person['password'],
55                'Role' : 'admin' }
56
57         ## connect to the peer's API
58         url=peer['peer_url']
59         apiserver = xmlrpclib.ServerProxy (url,allow_none=True)
60
61         peer_local_nodes = apiserver.GetNodes(auth,None,None,'local')
62         nb_new_nodes = peer.refresh_nodes(peer_local_nodes)
63         
64         # rough and temporary
65         peer_foreign_nodes = apiserver.GetNodes(auth,None,None,'foreign')
66         peer_local_slices = apiserver.GetSlices(auth,{'peer_id':None})
67         nb_new_slices = peer.refresh_slices(peer_local_slices,peer_foreign_nodes)
68         
69         return {'plcname':self.api.config.PLC_NAME,
70                 'new_nodes':nb_new_nodes,
71                 'new_slices':nb_new_slices}