- added class variables: event_type, object_type, object_ids (used by event logger)
[plcapi.git] / PLC / Methods / AddNodeToPCU.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 AddNodeToPCU(Method):
9     """
10     Adds a node to a port on a PCU. Faults if the node has already
11     been added to the PCU or if the port is already in use.
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         PasswordAuth(),
22         Mixed(Node.fields['node_id'],
23               Node.fields['hostname']),
24         PCU.fields['pcu_id'],
25         Parameter(int, 'PCU port number')
26         ]
27
28     returns = Parameter(int, '1 if successful')
29     event_type = 'AddTo'
30     object_type = 'PCU'
31     object_ids = []
32
33     def call(self, auth, node_id_or_hostname, pcu_id, port):
34          # Get node
35         nodes = Nodes(self.api, [node_id_or_hostname])
36         if not nodes:
37             raise PLCInvalidArgument, "No such node"
38
39         node = nodes.values()[0]
40
41         # Get PCU
42         pcus = PCUs(self.api, [pcu_id])
43         if not pcus:
44             raise PLCInvalidArgument, "No such PCU"
45
46         pcu = pcus.values()[0]
47
48         if 'admin' not in self.caller['roles']:
49             ok = False
50             sites = Sites(self.api, self.caller['site_ids']).values()
51             for site in sites:
52                 if pcu['pcu_id'] in site['pcu_ids']:
53                     ok = True
54                     break
55             if not ok:
56                 raise PLCPermissionDenied, "Not allowed to update that PCU"
57         
58         # Add node to PCU
59         if node['node_id'] in pcu['node_ids']:
60             raise PLCInvalidArgument, "Node already controlled by PCU"
61
62         if port in pcu['ports']:
63             raise PLCInvalidArgument, "PCU port already in use"
64
65         pcu.add_node(node, port)
66         self.object_ids = [pcu['pcu_id']]
67
68         return 1