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