bring over newinterface branch from Verivue
[plcapi.git] / PLC / Methods / UpdateIpAddress.py
diff --git a/PLC/Methods/UpdateIpAddress.py b/PLC/Methods/UpdateIpAddress.py
new file mode 100644 (file)
index 0000000..f07f1ff
--- /dev/null
@@ -0,0 +1,82 @@
+# $Id$
+# $URL$
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Table import Row
+from PLC.Auth import Auth
+
+from PLC.Nodes import Node, Nodes
+from PLC.TagTypes import TagTypes
+from PLC.InterfaceTags import InterfaceTags
+from PLC.Interfaces import Interface, Interfaces
+from PLC.IpAddresses import IpAddress, IpAddresses
+
+cannot_update = ['ip_address_id', 'interface_id']
+
+class UpdateIpAddress(Method):
+    """
+    PIs and techs may only update interfaces associated with their own
+    nodes. Admins may update any interface network.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin', 'pi', 'tech']
+
+    accepted_fields = Row.accepted_fields(cannot_update, IpAddress.fields,exclude=True)
+    accepted_fields.update(IpAddress.tags)
+
+    accepts = [
+        Auth(),
+        IpAddress.fields['ip_address_id'],
+        accepted_fields
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, ip_address_id, ip_address_fields):
+
+        [native,tags,rejected] = Row.split_fields(ip_address_fields,[IpAddress.fields,IpAddress.tags])
+
+        # type checking
+        native= Row.check_fields (native, self.accepted_fields)
+        if rejected:
+            raise PLCInvalidArgument, "Cannot update IpAddress column(s) %r"%rejected
+
+        # Get ip_address information
+        ip_addresses = IpAddresses(self.api, [ip_address_id])
+        if not ip_addresses:
+            raise PLCInvalidArgument, "No such ip address"
+
+        ip_address = ip_addresses[0]
+
+        # Authenticated function
+        assert self.caller is not None
+
+        # If we are not an admin, make sure that the caller is a
+        # member of the site where the node exists.
+        if 'admin' not in self.caller['roles']:
+            # Get interface information
+            interface_id = ip_address["interface_id"]
+            interfaces = Interfaces(self.api, [interface_id])
+            if not interfaces:
+                raise PLCPermissionDenied, "Ip address is not associated with an interface"
+            interface = interfaces[0]
+
+            nodes = Nodes(self.api, [interface['node_id']])
+            if not nodes:
+                raise PLCPermissionDenied, "Interface is not associated with a node"
+            node = nodes[0]
+            if node['site_id'] not in self.caller['site_ids']:
+                raise PLCPermissionDenied, "Not allowed to update address"
+
+        ip_address.update(native)
+        ip_address.update_last_updated(commit=False)
+        ip_address.sync()
+
+        self.event_objects = {'IpAddress': [ip_address['ip_address_id']]}
+        self.message = "Address %s updated"%ip_address['ip_addr']
+        self.message += "[%s]." % ", ".join(ip_address_fields.keys())
+
+        return 1