add filtering on node to RetrieveSliceSshKeys
[plcapi.git] / PLC / Methods / RetrieveSliceSshKeys.py
1 from types import StringTypes
2
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Filter import Filter
6 from PLC.Auth import Auth
7 from PLC.Nodes import Node, Nodes
8 from PLC.SliceTags import SliceTag, SliceTags
9 from PLC.Slices import Slice, Slices 
10
11 class RetrieveSliceSshKeys(Method):
12     """
13     This method exposes the public ssh keys for a slice's slivers.
14     It expects a slice name or id, and returns a dictionary on hostnames.
15     This method is designed to help third-party software authenticate
16     slivers (e.g. the OMF Experiment Controller). 
17     For this reason it is accessible with anonymous authentication.
18     """
19
20     roles = ['admin', 'pi', 'user', 'tech', 'anonymous' ]
21
22     applicable_fields = {
23         'slice_id' : Slice.fields['slice_id'],
24         'name' : Slice.fields['name'],
25         }
26
27     accepts = [
28         Auth(),
29         Mixed(Slice.fields['slice_id'],
30               Slice.fields['name']),
31         Filter(Node.fields),
32         ]
33
34     returns = Parameter (dict, " ssh keys hashed on hostnames")
35
36     def call(self, auth, slice_id_or_name, node_filter=None):
37         
38         filter={}
39         if isinstance(slice_id_or_name,int):
40             filter['slice_id']=slice_id_or_name
41         elif isinstance(slice_id_or_name,StringTypes):
42             filter['name']=slice_id_or_name
43         filter['tagname']='ssh_key'
44         # retrieve only sliver tags
45         filter['~node_id']=None
46         if node_filter:
47             # make sure we only deal with local nodes
48             node_filter['peer_id']=None
49             nodes = Nodes(self.api, node_filter, ['node_id'])
50             node_ids = [ node ['node_id'] for node in nodes ]
51             filter['node_id']=node_ids
52         
53         # slice_tags don't expose hostname, sigh..
54         slice_tags=SliceTags(self.api,filter,['node_id','tagname','value'])
55         node_ids = [st['node_id'] for st in slice_tags]
56         # fetch nodes
57         nodes=Nodes(self.api,node_ids,['node_id','hostname'])
58         # hash on node_id
59         nodes_hash=dict( [ (n['node_id'],n['hostname']) for n in nodes])
60         # return values hashed on hostname
61         return dict([ (nodes_hash[st['node_id']],st['value']) for st in slice_tags])