82d0f7ebf505dbf8692272b54b1e4b9fbc30fa94
[plcapi.git] / PLC / Methods / Legacy / DeleteInterface.py
1 from PLC.Faults import *
2 from PLC.Auth import Auth
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Table import Row
6
7 from PLC.Nodes import Node, Nodes
8 from PLC.Interfaces import Interface, Interfaces
9 from PLC.Methods.DeleteIpAddress import DeleteIpAddress
10
11 class DeleteInterface(Method):
12     """
13     Deletes an existing interface.
14
15     Admins may delete any interface. PIs and techs may only delete
16     interface interfaces associated with nodes at their sites.
17
18     Returns 1 if successful, faults otherwise.
19     """
20
21     roles = ['admin', 'pi', 'tech']
22
23     accepts = [
24         Auth(),
25         Interface.fields['interface_id']
26         ]
27
28     returns = Parameter(int, '1 if successful')
29
30
31     def call(self, auth, interface_id):
32
33         # Get interface information
34         interfaces = Interfaces(self.api, [interface_id])
35         if not interfaces:
36             raise PLCInvalidArgument, "No such interface %r"%interface_id
37         interface = interfaces[0]
38
39         # Get node information
40         nodes = Nodes(self.api, [interface['node_id']])
41         if not nodes:
42             raise PLCInvalidArgument, "No such node %r"%node_id
43         node = nodes[0]
44
45         # Authenticated functino
46         assert self.caller is not None
47
48         # If we are not an admin, make sure that the caller is a
49         # member of the site at which the node is located.
50         if 'admin' not in self.caller['roles']:
51             if node['site_id'] not in self.caller['site_ids']:
52                 raise PLCPermissionDenied, "Not allowed to delete this interface"
53
54         for ip_address_id in interface['ip_address_ids']:
55             DeleteIpAddress(self.api).__call__(auth, ip_address_id)
56
57         interface.delete()
58
59         # Logging variables
60         self.event_objects = {'Interface': [interface['interface_id']]}
61         self.message = "Interface %d deleted" % interface['interface_id']
62
63         return 1