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