2 # Thierry Parmentelat - INRIA
4 from PLC.Faults import *
5 from PLC.Auth import Auth
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Table import Row
10 from PLC.Nodes import Node, Nodes
11 from PLC.Interfaces import Interface, Interfaces
12 from PLC.IpAddresses import IpAddresses, IpAddress
13 from PLC.TagTypes import TagTypes
15 cannot_update = ['ip_address_id', 'interface_id']
17 class AddIpAddress(Method):
20 Adds a new address for an interface. Any values specified in
21 ip_address_fields are used, otherwise defaults are
24 PIs and techs may only add interfaces to their own nodes. Admins may
25 add interfaces to any node.
27 Returns the new ip_address_id (> 0) if successful, faults otherwise.
30 roles = ['admin', 'pi', 'tech']
32 accepted_fields = Row.accepted_fields(cannot_update, IpAddress.fields, exclude=True)
33 accepted_fields.update(IpAddress.tags)
37 Mixed(Interface.fields['interface_id']),
41 returns = Parameter(int, 'New ip_address_id (> 0) if successful')
44 def call(self, auth, interface_id, ip_address_fields):
46 [native,tags,rejected]=Row.split_fields(ip_address_fields,[IpAddress.fields,IpAddress.tags])
49 native = Row.check_fields (native, self.accepted_fields)
51 raise PLCInvalidArgument, "Cannot add IpAddress with column(s) %r"%rejected
53 # Get interface information
54 interfaces = Interfaces(self.api, [interface_id])
56 raise PLCInvalidArgument, "No such interfaces %r"%interface_id
57 interface = interfaces[0]
59 # Check if node exists
60 nodes = Nodes(self.api, [interface['node_id']])
62 raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
65 # Authenticated function
66 assert self.caller is not None
68 # If we are not an admin, make sure that the caller is a
69 # member of the site where the node exists.
70 if 'admin' not in self.caller['roles']:
71 if node['site_id'] not in self.caller['site_ids']:
72 raise PLCPermissionDenied, "Not allowed to add an interface to the specified node"
75 ip_address = IpAddress(self.api, native)
76 ip_address['interface_id'] = interface['interface_id']
80 self.event_objects = { 'Interace': [interface['interface_id']],
81 'IpAddress' : [ip_address['ip_address_id']] }
82 self.message = "Address %d added" % ip_address['ip_address_id']
84 return ip_address['ip_address_id']