2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.Interfaces import Interface, Interfaces
7 from PLC.Auth import Auth
9 can_update = lambda (field, value): field not in ['interface_id', 'node_id']
11 class AddInterface(Method):
14 Adds a new network for a node. Any values specified in
15 interface_fields are used, otherwise defaults are
16 used. Acceptable values for method may be retrieved via
17 GetNetworkMethods. Acceptable values for type may be retrieved via
20 If type is static, ip, gateway, network, broadcast, netmask, and
21 dns1 must all be specified in interface_fields. If type is dhcp,
22 these parameters, even if specified, are ignored.
24 PIs and techs may only add networks to their own nodes. Admins may
25 add networks to any node.
27 Returns the new interface_id (> 0) if successful, faults otherwise.
30 roles = ['admin', 'pi', 'tech']
32 interface_fields = dict(filter(can_update, Interface.fields.items()))
36 Mixed(Node.fields['node_id'],
37 Node.fields['hostname']),
41 returns = Parameter(int, 'New interface_id (> 0) if successful')
44 def call(self, auth, node_id_or_hostname, interface_fields):
45 interface_fields = dict(filter(can_update, interface_fields.items()))
47 # Check if node exists
48 nodes = Nodes(self.api, [node_id_or_hostname])
50 raise PLCInvalidArgument, "No such node"
53 # Authenticated function
54 assert self.caller is not None
56 # If we are not an admin, make sure that the caller is a
57 # member of the site where the node exists.
58 if 'admin' not in self.caller['roles']:
59 if node['site_id'] not in self.caller['site_ids']:
60 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
63 interface = Interface(self.api, interface_fields)
64 interface['node_id'] = node['node_id']
65 # if this is the first node network, make it primary
66 if not node['interface_ids']:
67 interface['is_primary'] = True
71 self.object_ids = [node['node_id'], interface['interface_id']]
72 self.messgage = "Node network %d added" % interface['interface_id']
74 return interface['interface_id']