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