5a3068488e641cc7e3de7dc8c505b3e92db8296a
[plcapi.git] / PLC / Methods / DeleteNodeFromPCU.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.PCUs import PCU, PCUs
7 from PLC.Sites import Site, Sites
8 from PLC.Auth import Auth
9
10 class DeleteNodeFromPCU(Method):
11     """
12     Deletes a node from a PCU.
13
14     Non-admins may only update PCUs at their sites.
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         PCU.fields['pcu_id']
26         ]
27
28     returns = Parameter(int, '1 if successful')
29
30     def call(self, auth, node_id_or_hostname, pcu_id):
31          # Get node
32         nodes = Nodes(self.api, [node_id_or_hostname])
33         if not nodes:
34             raise PLCInvalidArgument, "No such node"
35
36         node = nodes[0]
37
38         # Get PCU
39         pcus = PCUs(self.api, [pcu_id])
40         if not pcus:
41             raise PLCInvalidArgument, "No such PCU"
42
43         pcu = pcus[0]
44
45         if 'admin' not in self.caller['roles']:
46             ok = False
47             sites = Sites(self.api, self.caller['site_ids'])
48             for site in sites:
49                 if pcu['pcu_id'] in site['pcu_ids']:
50                     ok = True
51                     break
52             if not ok:
53                 raise PLCPermissionDenied, "Not allowed to update that PCU"
54         
55         # Removed node from PCU
56         
57         if node['node_id'] in pcu['node_ids']:
58             pcu.remove_node(node)
59
60         # Logging variables
61         self.event_objects = {'PCU': [pcu['pcu_id']],
62                               'Node': [node['node_id']]}        
63         self.message = 'Node %d removed from PCU %d' % \
64                 (node['node_id'], pcu['pcu_id']) 
65         
66         return 1