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