- make Add() calling convention consistent among all functions that
[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              ['name', 'abbreviated_name', 'login_base',
9               'is_public', 'latitude', 'longitude', 'url']
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     site_fields = dict(filter(can_update, Site.fields.items()))
23
24     accepts = [
25         PasswordAuth(),
26         site_fields
27         ]
28
29     returns = Parameter(int, 'New site_id (> 0) if successful')
30
31     event_type = 'Add'
32     object_type = 'Site'
33     object_ids = []
34
35     def call(self, auth, site_fields):
36         site_fields = dict(filter(can_update, site_fields.items()))
37         site = Site(self.api, site_fields)
38         site.sync()
39
40         self.object_ids = [site['site_id']]
41         
42         return site['site_id']