bring over newinterface branch from Verivue
[plcapi.git] / PLC / Methods / DeleteIpAddress.py
diff --git a/PLC/Methods/DeleteIpAddress.py b/PLC/Methods/DeleteIpAddress.py
new file mode 100644 (file)
index 0000000..06efce5
--- /dev/null
@@ -0,0 +1,64 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Auth import Auth
+from PLC.Nodes import Node, Nodes
+from PLC.Interfaces import Interface, Interfaces
+from PLC.IpAddresses import IpAddress, IpAddresses
+
+class DeleteIpAddress(Method):
+    """
+    Deletes an existing ip address.
+
+    Admins may delete any ip address. PIs and techs may only delete
+    ip addresses associated with interfaces on nodes at their sites.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin', 'pi', 'tech']
+
+    accepts = [
+        Auth(),
+        IpAddress.fields['ip_address_id']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+
+    def call(self, auth, ip_address_id):
+
+        # Get interface information
+        ip_addresses = IpAddresses(self.api, [ip_address_id])
+        if not ip_addresses:
+            raise PLCInvalidArgument, "No such ip_address %r"%ip_address_id
+        ip_address = ip_addresses[0]
+
+        # Authenticated functino
+        assert self.caller is not None
+
+        # If we are not an admin, make sure that the caller is a
+        # member of the site at which the node is located.
+        if 'admin' not in self.caller['roles']:
+            # Get interface information
+            interfaces = Interfaces(self.api, [ip_address['interface_id']])
+            if not interfaces:
+                raise PLCInvalidArgument, "No such interface %r"%ip_address['interface_id']
+            interface = interfaces[0]
+
+            # Get node information
+            nodes = Nodes(self.api, [interface['node_id']])
+            if not nodes:
+                raise PLCInvalidArgument, "No such node %r"%node_id
+            node = nodes[0]
+
+            if node['site_id'] not in self.caller['site_ids']:
+                raise PLCPermissionDenied, "Not allowed to delete this ip address"
+
+        ip_address.delete()
+
+        # Logging variables
+        self.event_objects = {'IpAddress': [ip_address['ip_address_id']]}
+        self.message = "IpAddress %d deleted" % ip_address['ip_address_id']
+
+        return 1