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