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