add/update/delete slice tags should be fine
[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"
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         granted=False
56         if 'admin' in self.caller['roles']:
57             granted=True
58         # does caller have right role(s) ? this knows how to deal with self.caller being a node
59         elif not AuthorizeHelpers.caller_may_access_tag_type (self.api, self.caller, tag_type):
60             granted=False
61         # node callers: check the node is in the slice
62         elif isinstance(self.caller, Node): 
63             # nodes can only set their own sliver tags
64             if node_id_or_hostname is None: 
65                 granted=False
66             elif not AuthorizeHelpers.node_match_id (self.api, self.caller, node_id_or_hostname):
67                 granted=False
68             elif not AuthorizeHelpers.node_in_slice (self.api, self.caller, slice):
69                 granted=False
70         # caller is a non-admin person
71         else:
72             # only admins can handle slice tags on a nodegroup
73             if nodegroup_id_or_name:
74                 raise PLCPermissionDenied, "%s, cannot set slice tag %s on nodegroup - restricted to admins"%\
75                     (self.name,tag_type['tagname'])
76             # if a node is specified it is expected to be in the slice
77             if node_id_or_hostname:
78                 if not AuthorizeHelpers.node_id_in_slice (self.api, node_id_or_hostname, slice):
79                     raise PLCPermissionDenied, "%s, node must be in slice when setting sliver tag"
80             # try all roles to find a match - tech are ignored b/c not in AddSliceTag.roles anyways
81             for role in AuthorizeHelpers.person_tag_type_common_roles(self.api,self.caller,tag_type):
82                 # regular users need to be in the slice
83                 if role=='user':
84                     if AuthorizeHelpers.person_in_slice(self.api, self.caller, slice):
85                         granted=True ; break
86                 # for convenience, pi's can tweak all the slices in their site
87                 elif role=='pi':
88                     if AuthorizeHelpers.slice_belongs_to_pi (self.api, slice, self.caller):
89                         granted=True ; break
90         if not granted:
91             raise PLCPermissionDenied, "%s, forbidden tag %s"%(self.name,tag_type['tagname'])
92
93         if slice_tag['tagname'] in ['initscript']:
94             initscripts = InitScripts(self.api, {'enabled': True, 'name': value})
95             if not initscripts:
96                 raise PLCInvalidArgument, "No such plc initscript"
97
98         slice_tag['value'] = unicode(value)
99         slice_tag.sync()
100         self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
101         return 1