- re-enable return_fields specification
[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     object_ids = []
45
46     def call(self, auth, node_id_or_hostname, nodenetwork_fields):
47         nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
48
49         # Check if node exists
50         nodes = Nodes(self.api, [node_id_or_hostname])
51         if not nodes:
52             raise PLCInvalidArgument, "No such node"
53         node = nodes[0]
54
55         # Authenticated function
56         assert self.caller is not None
57
58         # If we are not an admin, make sure that the caller is a
59         # member of the site where the node exists.
60         if 'admin' not in self.caller['roles']:
61             if node['site_id'] not in self.caller['site_ids']:
62                 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
63
64         # Add node network
65         nodenetwork = NodeNetwork(self.api, nodenetwork_fields)
66         nodenetwork['node_id'] = node['node_id']
67         # if this is the first node network, make it primary
68         if not node['nodenetwork_ids']:
69                 nodenetwork['is_primary'] = True
70         nodenetwork.sync()
71
72         self.object_ids = [node['node_id'], nodenetwork['nodenetwork_id']]      
73
74         return nodenetwork['nodenetwork_id']