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 Auth
6 from PLC.Sites import Site, Sites
8 can_update = lambda (field, value): field in \
9 ['ip', 'hostname', 'protocol',
10 'username', 'password',
15 Adds a new power control unit (PCU) to the specified site. Any
16 fields specified in pcu_fields are used, otherwise defaults are
19 PIs and technical contacts may only add PCUs to their own sites.
21 Returns the new pcu_id (> 0) if successful, faults otherwise.
24 roles = ['admin', 'pi', 'tech']
26 pcu_fields = dict(filter(can_update, PCU.fields.items()))
30 Mixed(Site.fields['site_id'],
31 Site.fields['login_base']),
35 returns = Parameter(int, 'New pcu_id (> 0) if successful')
38 def call(self, auth, site_id_or_login_base, pcu_fields):
39 pcu_fields = dict(filter(can_update, pcu_fields.items()))
41 # Get associated site details
42 sites = Sites(self.api, [site_id_or_login_base])
44 raise PLCInvalidArgument, "No such site"
47 if 'admin' not in self.caller['roles']:
48 if site['site_id'] not in self.caller['site_ids']:
49 raise PLCPermissionDenied, "Not allowed to add a PCU to that site"
51 pcu = PCU(self.api, pcu_fields)
52 pcu['site_id'] = site['site_id']
56 self.event_objects = {'Site': [site['site_id']],
57 'PCU': [pcu['pcu_id']]}
58 self.message = 'PCU %d added site %s' % \
59 (pcu['pcu_id'], site['site_id'])