more detailed info passed when raising an exception
[plcapi.git] / PLC / Methods / AddNodeNetwork.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.NodeNetworks import NodeNetwork, NodeNetworks
6 from PLC.Auth import Auth
7
8 can_update = lambda (field, value): field not in ['nodenetwork_id', 'node_id']
9
10 class AddNodeNetwork(Method):
11     """
12
13     Adds a new network for a node. Any values specified in
14     nodenetwork_fields are used, otherwise defaults are
15     used. Acceptable values for method may be retrieved via
16     GetNetworkMethods. Acceptable values for type may be retrieved via
17     GetNetworkTypes.
18
19     If type is static, ip, gateway, network, broadcast, netmask, and
20     dns1 must all be specified in nodenetwork_fields. If type is dhcp,
21     these parameters, even if specified, are ignored.
22
23     PIs and techs may only add networks to their own nodes. Admins may
24     add networks to any node.
25
26     Returns the new nodenetwork_id (> 0) if successful, faults otherwise.
27     """
28
29     roles = ['admin', 'pi', 'tech']
30
31     nodenetwork_fields = dict(filter(can_update, NodeNetwork.fields.items()))
32
33     accepts = [
34         Auth(),
35         Mixed(Node.fields['node_id'],
36               Node.fields['hostname']),
37         nodenetwork_fields
38         ]
39
40     returns = Parameter(int, 'New nodenetwork_id (> 0) if successful')
41
42     event_type = 'Add'
43     object_type = 'NodeNetwork'
44     
45     def call(self, auth, node_id_or_hostname, nodenetwork_fields):
46         nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
47
48         # Check if node exists
49         nodes = Nodes(self.api, [node_id_or_hostname])
50         if not nodes:
51             raise PLCInvalidArgument, "No such node"
52         node = nodes[0]
53
54         # Authenticated function
55         assert self.caller is not None
56
57         # If we are not an admin, make sure that the caller is a
58         # member of the site where the node exists.
59         if 'admin' not in self.caller['roles']:
60             if node['site_id'] not in self.caller['site_ids']:
61                 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
62
63         # Add node network
64         nodenetwork = NodeNetwork(self.api, nodenetwork_fields)
65         nodenetwork['node_id'] = node['node_id']
66         # if this is the first node network, make it primary
67         if not node['nodenetwork_ids']:
68                 nodenetwork['is_primary'] = True
69         nodenetwork.sync()
70
71         self.object_ids = [node['node_id'], nodenetwork['nodenetwork_id']]      
72
73         return nodenetwork['nodenetwork_id']