b472ca58c77d2e18a8b56d33db1451d0cf633a97
[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         ]
32
33     returns = Parameter (dict, " ssh keys hashed on hostnames")
34
35     def call(self, auth, slice_id_or_name):
36         filter={}
37         if isinstance(slice_id_or_name,int):
38             filter['slice_id']=slice_id_or_name
39         elif isinstance(slice_id_or_name,StringTypes):
40             filter['name']=slice_id_or_name
41         filter['tagname']='ssh_key'
42         # retrieve only sliver tags
43         filter['~node_id']=None
44         
45         # slice_tags don't expose hostname, sigh..
46         slice_tags=SliceTags(self.api,filter,['node_id','tagname','value'])
47         node_ids = [st['node_id'] for st in slice_tags]
48         # fetch nodes
49         nodes=Nodes(self.api,node_ids,['node_id','hostname'])
50         # hash on node_id
51         nodes_hash=dict( [ (n['node_id'],n['hostname']) for n in nodes])
52         # return values hashed on hostname
53         return dict([ (nodes_hash[st['node_id']],st['value']) for st in slice_tags])