3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Table import Row
7 from PLC.Auth import Auth
9 from PLC.Nodes import Node, Nodes
10 from PLC.TagTypes import TagTypes
11 from PLC.InterfaceTags import InterfaceTags
12 from PLC.Interfaces import Interface, Interfaces
13 from PLC.IpAddresses import IpAddress, IpAddresses
15 cannot_update = ['ip_address_id', 'interface_id']
17 class UpdateIpAddress(Method):
19 PIs and techs may only update interfaces associated with their own
20 nodes. Admins may update any interface network.
22 Returns 1 if successful, faults otherwise.
25 roles = ['admin', 'pi', 'tech']
27 accepted_fields = Row.accepted_fields(cannot_update, IpAddress.fields,exclude=True)
28 accepted_fields.update(IpAddress.tags)
32 IpAddress.fields['ip_address_id'],
36 returns = Parameter(int, '1 if successful')
38 def call(self, auth, ip_address_id, ip_address_fields):
40 [native,tags,rejected] = Row.split_fields(ip_address_fields,[IpAddress.fields,IpAddress.tags])
43 native= Row.check_fields (native, self.accepted_fields)
45 raise PLCInvalidArgument, "Cannot update IpAddress column(s) %r"%rejected
47 # Get ip_address information
48 ip_addresses = IpAddresses(self.api, [ip_address_id])
50 raise PLCInvalidArgument, "No such ip address"
52 ip_address = ip_addresses[0]
54 # Authenticated function
55 assert self.caller is not None
57 # If we are not an admin, make sure that the caller is a
58 # member of the site where the node exists.
59 if 'admin' not in self.caller['roles']:
60 # Get interface information
61 interface_id = ip_address["interface_id"]
62 interfaces = Interfaces(self.api, [interface_id])
64 raise PLCPermissionDenied, "Ip address is not associated with an interface"
65 interface = interfaces[0]
67 nodes = Nodes(self.api, [interface['node_id']])
69 raise PLCPermissionDenied, "Interface is not associated with a node"
71 if node['site_id'] not in self.caller['site_ids']:
72 raise PLCPermissionDenied, "Not allowed to update address"
74 ip_address.update(native)
75 ip_address.update_last_updated(commit=False)
78 self.event_objects = {'IpAddress': [ip_address['ip_address_id']]}
79 self.message = "Address %s updated"%ip_address['ip_addr']
80 self.message += "[%s]." % ", ".join(ip_address_fields.keys())