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