rename flush() to sync(), removed {default,all,join}_fields
[plcapi.git] / PLC / Methods / AdmAddNodeNetwork.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 PasswordAuth
7
8 class AdmAddNodeNetwork(Method):
9     """
10     Adds a new network for a node. Any values specified in
11     optional_vals are used, otherwise defaults are used. Acceptable
12     values for method are dhcp, static, proxy, tap, and
13     ipmi. Acceptable value for type is ipv4. If type is static, ip,
14     gateway, network, broadcast, netmask, and dns1 must all be
15     specified in optional_vals. If type is dhcp, these parameters,
16     even if specified, are ignored.
17
18     PIs and techs may only add networks to their own nodes. Admins may
19     add networks to any node.
20
21     Returns the new nodenetwork_id (> 0) if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'tech']
25
26     can_update = lambda (field, value): field in \
27                  ['ip', 'mac', 'gateway', 'network', 'broadcast', 'netmask',
28                   'dns1', 'dns2', 'hostname', 'bwlimit', 'is_primary']
29     update_fields = dict(filter(can_update, NodeNetwork.fields.items()))
30
31     accepts = [
32         PasswordAuth(),
33         Node.fields['node_id'],
34         NodeNetwork.fields['method'],
35         NodeNetwork.fields['type'],
36         update_fields
37         ]
38
39     returns = Parameter(int, 'New nodenetwork_id (> 0) if successful')
40
41     def call(self, auth, node_id, method, type, optional_vals = {}):
42         if filter(lambda field: field not in self.update_fields, optional_vals):
43             raise PLCInvalidArgument, "Invalid fields specified"
44
45         # Check if node exists
46         nodes = Nodes(self.api, [node_id]).values()
47         if not nodes:
48             raise PLCInvalidArgument, "No such node"
49         node = nodes[0]
50
51         # Authenticated function
52         assert self.caller is not None
53
54         # If we are not an admin, make sure that the caller is a
55         # member of the site where the node exists.
56         if 'admin' not in self.caller['roles']:
57             if node['site_id'] not in self.caller['site_ids']:
58                 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
59
60         # Add node network
61         nodenetwork = NodeNetwork(self.api, optional_vals)
62         nodenetwork['method'] = method
63         nodenetwork['type'] = type
64         nodenetwork.sync(commit = False)
65
66         # Associate node network with node
67         node.add_node_network(nodenetwork, commit = False)
68
69         if 'is_primary' in optional_vals and optional_vals['is_primary']:
70             node.set_primary_node_network(nodenetwork, commit = False)
71
72         self.api.db.commit()
73
74         return nodenetwork['nodenetwork_id']