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