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