a8de812dbba9387c7f5ef68eb756334ae9ab2e89
[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 class UpdateSliceTag(Method):
17     """
18     Updates the value of an existing slice or sliver attribute.
19
20     Users may only update attributes of slices or slivers of which
21     they are members. PIs may only update attributes of slices or
22     slivers at their sites, or of which they are members. Admins may
23     update attributes of any slice or sliver.
24
25     Returns 1 if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'user', 'node']
29
30     accepts = [
31         Auth(),
32         SliceTag.fields['slice_tag_id'],
33         Mixed(SliceTag.fields['value'],
34               InitScript.fields['name'])
35         ]
36
37     returns = Parameter(int, '1 if successful')
38
39     def call(self, auth, slice_tag_id, value):
40         slice_tags = SliceTags(self.api, [slice_tag_id])
41         if not slice_tags:
42             raise PLCInvalidArgument, "No such slice attribute"
43         slice_tag = slice_tags[0]
44
45         slices = Slices(self.api, [slice_tag['slice_id']])
46         if not slices:
47             raise PLCInvalidArgument, "No such slice %d"%slice_tag['slice_id']
48         slice = slices[0]
49
50         assert slice_tag['slice_tag_id'] in slice['slice_tag_ids']
51
52         # check authorizations
53         node_id_or_hostname=slice_tag['node_id']
54         nodegroup_id_or_name=slice_tag['nodegroup_id']
55         slice.caller_may_write_tag(self.api,self.caller,tag_type,node_id_or_hostname,nodegroup_id_or_name)
56
57         if slice_tag['tagname'] in ['initscript']:
58             initscripts = InitScripts(self.api, {'enabled': True, 'name': value})
59             if not initscripts:
60                 raise PLCInvalidArgument, "No such plc initscript"
61
62         slice_tag['value'] = unicode(value)
63         slice_tag.sync()
64         self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
65         return 1