add pcu/node functions
[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 PasswordAuth
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         PasswordAuth(),
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     def call(self, auth, node_id_or_hostname, pcu_id):
29          # Get node
30         nodes = Nodes(self.api, [node_id_or_hostname])
31         if not nodes:
32             raise PLCInvalidArgument, "No such node"
33
34         node = nodes.values()[0]
35
36         # Get PCU
37         pcus = PCUs(self.api, [pcu_id])
38         if not pcus:
39             raise PLCInvalidArgument, "No such PCU"
40
41         pcu = pcus.values()[0]
42
43         if 'admin' not in self.caller['roles']:
44             ok = False
45             sites = Sites(self.api, self.caller['site_ids']).values()
46             for site in sites:
47                 if pcu['pcu_id'] in site['pcu_ids']:
48                     ok = True
49                     break
50             if not ok:
51                 raise PLCPermissionDenied, "Not allowed to update that PCU"
52         
53         # Removed node from PCU
54         if node['node_id'] in pcu['node_ids']:
55             pcu.remove_node(node)
56
57         return 1