- added class variables: event_type, object_type, object_ids (used by event logger)
[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
10 class AddSite(Method):
11     """
12     Adds a new site, and creates a node group for that site. Any
13     fields specified in site_fields are used, otherwise defaults are
14     used.
15
16     Returns the new site_id (> 0) if successful, faults otherwise.
17     """
18
19     roles = ['admin']
20
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, 'New site_id (> 0) if successful')
32
33     event_type = 'Add'
34     object_type = 'Site'
35     object_ids = []
36
37     def call(self, auth, name, abbreviated_name, login_base, site_fields = {}):
38         site_fields = dict(filter(can_update, site_fields.items()))
39         site = Site(self.api, site_fields)
40         site['name'] = name
41         site['abbreviated_name'] = abbreviated_name
42         site['login_base'] = login_base
43         site.sync()
44         self.object_ids = [site['site_id']]
45         
46         return site['site_id']