svn keywords
[plcapi.git] / PLC / Methods / AddPCU.py
1 # $Id$
2 # $URL$
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.PCUs import PCU, PCUs
7 from PLC.Auth import Auth
8 from PLC.Sites import Site, Sites
9
10 can_update = lambda (field, value): field in \
11              ['ip', 'hostname', 'protocol',
12               'username', 'password',
13               'model', 'notes']
14
15 class AddPCU(Method):
16     """
17     Adds a new power control unit (PCU) to the specified site. Any
18     fields specified in pcu_fields are used, otherwise defaults are
19     used.
20
21     PIs and technical contacts may only add PCUs to their own sites.
22
23     Returns the new pcu_id (> 0) if successful, faults otherwise.
24     """
25
26     roles = ['admin', 'pi', 'tech']
27
28     pcu_fields = dict(filter(can_update, PCU.fields.items()))
29
30     accepts = [
31         Auth(),
32         Mixed(Site.fields['site_id'],
33               Site.fields['login_base']),
34         pcu_fields
35         ]
36
37     returns = Parameter(int, 'New pcu_id (> 0) if successful')
38     
39
40     def call(self, auth, site_id_or_login_base, pcu_fields):
41         pcu_fields = dict(filter(can_update, pcu_fields.items()))
42
43         # Get associated site details
44         sites = Sites(self.api, [site_id_or_login_base])
45         if not sites:
46             raise PLCInvalidArgument, "No such site"
47         site = sites[0]
48
49         if 'admin' not in self.caller['roles']:
50             if site['site_id'] not in self.caller['site_ids']:
51                 raise PLCPermissionDenied, "Not allowed to add a PCU to that site"
52
53         pcu = PCU(self.api, pcu_fields)
54         pcu['site_id'] = site['site_id']
55         pcu.sync()
56
57         # Logging variables
58         self.event_objects = {'Site': [site['site_id']],
59                               'PCU': [pcu['pcu_id']]}
60         self.message = 'PCU %d added site %s' % \
61                 (pcu['pcu_id'], site['site_id'])
62
63         return pcu['pcu_id']