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.Sites import Site, Sites
7 from PLC.Auth import PasswordAuth
9 class AdmAddNodeNetwork(Method):
11 Adds a new newtwork for a node. Any values specified in optional_vals are used,
12 otherwise defaults are used. Acceptable values for method are dhcp, static,
13 proxy, tap, and ipmi. Acceptable value for type is ipv4. If type is static,
14 the parameter optional_vals must be present and ip, gateway, network, broadcast,
15 netmask, and dns1 must all be specified. If type is dhcp, these parameters, even
16 if specified, are ignored. Returns the new nodenetwork_id (>0) if successful.
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 cant_update = lambda (field, value): field not in \
28 update_fields = dict(filter(cant_update, NodeNetwork.all_fields.items()))
32 NodeNetwork.all_fields['node_id'],
33 NodeNetwork.all_fields['method'],
34 NodeNetwork.all_fields['type'],
38 returns = Parameter(int, '1 if successful')
40 def call(self, auth, node_id, method, type, optional_vals = {}):
41 if filter(lambda field: field not in self.update_fields, optional_vals):
42 raise PLCInvalidArgument, "Invalid fields specified"
44 # check if node exists
45 nodes = Nodes(self.api, [node_id], Node.extra_fields).values()
47 raise PLCInvalidArgument, "No such node"
50 # Make sure node network doesnt already exist
51 nodenetworks = NodeNetworks(self.api).values()
53 for nodenetwork in nodenetworks:
54 if nodenetwork['node_id'] == node_id and nodenetwork['method'] == method and nodenetwork['type'] == type:
55 raise PLCInvalidArgument, "Node Network already exists"
57 # Authenticated function
58 assert self.caller is not None
60 # If we are not an admin, make sure that the caller is a
61 # member of the site where the node exists.
62 if 'admin' not in self.caller['roles']:
63 if node['site_id'] not in self.caller['site_ids']:
64 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
65 if 'tech' not in self.caller['roles']:
66 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
70 nodenetwork = NodeNetwork(self.api, optional_vals)
71 nodenetwork['node_id'] = node_id
72 nodenetwork['method'] = method
73 nodenetwork['type'] = type
76 return nodenetwork['nodenetwork_id']