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