bug fixes
[plcapi.git] / PLC / Methods / UpdateSite.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Sites import Site, Sites
5 from PLC.Auth import Auth
6
7 can_update = lambda (field, value): field in \
8              ['name', 'enabled', 'description'] 
9
10 class UpdateSite(Method):
11     """
12     Updates a site. Only the fields specified in update_fields are
13     updated, all other fields are left untouched.
14
15     PIs can only update sites they are a member of. Only admins can
16     update max_slices, max_slivers, and login_base.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     roles = ['admin', 'pi']
22
23     site_fields = dict(filter(can_update, Site.fields.items()))
24
25     accepts = [
26         Auth(),
27         Site.fields['site_id'],
28         site_fields
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, site_id, site_fields):
34         site_fields = dict(filter(can_update, site_fields.items()))
35
36         # Get site information
37         sites = Sites(self.api, site_id)
38         if not sites:
39             raise PLCInvalidArgument, "No such site"
40         site = sites[0]
41
42         # Authenticated function
43         assert self.caller is not None
44
45         # If we are not an admin, make sure that the caller is a
46         # member of the site.
47         if 'admin' not in self.caller['roles']:
48             if site['site_id'] not in self.caller['site_id']:
49                 raise PLCPermissionDenied, "Not allowed to modify specified site"
50
51             # Remove admin only fields
52
53         site.update(site_fields)
54         site.sync()
55
56         return 1