- silently remove fields that can't be updated
authorMark Huang <mlhuang@cs.princeton.edu>
Fri, 13 Oct 2006 20:19:25 +0000 (20:19 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Fri, 13 Oct 2006 20:19:25 +0000 (20:19 +0000)
14 files changed:
PLC/Methods/AddAddress.py
PLC/Methods/AddAddressType.py
PLC/Methods/AddAttribute.py
PLC/Methods/AddNode.py
PLC/Methods/AddNodeNetwork.py
PLC/Methods/AddPCU.py
PLC/Methods/AddSite.py
PLC/Methods/UpdateAddress.py
PLC/Methods/UpdateAddressType.py
PLC/Methods/UpdateKey.py
PLC/Methods/UpdateNode.py
PLC/Methods/UpdateNodeNetwork.py
PLC/Methods/UpdatePCU.py
PLC/Methods/UpdateSite.py

index bdced15..b7502c6 100644 (file)
@@ -5,6 +5,10 @@ from PLC.Addresses import Address, Addresses
 from PLC.Auth import PasswordAuth
 from PLC.Sites import Site, Sites
 
+can_update = lambda (field, value): field in \
+             ['line1', 'line2', 'line3',
+              'city', 'state', 'postalcode', 'country']
+
 class AddAddress(Method):
     """
     Adds a new address to a site. Fields specified in
@@ -17,9 +21,6 @@ class AddAddress(Method):
 
     roles = ['admin', 'pi']
 
-    can_update = lambda (field, value): field in \
-                 ['line1', 'line2', 'line3',
-                  'city', 'state', 'postalcode', 'country']
     update_fields = dict(filter(can_update, Address.fields.items()))
 
     accepts = [
@@ -32,8 +33,7 @@ class AddAddress(Method):
     returns = Parameter(int, 'New address_id (> 0) if successful')
 
     def call(self, auth, site_id_or_login_base, address_fields = {}):
-        if filter(lambda field: field not in self.update_fields, address_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+        address_fields = dict(filter(can_update, address_fields.items()))
 
         # Get associated site details
         sites = Sites(self.api, [site_id_or_login_base]).values()
index 25f9d42..d55aa4d 100644 (file)
@@ -4,6 +4,8 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.AddressTypes import AddressType, AddressTypes
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in ['description']
+
 class AddAddressType(Method):
     """
     Adds a new address type. Fields specified in address_type_fields
@@ -14,7 +16,6 @@ class AddAddressType(Method):
 
     roles = ['admin']
 
-    can_update = lambda (field, value): field in ['description']
     update_fields = dict(filter(can_update, AddressType.fields.items()))
 
     accepts = [
@@ -26,6 +27,7 @@ class AddAddressType(Method):
     returns = Parameter(int, 'New address_type_id (> 0) if successful')
 
     def call(self, auth, name, address_type_fields = {}):
+        address_type_fields = dict(filter(can_update, address_type_fields.items()))
         address_type = AddressType(self.api, address_type_fields)
         address_type['name'] = name
         address_type.sync()
index ca990cb..34cb104 100644 (file)
@@ -4,18 +4,19 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Attributes import Attribute, Attributes
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['description', 'min_role_id']
+
 class AddAttribute(Method):
     """
-    Adds a new type of attribute. Any fields specified in optional_vals
-    are used, otherwise defaults are used.
+    Adds a new type of attribute. Any fields specified in
+    attribute_fields are used, otherwise defaults are used.
 
     Returns the new attribute_id (> 0) if successful, faults otherwise.
     """
 
     roles = ['admin']
 
-    can_update = lambda (field, value): field in \
-                 ['description', 'min_role_id']
     update_fields = dict(filter(can_update, Attribute.fields.items()))
 
     accepts = [
@@ -26,11 +27,9 @@ class AddAttribute(Method):
 
     returns = Parameter(int, 'New attribute_id (> 0) if successful')
 
-    def call(self, auth, name, optional_vals = {}):
-        if filter(lambda field: field not in self.update_fields, optional_vals):
-            raise PLCInvalidArgument, "Invalid field specified"
-
-        attribute = Attribute(self.api, optional_vals)
+    def call(self, auth, name, attribute_fields = {}):
+        attribute_fields = dict(filter(can_update, attribute_fields.items()))
+        attribute = Attribute(self.api, attribute_fields)
         attribute['name'] = name
         attribute.sync()
 
index fc1b18a..086f5e0 100644 (file)
@@ -6,9 +6,12 @@ from PLC.NodeGroups import NodeGroup, NodeGroups
 from PLC.Sites import Site, Sites
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['boot_state', 'model', 'version']
+
 class AddNode(Method):
     """
-    Adds a new node. Any values specified in optional_vals are used,
+    Adds a new node. Any values specified in node_fields are used,
     otherwise defaults are used.
 
     PIs and techs may only add nodes to their own sites. Admins may
@@ -19,8 +22,6 @@ class AddNode(Method):
 
     roles = ['admin', 'pi', 'tech']
 
-    can_update = lambda (field, value): field in \
-                 ['boot_state', 'model', 'version']
     update_fields = dict(filter(can_update, Node.fields.items()))
 
     accepts = [
@@ -33,10 +34,9 @@ class AddNode(Method):
 
     returns = Parameter(int, 'New node_id (> 0) if successful')
 
-    def call(self, auth, site_id_or_login_base, hostname, optional_vals = {}):
-        if filter(lambda field: field not in self.update_fields, optional_vals):
-            raise PLCInvalidArgument, "Invalid fields specified"
-
+    def call(self, auth, site_id_or_login_base, hostname, node_fields = {}):
+        node_fields = dict(filter(can_update, node_fields.items()))
+        
         # Get site information
         sites = Sites(self.api, [site_id_or_login_base])
         if not sites:
@@ -56,7 +56,7 @@ class AddNode(Method):
             else:
                 assert self.caller['person_id'] in site['person_ids']
 
-        node = Node(self.api, optional_vals)
+        node = Node(self.api, node_fields)
         node['hostname'] = hostname
         node['site_id'] = site['site_id']
         node.sync()
index 693e5dd..b8ddba6 100644 (file)
@@ -5,14 +5,18 @@ from PLC.Nodes import Node, Nodes
 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['ip', 'mac', 'gateway', 'network', 'broadcast', 'netmask',
+              'dns1', 'dns2', 'hostname', 'bwlimit', 'is_primary']
+
 class AddNodeNetwork(Method):
     """
     Adds a new network for a node. Any values specified in
-    optional_vals are used, otherwise defaults are used. Acceptable
+    nodenetwork_fields are used, otherwise defaults are used. Acceptable
     values for method are dhcp, static, proxy, tap, and
     ipmi. Acceptable value for type is ipv4. If type is static, ip,
     gateway, network, broadcast, netmask, and dns1 must all be
-    specified in optional_vals. If type is dhcp, these parameters,
+    specified in nodenetwork_fields. If type is dhcp, these parameters,
     even if specified, are ignored.
 
     PIs and techs may only add networks to their own nodes. ins may
@@ -23,9 +27,6 @@ class AddNodeNetwork(Method):
 
     roles = ['admin', 'pi', 'tech']
 
-    can_update = lambda (field, value): field in \
-                 ['ip', 'mac', 'gateway', 'network', 'broadcast', 'netmask',
-                  'dns1', 'dns2', 'hostname', 'bwlimit', 'is_primary']
     update_fields = dict(filter(can_update, NodeNetwork.fields.items()))
 
     accepts = [
@@ -38,9 +39,8 @@ class AddNodeNetwork(Method):
 
     returns = Parameter(int, 'New nodenetwork_id (> 0) if successful')
 
-    def call(self, auth, node_id, method, type, optional_vals = {}):
-        if filter(lambda field: field not in self.update_fields, optional_vals):
-            raise PLCInvalidArgument, "Invalid fields specified"
+    def call(self, auth, node_id, method, type, nodenetwork_fields = {}):
+        nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
 
         # Check if node exists
         nodes = Nodes(self.api, [node_id]).values()
@@ -58,7 +58,7 @@ class AddNodeNetwork(Method):
                 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
 
         # Add node network
-       nodenetwork = NodeNetwork(self.api, optional_vals)
+       nodenetwork = NodeNetwork(self.api, nodenetwork_fields)
         nodenetwork['node_id'] = node_id
        nodenetwork['method'] = method
         nodenetwork['type'] = type
index 1f66dcf..8c47ade 100644 (file)
@@ -5,10 +5,15 @@ from PLC.PCUs import PCU, PCUs
 from PLC.Auth import PasswordAuth
 from PLC.Sites import Site, Sites
 
+can_update = lambda (field, value): field in \
+             ['hostname', 'ip', 'protocol',
+              'username', 'password',
+              'model', 'notes']
+
 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.
@@ -18,10 +23,6 @@ class AddPCU(Method):
 
     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()))
 
     accepts = [
@@ -33,9 +34,8 @@ class AddPCU(Method):
 
     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
         sites = Sites(self.api, [site_id_or_login_base]).values()
@@ -47,7 +47,7 @@ class AddPCU(Method):
             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()
 
index 171670d..322c356 100644 (file)
@@ -4,6 +4,10 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Sites import Site, Sites
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['is_public', 'latitude', 'longitude', 'url',
+              'organization_id', 'ext_consortium_id']
+
 class AddSite(Method):
     """
     Adds a new site, and creates a node group for that site. Any
@@ -15,9 +19,6 @@ class AddSite(Method):
 
     roles = ['admin']
 
-    can_update = lambda (field, value): field in \
-                 ['is_public', 'latitude', 'longitude', 'url',
-                  'organization_id', 'ext_consortium_id']
     update_fields = dict(filter(can_update, Site.fields.items()))
 
     accepts = [
@@ -30,11 +31,9 @@ class AddSite(Method):
 
     returns = Parameter(int, 'New site_id (> 0) if successful')
 
-    def call(self, auth, name, abbreviated_name, login_base, optional_vals = {}):
-        if filter(lambda field: field not in self.update_fields, optional_vals):
-            raise PLCInvalidArgument, "Invalid field specified"
-
-        site = Site(self.api, optional_vals)
+    def call(self, auth, name, abbreviated_name, login_base, site_fields = {}):
+        site_fields = dict(filter(can_update, site_fields.items()))
+        site = Site(self.api, site_fields)
         site['name'] = name
         site['abbreviated_name'] = abbreviated_name
         site['login_base'] = login_base
index 2428b2f..0cd76ab 100644 (file)
@@ -4,6 +4,10 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Addresses import Address, Addresses
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['line1', 'line2', 'line3',
+              'city', 'state', 'postalcode', 'country']
+
 class UpdateAddress(Method):
     """
     Updates the parameters of an existing address with the values in
@@ -16,9 +20,6 @@ class UpdateAddress(Method):
 
     roles = ['admin', 'pi']
 
-    can_update = lambda (field, value): field in \
-                 ['line1', 'line2', 'line3',
-                  'city', 'state', 'postalcode', 'country']
     update_fields = dict(filter(can_update, Address.fields.items()))
 
     accepts = [
@@ -30,8 +31,7 @@ class UpdateAddress(Method):
     returns = Parameter(int, '1 if successful')
 
     def call(self, auth, address_id, address_fields):
-        if filter(lambda field: field not in self.update_fields, address_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+        address_fields = dict(filter(can_update, address_fields.items()))
 
         # Get associated address details
         addresses = Addresses(self.api, [address_id]).values()
index f8ad7c4..950c311 100644 (file)
@@ -4,6 +4,8 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.AddressTypes import AddressType, AddressTypes
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in ['name', 'description']
+
 class UpdateAddressType(Method):
     """
     Updates the parameters of an existing address type with the values
@@ -14,7 +16,6 @@ class UpdateAddressType(Method):
 
     roles = ['admin']
 
-    can_update = lambda (field, value): field in ['name', 'description']
     update_fields = dict(filter(can_update, AddressType.fields.items()))
 
     accepts = [
@@ -27,8 +28,7 @@ class UpdateAddressType(Method):
     returns = Parameter(int, '1 if successful')
 
     def call(self, auth, address_type_id_or_name, address_type_fields):
-        if filter(lambda field: field not in self.update_fields, address_type_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+        address_type_fields = dict(filter(can_update, address_type_fields.items()))
 
         address_types = AddressTypes(self.api, [address_type_id_or_name]).values()
         if not address_types:
index 7aa21cc..97490c2 100644 (file)
@@ -4,6 +4,9 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Keys import Key, Keys
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['key_type', 'key']
+
 class UpdateKey(Method):
     """
     Updates the parameters of an existing key with the values in
@@ -16,8 +19,6 @@ class UpdateKey(Method):
 
     roles = ['admin', 'pi', 'tech', 'user']
 
-    can_update = lambda (field, value): field in \
-                 ['key_type', 'key']
     update_fields = dict(filter(can_update, Key.fields.items()))
 
     accepts = [
@@ -29,9 +30,7 @@ class UpdateKey(Method):
     returns = Parameter(int, '1 if successful')
 
     def call(self, auth, key_id, key_fields):
-       # Make sure only valid fields are specified
-       if filter(lambda field: field not in self.update_fields, key_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+        key_fields = dict(filter(can_update, key_fields.items()))
 
         # Get key information
         keys = Keys(self.api, [key_id]).values()
index f9d02d4..578fdbb 100644 (file)
@@ -4,9 +4,12 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Nodes import Node, Nodes
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['hostname', 'boot_state', 'model', 'version']
+
 class UpdateNode(Method):
     """
-    Updates a node. Only the fields specified in update_fields are
+    Updates a node. Only the fields specified in node_fields are
     updated, all other fields are left untouched.
 
     To remove a value without setting a new one in its place (for
@@ -21,8 +24,6 @@ class UpdateNode(Method):
 
     roles = ['admin', 'pi', 'tech']
 
-    can_update = lambda (field, value): field in \
-                 ['hostname', 'boot_state', 'model', 'version']
     update_fields = dict(filter(can_update, Node.fields.items()))
 
     accepts = [
@@ -34,9 +35,8 @@ class UpdateNode(Method):
 
     returns = Parameter(int, '1 if successful')
 
-    def call(self, auth, node_id_or_hostname, update_fields):
-        if filter(lambda field: field not in self.update_fields, update_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+    def call(self, auth, node_id_or_hostname, node_fields):
+        node_fields = dict(filter(can_update, node_fields.items()))
 
         # Get account information
         nodes = Nodes(self.api, [node_id_or_hostname])
@@ -54,7 +54,7 @@ class UpdateNode(Method):
             if node['site_id'] not in self.caller['site_ids']:
                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
 
-        node.update(update_fields)
+        node.update(node_fields)
         node.sync()
 
         return 1
index 393b6e4..26063bf 100644 (file)
@@ -1,4 +1,3 @@
-
 from PLC.Faults import *
 from PLC.Method import Method
 from PLC.Parameter import Parameter, Mixed
@@ -6,6 +5,9 @@ from PLC.Nodes import Node, Nodes
 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field not in \
+             ['nodenetwork_id']
+
 class UpdateNodeNetwork(Method):
     """
     Updates an existing node network. Any values specified in update_fields 
@@ -23,8 +25,6 @@ class UpdateNodeNetwork(Method):
 
     roles = ['admin', 'pi', 'tech']
 
-    can_update = lambda (field, value): field not in \
-                 ['nodenetwork_id']
     update_fields = dict(filter(can_update, NodeNetwork.fields.items()))
 
     accepts = [
@@ -36,10 +36,8 @@ class UpdateNodeNetwork(Method):
 
     returns = Parameter(int, '1 if successful')
 
-    def call(self, auth, nodenetwork_id_or_hostname, update_fields):
-        # Check for invalid fields
-        if filter(lambda field: field not in self.update_fields, update_fields):
-            raise PLCInvalidArgument, "Invalid fields specified"
+    def call(self, auth, nodenetwork_id_or_hostname, nodenetwork_fields):
+        nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
 
        # Get node network information
        nodenetworks = NodeNetworks(self.api, [nodenetwork_id_or_hostname]).values()
@@ -62,7 +60,7 @@ class UpdateNodeNetwork(Method):
                 raise PLCPermissionDenied, "Not allowed to update node network"
 
        # Update node network
-       nodenetwork.update(update_fields)
+       nodenetwork.update(nodenetwork_fields)
         nodenetwork.sync()
        
         return 1
index b338d7e..4459b80 100644 (file)
@@ -4,6 +4,9 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.PCUs import PCU, PCUs
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field not in \
+             ['pcu_id', 'site_id']
+
 class UpdatePCU(Method):
     """
     Updates the parameters of an existing PCU with the values in
@@ -16,8 +19,6 @@ class UpdatePCU(Method):
 
     roles = ['admin', 'pi', 'tech']
 
-    can_update = lambda (field, value): field not in \
-                 ['pcu_id', 'site_id']
     update_fields = dict(filter(can_update, PCU.fields.items()))
 
     accepts = [
@@ -29,9 +30,7 @@ class UpdatePCU(Method):
     returns = Parameter(int, '1 if successful')
 
     def call(self, auth, pcu_id, pcu_fields):
-       # Make sure only valid fields are specified
-       if filter(lambda field: field not in self.update_fields, pcu_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+        pcu_fields = dict(filter(can_update, pcu_fields.items()))
 
         # Get associated PCU details
         pcus = PCUs(self.api, [pcu_id]).values()
index bb51747..c405f49 100644 (file)
@@ -4,6 +4,11 @@ from PLC.Parameter import Parameter, Mixed
 from PLC.Sites import Site, Sites
 from PLC.Auth import PasswordAuth
 
+can_update = lambda (field, value): field in \
+             ['name', 'abbreviated_name',
+              'is_public', 'latitude', 'longitude', 'url',
+              'max_slices', 'max_slivers']
+
 class UpdateSite(Method):
     """
     Updates a site. Only the fields specified in update_fields are
@@ -22,10 +27,6 @@ class UpdateSite(Method):
 
     roles = ['admin', 'pi']
 
-    can_update = lambda (field, value): field in \
-                 ['name', 'abbreviated_name',
-                  'is_public', 'latitude', 'longitude', 'url',
-                  'max_slices', 'max_slivers']
     update_fields = dict(filter(can_update, Site.fields.items()))
 
     accepts = [
@@ -37,10 +38,8 @@ class UpdateSite(Method):
 
     returns = Parameter(int, '1 if successful')
 
-    def call(self, auth, site_id_or_login_base, update_fields):
-       # Check for invalid fields
-        if filter(lambda field: field not in self.update_fields, update_fields):
-            raise PLCInvalidArgument, "Invalid field specified"
+    def call(self, auth, site_id_or_login_base, site_fields):
+        site_fields = dict(filter(can_update, site_fields.items()))
 
         # Get site information
         sites = Sites(self.api, [site_id_or_login_base])
@@ -58,10 +57,10 @@ class UpdateSite(Method):
             if site['site_id'] not in self.caller['site_ids']:
                 raise PLCPermissionDenied, "Not allowed to modify specified site"
 
-            if 'max_slices' or 'max_slivers' in update_fields:
+            if 'max_slices' or 'max_slivers' in site_fields:
                 raise PLCInvalidArgument, "Only admins can update max_slices and max_slivers"
 
-        site.update(update_fields)
+        site.update(site_fields)
        site.sync()
        
        return 1