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 class AdmAddNodeNetwork(Method):
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.
18 PIs and techs may only add networks to their own nodes. Admins may
19 add networks to any node.
21 Returns the new nodenetwork_id (> 0) if successful, faults otherwise.
24 roles = ['admin', 'pi', 'tech']
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()))
33 Node.fields['node_id'],
34 NodeNetwork.fields['method'],
35 NodeNetwork.fields['type'],
39 returns = Parameter(int, 'New nodenetwork_id (> 0) if successful')
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"
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, optional_vals)
62 nodenetwork['node_id'] = node_id
63 nodenetwork['method'] = method
64 nodenetwork['type'] = type
67 return nodenetwork['nodenetwork_id']