Exemplo n.º 1
0
 def test_get_patched_ip_address(self):
     self.assertEqual(utils.get_patched_ip_address('45', '10.10.0.0/24'),
                      '10.10.0.45')
     self.assertEqual(utils.get_patched_ip_address('0.45', '10.10.0.0/24'),
                      '10.10.0.45')
     self.assertEqual(
         utils.get_patched_ip_address('0.0.45', '10.10.0.0/24'),
         '10.10.0.45')
     self.assertEqual(
         utils.get_patched_ip_address('1.0.0.23', '10.10.0.0/24'),
         '1.0.0.23')
     self.assertEqual(utils.get_patched_ip_address('.45', '10.10.0.0/24'),
                      '10.10.0.45')
    def execute(self, vthunder, member, vrid):
        device_vrid_ip = None
        if vrid:
            device_vrid_ip = vrid.vrid_floating_ip

        conf_floating_ip = a10_utils.get_vrid_floating_ip_for_project(member.project_id)
        if conf_floating_ip:
            subnet = self.network_driver.get_subnet(member.subnet_id)
            subnet_ip, subnet_mask = a10_utils.get_net_info_from_cidr(subnet.cidr)
            if conf_floating_ip.lower() == 'dhcp':
                if not a10_utils.check_ip_in_subnet_range(device_vrid_ip, subnet_ip, subnet_mask):
                    try:
                        self.fip_port = self.network_driver.create_port(subnet.network_id,
                                                                        member.subnet_id)
                    except Exception as e:
                        LOG.error("Failed to create neutron port for member: %s", member.id)
                        raise e

            else:
                conf_floating_ip = a10_utils.get_patched_ip_address(conf_floating_ip, subnet.cidr)
                if not a10_utils.check_ip_in_subnet_range(conf_floating_ip, subnet_ip, subnet_mask):
                    msg = "Invalid VRID floating IP. IP out of subnet range: "
                    msg += str(conf_floating_ip)
                    raise exceptions.VRIDIPNotInSubentRangeError(msg)

                if conf_floating_ip != device_vrid_ip:
                    try:
                        self.fip_port = self.network_driver.create_port(subnet.network_id,
                                                                        member.subnet_id,
                                                                        fixed_ip=conf_floating_ip)
                    except Exception as e:
                        LOG.error("Failed to create neutron port for member: %s with "
                                  "floating IP %s", member.id, conf_floating_ip)
                        raise e

            if self.fip_port:
                self.update_device_vrid_fip(self.fip_port.fixed_ips[0].ip_address, vthunder, vrid)

        if vrid and vrid.vrid_port_id and (self.fip_port or not conf_floating_ip):
            try:
                self.network_driver.delete_port(vrid.vrid_port_id)
            except Exception as e:
                LOG.error("Failed to delete neutron port: %s for member %s on vrid %s",
                          vrid.vrid_port_id, member.id, str(vrid))
                raise e
            if not conf_floating_ip:
                try:
                    self.axapi_client.vrrpa.delete(vrid.vrid)
                except Exception as e:
                    LOG.exceptions("Failed to delete vrid %s for member %s", str(vrid), member.id)
                    raise e

        return self.fip_port
    def execute(self,
                vthunder,
                lb_resource,
                vrid_list,
                subnet,
                vthunder_config,
                use_device_flavor=False):
        """
        :param vthunder:
        :param lb_resource: Can accept LB or member
        :param vrid_list: VRID object list for LB resource's project.
        :param subnet: subnet of the resource in question, will be helpful if there is no
        VRID object present for the provided subnet then is should create new VRID
        floating IP instead of updating existing(delete + create -> update)

        :return: return the update list of VRID object, If empty the need to remove all VRID
        objects from DB else need update existing ones.
        """
        vrid_value = CONF.a10_global.vrid
        prev_vrid_value = vrid_list[0].vrid if vrid_list else None
        updated_vrid_list = copy.copy(vrid_list)
        if use_device_flavor:
            if vthunder_config.vrid_floating_ip:
                conf_floating_ip = vthunder_config.vrid_floating_ip
            else:
                conf_floating_ip = CONF.a10_global.vrid_floating_ip
        else:
            conf_floating_ip = a10_utils.get_vrid_floating_ip_for_project(
                lb_resource.project_id)

        if not conf_floating_ip:
            for vrid in updated_vrid_list:
                self._delete_vrid_port(vrid.vrid_port_id)
            vrid_value = prev_vrid_value if prev_vrid_value else vrid_value
            self._remove_device_vrid_fip(vthunder.partition_name, vrid_value)
            return []

        vrid_floating_ips = []
        update_vrid_flag = False
        existing_fips = []
        owner = vthunder.ip_address + "_" + vthunder.partition_name
        self._add_vrid_to_list(updated_vrid_list, subnet, owner)
        for vrid in updated_vrid_list:
            try:
                vrid_summary = self.axapi_client.vrrpa.get(vrid.vrid)
            except Exception as e:
                vrid_summary = {}
                LOG.exception("Failed to get existing VRID summary due to: %s",
                              str(e))

            if vrid_summary and 'floating-ip' in vrid_summary['vrid']:
                vrid_fip = vrid_summary['vrid']['floating-ip']
                if vthunder.partition_name != 'shared':
                    for i in range(len(vrid_fip['ip-address-part-cfg'])):
                        existing_fips.append(vrid_fip['ip-address-part-cfg'][i]
                                             ['ip-address-partition'])
                else:
                    for i in range(len(vrid_fip['ip-address-cfg'])):
                        existing_fips.append(
                            vrid_fip['ip-address-cfg'][i]['ip-address'])
            vrid_subnet = self.network_driver.get_subnet(vrid.subnet_id)
            vrid.vrid = vrid_value
            if conf_floating_ip.lower() == 'dhcp':
                subnet_ip, subnet_mask = a10_utils.get_net_info_from_cidr(
                    vrid_subnet.cidr)
                if not a10_utils.check_ip_in_subnet_range(
                        vrid.vrid_floating_ip, subnet_ip, subnet_mask):
                    vrid = self._replace_vrid_port(vrid, vrid_subnet,
                                                   lb_resource)
                    update_vrid_flag = True
            else:
                new_ip = a10_utils.get_patched_ip_address(
                    conf_floating_ip, vrid_subnet.cidr)
                if new_ip != vrid.vrid_floating_ip:
                    vrid = self._replace_vrid_port(vrid, vrid_subnet,
                                                   lb_resource, new_ip)
                    update_vrid_flag = True
            if vrid_subnet.id == subnet.id or vrid.vrid_floating_ip in existing_fips:
                vrid_floating_ips.append(vrid.vrid_floating_ip)

        if (prev_vrid_value is not None) and (prev_vrid_value != vrid_value):
            self._remove_device_vrid_fip(vthunder.partition_name,
                                         prev_vrid_value)
            self._update_device_vrid_fip(vthunder.partition_name,
                                         vrid_floating_ips, vrid_value)
        elif update_vrid_flag:
            self._update_device_vrid_fip(vthunder.partition_name,
                                         vrid_floating_ips, vrid_value)

        return updated_vrid_list
