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
10 can_update = lambda (field, value): field in \
11 ['ip', 'hostname', 'protocol',
12 'username', 'password',
17 Adds a new power control unit (PCU) to the specified site. Any
18 fields specified in pcu_fields are used, otherwise defaults are
21 PIs and technical contacts may only add PCUs to their own sites.
23 Returns the new pcu_id (> 0) if successful, faults otherwise.
26 roles = ['admin', 'pi', 'tech']
28 pcu_fields = dict(filter(can_update, PCU.fields.items()))
32 Mixed(Site.fields['site_id'],
33 Site.fields['login_base']),
37 returns = Parameter(int, 'New pcu_id (> 0) if successful')
40 def call(self, auth, site_id_or_login_base, pcu_fields):
41 pcu_fields = dict(filter(can_update, pcu_fields.items()))
43 # Get associated site details
44 sites = Sites(self.api, [site_id_or_login_base])
46 raise PLCInvalidArgument, "No such site"
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"
53 pcu = PCU(self.api, pcu_fields)
54 pcu['site_id'] = site['site_id']
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'])