Merge from trunk
[plcapi.git] / trunk / PLC / Methods / UpdatePeer.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import Auth
5 from PLC.Peers import Peer, Peers
6
7 can_update = lambda (field, value): field in \
8              ['peername', 'peer_url', 'key', 'cacert']
9
10 class UpdatePeer(Method):
11     """
12     Updates a peer. Only the fields specified in peer_fields are
13     updated, all other fields are left untouched.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     peer_fields = dict(filter(can_update, Peer.fields.items()))
21
22     accepts = [
23         Auth(),
24         Mixed(Peer.fields['peer_id'],
25               Peer.fields['peername']),
26         peer_fields
27         ]
28
29     returns = Parameter(int, "1 if successful")
30
31     def call(self, auth, peer_id_or_name, peer_fields):
32         peer_fields = dict(filter(can_update, peer_fields.items()))
33
34         # Get account information
35         peers = Peers(self.api, [peer_id_or_name])
36         if not peers:
37             raise PLCInvalidArgument, "No such peer"
38         peer = peers[0]
39
40         if isinstance(self.caller, Peer):
41             if self.caller['peer_id'] != peer['peer_id']:
42                 raise PLCPermissionDenied, "Not allowed to update specified peer"
43
44         peer.update(peer_fields)
45         peer.sync()
46
47         # Log affected objects
48         self.event_objects = {'Peer': [peer['peer_id']]}
49
50         return 1