get rid of svn keywords once and for good
[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.Sites import Site, Sites
7 from PLC.Auth import Auth
8
9 class DeleteNodeFromPCU(Method):
10     """
11     Deletes a node from a PCU.
12
13     Non-admins may only update PCUs at their sites.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin', 'pi', 'tech']
19
20     accepts = [
21         Auth(),
22         Mixed(Node.fields['node_id'],
23               Node.fields['hostname']),
24         PCU.fields['pcu_id']
25         ]
26
27     returns = Parameter(int, '1 if successful')
28
29     def call(self, auth, node_id_or_hostname, pcu_id):
30          # Get node
31         nodes = Nodes(self.api, [node_id_or_hostname])
32         if not nodes:
33             raise PLCInvalidArgument, "No such node"
34
35         node = nodes[0]
36
37         # Get PCU
38         pcus = PCUs(self.api, [pcu_id])
39         if not pcus:
40             raise PLCInvalidArgument, "No such PCU"
41
42         pcu = pcus[0]
43
44         if 'admin' not in self.caller['roles']:
45             ok = False
46             sites = Sites(self.api, self.caller['site_ids'])
47             for site in sites:
48                 if pcu['pcu_id'] in site['pcu_ids']:
49                     ok = True
50                     break
51             if not ok:
52                 raise PLCPermissionDenied, "Not allowed to update that PCU"
53
54         # Removed node from PCU
55
56         if node['node_id'] in pcu['node_ids']:
57             pcu.remove_node(node)
58
59         # Logging variables
60         self.event_objects = {'PCU': [pcu['pcu_id']],
61                               'Node': [node['node_id']]}
62         self.message = 'Node %d removed from PCU %d' % \
63                 (node['node_id'], pcu['pcu_id'])
64
65         return 1