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