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