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