Tony has reported that in some rare occurences, RefreshPeer creates
[plcapi.git] / PLC / Methods / AddPCU.py
index 1f66dcf..02fb296 100644 (file)
@@ -1,14 +1,21 @@
+# $Id$
+# $URL$
 from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
 from PLC.PCUs import PCU, PCUs
 from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
 from PLC.PCUs import PCU, PCUs
-from PLC.Auth import PasswordAuth
+from PLC.Auth import Auth
 from PLC.Sites import Site, Sites
 
 from PLC.Sites import Site, Sites
 
+can_update = lambda (field, value): field in \
+             ['ip', 'hostname', 'protocol',
+              'username', 'password',
+              'model', 'notes']
+
 class AddPCU(Method):
     """
     Adds a new power control unit (PCU) to the specified site. Any
 class AddPCU(Method):
     """
     Adds a new power control unit (PCU) to the specified site. Any
-    fields specified in optional_vals are used, otherwise defaults are
+    fields specified in pcu_fields are used, otherwise defaults are
     used.
 
     PIs and technical contacts may only add PCUs to their own sites.
     used.
 
     PIs and technical contacts may only add PCUs to their own sites.
@@ -18,27 +25,23 @@ class AddPCU(Method):
 
     roles = ['admin', 'pi', 'tech']
 
 
     roles = ['admin', 'pi', 'tech']
 
-    can_update = lambda (field, value): field in \
-                 ['hostname', 'ip', 'protocol',
-                  'username', 'password',
-                  'model', 'notes']
-    update_fields = dict(filter(can_update, PCU.fields.items()))
+    pcu_fields = dict(filter(can_update, PCU.fields.items()))
 
     accepts = [
 
     accepts = [
-        PasswordAuth(),
+        Auth(),
         Mixed(Site.fields['site_id'],
               Site.fields['login_base']),
         Mixed(Site.fields['site_id'],
               Site.fields['login_base']),
-        update_fields
+        pcu_fields
         ]
 
     returns = Parameter(int, 'New pcu_id (> 0) if successful')
 
         ]
 
     returns = Parameter(int, 'New pcu_id (> 0) if successful')
 
-    def call(self, auth, site_id_or_login_base, optional_vals = {}):
-        if filter(lambda field: field not in self.update_fields, optional_vals):
-            raise PLCInvalidArgument, "Invalid field specified"
+
+    def call(self, auth, site_id_or_login_base, pcu_fields):
+        pcu_fields = dict(filter(can_update, pcu_fields.items()))
 
         # Get associated site details
 
         # Get associated site details
-        sites = Sites(self.api, [site_id_or_login_base]).values()
+        sites = Sites(self.api, [site_id_or_login_base])
         if not sites:
             raise PLCInvalidArgument, "No such site"
         site = sites[0]
         if not sites:
             raise PLCInvalidArgument, "No such site"
         site = sites[0]
@@ -47,8 +50,14 @@ class AddPCU(Method):
             if site['site_id'] not in self.caller['site_ids']:
                 raise PLCPermissionDenied, "Not allowed to add a PCU to that site"
 
             if site['site_id'] not in self.caller['site_ids']:
                 raise PLCPermissionDenied, "Not allowed to add a PCU to that site"
 
-        pcu = PCU(self.api, optional_vals)
+        pcu = PCU(self.api, pcu_fields)
         pcu['site_id'] = site['site_id']
         pcu.sync()
 
         pcu['site_id'] = site['site_id']
         pcu.sync()
 
+        # Logging variables
+        self.event_objects = {'Site': [site['site_id']],
+                              'PCU': [pcu['pcu_id']]}
+        self.message = 'PCU %d added site %s' % \
+                (pcu['pcu_id'], site['site_id'])
+
         return pcu['pcu_id']
         return pcu['pcu_id']