force import of AuthorizeHelpers
[plcapi.git] / PLC / Methods / DeleteSliceTag.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.Slices import Slice, Slices
11 from PLC.Nodes import Node, Nodes
12
13 # need to import so the core classes get decorated with caller_may_write_tag
14 from PLC.AuthorizeHelpers import AuthorizeHelpers
15
16 class DeleteSliceTag(Method):
17     """
18     Deletes the specified slice or sliver attribute.
19
20     Attributes may require the caller to have a particular role in
21     order to be deleted. Users may only delete attributes of
22     slices or slivers of which they are members. PIs may only delete
23     attributes of slices or slivers at their sites, or of which they
24     are members. Admins may delete attributes of any slice or sliver.
25
26     Returns 1 if successful, faults otherwise.
27     """
28
29     roles = ['admin', 'pi', 'user', 'tech']
30
31     accepts = [
32         Auth(),
33         SliceTag.fields['slice_tag_id']
34         ]
35
36     returns = Parameter(int, '1 if successful')
37
38     def call(self, auth, slice_tag_id):
39         slice_tags = SliceTags(self.api, [slice_tag_id])
40         if not slice_tags:
41             raise PLCInvalidArgument, "No such slice attribute"
42         slice_tag = slice_tags[0]
43
44         slices = Slices(self.api, [slice_tag['slice_id']])
45         if not slices:
46             raise PLCInvalidArgument, "No such slice %d"%slice_tag['slice_id']
47         slice = slices[0]
48
49         assert slice_tag['slice_tag_id'] in slice['slice_tag_ids']
50
51         # check authorizations
52         node_id_or_hostname=slice_tag['node_id']
53         nodegroup_id_or_name=slice_tag['nodegroup_id']
54         slice.caller_may_write_tag(self.api,self.caller,tag_type,node_id_or_hostname,nodegroup_id_or_name)
55
56         slice_tag.delete()
57         self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
58
59         return 1