1 # $Id: AddSiteTag.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/AddSiteTag.py $
4 # Thierry Parmentelat - INRIA
8 from PLC.Faults import *
9 from PLC.Method import Method
10 from PLC.Parameter import Parameter, Mixed
11 from PLC.Auth import Auth
13 from PLC.TagTypes import TagType, TagTypes
14 from PLC.SiteTags import SiteTag, SiteTags
15 from PLC.Sites import Site, Sites
17 from PLC.Nodes import Nodes
18 from PLC.Sites import Sites
20 class AddSiteTag(Method):
22 Sets the specified setting for the specified site
23 to the specified value.
25 In general only tech(s), PI(s) and of course admin(s) are allowed to
26 do the change, but this is defined in the tag type object.
28 Returns the new site_tag_id (> 0) if successful, faults
32 roles = ['admin', 'pi', 'tech', 'user']
36 # no other way to refer to a site
37 SiteTag.fields['site_id'],
38 Mixed(TagType.fields['tag_type_id'],
39 TagType.fields['tagname']),
40 SiteTag.fields['value'],
43 returns = Parameter(int, 'New site_tag_id (> 0) if successful')
48 def call(self, auth, site_id, tag_type_id_or_name, value):
49 sites = Sites(self.api, [site_id])
51 raise PLCInvalidArgument, "No such site %r"%site_id
54 tag_types = TagTypes(self.api, [tag_type_id_or_name])
56 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
57 tag_type = tag_types[0]
59 # checks for existence - does not allow several different settings
60 conflicts = SiteTags(self.api,
61 {'site_id':site['site_id'],
62 'tag_type_id':tag_type['tag_type_id']})
65 raise PLCInvalidArgument, "Site %d already has setting %d"%(site['site_id'],
66 tag_type['tag_type_id'])
68 # check permission : it not admin, is the user affiliated with the right site
69 if 'admin' not in self.caller['roles']:
71 site = Sites (self.api, site_id)[0]
72 # check caller is affiliated with this site
73 if self.caller['person_id'] not in site['person_ids']:
74 raise PLCPermissionDenied, "Not a member of the hosting site %s"%site['abbreviated_site']
76 required_min_role = tag_type ['min_role_id']
77 if required_min_role is not None and \
78 min(self.caller['role_ids']) > required_min_role:
79 raise PLCPermissionDenied, "Not allowed to modify the specified site setting, requires role %d",required_min_role
81 site_tag = SiteTag(self.api)
82 site_tag['site_id'] = site['site_id']
83 site_tag['tag_type_id'] = tag_type['tag_type_id']
84 site_tag['value'] = value
87 self.object_ids = [site_tag['site_tag_id']]
89 return site_tag['site_tag_id']