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