def remove_ip_from_vlan(self, vlan_number, ip_network): vlan = self._get_vlan(vlan_number, include_vif_data=True) existing_ip = next( (ip for ip in vlan.ips if ip.ip == ip_network.ip and ip.netmask == ip_network.netmask), False) if not existing_ip: raise UnknownIP(ip_network) with self.config(), self.interface_vlan(vlan): on_hold = [] if not existing_ip.is_secondary: for ip in vlan.ips: if ip.is_secondary and ip in existing_ip: on_hold.append(ip) self.shell.do("no ip address {}".format(ip)) self.shell.do("no ip address {}".format(existing_ip)) if len(on_hold) > 0: self.shell.do("ip address {}".format(on_hold[0])) for ip in on_hold[1:]: self.shell.do("ip address {} secondary".format(ip))
def _get_address_that_contains_all_ips(self, subnets, ips): def subnet_contains_all_ips(ips, subnet): return all(ip in subnet for ip in ips) subnet_found = None for subnet in subnets: if subnet_contains_all_ips(ips, subnet): subnet_found = subnet if not subnet_found: raise UnknownIP(",".join(map(str, ips))) return subnet_found
def remove_ip_from_vlan(self, vlan_number, ip_network): vlan = self.get_vlan_interface_data(vlan_number) existing_ip = next((ip for ip in vlan.ips if ip.ip == ip_network.ip and ip.netmask == ip_network.netmask), False) if existing_ip: ip_index = vlan.ips.index(existing_ip) with self.config(), self.interface_vlan(vlan_number): if ip_index == 0: if len(vlan.ips) == 1: self.ssh.do('no ip address {} {}'.format(ip_network.ip, ip_network.netmask)) else: self.ssh.do('ip address {} {}'.format(vlan.ips[1].ip, vlan.ips[1].netmask)) else: self.ssh.do('no ip address {} {} secondary'.format(ip_network.ip, ip_network.netmask)) else: raise UnknownIP(ip_network)
def remove_ip_from_vlan(self, vlan_number, ip_network): config = self.query(one_interface_vlan(vlan_number)) if len(config.xpath( "data/configuration/interfaces/interface/unit")) < 1: raise UnknownVlan(vlan_number) for addr_node in config.xpath( "data/configuration/interfaces/interface/unit/family/inet/address/name" ): address = IPNetwork(addr_node.text) if ip_network in address: update = Update() update.add_interface( irb_address_update(vlan_number, ip_network, operation="delete")) self._push(update) return raise UnknownIP(ip_network)
def remove_ip_from_vlan(self, vlan_number, ip_network): vlan = self.get_vlan(vlan_number) existing_ip = next((ip for ip in vlan.ips if ip.ip == ip_network.ip and ip.netmask == ip_network.netmask), False) if existing_ip: ip_index = vlan.ips.index(existing_ip) if ip_index == 0: if len(vlan.ips) == 1: remove_ip_command = "no ip address {}".format(str(ip_network)) else: remove_ip_command = "ip address {}".format(str(vlan.ips[1])) else: remove_ip_command = "no ip address {} secondary".format(str(ip_network)) commands = [ "interface Vlan{}".format(vlan_number), remove_ip_command ] self.node.config(commands) else: raise UnknownIP(ip_network)