Exemplo n.º 4
0
    def execute(self, vthunder, lb_resource, vrid_list, subnet):
        """

        :param vthunder:
        :param lb_resource: Can accept LB or member
        :param vrid_list: VRID object list for LB resource's project.
        :param subnet: subnet of the resource in question, will be helpful if there is no
        VRID object present for the provided subnet then is should create new VRID
        floating IP instead of updating existing(delete + create -> update)
        :return: return the update list of VRID object, If empty the need to remove all VRID
        objects from DB else need update existing ones.
        """
        vrid_floating_ips = []
        update_vrid_flag = False
        vrid_value = CONF.a10_global.vrid
        conf_floating_ip = a10_utils.get_vrid_floating_ip_for_project(
            lb_resource.project_id)
        prev_vrid_value = copy.deepcopy(
            vrid_list[0].vrid) if vrid_list else None

        if conf_floating_ip:
            for vr in vrid_list:
                if vr.subnet_id == subnet.id:
                    break
            else:
                vrid_list.append(
                    data_models.VRID(vrid=vrid_value,
                                     project_id=lb_resource.project_id,
                                     vrid_port_id=None,
                                     vrid_floating_ip=None,
                                     subnet_id=subnet.id))
            if conf_floating_ip.lower() == 'dhcp':
                for vrid in vrid_list:
                    subnet = self.network_driver.get_subnet(vrid.subnet_id)
                    subnet_ip, subnet_mask = a10_utils.get_net_info_from_cidr(
                        subnet.cidr)
                    vrid.vrid = vrid_value
                    if not a10_utils.check_ip_in_subnet_range(
                            vrid.vrid_floating_ip, subnet_ip, subnet_mask):
                        try:
                            # delete existing port associated to vrid in
                            # question.
                            if vrid.vrid_port_id:
                                self.network_driver.delete_port(
                                    vrid.vrid_port_id)
                            fip_obj = self.network_driver.create_port(
                                subnet.network_id, subnet.id)
                            self.added_fip_ports.append(fip_obj)
                            vrid.vrid_floating_ip = fip_obj.fixed_ips[
                                0].ip_address
                            vrid.vrid_port_id = fip_obj.id
                            update_vrid_flag = True
                        except Exception as e:
                            LOG.error(
                                "Failed to create neutron port for lb_resource: %s",
                                lb_resource.id)
                            raise e
                    vrid_floating_ips.append(vrid.vrid_floating_ip)
            else:
                for vrid in vrid_list:
                    subnet = self.network_driver.get_subnet(vrid.subnet_id)
                    conf_floating_ip = a10_utils.get_vrid_floating_ip_for_project(
                        lb_resource.project_id)
                    conf_floating_ip = a10_utils.get_patched_ip_address(
                        conf_floating_ip, subnet.cidr)
                    vrid.vrid = vrid_value
                    if conf_floating_ip != vrid.vrid_floating_ip:
                        try:
                            # delete existing port associated to vrid in
                            # question.
                            if vrid.vrid_port_id:
                                self.network_driver.delete_port(
                                    vrid.vrid_port_id)
                            fip_obj = self.network_driver.create_port(
                                subnet.network_id,
                                subnet.id,
                                fixed_ip=conf_floating_ip)
                            self.added_fip_ports.append(fip_obj)
                            vrid.vrid_floating_ip = fip_obj.fixed_ips[
                                0].ip_address
                            vrid.vrid_port_id = fip_obj.id
                            update_vrid_flag = True
                        except Exception as e:
                            LOG.error(
                                "Failed to create neutron port for loadbalancer resource: %s with "
                                "floating IP %s", lb_resource.id,
                                conf_floating_ip)
                            raise e
                    vrid_floating_ips.append(vrid.vrid_floating_ip)
        else:
            for vrid in vrid_list:
                try:
                    self.network_driver.delete_port(vrid.vrid_port_id)
                except Exception as e:
                    LOG.error("Failed to delete neutron port for VRID FIP: %s",
                              vrid.vrid_floating_ip)
                    raise e
                update_vrid_flag = True
            vrid_list = []
        if (prev_vrid_value is not None) and (prev_vrid_value != vrid_value):
            self.update_device_vrid_fip(vthunder, [], prev_vrid_value)
            self.update_device_vrid_fip(vthunder, vrid_floating_ips,
                                        vrid_value)
        elif update_vrid_flag:
            self.update_device_vrid_fip(vthunder, vrid_floating_ips,
                                        vrid_value)
        return vrid_list