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