StringTypes has gone
[plcapi.git] / PLC / Methods / RetrieveSliceSliverKeys.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 RetrieveSliceSliverKeys(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         Filter(Node.fields),
30         ]
31
32     returns = Parameter (dict, " ssh keys hashed on hostnames")
33
34     def call(self, auth, slice_id_or_name, node_filter=None):
35
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, str):
40             filter['name'] = slice_id_or_name
41         filter['tagname'] = 'ssh_key'
42         # retrieve only sliver tags
43         filter['~node_id'] = None
44         if node_filter:
45             # make sure we only deal with local nodes
46             node_filter['peer_id'] = None
47             nodes = Nodes(self.api, node_filter, ['node_id'])
48             node_ids = [node['node_id'] for node in nodes]
49             filter['node_id'] = node_ids
50
51         # slice_tags don't expose hostname, sigh..
52         slice_tags = SliceTags(self.api, filter, ['node_id', 'tagname', 'value'])
53         node_ids = [st['node_id'] for st in slice_tags]
54         # fetch nodes
55         nodes = Nodes(self.api, node_ids, ['node_id', 'hostname'])
56         # hash on node_id
57         nodes_hash = {n['node_id']: n['hostname'] for n in nodes}
58         # return values hashed on hostname
59         return {nodes_hash[st['node_id']]: st['value'] for st in slice_tags}