- allow max_slivers to be updated
[plcapi.git] / PLC / Methods / AdmUpdateSite.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 PasswordAuth
6
7 class AdmUpdateSite(Method):
8     """
9     Updates a site. Only the fields specified in update_fields are
10     updated, all other fields are left untouched.
11
12     To remove a value without setting a new one in its place (for
13     example, to remove an address from the node), specify -1 for int
14     and double fields and 'null' for string fields. hostname and
15     boot_state cannot be unset.
16     
17     PIs can only update sites they are a member of. Only admins can 
18     update max_slices.
19
20     Returns 1 if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi']
24
25     can_update = lambda (field, value): field in \
26                  ['name', 'abbreviated_name',
27                   'is_public', 'latitude', 'longitude', 'url',
28                   'max_slices', 'max_slivers']
29     update_fields = dict(filter(can_update, Site.fields.items()))
30
31     accepts = [
32         PasswordAuth(),
33         Mixed(Site.fields['site_id'],
34               Site.fields['login_base']),
35         update_fields
36         ]
37
38     returns = Parameter(int, '1 if successful')
39
40     def call(self, auth, site_id_or_login_base, update_fields):
41         # Check for invalid fields
42         if filter(lambda field: field not in self.update_fields, update_fields):
43             raise PLCInvalidArgument, "Invalid field specified"
44
45         # XML-RPC cannot marshal None, so we need special values to
46         # represent "unset".
47         for key, value in update_fields.iteritems():
48             if value == -1 or value == "null":
49                 if key not in ['latitude', 'longitude', 'url']:
50                     raise PLCInvalidArgument, "%s cannot be unset" % key
51                 update_fields[key] = None
52
53         # Get site information
54         sites = Sites(self.api, [site_id_or_login_base])
55         if not sites:
56             raise PLCInvalidArgument, "No such site"
57
58         site = sites.values()[0]
59
60         # Authenticated function
61         assert self.caller is not None
62
63         # If we are not an admin, make sure that the caller is a
64         # member of the site.
65         if 'admin' not in self.caller['roles']:
66             if site['site_id'] not in self.caller['site_ids']:
67                 raise PLCPermissionDenied, "Not allowed to modify specified site"
68
69             if 'max_slices' or 'max_slivers' in update_fields:
70                 raise PLCInvalidArgument, "Only admins can update max_slices and max_slivers"
71
72         site.update(update_fields)
73         site.sync()
74         
75         return 1