b2d6e8f9ad87965fd3ef91f4b88920755b2244b6
[plcapi.git] / PLC / Methods / AddSite.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Sites import Site, Sites
6 from PLC.Auth import Auth
7
8 can_update = lambda (field, value): field in \
9              ['name', 'abbreviated_name', 'login_base',
10               'is_public', 'latitude', 'longitude', 'url',
11               'max_slices', 'max_slivers', 'enabled']
12
13 class AddSite(Method):
14     """
15     Adds a new site, and creates a node group for that site. Any
16     fields specified in site_fields are used, otherwise defaults are
17     used.
18
19     Returns the new site_id (> 0) if successful, faults otherwise.
20     """
21
22     roles = ['admin']
23
24     site_fields = dict(filter(can_update, Site.fields.items()))
25
26     accepts = [
27         Auth(),
28         site_fields
29         ]
30
31     returns = Parameter(int, 'New site_id (> 0) if successful')
32
33     def call(self, auth, site_fields):
34         site_fields = dict(filter(can_update, site_fields.items()))
35         site = Site(self.api, site_fields)
36         site.sync()
37         
38         # Logging variables 
39         self.event_objects = {'Site': [site['site_id']]}
40         self.message = 'Site %d created' % site['site_id']
41         
42         return site['site_id']