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
8 can_update = lambda (field, value): field in \
9 ['ip', 'mac', 'gateway', 'network', 'broadcast', 'netmask',
10 'dns1', 'dns2', 'hostname', 'bwlimit', 'is_primary']
12 class AddNodeNetwork(Method):
14 Adds a new network for a node. Any values specified in
15 nodenetwork_fields are used, otherwise defaults are used. Acceptable
16 values for method are dhcp, static, proxy, tap, and
17 ipmi. Acceptable value for type is ipv4. If type is static, ip,
18 gateway, network, broadcast, netmask, and dns1 must all be
19 specified in nodenetwork_fields. If type is dhcp, these parameters,
20 even if specified, are ignored.
22 PIs and techs may only add networks to their own nodes. ins may
23 add networks to any node.
25 Returns the new nodenetwork_id (> 0) if successful, faults otherwise.
28 roles = ['admin', 'pi', 'tech']
30 update_fields = dict(filter(can_update, NodeNetwork.fields.items()))
34 Node.fields['node_id'],
35 NodeNetwork.fields['method'],
36 NodeNetwork.fields['type'],
40 returns = Parameter(int, 'New nodenetwork_id (> 0) if successful')
42 def call(self, auth, node_id, method, type, nodenetwork_fields = {}):
43 nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
45 # Check if node exists
46 nodes = Nodes(self.api, [node_id]).values()
48 raise PLCInvalidArgument, "No such node"
51 # Authenticated function
52 assert self.caller is not None
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"
61 nodenetwork = NodeNetwork(self.api, nodenetwork_fields)
62 nodenetwork['node_id'] = node_id
63 nodenetwork['method'] = method
64 nodenetwork['type'] = type
67 return nodenetwork['nodenetwork_id']