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