70eb956bf4fd9917e709522e652a3bff9d3b1cac
[plcapi.git] / PLC / Methods / AddSliceToNodesWhitelist.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.Slices import Slice, Slices
7 from PLC.Auth import Auth
8
9 class AddSliceToNodesWhitelist(Method):
10     """
11     Adds the specified slice to the whitelist on the specified nodes. Nodes may be
12     either local or foreign nodes.
13
14     If the slice is already associated with a node, no errors are
15     returned.
16
17     Returns 1 if successful, faults otherwise.
18     """
19
20     roles = ['admin']
21
22     accepts = [
23         Auth(),
24         Mixed(Slice.fields['slice_id'],
25               Slice.fields['name']),
26         [Mixed(Node.fields['node_id'],
27                Node.fields['hostname'])]
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, slice_id_or_name, node_id_or_hostname_list):
33         # Get slice information
34         slices = Slices(self.api, [slice_id_or_name])
35         if not slices:
36             raise PLCInvalidArgument, "No such slice"
37         slice = slices[0]
38
39         if slice['peer_id'] is not None:
40             raise PLCInvalidArgument, "Not a local slice"
41
42         # Get specified nodes, add them to the slice         
43         nodes = Nodes(self.api, node_id_or_hostname_list)
44         for node in nodes:
45             if node['peer_id'] is not None:
46                 raise PLCInvalidArgument, "%s not a local node" % node['hostname']
47             if slice['slice_id'] not in node['slice_ids_whitelist']:
48                 slice.add_to_node_whitelist(node, commit = False)
49             
50         slice.sync()
51
52         self.event_objects = {'Node': [node['node_id'] for node in nodes],
53                               'Slice': [slice['slice_id']]}
54
55         return 1