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