retrieve tag_type needed for caller_may_write_tag
[plcapi.git] / PLC / Methods / UpdateSliceTag.py
1 #
2 # Thierry Parmentelat - INRIA
3 #
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
8
9 from PLC.SliceTags import SliceTag, SliceTags
10 from PLC.Nodes import Node
11 from PLC.Slices import Slice, Slices
12 from PLC.InitScripts import InitScript, InitScripts
13
14 from PLC.AuthorizeHelpers import AuthorizeHelpers
15
16 # need to import so the core classes get decorated with caller_may_write_tag
17 from PLC.AuthorizeHelpers import AuthorizeHelpers
18
19 class UpdateSliceTag(Method):
20     """
21     Updates the value of an existing slice or sliver attribute.
22
23     Users may only update attributes of slices or slivers of which
24     they are members. PIs may only update attributes of slices or
25     slivers at their sites, or of which they are members. Admins may
26     update attributes of any slice or sliver.
27
28     Returns 1 if successful, faults otherwise.
29     """
30
31     roles = ['admin', 'pi', 'user', 'node']
32
33     accepts = [
34         Auth(),
35         SliceTag.fields['slice_tag_id'],
36         Mixed(SliceTag.fields['value'],
37               InitScript.fields['name'])
38         ]
39
40     returns = Parameter(int, '1 if successful')
41
42     def call(self, auth, slice_tag_id, value):
43         slice_tags = SliceTags(self.api, [slice_tag_id])
44         if not slice_tags:
45             raise PLCInvalidArgument, "No such slice attribute"
46         slice_tag = slice_tags[0]
47
48         tag_type_id = node_tag['tag_type_id']
49         tag_type = TagTypes (self.api,[tag_type_id])[0]
50
51         slices = Slices(self.api, [slice_tag['slice_id']])
52         if not slices:
53             raise PLCInvalidArgument, "No such slice %d"%slice_tag['slice_id']
54         slice = slices[0]
55
56         assert slice_tag['slice_tag_id'] in slice['slice_tag_ids']
57
58         # check authorizations
59         node_id_or_hostname=slice_tag['node_id']
60         nodegroup_id_or_name=slice_tag['nodegroup_id']
61         slice.caller_may_write_tag(self.api,self.caller,tag_type,node_id_or_hostname,nodegroup_id_or_name)
62
63         if slice_tag['tagname'] in ['initscript']:
64             initscripts = InitScripts(self.api, {'enabled': True, 'name': value})
65             if not initscripts:
66                 raise PLCInvalidArgument, "No such plc initscript"
67
68         slice_tag['value'] = unicode(value)
69         slice_tag.sync()
70         self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
71         return 1