0b65333bc1e0c5e35ee32e1b5ad61b4e0055f813
[plcapi.git] / PLC / Methods / UpdateSliceTag.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.SliceTags import SliceTag, SliceTags
6 from PLC.Nodes import Node
7 from PLC.Slices import Slice, Slices
8 from PLC.InitScripts import InitScript, InitScripts
9 from PLC.Auth import Auth
10
11 class UpdateSliceTag(Method):
12     """
13     Updates the value of an existing slice or sliver attribute.
14
15     Users may only update attributes of slices or slivers of which
16     they are members. PIs may only update attributes of slices or
17     slivers at their sites, or of which they are members. Admins may
18     update attributes of any slice or sliver.
19
20     Returns 1 if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi', 'user', 'node']
24
25     accepts = [
26         Auth(),
27         SliceTag.fields['slice_tag_id'],
28         Mixed(SliceTag.fields['value'],
29               InitScript.fields['name'])
30         ]
31
32     returns = Parameter(int, '1 if successful')
33
34     def call(self, auth, slice_tag_id, value):
35         slice_tags = SliceTags(self.api, [slice_tag_id])
36         if not slice_tags:
37             raise PLCInvalidArgument, "No such slice attribute"
38         slice_tag = slice_tags[0]
39
40         slices = Slices(self.api, [slice_tag['slice_id']])
41         if not slices:
42             raise PLCInvalidArgument, "No such slice"
43         slice = slices[0]
44
45         assert slice_tag['slice_tag_id'] in slice['slice_tag_ids']
46
47         if not isinstance(self.caller, Node):
48             if 'admin' not in self.caller['roles']:
49                 if self.caller['person_id'] in slice['person_ids']:
50                     pass
51                 elif 'pi' not in self.caller['roles']:
52                     raise PLCPermissionDenied, "Not a member of the specified slice"
53                 elif slice['site_id'] not in self.caller['site_ids']:
54                     raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
55
56                 if slice_tag['min_role_id'] is not None and \
57                        min(self.caller['role_ids']) > slice_tag['min_role_id']:
58                     raise PLCPermissionDenied, "Not allowed to update the specified attribute"
59         else:
60             ### make node's min_role_id == PI min_role_id
61             node_role_id = 20
62             if slice_tag['min_role_id'] is not None and node_role_id > slice_tag['min_role_id']:
63                 raise PLCPermissionDenied, "Not allowed to update the specified slice attribute"
64         
65         if slice_tag['tagname'] in ['initscript']:
66             initscripts = InitScripts(self.api, {'enabled': True, 'name': value})
67             if not initscripts:
68                 raise PLCInvalidArgument, "No such plc initscript"      
69
70         slice_tag['value'] = unicode(value)
71         slice_tag.sync()
72         self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
73         return 1