svn keywords
[plcapi.git] / PLC / Methods / 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     def call(self, auth, node_id_or_hostname):
30         # Get account information
31         nodes = Nodes(self.api, [node_id_or_hostname])
32         if not nodes:
33             raise PLCInvalidArgument, "No such node"
34         node = nodes[0]
35
36         if node['peer_id'] is not None:
37             raise PLCInvalidArgument, "Not a local node"
38
39         # If we are not an admin, make sure that the caller is a
40         # member of the site at which the node is located.
41         if 'admin' not in self.caller['roles']:
42             # Authenticated function
43             assert self.caller is not None
44
45             if node['site_id'] not in self.caller['site_ids']:
46                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
47
48         node_id=node['node_id']
49         site_id=node['site_id']
50         node.delete()
51
52         # Logging variables
53         # it's not much use to attach to the node as it's going to vanish...
54         self.event_objects = {'Node': [node_id], 'Site': [site_id] }
55         self.message = "Node %d deleted" % node['node_id']
56
57         return 1