Initial checkin of new API implementation
[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', 'tech']
24
25     cant_update = lambda (field, value): field not in \
26                  ['site_id', 'nodegroup_id', 'organization_id', 'ext_consortium_id', 'date_created']
27     update_fields = dict(filter(cant_update, Site.all_fields.items()))
28
29     accepts = [
30         PasswordAuth(),
31         Mixed(Site.fields['site_id'],
32               Site.fields['abbreviated_name']),
33         update_fields
34         ]
35
36     returns = Parameter(int, '1 if successful')
37
38     def call(self, auth, site_id_or_abbrev_name, update_fields):
39         # Only admin can update max_slices
40         #if 'admin' not in self.caller['roles']:
41         #       if update_fields.has_key('max_slices '):
42         #               raise PLCInvalidArgument, "Only admins can update max_slices"
43         
44         # Check for invalid fields
45         if filter(lambda field: field not in self.update_fields, update_fields):
46             raise PLCInvalidArgument, "Invalid field specified"
47
48         # XML-RPC cannot marshal None, so we need special values to
49         # represent "unset".
50         for key, value in update_fields.iteritems():
51             if value == -1 or value == "null":
52                 if key in ['name', 'abbreviated_name', 'login_base', 'is_public', 'max_slices']:
53                     raise PLCInvalidArgument, "%s cannot be unset" % key
54                 update_fields[key] = None
55
56         # Get site information
57         sites = Sites(self.api, [site_id_or_abbrev_name], Site.all_fields)
58         if not sites :
59             raise PLCInvalidArgument, "No such site"
60
61         site = sites.values()[0]
62
63         # If we are not an admin, make sure that the caller is a
64         # member of the site at which the node is located.
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                 if 'tech' not in self.caller['roles']:
69                         raise PLCPermissionDenied, "Not allowed to add node network for specified node"
70                 
71         
72         site.update(update_fields)
73         site.flush()
74         
75         return 1
76