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