- make Add() calling convention consistent among all functions that
[plcapi.git] / PLC / Methods / AddPCU.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.PCUs import PCU, PCUs
5 from PLC.Auth import PasswordAuth
6 from PLC.Sites import Site, Sites
7
8 can_update = lambda (field, value): field in \
9              ['site_id',
10               'ip', 'hostname', 'protocol',
11               'username', 'password',
12               'model', 'notes']
13
14 class AddPCU(Method):
15     """
16     Adds a new power control unit (PCU) to the specified site. Any
17     fields specified in pcu_fields are used, otherwise defaults are
18     used.
19
20     PIs and technical contacts may only add PCUs to their own sites.
21
22     Returns the new pcu_id (> 0) if successful, faults otherwise.
23     """
24
25     roles = ['admin', 'pi', 'tech']
26
27     pcu_fields = dict(filter(can_update, PCU.fields.items()))
28
29     accepts = [
30         PasswordAuth(),
31         pcu_fields
32         ]
33
34     returns = Parameter(int, 'New pcu_id (> 0) if successful')
35     
36     event_type = 'Add'
37     object_type = 'PCU'
38     object_ids = []
39
40     def call(self, auth, pcu_fields = {}):
41         pcu_fields = dict(filter(can_update, pcu_fields.items()))
42
43         # Get associated site details
44         sites = Sites(self.api, [pcu_fields['site_id']]).values()
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.sync()
55
56         self.object_ids = [site['site_id'], pcu['pcu_id']]
57
58         return pcu['pcu_id']