(*) slice atttribute types get cached
[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 from PLC.Cache import Cache
16
17 class RefreshPeer(Method):
18     """
19     Query a peer PLC for its list of nodes, and refreshes
20     the local database accordingly
21     
22     Returns a dict containing
23     (*) 'plcname' :   the peer name
24     (*) 'new_sites':  the number of new sites from that peer - may be negative
25     (*) 'new_keys':    
26     (*) 'new_nodes':   
27     (*) 'new_persons': 
28     (*) 'new_slices':  
29     """
30     
31     roles = ['admin']
32     
33     accepts = [ Auth(),
34                 Parameter (int, "Peer id"),
35                 ]
36     
37     returns = Parameter(dict, "plcname, new_sites, new_keys, new_nodes, new_persons, new_slices")
38
39     def call (self, auth, peer_id):
40         
41         ### retrieve peer info
42         peers = Peers (self.api,[peer_id])
43         try:
44             peer=peers[0]
45         except:
46             raise PLCInvalidArgument,'no such peer_id:%d'%peer_id
47         
48         ### retrieve account info
49         person_id = peer['person_id']
50         persons = Persons (self.api,[person_id])
51         try:
52             person = persons[0]
53         except:
54             raise PLCInvalidArgument,'no such person_id:%d'%person_id
55         
56         ## connect to the peer's API
57         url=peer['peer_url']
58         apiserver = xmlrpclib.ServerProxy (url,allow_none=True)
59
60         ### build up foreign auth
61         auth={ 'Username': person['email'],
62                'AuthMethod' : 'password',
63                'AuthString' : person['password'],
64                'Role' : 'admin' }
65
66         cache = Cache (self.api, peer_id, apiserver, auth)
67         return cache.refresh_peer ()