fix PLCAPI doc that was whining about duplicate ids in docbook xml output
[plcapi.git] / PLC / Methods / Legacy / DeleteNode.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
9 class DeleteNode(Method):
10     """
11     Mark an existing node as deleted.
12
13     PIs and techs may only delete nodes at their own sites. ins may
14     delete nodes at any site.
15
16     Returns 1 if successful, faults otherwise.
17     """
18
19     roles = ['admin', 'pi', 'tech']
20
21     accepts = [
22         Auth(),
23         Mixed(Node.fields['node_id'],
24               Node.fields['hostname'])
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29     # needed for generating the doc and prevent conflicts in the xml ids
30     status = 'legacy'
31
32     def call(self, auth, node_id_or_hostname):
33         # Get account information
34         nodes = Nodes(self.api, [node_id_or_hostname])
35         if not nodes:
36             raise PLCInvalidArgument, "No such node"
37         node = nodes[0]
38
39         if node['peer_id'] is not None:
40             raise PLCInvalidArgument, "Not a local node"
41
42         # If we are not an admin, make sure that the caller is a
43         # member of the site at which the node is located.
44         if 'admin' not in self.caller['roles']:
45             # Authenticated function
46             assert self.caller is not None
47
48             if node['site_id'] not in self.caller['site_ids']:
49                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
50
51         node_id=node['node_id']
52         site_id=node['site_id']
53         node.delete()
54
55         # Logging variables
56         # it's not much use to attach to the node as it's going to vanish...
57         self.event_objects = {'Node': [node_id], 'Site': [site_id] }
58         self.message = "Node %d deleted" % node['node_id']
59
60         return 1