# $Id$ # $URL$ from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed from PLC.Table import Row from PLC.Auth import Auth from PLC.Nodes import Node, Nodes from PLC.IpAddresses import IpAddress, IpAddresses from PLC.TagTypes import TagTypes from PLC.InterfaceTags import InterfaceTags from PLC.Interfaces import Interface, Interfaces from PLC.Methods.AddIpAddress import AddIpAddress from PLC.Methods.DeleteIpAddress import DeleteIpAddress from PLC.Methods.DeleteRoute import DeleteRoute from PLC.Methods.AddRoute import AddRoute from PLC.Methods.UpdateNode import UpdateNode from PLC.Methods.AddInterfaceTag import AddInterfaceTag from PLC.Methods.UpdateInterfaceTag import UpdateInterfaceTag cannot_update = ['interface_id','node_id'] legacy_interface_fields = { 'interface_id': Parameter(int, "Node interface identifier"), 'method': Parameter(str, "Addressing method (e.g., 'static' or 'dhcp')"), 'type': Parameter(str, "Address type (e.g., 'ipv4')"), 'ip': Parameter(str, "IP address", nullok = True), 'mac': Parameter(str, "MAC address", nullok = True), 'gateway': Parameter(str, "IP address of primary gateway", nullok = True), 'network': Parameter(str, "Subnet address", nullok = True), 'broadcast': Parameter(str, "Network broadcast address", nullok = True), 'netmask': Parameter(str, "Subnet mask", nullok = True), 'dns1': Parameter(str, "IP address of primary DNS server", nullok = True), 'dns2': Parameter(str, "IP address of secondary DNS server", nullok = True), 'bwlimit': Parameter(int, "Bandwidth limit", min = 0, nullok = True), 'hostname': Parameter(str, "(Optional) Hostname", nullok = True), 'node_id': Parameter(int, "Node associated with this interface"), 'is_primary': Parameter(bool, "Is the primary interface for this node"), 'interface_tag_ids' : Parameter([int], "List of interface settings"), 'last_updated': Parameter(int, "Date and time when the interface entry was last updated"), } class UpdateInterface(Method): """ Updates an existing interface network. Any values specified in interface_fields are used, otherwise defaults are used. Acceptable values for method are dhcp and static. If type is static, then ip, gateway, network, broadcast, netmask, and dns1 must all be specified in interface_fields. If type is dhcp, these parameters, even if specified, are ignored. PIs and techs may only update interfaces associated with their own nodes. Admins may update any interface network. Returns 1 if successful, faults otherwise. """ roles = ['admin', 'pi', 'tech'] accepted_fields = Row.accepted_fields(cannot_update, legacy_interface_fields,exclude=True) accepted_fields.update(Interface.tags) accepts = [ Auth(), Interface.fields['interface_id'], accepted_fields ] returns = Parameter(int, '1 if successful') # needed for generating the doc and prevent conflicts in the xml ids status = 'legacy' def call(self, auth, interface_id, interface_fields): [native,tags,rejected] = Row.split_fields(interface_fields,[legacy_interface_fields,Interface.tags]) # type checking native= Row.check_fields (native, self.accepted_fields) if rejected: raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected # Get interface information interfaces = Interfaces(self.api, [interface_id]) if not interfaces: raise PLCInvalidArgument, "No such interface" interface = interfaces[0] # Authenticated function assert self.caller is not None # If we are not an admin, make sure that the caller is a # member of the site where the node exists. if 'admin' not in self.caller['roles']: nodes = Nodes(self.api, [interface['node_id']]) if not nodes: raise PLCPermissionDenied, "Interface is not associated with a node" node = nodes[0] if node['site_id'] not in self.caller['site_ids']: raise PLCPermissionDenied, "Not allowed to update interface" # In case the user is updating one of (ip, type, network), but not the # other two, then fetch the address and get the other two. If the # interface has more than one address, then the behavior is undefined. # we'll update the first IpAddress only ip_address_ids = interface["ip_address_ids"] address_fields = {'type': 'ipv4'} if ip_address_ids: address_fields = IpAddresses(self.api, ip_address_ids[0])[0] def clean_field(obj, key): val = None if obj.has_key(key): val = obj[key] del obj[key] return val val = clean_field(native, 'ip') if val: address_fields["ip_addr"] = val val = clean_field(native, 'type') if val: address_fields["type"] = val val = clean_field(native, 'netmask') if val: address_fields["netmask"] = val for key in ("network", "broadcast"): clean_field(native, key) for key in ("last_updated", "ip_address_id", "interface_id"): clean_field(address_fields, key) # check if DNS or gateway is changed if this is primary interface dns = "" route_fields = {} if interface['is_primary'] and native.has_key('gateway'): route_fields['node_id'] = interface['node_id'] route_fields['interface_id'] = interface['interface_id'] route_fields['subnet'] = '0.0.0.0/0' route_fields['next_hop'] = native['gateway'] dns = "" if native.has_key('dns1'): dns += native['dns1'] if native.has_key('dns2'): dns += ",%s" % native['dns2'] interface.update(native) interface.sync() # we have no idea which one to delete if there's multiple interfaces, # so delete them all. for ip_address_id in ip_address_ids: DeleteIpAddress(self.api).__call__(auth, ip_address_id) AddIpAddress(self.api).__call__(auth, interface_id, address_fields) # remove routes for interface and add new default gw if this is a primary interface if route_fields: routes = Routes(self.api, {'interface_id' : route_fields['interface_id'], 'subnet' : route_fields['subnet']}) for route in routes: DeleteRoute(self.api).__call__(auth, route['route_id']) AddRoute(self.api).__call(auth, route_fields) # update dns if this is primary if dns: UpdateNode(self.api).__call__(auth, interface['node_id'], {'dns':dns}) for (tagname,value) in tags.iteritems(): # the tagtype instance is assumed to exist, just check that if not TagTypes(self.api,{'tagname':tagname}): raise PLCInvalidArgument,"No such TagType %s"%tagname interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']}) if not interface_tags: AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value) else: UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value) self.event_objects = {'Interface': [interface['interface_id']]} if 'ip' in interface: self.message = "Interface %s updated"%interface['ip'] else: self.message = "Interface %d updated"%interface['interface_id'] self.message += "[%s]." % ", ".join(interface_fields.keys()) return 1