fix PLCAPI doc that was whining about duplicate ids in docbook xml output
[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     # needed for generating the doc and prevent conflicts in the xml ids
31     status = 'legacy'
32
33     def call(self, auth, interface_id):
34
35         # Get interface information
36         interfaces = Interfaces(self.api, [interface_id])
37         if not interfaces:
38             raise PLCInvalidArgument, "No such interface %r"%interface_id
39         interface = interfaces[0]
40
41         # Get node information
42         nodes = Nodes(self.api, [interface['node_id']])
43         if not nodes:
44             raise PLCInvalidArgument, "No such node %r"%node_id
45         node = nodes[0]
46
47         # Authenticated functino
48         assert self.caller is not None
49
50         # If we are not an admin, make sure that the caller is a
51         # member of the site at which the node is located.
52         if 'admin' not in self.caller['roles']:
53             if node['site_id'] not in self.caller['site_ids']:
54                 raise PLCPermissionDenied, "Not allowed to delete this interface"
55
56         for ip_address_id in interface['ip_address_ids']:
57             DeleteIpAddress(self.api).__call__(auth, ip_address_id)
58
59         interface.delete()
60
61         # Logging variables
62         self.event_objects = {'Interface': [interface['interface_id']]}
63         self.message = "Interface %d deleted" % interface['interface_id']
64
65         return 1