c9c40b7fe2fd921941db18109b2314ce940fa98a
[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', 'abbreviated_name', 'login_base',
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     PIs can only update sites they are a member of. Only admins can 
18     update max_slices, max_slivers, and login_base.
19
20     Returns 1 if successful, faults otherwise.
21     """
22
23     roles = ['admin', 'pi']
24
25     site_fields = dict(filter(can_update, Site.fields.items()))
26
27     accepts = [
28         Auth(),
29         Mixed(Site.fields['site_id'],
30               Site.fields['login_base']),
31         site_fields
32         ]
33
34     returns = Parameter(int, '1 if successful')
35
36     def call(self, auth, site_id_or_login_base, site_fields):
37         site_fields = dict(filter(can_update, site_fields.items()))
38
39         # Get site information
40         sites = Sites(self.api, [site_id_or_login_base])
41         if not sites:
42             raise PLCInvalidArgument, "No such site"
43         site = sites[0]
44
45         if site['peer_id'] is not None:
46             raise PLCInvalidArgument, "Not a local site"
47
48         # Authenticated function
49         assert self.caller is not None
50
51         # If we are not an admin, make sure that the caller is a
52         # member of the site.
53         if 'admin' not in self.caller['roles']:
54             if site['site_id'] not in self.caller['site_ids']:
55                 raise PLCPermissionDenied, "Not allowed to modify specified site"
56
57             # Remove admin only fields
58             for key in 'max_slices', 'max_slivers', 'login_base':
59                 del site_fields[key]
60
61         site.update(site_fields)
62         site.sync()
63         
64         # Logging variables
65         self.object_ids = [site['site_id']]
66         self.message = 'Site %d updated: %s' % \
67                 (site['site_id'], ", ".join(site_fields.keys()))        
68         
69         return 1