Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmAddSite.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 AdmAddSite(Method):
8     """
9     Adds a new site, and creates a node group for that site. Any
10     fields specified in optional_vals are used, otherwise defaults are
11     used.
12
13     Returns the new site_id (> 0) if successful, faults otherwise.
14     """
15
16     roles = ['admin']
17
18     can_update = lambda (field, value): field in \
19                  ['is_public', 'latitude', 'longitude', 'url',
20                   'organization_id', 'ext_consortium_id']
21     update_fields = dict(filter(can_update, Site.fields.items()))
22
23     accepts = [
24         PasswordAuth(),
25         Site.fields['name'],
26         Site.fields['abbreviated_name'],
27         Site.fields['login_base'],
28         update_fields
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, name, abbreviated_name, login_base, optional_vals = {}):
34         if filter(lambda field: field not in self.update_fields, optional_vals):
35             raise PLCInvalidArgument, "Invalid field specified"
36
37         site = Site(self.api, optional_vals)
38         site['name'] = name
39         site['abbreviated_name'] = abbreviated_name
40         site['login_base'] = login_base
41         site.flush()
42
43         return site['site_id']