add site_tags : GetSites() now returns 'site_tag_ids' attribute.
[plcapi.git] / PLC / Methods / DeleteSiteTag.py
1 # $Id: DeleteSiteTag.py 14587 2009-07-19 13:18:50Z thierry $
2 # $URL: http://svn.planet-lab.org/svn/PLCAPI/tags/PLCAPI-4.3-27/PLC/Methods/DeleteSiteTag.py $
3 #
4 # Thierry Parmentelat - INRIA
5 #
6 # $Revision: 14587 $
7 #
8
9 from PLC.Faults import *
10 from PLC.Method import Method
11 from PLC.Parameter import Parameter, Mixed
12 from PLC.Auth import Auth
13
14 from PLC.SiteTags import SiteTag, SiteTags
15 from PLC.Sites import Site, Sites
16
17 from PLC.Nodes import Node, Nodes
18 from PLC.Sites import Site, Sites
19
20 class DeleteSiteTag(Method):
21     """
22     Deletes the specified site setting
23
24     Attributes may require the caller to have a particular role in order
25     to be deleted, depending on the related tag type.
26     Admins may delete attributes of any slice or sliver.
27
28     Returns 1 if successful, faults otherwise.
29     """
30
31     roles = ['admin', 'pi', 'user']
32
33     accepts = [
34         Auth(),
35         SiteTag.fields['site_tag_id']
36         ]
37
38     returns = Parameter(int, '1 if successful')
39
40     object_type = 'Site'
41
42
43     def call(self, auth, site_tag_id):
44         site_tags = SiteTags(self.api, [site_tag_id])
45         if not site_tags:
46             raise PLCInvalidArgument, "No such site tag %r"%site_tag_id
47         site_tag = site_tags[0]
48
49         ### reproducing a check from UpdateSliceTag, looks dumb though
50         sites = Sites(self.api, [site_tag['site_id']])
51         if not sites:
52             raise PLCInvalidArgument, "No such site %r"%site_tag['site_id']
53         site = sites[0]
54
55         assert site_tag['site_tag_id'] in site['site_tag_ids']
56
57         # check permission : it not admin, is the user affiliated with the right site
58         if 'admin' not in self.caller['roles']:
59             # check caller is affiliated with this site
60             if self.caller['person_id'] not in site['person_ids']:
61                 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
62             
63             required_min_role = tag_type ['min_role_id']
64             if required_min_role is not None and \
65                     min(self.caller['role_ids']) > required_min_role:
66                 raise PLCPermissionDenied, "Not allowed to modify the specified site setting, requires role %d",required_min_role
67
68         site_tag.delete()
69         self.object_ids = [site_tag['site_tag_id']]
70
71         return 1