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 not in ['nodenetwork_id']
10 class AddNodeNetwork(Method):
12 Adds a new network for a node. Any values specified in
13 nodenetwork_fields are used, otherwise defaults are used. Acceptable
14 values for method are dhcp, static, proxy, tap, and
15 ipmi. Acceptable value for type is ipv4. If type is static, ip,
16 gateway, network, broadcast, netmask, and dns1 must all be
17 specified in nodenetwork_fields. If type is dhcp, these parameters,
18 even if specified, are ignored.
20 PIs and techs may only add networks to their own nodes. ins may
21 add networks to any node.
23 Returns the new nodenetwork_id (> 0) if successful, faults otherwise.
26 roles = ['admin', 'pi', 'tech']
28 nodenetwork_fields = dict(filter(can_update, NodeNetwork.fields.items()))
35 returns = Parameter(int, 'New nodenetwork_id (> 0) if successful')
38 object_type = 'NodeNetwork'
41 def call(self, auth, nodenetwork_fields = {}):
42 nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
44 # Check if node exists
45 nodes = Nodes(self.api, [nodenetwork_fields['node_id']]).values()
47 raise PLCInvalidArgument, "No such node"
50 # Authenticated function
51 assert self.caller is not None
53 # If we are not an admin, make sure that the caller is a
54 # member of the site where the node exists.
55 if 'admin' not in self.caller['roles']:
56 if node['site_id'] not in self.caller['site_ids']:
57 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
60 nodenetwork = NodeNetwork(self.api, nodenetwork_fields)
63 self.object_ids = [node['node_id'], nodenetwork['nodenetwork_id']]
65 return nodenetwork['nodenetwork_id']