97f568dff42b86c6c59a21a890c7dfe182bf0ab5
[plcapi.git] / PLC / Methods / GetPeers.py
1 # $Id$
2 #
3 # Thierry Parmentelat - INRIA
4
5
6 from PLC.Faults import *
7 from PLC.Method import Method
8 from PLC.Parameter import Parameter, Mixed
9 from PLC.Filter import Filter
10 from PLC.Auth import Auth
11
12 from PLC.Persons import Person
13 from PLC.Peers import Peer, Peers
14
15 class GetPeers (Method):
16     """
17     Returns an array of structs containing details about peers. If
18     person_filter is specified and is an array of peer identifiers or
19     peer names, or a struct of peer attributes, only peers matching
20     the filter will be returned. If return_fields is specified, only the
21     specified details will be returned.
22     """
23
24     roles = ['admin', 'node','pi','user']
25
26     accepts = [
27         Auth(),
28         Mixed([Mixed(Peer.fields['peer_id'],
29                      Peer.fields['peername'])],
30               Filter(Peer.fields)),
31         Parameter([str], "List of fields to return", nullok = True)        
32         ]
33
34     returns = [Peer.fields]
35
36     def call (self, auth, peer_filter = None, return_fields = None):
37         
38         peers = Peers(self.api, peer_filter, return_fields)
39
40         # Remove admin only fields
41         if not isinstance(self.caller, Person) or \
42                 'admin' not in self.caller['roles']:
43             for peer in peers:    
44                 for field in ['key', 'cacert']:
45                     if field in peer:
46                         del peer[field]
47
48         return peers