- Change .py files to use 4-space indents and no hard tab characters.
[plcapi.git] / PLC / Methods / DeleteNodeFromPCU.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.Nodes import Node, Nodes
7 from PLC.PCUs import PCU, PCUs
8 from PLC.Sites import Site, Sites
9 from PLC.Auth import Auth
10
11 class DeleteNodeFromPCU(Method):
12     """
13     Deletes a node from a PCU.
14
15     Non-admins may only update PCUs at their sites.
16
17     Returns 1 if successful, faults otherwise.
18     """
19
20     roles = ['admin', 'pi', 'tech']
21
22     accepts = [
23         Auth(),
24         Mixed(Node.fields['node_id'],
25               Node.fields['hostname']),
26         PCU.fields['pcu_id']
27         ]
28
29     returns = Parameter(int, '1 if successful')
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.event_objects = {'PCU': [pcu['pcu_id']],
63                               'Node': [node['node_id']]}
64         self.message = 'Node %d removed from PCU %d' % \
65                 (node['node_id'], pcu['pcu_id'])
66
67         return 1