f695bab5a063a420c6c0baa71f852362b1c1e8e7
[plcapi.git] / PLC / Methods / AddPeer.py
1 # $Id$
2 #
3 # Thierry Parmentelat - INRIA
4
5
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Auth import Auth
9 from PLC.Peers import Peer, Peers
10
11 can_update = lambda (field, value): field in \
12              ['peername', 'peer_url', 'key', 'cacert', 'shortname', 'hrn_root']
13
14 class AddPeer(Method):
15     """
16     Adds a new peer.
17
18     Returns the new peer_id (> 0) if successful, faults otherwise.
19     """
20
21     roles = ['admin']
22
23     peer_fields = dict(filter(can_update, Peer.fields.items()))
24
25     accepts = [
26         Auth(),
27         peer_fields
28         ]
29
30     returns = Parameter(int, "New peer_id (> 0) if successful")
31
32     def call(self, auth, peer_fields):
33         peer = Peer(self.api, peer_fields);
34         peer.sync()
35         self.event_objects = {'Peer': [peer['peer_id']]}
36
37         return peer['peer_id']