13ac1fd659324b1d868fc59e601facf6b2242cc2
[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 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', 'enabled', 'ext_consortium_id']
11
12 class AddSite(Method):
13     """
14     Adds a new site, and creates a node group for that site. Any
15     fields specified in site_fields are used, otherwise defaults are
16     used.
17
18     Returns the new site_id (> 0) if successful, faults otherwise.
19     """
20
21     roles = ['admin']
22
23     site_fields = dict(filter(can_update, Site.fields.items()))
24
25     accepts = [
26         Auth(),
27         site_fields
28         ]
29
30     returns = Parameter(int, 'New site_id (> 0) if successful')
31
32     def call(self, auth, site_fields):
33         site_fields = dict(filter(can_update, site_fields.items()))
34         site = Site(self.api, site_fields)
35         site.sync()
36
37         # Logging variables
38         self.event_objects = {'Site': [site['site_id']]}
39         self.message = 'Site %d created' % site['site_id']
40
41         return site['site_id']