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