Пример #1
0
def __applyConfig(equipment,filename, equipment_access=None,source_server=None,port=22):
	'''Apply configuration file on equipment

	Args:
		equipment: networkapi.equipamento.Equipamento()
		filename: relative file path from TFTPBOOT_FILES_PATH to apply in equipment
		equipment_access: networkapi.equipamento.EquipamentoAcesso() to use
		source_server: source TFTP server address
		port: ssh tcp port

	Returns:
		equipment output

	Raises:
	'''

	if equipment.maintenance == True:
		return "Equipment is in maintenance mode. No action taken."
		
	if source_server == None:
		source_server = TFTP_SERVER_ADDR
	
	equip_plugin = PluginFactory.factory(equipment)
	equip_plugin.connect()
	equip_plugin.ensure_privilege_level()
	equip_output = equip_plugin.copyScriptFileToConfig(filename)
	equip_plugin.close()

	return equip_output
Пример #2
0
def _remove_svi(equipment, vlan_num):
    equip_plugin = PluginFactory.factory(equipment)
    equip_plugin.connect()
    output = equip_plugin.remove_svi(vlan_num)
    equip_plugin.close()

    return output
Пример #3
0
def _applyconfig(equipment, filename, equipment_access=None, source_server=None, port=22):
    """Apply configuration file on equipment

    Args:
            equipment: networkapi.equipamento.Equipamento()
            filename: relative file path from TFTPBOOT_FILES_PATH to apply in equipment
            equipment_access: networkapi.equipamento.EquipamentoAcesso() to use
            source_server: source TFTP server address
            port: ssh tcp port

    Returns:
            equipment output

    Raises:
    """

    if equipment.maintenance is True:
        return 'Equipment is in maintenance mode. No action taken.'

    if source_server is None:
        source_server = TFTP_SERVER_ADDR

    # TODO: Handle exceptions from the following methods and generate response
    # for the caller

    equip_plugin = PluginFactory.factory(equipment)
    equip_plugin.connect()
    equip_plugin.ensure_privilege_level()
    equip_output = equip_plugin.copyScriptFileToConfig(filename)
    equip_plugin.close()

    return equip_output
Пример #4
0
def deploy_neighbor_v4(neighbor_id):

    neighbor = NeighborV4.objects.get(id=neighbor_id)

    if neighbor.created:
        raise NeighborV4IsDeployed(neighbor)

    locks_list = lock_resources_used_by_neighbor_v4(neighbor)

    try:
        depl_v4 = get_created_neighbors_v4_shares_same_eqpt_and_peer(neighbor)
        depl_v6 = get_created_neighbors_v6_shares_same_eqpt_and_peer(neighbor)

        eqpt = get_v4_equipment(neighbor)

        plugin = PluginFactory.factory(eqpt)
        plugin.bgp().deploy_neighbor(neighbor)

        neighbor.deploy()

    except Exception as e:
        raise NetworkAPIException(str(e))

    finally:
        destroy_lock(locks_list)
Пример #5
0
def _remove_svi(equipment, vlan_num):
    equip_plugin = PluginFactory.factory(equipment)
    equip_plugin.connect()
    output = equip_plugin.remove_svi(vlan_num)
    equip_plugin.close()

    return output
Пример #6
0
def deploy_neighbor_v4(neighbor_id):

    neighbor = NeighborV4.objects.get(id=neighbor_id)

    if neighbor.created:
        raise NeighborV4IsDeployed(neighbor)

    locks_list = lock_resources_used_by_neighbor_v4(neighbor)

    try:
        depl_v4 = get_created_neighbors_v4_shares_same_eqpt_and_peer(neighbor)
        depl_v6 = get_created_neighbors_v6_shares_same_eqpt_and_peer(neighbor)

        eqpt = get_v4_equipment(neighbor)

        plugin = PluginFactory.factory(eqpt)
        plugin.bgp().deploy_neighbor(neighbor)

        neighbor.deploy()

    except Exception as e:
        raise NetworkAPIException(str(e))

    finally:
        destroy_lock(locks_list)
Пример #7
0
def _applyconfig(equipment, filename, equipment_access=None, source_server=None, port=22):
    """Apply configuration file on equipment

    Args:
            equipment: networkapi.equipamento.Equipamento()
            filename: relative file path from TFTPBOOT_FILES_PATH to apply in equipment
            equipment_access: networkapi.equipamento.EquipamentoAcesso() to use
            source_server: source TFTP server address
            port: ssh tcp port

    Returns:
            equipment output

    Raises:
    """

    if equipment.maintenance is True:
        return 'Equipment is in maintenance mode. No action taken.'

    if source_server is None:
        source_server = TFTP_SERVER_ADDR

    # TODO: Handle exceptions from the following methods and generate response
    # for the caller

    equip_plugin = PluginFactory.factory(equipment)
    equip_plugin.connect()
    equip_plugin.ensure_privilege_level()
    vrf = equip_plugin.equipment_access.vrf.internal_name if equip_plugin.equipment_access.vrf else None
    equip_output = equip_plugin.copyScriptFileToConfig(filename, use_vrf=vrf)
    equip_plugin.close()

    return equip_output
Пример #8
0
def undeploy_neighbor_v6(neighbor_id):

    neighbor = NeighborV6.objects.get(id=neighbor_id)

    if not neighbor.created:
        raise exceptions.NeighborV6IsUndeployed(neighbor)

    locks_list = lock_resources_used_by_neighbor_v6(neighbor)

    try:
        get_created_neighbors_v4_shares_same_eqpt_and_peer(neighbor)
        get_created_neighbors_v6_shares_same_eqpt_and_peer(neighbor)

        eqpt = get_v6_equipment(neighbor)

        plugin = PluginFactory.factory(eqpt)
        plugin.bgp().undeploy_neighbor(neighbor)

        neighbor.deploy()

    except Exception as e:
        raise api_rest_exceptions.NetworkAPIException(str(e))

    finally:
        destroy_lock(locks_list)
Пример #9
0
def undeploy_neighbor_v6(neighbor_id):

    neighbor = NeighborV6.objects.get(id=neighbor_id)

    if not neighbor.created:
        raise exceptions.NeighborV6IsUndeployed(neighbor)

    locks_list = lock_resources_used_by_neighbor_v6(neighbor)

    try:
        get_created_neighbors_v4_shares_same_eqpt_and_peer(neighbor)
        get_created_neighbors_v6_shares_same_eqpt_and_peer(neighbor)

        eqpt = get_v6_equipment(neighbor)

        plugin = PluginFactory.factory(eqpt)
        plugin.bgp().undeploy_neighbor(neighbor)

        neighbor.deploy()

    except Exception as e:
        raise api_rest_exceptions.NetworkAPIException(str(e))

    finally:
        destroy_lock(locks_list)
Пример #10
0
    def test_factory_bgp_deploy_prefix_list(self):
        equipment = self._mock_equipment()
        plugin = PluginFactory.factory(equipment)

        neighbor = {}

        self.assertEqual(
            hasattr(plugin.bgp(neighbor), 'deploy_prefix_list'), True)
Пример #11
0
    def test_factory_bgp_deploy_neighbor(self):
        equipment = self._mock_equipment()
        plugin = PluginFactory.factory(equipment)

        neighbor = {}

        self.assertEqual(hasattr(plugin.bgp(neighbor), 'deploy_neighbor'),
                         True)
Пример #12
0
    def test_factory_bgp_undeploy_prefix_list(self):
        equipment = self._mock_equipment()
        plugin = PluginFactory.factory(equipment)

        neighbor = {}

        self.assertEqual(hasattr(plugin.bgp(neighbor), 'undeploy_prefix_list'),
                         True)
Пример #13
0
    def test_factory_bgp_undeploy_neighbor(self):
        equipment = self._mock_equipment()
        plugin = PluginFactory.factory(equipment)

        neighbor = {}

        self.assertEqual(hasattr(plugin.bgp(neighbor), 'undeploy_neighbor'),
                         True)
Пример #14
0
def remove_svi(equipment, vlan_num):
    """Call function "remove_svi" of Plugin for model of equipment."""

    equip_plugin = PluginFactory.factory(equipment)
    equip_plugin.connect()
    output = equip_plugin.remove_svi(vlan_num)
    equip_plugin.close()

    return output
Пример #15
0
def remove_svi(equipment, vlan_num):
    """Call function "remove_svi" of Plugin for model of equipment."""

    equip_plugin = PluginFactory.factory(equipment)
    equip_plugin.connect()
    output = equip_plugin.remove_svi(vlan_num)
    equip_plugin.close()

    return output
Пример #16
0
def _prepare_apply_state(pools, user=None):
    load_balance = dict()
    keys = list()

    for pool in pools:
        server_pool = ServerPool.objects.get(id=pool['id'])

        server_pool_members = server_pool.serverpoolmember_set.all()

        if pool['server_pool_members']:
            equips = _validate_pool_members_to_apply(pool, user)

            keys.append(sorted([str(eqpt.id) for eqpt in equips]))

            for e in equips:
                eqpt_id = str(e.id)
                equipment_access = EquipamentoAcesso.search(equipamento=e.id)

                plugin = PluginFactory.factory(e)

                if not load_balance.get(eqpt_id):

                    load_balance[eqpt_id] = {
                        'plugin': plugin,
                        'access': equipment_access,
                        'pools': [],
                    }

                mbs = pool['server_pool_members']
                idx_mbs = [mb['id'] for mb in mbs]

                load_balance[eqpt_id]['pools'].append({
                    'id':
                    server_pool.id,
                    'nome':
                    server_pool.identifier,
                    'pools_members': [{
                        'id':
                        pool_member.id,
                        'ip':
                        pool_member.ip.ip_formated
                        if pool_member.ip else pool_member.ipv6.ip_formated,
                        'port':
                        pool_member.port_real,
                        'member_status':
                        mbs[idx_mbs.index(pool_member.id)]['member_status']
                    } for pool_member in server_pool_members
                                      if pool_member.id in idx_mbs]
                })
    # pools are in differents load balancers
    keys = [','.join(key) for key in keys]
    if len(list(set(keys))) > 1:
        raise Exception('Pools are in differents load balancers')

    return load_balance
Пример #17
0
def flush_flows(env_id):
    """ Flushes flow from a environment without restore it """
    eqpts = get_controller_by_envid(env_id)
    for eqpt in eqpts:
        plugin = PluginFactory.factory(eqpt, env_id=env_id)
        try:
            plugin.flush_flows()
        except Exception as e:
            log.error(e)
            raise NetworkAPIException('Failed to flush Controller '
                                      'plugin. %s' % e)
Пример #18
0
def flush_flows(env_id):
    """ Flushes flow from a environment without restore it """
    eqpts = get_controller_by_envid(env_id)
    for eqpt in eqpts:
        plugin = PluginFactory.factory(eqpt, env_id=env_id)
        try:
            plugin.flush_flows()
        except Exception as e:
            log.error(e)
            raise NetworkAPIException('Failed to flush Controller '
                                      'plugin. %s' % e)
Пример #19
0
def insert_flow(env_id, data, user_id):
    eqpts = get_controller_by_envid(env_id)
    plugins = []
    for eqpt in eqpts:
        plugins.append(PluginFactory.factory(eqpt, env_id=env_id))
    try:
        return async_add_flow.apply_async(args=[plugins, user_id, data],
                                          queue='napi.odl_flow')
    except Exception as e:
        log.error(e)
        raise NetworkAPIException('Failed to insert flow(s) ' 'plugin. %s' % e)
Пример #20
0
def delete_flow(env_id, flow_id):
    eqpts = get_controller_by_envid(env_id)

    for eqpt in eqpts:
        plugin = PluginFactory.factory(eqpt, env_id=env_id)
        try:
            plugin.del_flow(flow_id=flow_id)
        except Exception as e:
            log.error(e)
            raise NetworkAPIException('Failed to delete flow '
                                      'plugin. %s' % e)
Пример #21
0
def _prepare_apply_state(pools, user=None):
    load_balance = dict()
    keys = list()

    for pool in pools:
        server_pool = ServerPool.objects.get(id=pool['id'])

        server_pool_members = server_pool.serverpoolmember_set.all()

        if pool['server_pool_members']:
            equips = _validate_pool_members_to_apply(pool, user)

            keys.append(sorted([str(eqpt.id) for eqpt in equips]))

            for e in equips:
                eqpt_id = str(e.id)
                equipment_access = EquipamentoAcesso.search(
                    equipamento=e.id
                )

                plugin = PluginFactory.factory(e)

                if not load_balance.get(eqpt_id):

                    load_balance[eqpt_id] = {
                        'plugin': plugin,
                        'access': equipment_access,
                        'pools': [],
                    }

                mbs = pool['server_pool_members']
                idx_mbs = [mb['id'] for mb in mbs]

                load_balance[eqpt_id]['pools'].append({
                    'id': server_pool.id,
                    'nome': server_pool.identifier,
                    'pools_members': [{
                        'id': pool_member.id,
                        'ip': pool_member.ip.ip_formated
                        if pool_member.ip else pool_member.ipv6.ip_formated,
                        'port': pool_member.port_real,
                        'member_status': mbs
                        [idx_mbs.index(pool_member.id)]['member_status']
                    } for pool_member in server_pool_members
                        if pool_member.id in idx_mbs]
                })
    # pools are in differents load balancers
    keys = [','.join(key) for key in keys]
    if len(list(set(keys))) > 1:
        raise Exception('Pools are in differents load balancers')

    return load_balance
    def setUp(self):
        # Must chose the equipment with the right version running on docker
        # Beryllium
        self.equipment = Equipamento.objects.filter(id=10).uniqueResult()
        # Nitrogen
        #self.equipment = Equipamento.objects.filter(id=11).uniqueResult()

        self.equipment_access = EquipamentoAcesso.objects.filter(id=1).uniqueResult()
        self.utils.set_controller_endpoint(self.equipment_access)

        self.odl = PluginFactory.factory(self.equipment)

        self.flow_key = "flow-node-inventory:flow"
Пример #23
0
def insert_flow(env_id, data, user_id):
    eqpts = get_controller_by_envid(env_id)
    plugins = []
    for eqpt in eqpts:
        plugins.append(PluginFactory.factory(eqpt, env_id=env_id))
    try:
        return async_add_flow.apply_async(
            args=[plugins, user_id, data], queue='napi.odl_flow'
        )
    except Exception as e:
        log.error(e)
        raise NetworkAPIException('Failed to insert flow(s) '
                                  'plugin. %s' % e)
    def setUp(self):
        # Must chose the equipment with the right version running on docker
        # Beryllium
        self.equipment = Equipamento.objects.filter(id=10).uniqueResult()
        # Nitrogen
        #self.equipment = Equipamento.objects.filter(id=11).uniqueResult()

        self.equipment_access = EquipamentoAcesso.objects.filter(
            id=1).uniqueResult()
        self.utils.set_controller_endpoint(self.equipment_access)

        self.odl = PluginFactory.factory(self.equipment)

        self.flow_key = "flow-node-inventory:flow"
Пример #25
0
def update_flows(env_id, data, user_id):
    """ Call equipment plugin to asynchronously flush the environment """
    eqpts = get_controller_by_envid(env_id)
    plugins = []
    for eqpt in eqpts:
        plugins.append(PluginFactory.factory(eqpt, env_id=env_id))

    try:
        return async_flush_environment.apply_async(
            args=[plugins, user_id, data], queue='napi.odl_flow')
    except Exception as e:
        log.error(e)
        raise NetworkAPIException('Failed to flush flow(s) '
                                  'from environment: %s \n%s' % (env_id, e))
Пример #26
0
def update_flows(env_id, data, user_id):
    """ Call equipment plugin to asynchronously flush the environment """
    eqpts = get_controller_by_envid(env_id)
    plugins = []
    for eqpt in eqpts:
        plugins.append(PluginFactory.factory(eqpt, env_id=env_id))

    try:
        return async_flush_environment.apply_async(
            args=[plugins, user_id, data], queue='napi.odl_flow'
        )
    except Exception as e:
        log.error(e)
        raise NetworkAPIException('Failed to flush flow(s) '
                                  'from environment: %s \n%s' % (env_id, e))
Пример #27
0
def delete_flow(env_id, flow_id, user_id):
    """ Deletes one flow by id using the async task """

    eqpts = get_controller_by_envid(env_id)

    plugins = []
    for eqpt in eqpts:
        plugins.append(PluginFactory.factory(eqpt, env_id=env_id))

    try:
        return async_delete_flow.apply_async(args=[plugins, user_id, flow_id],
                                             queue='napi.odl_flow')
    except Exception as err:
        log.error(err)
        raise NetworkAPIException('Failed to delete flow with error: %s' % err)
Пример #28
0
def delete_flow(env_id, flow_id, user_id):
    """ Deletes one flow by id using the async task """

    eqpts = get_controller_by_envid(env_id)

    plugins = []
    for eqpt in eqpts:
        plugins.append(PluginFactory.factory(eqpt, env_id=env_id))

    try:
        return async_delete_flow.apply_async(
            args=[plugins, user_id, flow_id], queue='napi.odl_flow'
        )
    except Exception as err:
        log.error(err)
        raise NetworkAPIException('Failed to delete flow with error: %s' % err)
Пример #29
0
    def create_v3(self, vip_port_map):
        # save port

        facade_eqpt = get_app('api_equipment', 'facade')

        self.vip_request = VipRequest.get_by_pk(
            vip_port_map.get('vip_request'))
        self.port = vip_port_map.get('port')

        eqpts = facade_eqpt.get_eqpt_by_envvip(
            self.vip_request.environmentvip.id)
        if eqpts:
            plugin = PluginFactory.factory(eqpts[0])
            identifier = plugin.get_name_eqpt(
                self.vip_request, vip_port_map['port'])
            self.identifier = identifier

        self.save()

        # L4 Protocol
        opt = VipRequestPortOptionVip()
        opt_map = {
            'vip_request_port': self.id,
            'optionvip': vip_port_map['options']['l4_protocol']
        }
        opt.create_v3(opt_map)

        # L7 Protocol
        opt = VipRequestPortOptionVip()
        opt_map = {
            'vip_request_port': self.id,
            'optionvip': vip_port_map['options']['l7_protocol']
        }
        opt.create_v3(opt_map)

        # Pools
        for pool in vip_port_map.get('pools'):
            pool_map = {
                'vip_request_port': self.id,
                'server_pool': pool.get('server_pool'),
                'optionvip': pool.get('l7_rule'),
                'val_optionvip': pool.get('l7_value'),
                'order': pool.get('order')
            }
            pl = VipRequestPortPool()
            pl.create_v3(pool_map)
Пример #30
0
    def create_v3(self, vip_port_map):
        # save port

        facade_eqpt = get_app('api_equipment', 'facade')

        self.vip_request = VipRequest.get_by_pk(
            vip_port_map.get('vip_request'))
        self.port = vip_port_map.get('port')

        eqpts = facade_eqpt.get_eqpt_by_envvip(
            self.vip_request.environmentvip.id)
        if eqpts:
            plugin = PluginFactory.factory(eqpts[0])
            identifier = plugin.get_name_eqpt(
                self.vip_request, vip_port_map['port'])
            self.identifier = identifier

        self.save()

        # L4 Protocol
        opt = VipRequestPortOptionVip()
        opt_map = {
            'vip_request_port': self.id,
            'optionvip': vip_port_map['options']['l4_protocol']
        }
        opt.create_v3(opt_map)

        # L7 Protocol
        opt = VipRequestPortOptionVip()
        opt_map = {
            'vip_request_port': self.id,
            'optionvip': vip_port_map['options']['l7_protocol']
        }
        opt.create_v3(opt_map)

        # Pools
        for pool in vip_port_map.get('pools'):
            pool_map = {
                'vip_request_port': self.id,
                'server_pool': pool.get('server_pool'),
                'optionvip': pool.get('l7_rule'),
                'val_optionvip': pool.get('l7_value'),
                'order': pool.get('order')
            }
            pl = VipRequestPortPool()
            pl.create_v3(pool_map)
Пример #31
0
def list_flows_by_envid(env_id, flow_id=0):
    eqpts = get_controller_by_envid(env_id)
    flows_list = {}

    for eqpt in eqpts:
        plugin = PluginFactory.factory(eqpt, env_id=env_id)
        try:
            if flow_id > 0:
                flows_list.update(plugin.get_flow(flow_id=flow_id))
            else:
                flows_list.update(plugin.get_flows())

        except Exception as e:
            log.error(e)
            raise NetworkAPIException('Failed to list flow(s)'
                                      'plugin. %s' % e)

    return flows_list
Пример #32
0
def list_flows_by_envid(env_id, flow_id=0):
    eqpts = get_controller_by_envid(env_id)
    flows_list = {}

    for eqpt in eqpts:
        plugin = PluginFactory.factory(eqpt, env_id=env_id)
        try:
            if flow_id > 0:
                flows_list.update(plugin.get_flow(flow_id=flow_id))
            else:
                flows_list.update(plugin.get_flows())

        except Exception as e:
            log.error(e)
            raise NetworkAPIException('Failed to list flow(s)'
                                      'plugin. %s' % e)

    return flows_list
Пример #33
0
def _create_port(ports, vip_request):
    """Create ports"""

    for port in ports:
        # save port
        pt = models.VipRequestPort()
        pt.vip_request_id = vip_request.id
        pt.port = port['port']

        eqpts = facade_eqpt.get_eqpt_by_envvip(vip_request.environmentvip.id)
        if eqpts:
            plugin = PluginFactory.factory(eqpts[0])
            identifier = plugin.get_name_eqpt(vip_request, port['port'])
            pt.identifier = identifier

        pt.save()

        # save port option l7_protocol
        opt = models.VipRequestPortOptionVip()
        opt.vip_request_port_id = pt.id
        opt.optionvip_id = port['options']['l4_protocol']
        opt.save()

        # save port option l7_protocol
        opt = models.VipRequestPortOptionVip()
        opt.vip_request_port_id = pt.id
        opt.optionvip_id = port['options']['l7_protocol']
        opt.save()

        # save pool by port
        for pool in port.get('pools'):
            pl = models.VipRequestPortPool()
            pl.vip_request_port_id = pt.id
            pl.server_pool_id = pool.get('server_pool')
            pl.optionvip_id = pool.get('l7_rule')
            pl.val_optionvip = pool.get('l7_value')
            pl.order = pool.get('order')
            pl.save()
Пример #34
0
def undeploy_neighbors(neighbors):

    undeployed_ids = list()
    for neighbor in neighbors:
        id_ = neighbor['id']

        if neighbor['created'] is False:
            raise NeighborNotCreated(id_)

        remote_ip = neighbor['remote_ip']
        try:

            equipment = get_equipment(id_, remote_ip)

            plugin = PluginFactory.factory(equipment)

            asn = As.objects.get(asequipment__equipment=equipment.id)
            vrf = Vrf.objects.get(
                virtualinterface__id=neighbor['virtual_interface'])
            virtual_interface = VirtualInterface.get_by_pk(
                neighbor['virtual_interface'])

            asn = AsV4Serializer(asn).data
            vrf = VrfV3Serializer(vrf).data
            virtual_interface = VirtualInterfaceV4Serializer(
                virtual_interface).data

            plugin.bgp(neighbor, virtual_interface, asn, vrf).\
                undeploy_neighbor()

        except Exception as e:
            raise NetworkAPIException(e.message)

        undeployed_ids.append(id_)

    neighbors_obj = Neighbor.objects.filter(id__in=undeployed_ids)
    neighbors_obj.update(created=False)
Пример #35
0
def prepare_apply(load_balance, vip, created=True, user=None):

    vip_request = copy.deepcopy(vip)

    id_vip = str(vip_request.get('id'))

    equips, conf, cluster_unit = _validate_vip_to_apply(
        vip_request, created, user)

    cache_group = OptionVip.objects.get(
        id=vip_request.get('options').get('cache_group'))
    traffic_return = OptionVip.objects.get(
        id=vip_request.get('options').get('traffic_return'))
    timeout = OptionVip.objects.get(
        id=vip_request.get('options').get('timeout'))
    persistence = OptionVip.objects.get(
        id=vip_request.get('options').get('persistence'))

    if vip_request['ipv4']:
        ipv4 = Ip.get_by_pk(vip_request['ipv4']) if vip_request[
            'ipv4'] else None
        vip_request['ipv4'] = {
            'id': ipv4.id,
            'ip_formated': ipv4.ip_formated
        }

    if vip_request['ipv6']:
        ipv6 = Ipv6.get_by_pk(vip_request['ipv6']) if vip_request[
            'ipv6'] else None
        vip_request['ipv6'] = {
            'id': ipv6.id,
            'ip_formated': ipv6.ip_formated
        }

    if conf:
        conf = json.loads(conf)

    vip_request['options'] = dict()
    vip_request['options']['cache_group'] = {
        'id': cache_group.id,
        'nome_opcao_txt': cache_group.nome_opcao_txt
    }
    vip_request['options']['traffic_return'] = {
        'id': traffic_return.id,
        'nome_opcao_txt': traffic_return.nome_opcao_txt
    }
    vip_request['options']['timeout'] = {
        'id': timeout.id,
        'nome_opcao_txt': timeout.nome_opcao_txt
    }
    vip_request['options']['persistence'] = {
        'id': persistence.id,
        'nome_opcao_txt': persistence.nome_opcao_txt
    }
    vip_request['options']['cluster_unit'] = cluster_unit

    try:
        vip_request['options']['dscp'] = VipRequestDSCP.objects.get(
            vip_request=vip_request['id']
        ).dscp
    except:
        vip_request['options']['dscp'] = None
        pass

    for idx, port in enumerate(vip_request['ports']):
        for i, pl in enumerate(port['pools']):

            pool = get_pool_by_id(pl['server_pool'])
            pool_serializer = pool_slz.PoolV3Serializer(pool)

            l7_rule = OptionVip.objects.get(
                id=pl['l7_rule']).nome_opcao_txt

            healthcheck = pool_serializer.data['healthcheck']
            healthcheck['identifier'] = reserve_name_healthcheck(
                pool_serializer.data['identifier'])
            healthcheck['new'] = True
            vip_request['ports'][idx]['pools'][i]['server_pool'] = {
                'id': pool_serializer.data['id'],
                'nome': pool_serializer.data['identifier'],
                'lb_method': pool_serializer.data['lb_method'],
                'healthcheck': healthcheck,
                'action': pool_serializer.data['servicedownaction']['name'],
                'pool_created': pool_serializer.data['pool_created'],
                'pools_members': [{
                    'id': pool_member['id'],
                    'identifier': pool_member['identifier'],
                    'ip': pool_member['ip']['ip_formated'] if pool_member['ip'] else pool_member['ipv6']['ip_formated'],
                    'port': pool_member['port_real'],
                    'member_status': pool_member['member_status'],
                    'limit': pool_member['limit'],
                    'priority': pool_member['priority'],
                    'weight': pool_member['weight']
                } for pool_member in pool_serializer.data['server_pool_members']]
            }

            vip_request['ports'][idx]['pools'][i]['l7_rule'] = l7_rule
        l7_protocol = OptionVip.objects.get(
            id=port['options']['l7_protocol'])
        l4_protocol = OptionVip.objects.get(
            id=port['options']['l4_protocol'])

        vip_request['ports'][idx]['options'] = dict()
        vip_request['ports'][idx]['options']['l7_protocol'] = {
            'id': l7_protocol.id,
            'nome_opcao_txt': l7_protocol.nome_opcao_txt
        }
        vip_request['ports'][idx]['options']['l4_protocol'] = {
            'id': l4_protocol.id,
            'nome_opcao_txt': l4_protocol.nome_opcao_txt
        }

    vip_request['conf'] = conf

    if conf:
        for idx, layer in enumerate(conf['conf']['layers']):
            requiments = layer.get('requiments')
            if requiments:
                # validate for port
                for idx_port, port in enumerate(vip['ports']):
                    for requiment in requiments:
                        condicionals = requiment.get('condicionals')
                        for condicional in condicionals:

                            validated = True

                            validations = condicional.get('validations')
                            for validation in validations:
                                if validation.get('type') == 'optionvip':
                                    validated &= valid_expression(
                                        validation.get('operator'),
                                        int(vip['options'][
                                            validation.get('variable')]),
                                        int(validation.get('value'))
                                    )

                                if validation.get('type') == 'portoptionvip':
                                    validated &= valid_expression(
                                        validation.get('operator'),
                                        int(port['options'][
                                            validation.get('variable')]),
                                        int(validation.get('value'))
                                    )

                                if validation.get('type') == 'field' and validation.get('variable') == 'cluster_unit':
                                    validated &= valid_expression(
                                        validation.get('operator'),
                                        cluster_unit,
                                        validation.get('value')
                                    )
                            if validated:
                                use = condicional.get('use')
                                for item in use:
                                    definitions = item.get('definitions')
                                    eqpts = item.get('eqpts')
                                    if eqpts:

                                        eqpts = Equipamento.objects.filter(
                                            id__in=eqpts,
                                            maintenance=0,
                                            tipo_equipamento__tipo_equipamento=u'Balanceador').distinct()

                                        if facade_eqpt.all_equipments_are_in_maintenance(equips):
                                            raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException()

                                        if user:
                                            if not facade_eqpt.all_equipments_can_update_config(equips, user):
                                                raise exceptions_eqpt.UserDoesNotHavePermInAllEqptException(
                                                    'User does not have permission to update conf in eqpt. \
                                                    Verify the permissions of user group with equipment group. Vip:{}'.format(
                                                        vip_request['id']))

                                        for eqpt in eqpts:
                                            eqpt_id = str(eqpt.id)

                                            if not load_balance.get(eqpt_id):
                                                equipment_access = EquipamentoAcesso.search(
                                                    equipamento=eqpt.id
                                                )

                                                plugin = PluginFactory.factory(
                                                    eqpt)

                                                load_balance[eqpt_id] = {
                                                    'plugin': plugin,
                                                    'access': equipment_access,
                                                    'vips': [],
                                                    'layers': {},
                                                }

                                            idx_layer = str(idx)
                                            idx_port_str = str(port['port'])
                                            if not load_balance[eqpt_id]['layers'].get(id_vip):
                                                load_balance[eqpt_id][
                                                    'layers'][id_vip] = dict()

                                            if load_balance[eqpt_id]['layers'][id_vip].get(idx_layer):
                                                if load_balance[eqpt_id]['layers'][id_vip].get(idx_layer).get('definitions').get(idx_port_str):
                                                    load_balance[eqpt_id]['layers'][id_vip][idx_layer][
                                                        'definitions'][idx_port_str] += definitions
                                                else:
                                                    load_balance[eqpt_id]['layers'][id_vip][idx_layer][
                                                        'definitions'][idx_port_str] = definitions
                                            else:
                                                load_balance[eqpt_id]['layers'][id_vip][idx_layer] = {
                                                    'vip_request': vip_request,
                                                    'definitions': {
                                                        idx_port_str: definitions
                                                    }
                                                }
                                # In first validated==True stops conditionals.
                                # Removing this break will add a wrong
                                # conditional.
                                break

    for e in equips:
        eqpt_id = str(e.id)

        if not load_balance.get(eqpt_id):

            equipment_access = EquipamentoAcesso.search(
                equipamento=e.id
            )

            plugin = PluginFactory.factory(e)

            load_balance[eqpt_id] = {
                'plugin': plugin,
                'access': equipment_access,
                'vips': [],
                'layers': {},
            }

        load_balance[eqpt_id]['vips'].append({'vip_request': vip_request})

    return load_balance
Пример #36
0
def _update_port(ports, vip_request):
    """Update ports"""

    for port in ports:
        # save port
        try:
            pt = models.VipRequestPort.objects.get(
                vip_request_id=vip_request.id, port=port['port'])
        except:
            pt = models.VipRequestPort()
            pt.vip_request_id = vip_request.id
            pt.port = port['port']
            pt.save()

        if not pt.identifier or pt.port != port['port']:
            eqpts = facade_eqpt.get_eqpt_by_envvip(
                vip_request.environmentvip.id)
            if eqpts:
                plugin = PluginFactory.factory(eqpts[0])
                identifier = plugin.get_name_eqpt(vip_request, port['port'])
                pt.identifier = identifier

        pt.save()

        # save port option l4_protocol
        try:
            opt = models.VipRequestPortOptionVip.objects.get(
                vip_request_port_id=pt.id,
                optionvip_id=port['options']['l4_protocol'])
        except:
            opt = models.VipRequestPortOptionVip()
            opt.vip_request_port_id = pt.id
            opt.optionvip_id = port['options']['l4_protocol']
            opt.save()

        # save port option l7_protocol
        try:
            opt = models.VipRequestPortOptionVip.objects.get(
                vip_request_port_id=pt.id,
                optionvip_id=port.get('options').get('l7_protocol'))
        except:
            opt = models.VipRequestPortOptionVip()
            opt.vip_request_port_id = pt.id
            opt.optionvip_id = port.get('options').get('l7_protocol')
            opt.save()

        # delete option by port
        models.VipRequestPortOptionVip.objects.filter(
            vip_request_port_id=pt.id).exclude(optionvip_id__in=[
                port.get('options').get('l4_protocol'),
                port.get('options').get('l7_protocol')
            ]).delete()

        # save pool by port
        pools = list()
        for pool in port.get('pools'):
            try:
                pl = models.VipRequestPortPool.objects.get(
                    vip_request_port=pt.id, id=pool.get('id'))
            except:
                pl = models.VipRequestPortPool()
                pl.vip_request_port_id = pt.id
            finally:
                if pl.optionvip_id != pool.get('l7_rule') or \
                        pl.val_optionvip != pool.get('l7_value') or \
                        pl.order != pool.get('order') or \
                        pl.server_pool_id != pool.get('server_pool'):
                    pl.server_pool_id = pool.get('server_pool')
                    pl.optionvip_id = pool.get('l7_rule')
                    pl.val_optionvip = pool.get('l7_value')
                    pl.order = pool.get('order')
                    pl.save()
            pools.append(pl.id)

        # delete pool by port
        models.VipRequestPortPool.objects.filter(vip_request_port=pt).exclude(
            id__in=pools).delete()

    # delete port
    ports_ids = [port.get('port') for port in ports]
    models.VipRequestPort.objects.filter(
        vip_request_id=vip_request.id).exclude(port__in=ports_ids).delete()
Пример #37
0
def old_to_new(vip_request):

    from networkapi.ambiente.models import EnvironmentVip
    from networkapi.api_vip_request.models import VipRequest, VipRequestDSCP, VipRequestOptionVip, \
        VipRequestPort, VipRequestPortOptionVip, VipRequestPortPool
    from networkapi.requisicaovips.models import DsrL3_to_Vip, OptionVip, OptionVipEnvironmentVip, \
        VipPortToPool

    mp = vip_request.variables_to_map()
    try:

        try:
            ev = EnvironmentVip().get_by_values(
                mp['finalidade'], mp['cliente'], mp['ambiente'])
        except:
            ev = EnvironmentVip()
            ev.finalidade_txt = mp['finalidade']
            ev.cliente_txt = mp['cliente']
            ev.ambiente_p44_txt = mp['ambiente']
            ev.description = '%s - %s - %s' % (
                mp['finalidade'], mp['cliente'], mp['ambiente'])
            ev.save()
        finally:

            vp = VipRequest()
            vp.environmentvip = ev
            vp.id = vip_request.id
            vp.name = mp['host'] if mp.get('host') else None
            vp.business = mp['areanegocio'] if mp.get(
                'areanegocio') else vp.name
            vp.service = mp['nome_servico'] if mp.get(
                'nome_servico') else vp.name
            vp.ipv4 = vip_request.ip if vip_request.ip else None
            vp.ipv6 = vip_request.ipv6 if vip_request.ipv6 else None
            vp.created = vip_request.vip_criado
            vp.save()

            if mp.get('persistencia'):
                persistencia = mp['persistencia']
            else:
                persistencia = '(nenhum)'

            try:
                op_per = OptionVip.objects.filter(
                    tipo_opcao=u'Persistencia',
                    nome_opcao_txt=persistencia)[0]
                try:
                    opv = OptionVipEnvironmentVip.objects.get(
                        option=op_per,
                        environment=ev)
                except:
                    opv = OptionVipEnvironmentVip()
                    opv.option = op_per
                    opv.environment = ev
                    opv.save()
            except:
                op_per = OptionVip()
                op_per.tipo_opcao = u'Persistencia'
                op_per.nome_opcao_txt = persistencia
                op_per.save()
                opv = OptionVipEnvironmentVip()
                opv.option = op_per
                opv.environment = ev
                opv.save()
            finally:
                try:
                    vro = VipRequestOptionVip.objects.get(
                        optionvip=op_per,
                        vip_request=vp)
                except:
                    vro = VipRequestOptionVip()
                    vro.optionvip = op_per
                    vro.vip_request = vp
                    vro.save()

            if mp.get('timeout'):
                timeout = mp['timeout']
            else:
                timeout = '5'

            try:
                op_time = OptionVip.objects.filter(
                    tipo_opcao=u'timeout',
                    nome_opcao_txt=timeout)[0]
                try:
                    opv = OptionVipEnvironmentVip.objects.get(
                        option=op_time,
                        environment=ev)
                except:
                    opv = OptionVipEnvironmentVip()
                    opv.option = op_time
                    opv.environment = ev
                    opv.save()
            except:
                op_time = OptionVip()
                op_time.tipo_opcao = u'timeout'
                op_time.nome_opcao_txt = timeout
                op_time.save()
                opv = OptionVipEnvironmentVip()
                opv.option = op_time
                opv.environment = ev
                opv.save()
            finally:
                try:
                    vro = VipRequestOptionVip.objects.get(
                        optionvip=op_time,
                        vip_request=vp)
                except:
                    vro = VipRequestOptionVip()
                    vro.optionvip = op_time
                    vro.vip_request = vp
                    vro.save()

            if mp.get('trafficreturn'):
                trafficreturn = mp['trafficreturn']
            else:
                trafficreturn = 12

            try:
                op_traffic = OptionVip.objects.filter(
                    tipo_opcao=u'Retorno de trafego',
                    id=trafficreturn)[0]
                try:
                    opv = OptionVipEnvironmentVip.objects.get(
                        option=op_traffic,
                        environment=ev)
                except:
                    opv = OptionVipEnvironmentVip()
                    opv.option = op_traffic
                    opv.environment = ev
                    opv.save()
            except:
                op_traffic = OptionVip()
                op_traffic.tipo_opcao = u'Retorno de trafego'
                op_traffic.nome_opcao_txt = trafficreturn
                op_traffic.save()
                opv = OptionVipEnvironmentVip()
                opv.option = op_traffic
                opv.environment = ev
                opv.save()
            finally:
                try:
                    vro = VipRequestOptionVip.objects.get(
                        optionvip=op_traffic,
                        vip_request=vp)
                except:
                    vro = VipRequestOptionVip()
                    vro.optionvip = op_traffic
                    vro.vip_request = vp
                    vro.save()

            if int(trafficreturn) == 48:
                dsrl3 = DsrL3_to_Vip.get_by_vip_id(vip_request.id)

                try:
                    vp_dsrl3 = VipRequestDSCP.objects.get(vip_request=vp)
                except:
                    vp_dsrl3 = VipRequestDSCP()
                    vp_dsrl3.vip_request = vp

                vp_dsrl3.dscp = dsrl3.id_dsrl3
                vp_dsrl3.save()
            else:
                try:
                    vp_dsrl3 = VipRequestDSCP.objects.get(vip_request=vp)
                    vp_dsrl3.delete()
                except:
                    pass

            if mp.get('cache'):
                cache = mp['cache']
            else:
                cache = '(nenhum)'

            try:
                op_cache = OptionVip.objects.filter(
                    tipo_opcao=u'cache',
                    nome_opcao_txt=cache)[0]
                try:
                    opv = OptionVipEnvironmentVip.objects.get(
                        option=op_cache,
                        environment=ev)
                except:
                    opv = OptionVipEnvironmentVip()
                    opv.option = op_cache
                    opv.environment = ev
                    opv.save()
            except:
                op_cache = OptionVip()
                op_cache.tipo_opcao = u'cache'
                op_cache.nome_opcao_txt = cache
                op_cache.save()
                opv = OptionVipEnvironmentVip()
                opv.option = op_cache
                opv.environment = ev
                opv.save()
            finally:

                try:
                    vro = VipRequestOptionVip.objects.get(
                        optionvip=op_cache,
                        vip_request=vp)
                except:
                    vro = VipRequestOptionVip()
                    vro.optionvip = op_cache
                    vro.vip_request = vp
                    vro.save()

            # delete optionvip(op_cache, op_traffic, op_time, op_per)
            # in port removed in old table
            VipRequestOptionVip.objects.filter(
                vip_request=vp
            ).exclude(
                optionvip__id__in=[op_cache.id, op_traffic.id,
                                   op_time.id, op_per.id]
            ).delete()

            pools = VipPortToPool.get_by_vip_id(vip_request.id)
            # delete old ports
            ports_current = VipRequestPort.objects.filter(vip_request=vp)
            ports_ids = [port.id for port in ports_current]
            pools_ids = [pl.id for pl in pools]
            ids_to_del = list(set(ports_ids) - set(pools_ids))
            ports_current.filter(id__in=ids_to_del).delete()

            # delete pools removed in old table
            # ptid = [ptid.server_pool.id for ptid in pools]
            # VipRequestPortPool.objects.filter(
            #     vip_request_port=vrp
            # ).exclude(
            #     server_pool__id__in=ptid
            # ).delete()

            for pool in pools:

                # saving ports of vip

                vrp = VipRequestPort()
                vrp.id = pool.id
                vrp.vip_request = vp
                vrp.port = pool.port_vip

                if not pool.identifier:
                    from networkapi.api_equipment import facade as facade_eqpt
                    from networkapi.plugins.factory import PluginFactory

                    eqpts = facade_eqpt.get_eqpt_by_envvip(ev.id)
                    if eqpts:
                        plugin = PluginFactory.factory(eqpts[0])
                        vrp.identifier = plugin.get_name_eqpt(
                            vp, pool.port_vip)
                        pool.identifier = vrp.identifier
                        pool.save()

                vrp.save()

                # descobre protocolo l7 e l4
                tipo_opcao = 'l7_rule'
                nome_opcao_txt = 'default_vip'
                l4_protocol = 'TCP'
                l7_protocol = 'Outros'
                if mp.get('healthcheck_type') == 'HTTP':
                    tipo_opcao = 'l7_rule'
                    nome_opcao_txt = 'default_vip'
                    l4_protocol = 'TCP'
                    l7_protocol = 'HTTP'

                if mp.get('healthcheck_type') == 'TCP':
                    l4_protocol = 'TCP'
                    l7_protocol = 'Outros'

                if mp.get('healthcheck_type') == 'UDP':
                    l4_protocol = 'UDP'
                    l7_protocol = 'Outros'

                if pool.port_vip == 20 or pool.port_vip == 21:
                    l4_protocol = 'TCP'
                    l7_protocol = 'FTP'

                if pool.port_vip == 443:
                    l4_protocol = 'TCP'
                    l7_protocol = 'HTTPS'

                # l4_protocol
                try:
                    op_l4 = OptionVip.objects.filter(
                        tipo_opcao='l4_protocol', nome_opcao_txt=l4_protocol)[0]
                    try:
                        opv = OptionVipEnvironmentVip.objects.get(
                            option=op_l4, environment=ev)
                    except:
                        opv = OptionVipEnvironmentVip()
                        opv.option = op_l4
                        opv.environment = ev
                        opv.save()
                except:
                    op_l4 = OptionVip()
                    op_l4.tipo_opcao = u'l4_protocol'
                    op_l4.nome_opcao_txt = l4_protocol
                    op_l4.save()
                    opv = OptionVipEnvironmentVip()
                    opv.option = op_l4
                    opv.environment = ev
                    opv.save()
                finally:
                    try:
                        vro = VipRequestPortOptionVip.objects.filter(
                            optionvip=op_l4, vip_request_port=vrp)[0]
                    except:
                        vro = VipRequestPortOptionVip()
                        vro.optionvip = op_l4
                        vro.vip_request_port = vrp
                        vro.save()

                # l7_protocol
                try:
                    op_l7 = OptionVip.objects.filter(
                        tipo_opcao='l7_protocol', nome_opcao_txt=l7_protocol)[0]
                    try:
                        opv = OptionVipEnvironmentVip.objects.get(
                            option=op_l7, environment=ev)
                    except:
                        opv = OptionVipEnvironmentVip()
                        opv.option = op_l7
                        opv.environment = ev
                        opv.save()
                except:
                    op_l7 = OptionVip()
                    op_l7.tipo_opcao = u'l7_protocol'
                    op_l7.nome_opcao_txt = l7_protocol
                    op_l7.save()
                    opv = OptionVipEnvironmentVip()
                    opv.option = op_l7
                    opv.environment = ev
                    opv.save()
                finally:
                    try:
                        vro = VipRequestPortOptionVip.objects.filter(
                            optionvip=op_l7, vip_request_port=vrp)[0]
                    except:
                        vro = VipRequestPortOptionVip()
                        vro.optionvip = op_l7
                        vro.vip_request_port = vrp
                        vro.save()

                # delete optionvip(l7 and l4) in port removed in old table
                VipRequestPortOptionVip.objects.filter(
                    vip_request_port=vrp
                ).exclude(
                    optionvip__id__in=[op_l7.id, op_l4.id]
                ).delete()

                # saving pools of port
                try:
                    op_pt = OptionVip.objects.filter(
                        tipo_opcao=tipo_opcao,
                        nome_opcao_txt=nome_opcao_txt)[0]
                    try:
                        opv = OptionVipEnvironmentVip.objects.get(
                            option=op_pt,
                            environment=ev)
                    except:
                        opv = OptionVipEnvironmentVip()
                        opv.option = op_pt
                        opv.environment = ev
                        opv.save()
                except:
                    op_pt = OptionVip()
                    op_pt.tipo_opcao = tipo_opcao
                    op_pt.nome_opcao_txt = nome_opcao_txt
                    op_pt.save()
                    opv = OptionVipEnvironmentVip()
                    opv.option = op_pt
                    opv.environment = ev
                    opv.save()
                finally:
                    try:
                        vrpp = VipRequestPortPool.objects.filter(
                            server_pool=pool.server_pool, vip_request_port=vrp)[0]
                    except:
                        vrpp = VipRequestPortPool()
                        vrpp.server_pool = pool.server_pool
                        vrpp.vip_request_port = vrp
                        vrpp.optionvip = op_pt
                        vrpp.save()

    except Exception, e:
        log.error(e)
        raise e
Пример #38
0
    def update_v3(self, vip_port_map):
        facade_eqpt = get_app('api_equipment', 'facade')

        if not self.identifier or self.port != vip_port_map['port']:
            eqpts = facade_eqpt\
                .get_eqpt_by_envvip(self.vip_request.environmentvip.id)
            if eqpts:
                plugin = PluginFactory.factory(eqpts[0])
                identifier = plugin.get_name_eqpt(
                    self.vip_request,
                    vip_port_map['port']
                )
                self.identifier = identifier

        self.save()

        # L4 Protocol
        try:
            opt = VipRequestPortOptionVip.objects.get(
                vip_request_port_id=self.id,
                optionvip_id=vip_port_map.get('options').get('l4_protocol'))
        except:
            opt = VipRequestPortOptionVip()
            opt_map = {
                'vip_request_port': self.id,
                'optionvip': vip_port_map['options']['l4_protocol']
            }
            opt.create_v3(opt_map)
        # L7 Protocol
        try:
            opt = VipRequestPortOptionVip.objects.get(
                vip_request_port_id=self.id,
                optionvip_id=vip_port_map.get('options').get('l7_protocol'))
        except:
            opt = VipRequestPortOptionVip()
            opt_map = {
                'vip_request_port': self.id,
                'optionvip': vip_port_map['options']['l7_protocol']
            }
            opt.create_v3(opt_map)

        # Deletes option by port
        VipRequestPortOptionVip.objects.filter(
            vip_request_port_id=self.id
        ).exclude(
            optionvip_id__in=[
                vip_port_map.get('options').get('l4_protocol'),
                vip_port_map.get('options').get('l7_protocol')]
        ).delete()

        # Pools
        pools = list()
        for pool in vip_port_map.get('pools'):
            pool_map = {
                'vip_request_port': self.id,
                'server_pool': pool.get('server_pool'),
                'optionvip': pool.get('l7_rule'),
                'val_optionvip': pool.get('l7_value'),
                'order': pool.get('order')
            }
            try:
                pl = VipRequestPortPool.objects.get(
                    vip_request_port=self.id,
                    id=pool.get('id'))
            except:
                pl = VipRequestPortPool()
                pl.create_v3(pool_map)
            else:
                pl.update_v3(pool_map)

            pools.append(pl.id)

        # Deletes pool by port
        VipRequestPortPool.objects.filter(
            vip_request_port=self.id
        ).exclude(
            id__in=pools
        ).delete()
Пример #39
0
    def test_factory_bgp(self):
        equipment = self._mock_equipment()
        plugin = PluginFactory.factory(equipment)

        self.assertEqual(hasattr(plugin, 'bgp'), True)
    def setUp(self):
        self.equipment = Equipamento.objects.filter(id=10)[0]
        self.equipment_access = EquipamentoAcesso.objects.filter(id=1)[0]
        self.utils.set_controller_endpoint(self.equipment_access)

        self.odl = PluginFactory.factory(self.equipment)
Пример #41
0
def update_real_pool(request):
    """
    - update real pool in eqpt
    - update data pool in db
    """
    pools = request.DATA.get("pools", [])
    load_balance = {}

    # valid data for save in DB and apply in eqpt
    ps, sp = valid_to_save_reals_v2(pools)

    for pool in pools:

        ids = [p['id'] for p in pool['server_pool_members'] if p['id']]
        db_members = ServerPoolMember.objects.filter(id__in=ids)
        db_members_remove = ServerPoolMember.objects.filter(server_pool__id=pool['server_pool']['id']).exclude(id__in=ids)
        db_members_id = [str(s.id) for s in db_members]

        pools_members = list()
        for pool_member in pool['server_pool_members']:

            if not pool_member['ipv6']:
                ip = pool_member['ip']['ip_formated']
            else:
                ip = pool_member['ipv6']['ip_formated']

            if pool_member['id']:
                member = db_members[db_members_id.index(str(pool_member['id']))]
                if not member.ipv6:
                    ip_db = member.ip.ip_formated
                else:
                    ip_db = member.ipv6.ip_formated

                if member.port_real == pool_member['port_real'] and ip_db == ip:
                    pools_members.append({
                        'id': pool_member['id'],
                        'ip': ip,
                        'port': pool_member['port_real'],
                        'limit': pool_member['limit'],
                        'priority': pool_member['priority'],
                        'weight': pool_member['weight'],
                    })
                else:
                    pools_members.append({
                        'id': None,
                        'ip': ip_db,
                        'port': member.port_real,
                        'remove': 1
                    })
                    pools_members.append({
                        'id': pool_member['id'],
                        'ip': ip,
                        'port': pool_member['port_real'],
                        'limit': pool_member['limit'],
                        'priority': pool_member['priority'],
                        'weight': pool_member['weight'],
                        'new': 1
                    })
            else:
                pools_members.append({
                    'id': None,
                    'ip': ip,
                    'port': pool_member['port_real'],
                    'limit': pool_member['limit'],
                    'priority': pool_member['priority'],
                    'weight': pool_member['weight'],
                    'new': 1
                })

        # members to remove
        for member in db_members_remove:
            if not member.ipv6:
                ip_db = member.ip.ip_formated
            else:
                ip_db = member.ipv6.ip_formated
            pools_members.append({
                'id': member.id,
                'ip': ip_db,
                'port': member.port_real,
                'remove': 1
            })

        # get eqpts associate with pool
        equips = EquipamentoAmbiente.objects.filter(
            ambiente__id=pool['server_pool']['environment']['id'],
            equipamento__tipo_equipamento__tipo_equipamento=u'Balanceador')

        equipment_list = [e.equipamento for e in equips]
        if all_equipments_are_in_maintenance(equipment_list):
            raise AllEquipmentsAreInMaintenanceException()

        for e in equips:
            eqpt_id = str(e.equipamento.id)
            equipment_access = EquipamentoAcesso.search(
                equipamento=e.equipamento.id,
                protocolo="https"
            ).uniqueResult()
            equipment = Equipamento.get_by_pk(e.equipamento.id)

            plugin = PluginFactory.factory(equipment)

            if not load_balance.get(eqpt_id):

                load_balance[eqpt_id] = {
                    'plugin': plugin,
                    'fqdn': equipment_access.fqdn,
                    'user': equipment_access.user,
                    'password': equipment_access.password,
                    'pools': [],
                }

            load_balance[eqpt_id]['pools'].append({
                'id': pool['server_pool']['id'],
                'nome': pool['server_pool']['identifier'],
                'lb_method': pool['server_pool']['lb_method'],
                'healthcheck': pool['server_pool']['healthcheck'],
                'action': pool['server_pool']['servicedownaction']['name'],
                'pools_members': pools_members
            })

    # get ids from pools created
    names = [sp[p].id for idx, p in enumerate(ps) if sp[p].pool_created]
    environments = [sp[p].id for idx, p in enumerate(ps) if sp[p].pool_created]

    # call plugin to change in load balance
    for lb in load_balance:
        lbe = [l for l in load_balance[lb]['pools'] if l['id'] in names if l['id'] in environments]

        if len(lbe) > 0:
            json = load_balance[lb]
            json['pools'] = lbe
            json['plugin'].updatePool(json)

    # save pool in DB
    for idx in sp:
        idx = str(idx)
        sp[idx].identifier = ps[idx]['server_pool']['identifier']
        sp[idx].environment = Ambiente.objects.get(
            id=ps[idx]['server_pool']['environment']['id'])
        sp[idx].default_limit = ps[idx]['server_pool']['default_limit']
        sp[idx].default_port = ps[idx]['server_pool']['default_port']
        sp[idx].lb_method = ps[idx]['server_pool']['lb_method']
        sp[idx].servicedownaction = OptionPool.objects.get(
            id=ps[idx]['server_pool']['servicedownaction']['id'])
        is_healthcheck_valid(ps[idx]['server_pool']['healthcheck'])
        ps[idx]['server_pool']['healthcheck'] = ps[idx]['server_pool']['healthcheck']
        sp[idx].healthcheck = get_or_create_healthcheck(
            request.user,
            ps[idx]['server_pool']['healthcheck']['healthcheck_expect'],
            ps[idx]['server_pool']['healthcheck']['healthcheck_type'],
            ps[idx]['server_pool']['healthcheck']['healthcheck_request'],
            ps[idx]['server_pool']['healthcheck']['destination'],
            ps[idx]['server_pool']['healthcheck']['identifier'])

        sp[idx].save()

    members_id = [p['id'] for p in pool['server_pool_members'] for pool in pools if p['id']]
    pms = ServerPoolMember.objects.filter(id__in=members_id)
    pms_delete = ServerPoolMember.objects.exclude(id__in=members_id).filter(server_pool__id__in=[pool['server_pool']['id'] for pool in pools])

    members = dict()
    for pool in pools:
        for member in pool['server_pool_members']:
            if member['id']:
                members[str(member['id'])] = member

    # update pool members

    log.info(pools)
    for pm in pms:
        if members.get(str(pm.id)):
            pm.port_real = members.get(str(pm.id))['port_real']
            pm.priority = members.get(str(pm.id))['priority']
            pm.weight = members.get(str(pm.id))['weight']
            pm.limit = members.get(str(pm.id))['limit']
            pm.save()

    # delete pool members
    for pm in pms_delete:
        pm.delete()

    # create new pool members
    members = [p for p in pool['server_pool_members'] for pool in pools if not p['id']]
    for member in members:
        pm = ServerPoolMember()
        pm.server_pool_id = member['server_pool']['id']
        pm.limit = member['limit']
        if member['ip']:
            pm.ip_id = member['ip']['id']
        if member['ipv6']:
            pm.ipv6_id = member['ipv6']['id']
        pm.identifier = member['identifier']
        pm.weight = member['weight']
        pm.priority = member['priority']
        pm.port_real = member['port_real']

        pm.save()

    # Save reals
    # save_server_pool_member(request.user, sp, list_server_pool_member)

    return {}
Пример #42
0
def delete_real_pool(request):
    """
    delete real pool in eqpt
    """
    pools = request.DATA.get("pools", [])

    load_balance = {}

    for pool in pools:

        pools_members = []
        for pool_member in pool['server_pool_members']:
            if pool_member['ipv6'] is None:
                ip = pool_member['ip']['ip_formated']
            else:
                ip = pool_member['ipv6']['ip_formated']

            pools_members.append({
                'id': pool_member['id'],
                'ip': ip,
                'port': pool_member['port_real'],
                'member_status': pool_member['member_status'],
                'limit': pool_member['limit'],
                'priority': pool_member['priority'],
                'weight': pool_member['weight']
            })

        equips = EquipamentoAmbiente.objects.filter(
            ambiente__id=pool['server_pool']['environment']['id'],
            equipamento__tipo_equipamento__tipo_equipamento=u'Balanceador')

        equipment_list = [e.equipamento for e in equips]
        if all_equipments_are_in_maintenance(equipment_list):
            raise AllEquipmentsAreInMaintenanceException()

        for e in equips:
            eqpt_id = str(e.equipamento.id)
            equipment_access = EquipamentoAcesso.search(
                equipamento=e.equipamento.id,
                protocolo="https"
            ).uniqueResult()
            equipment = Equipamento.get_by_pk(e.equipamento.id)

            plugin = PluginFactory.factory(equipment)

            if not load_balance.get(eqpt_id):

                load_balance[eqpt_id] = {
                    'plugin': plugin,
                    'fqdn': equipment_access.fqdn,
                    'user': equipment_access.user,
                    'password': equipment_access.password,
                    'pools': [],
                }

            load_balance[eqpt_id]['pools'].append({
                'id': pool['server_pool']['id'],
                'nome': pool['server_pool']['identifier'],
                'lb_method': pool['server_pool']['lb_method'],
                'healthcheck': pool['server_pool']['healthcheck'],
                'action': pool['server_pool']['servicedownaction']['name'],
                'pools_members': pools_members
            })

    for lb in load_balance:
        load_balance[lb]['plugin'].deletePool(load_balance[lb])

    ids = [pool['server_pool']['id'] for pool in pools]
    ServerPool.objects.filter(id__in=ids).update(pool_created=False)

    return {}
Пример #43
0
def _prepare_apply(pools, created=False, user=None):

    load_balance = dict()
    keys = list()

    for pool in pools:

        equips = _validate_pool_members_to_apply(pool, user)

        keys.append(sorted([str(eqpt.id) for eqpt in equips]))

        healthcheck = pool['healthcheck']
        healthcheck['identifier'] = facade_v3.\
            reserve_name_healthcheck(pool['identifier'])
        healthcheck['new'] = True

        for e in equips:

            eqpt_id = str(e.id)
            equipment_access = EquipamentoAcesso.search(
                equipamento=e.id
            )

            plugin = PluginFactory.factory(e)

            if not load_balance.get(eqpt_id):

                load_balance[eqpt_id] = {
                    'plugin': plugin,
                    'access': equipment_access,
                    'pools': [],
                }

            vips_requests = ServerPool().get_vips_related(pool['id'])

            serializer_vips = serializers_vip.VipRequestV3Serializer(
                vips_requests,
                many=True,
                include=(
                    'ipv4__details',
                    'ipv6__details',
                    'ports__identifier',
                    'ports__pools__server_pool__basic__lb_method',
                )
            )
            vips = serializer_vips.data

            load_balance[eqpt_id]['pools'].append({
                'id': pool['id'],
                'nome': pool['identifier'],
                'lb_method': pool['lb_method'],
                'healthcheck': healthcheck,
                'action': pool['servicedownaction']['name'],
                'vips': vips,
                'pools_members': [{
                    'id': pool_member['id'],
                    'identifier': pool_member['identifier'],
                    'ip': pool_member['ip']['ip_formated']
                    if pool_member['ip'] else
                    pool_member['ipv6']['ip_formated'],
                    'port': pool_member['port_real'],
                    'member_status': pool_member['member_status'],
                    'limit': pool_member['limit'],
                    'priority': pool_member['priority'],
                    'weight': pool_member['weight']
                } for pool_member in pool['server_pool_members']]
            })

    # pools are in differents load balancers
    keys = [','.join(key) for key in keys]
    if len(list(set(keys))) > 1:
        raise Exception('Pools are in differents load balancers')

    return load_balance
Пример #44
0
def update_real_pool(pools, user):
    """Update real pool in Load Balancer and DB."""

    load_balance = dict()
    keys = list()

    for pool in pools:

        pool_obj = facade_v3.get_pool_by_id(pool['id'])

        healthcheck_old = serializers.HealthcheckV3Serializer(
            pool_obj.healthcheck).data

        db_members = pool_obj.serverpoolmember_set.all()
        member_ids = [
            spm['id'] for spm in pool['server_pool_members'] if spm['id']
        ]
        db_members_remove = list(db_members.exclude(id__in=member_ids))
        db_members_id = [str(s.id) for s in db_members]

        pool_obj.update_v3(pool, user, permit_created=True)

        pools_members = list()
        for pool_member in pool['server_pool_members']:

            ip = pool_member['ip']['ip_formated'] if pool_member[
                'ip'] else pool_member['ipv6']['ip_formated']

            if pool_member.get('id', None) is not None:

                member = db_members[db_members_id.index(str(
                    pool_member['id']))]
                ip_db = member.ip.ip_formated \
                    if member.ip else member.ipv6.ip_formated

                if member.port_real == pool_member['port_real'] \
                        and ip_db == ip:
                    # update info of member
                    pools_members.append({
                        'id':
                        pool_member['id'],
                        'identifier':
                        member.identifier,
                        'ip':
                        ip,
                        'port':
                        pool_member['port_real'],
                        'limit':
                        pool_member['limit'],
                        'priority':
                        pool_member['priority'],
                        'member_status':
                        pool_member['member_status'],
                        'weight':
                        pool_member['weight'],
                    })
                else:
                    # delete member with old port and old ip
                    pools_members.append({
                        'id': None,
                        'identifier': member.identifier,
                        'ip': ip_db,
                        'port': member.port_real,
                        'remove': 1
                    })
                    # create member with new port and new ip
                    pools_members.append({
                        'id':
                        pool_member['id'],
                        'identifier':
                        ip,
                        'ip':
                        ip,
                        'port':
                        pool_member['port_real'],
                        'limit':
                        pool_member['limit'],
                        'priority':
                        pool_member['priority'],
                        'weight':
                        pool_member['weight'],
                        'member_status':
                        pool_member['member_status'],
                        'new':
                        1
                    })
            else:
                # create member
                pools_members.append({
                    'id':
                    None,
                    'identifier':
                    ip,
                    'ip':
                    ip,
                    'port':
                    pool_member['port_real'],
                    'limit':
                    pool_member['limit'],
                    'priority':
                    pool_member['priority'],
                    'weight':
                    pool_member['weight'],
                    'member_status':
                    pool_member['member_status'],
                    'new':
                    1
                })

        # delete members
        for member in db_members_remove:
            pools_members.append({
                'id':
                member.id,
                'identifier':
                member.identifier,
                'ip':
                member.ip.ip_formated
                if member.ip else member.ipv6.ip_formated,
                'port':
                member.port_real,
                'remove':
                1
            })

        # get eqpts associate with pool
        equips = _validate_pool_to_apply(pool, update=True, user=user)

        keys.append(sorted([str(eqpt.id) for eqpt in equips]))

        healthcheck = copy.deepcopy(pool['healthcheck'])
        healthcheck['new'] = False
        if json_delta.diff(healthcheck_old, pool['healthcheck']):
            healthcheck['identifier'] = facade_v3.reserve_name_healthcheck(
                pool['identifier'])
            healthcheck['new'] = True

        for e in equips:
            eqpt_id = str(e.id)
            equipment_access = EquipamentoAcesso.search(equipamento=e.id)

            plugin = PluginFactory.factory(e)

            if not load_balance.get(eqpt_id):

                load_balance[eqpt_id] = {
                    'plugin': plugin,
                    'access': equipment_access,
                    'pools': [],
                }

            vips_requests = ServerPool().get_vips_related(pool['id'])

            serializer_vips = serializers_vip.VipRequestV3Serializer(
                vips_requests,
                many=True,
                include=(
                    'ipv4__details',
                    'ipv6__details',
                    'ports__identifier',
                    'ports__pools__server_pool__basic__lb_method',
                ))
            vips = serializer_vips.data

            load_balance[eqpt_id]['pools'].append({
                'id':
                pool['id'],
                'nome':
                pool['identifier'],
                'lb_method':
                pool['lb_method'],
                'healthcheck':
                healthcheck,
                'action':
                pool['servicedownaction']['name'],
                'vips':
                vips,
                'pools_members':
                pools_members
            })

    # pools are in differents load balancers
    keys = [','.join(key) for key in keys]
    if len(list(set(keys))) > 1:
        raise Exception('Pools are in differents load balancers')

    for lb in load_balance:
        load_balance[lb]['plugin'].update_pool(load_balance[lb])

    return {}
Пример #45
0
def _prepare_apply(pools, created=False, user=None):

    load_balance = dict()
    keys = list()

    for pool in pools:

        equips = _validate_pool_members_to_apply(pool, user)

        keys.append(sorted([str(eqpt.id) for eqpt in equips]))

        healthcheck = pool['healthcheck']
        healthcheck['identifier'] = facade_v3.\
            reserve_name_healthcheck(pool['identifier'])
        healthcheck['new'] = True

        for e in equips:

            eqpt_id = str(e.id)
            equipment_access = EquipamentoAcesso.search(equipamento=e.id)

            plugin = PluginFactory.factory(e)

            if not load_balance.get(eqpt_id):

                load_balance[eqpt_id] = {
                    'plugin': plugin,
                    'access': equipment_access,
                    'pools': [],
                }

            vips_requests = ServerPool().get_vips_related(pool['id'])

            serializer_vips = serializers_vip.VipRequestV3Serializer(
                vips_requests,
                many=True,
                include=(
                    'ipv4__details',
                    'ipv6__details',
                    'ports__identifier',
                    'ports__pools__server_pool__basic__lb_method',
                ))
            vips = serializer_vips.data

            load_balance[eqpt_id]['pools'].append({
                'id':
                pool['id'],
                'nome':
                pool['identifier'],
                'lb_method':
                pool['lb_method'],
                'healthcheck':
                healthcheck,
                'action':
                pool['servicedownaction']['name'],
                'vips':
                vips,
                'pools_members': [{
                    'id':
                    pool_member['id'],
                    'identifier':
                    pool_member['identifier'],
                    'ip':
                    pool_member['ip']['ip_formated'] if pool_member['ip'] else
                    pool_member['ipv6']['ip_formated'],
                    'port':
                    pool_member['port_real'],
                    'member_status':
                    pool_member['member_status'],
                    'limit':
                    pool_member['limit'],
                    'priority':
                    pool_member['priority'],
                    'weight':
                    pool_member['weight']
                } for pool_member in pool['server_pool_members']]
            })

    # pools are in differents load balancers
    keys = [','.join(key) for key in keys]
    if len(list(set(keys))) > 1:
        raise Exception('Pools are in differents load balancers')

    return load_balance
Пример #46
0
    def test_factory_bgp(self):
        equipment = self._mock_equipment()
        plugin = PluginFactory.factory(equipment)

        self.assertEqual(hasattr(plugin, 'bgp'), True)
Пример #47
0
    def update_v3(self, vip_port_map):
        facade_eqpt = get_app('api_equipment', 'facade')

        if not self.identifier or self.port != vip_port_map['port']:
            eqpts = facade_eqpt\
                .get_eqpt_by_envvip(self.vip_request.environmentvip.id)
            if eqpts:
                plugin = PluginFactory.factory(eqpts[0])
                identifier = plugin.get_name_eqpt(
                    self.vip_request,
                    vip_port_map['port']
                )
                self.identifier = identifier

        self.save()

        # L4 Protocol
        try:
            opt = VipRequestPortOptionVip.objects.get(
                vip_request_port_id=self.id,
                optionvip_id=vip_port_map.get('options').get('l4_protocol'))
        except:
            opt = VipRequestPortOptionVip()
            opt_map = {
                'vip_request_port': self.id,
                'optionvip': vip_port_map['options']['l4_protocol']
            }
            opt.create_v3(opt_map)
        # L7 Protocol
        try:
            opt = VipRequestPortOptionVip.objects.get(
                vip_request_port_id=self.id,
                optionvip_id=vip_port_map.get('options').get('l7_protocol'))
        except:
            opt = VipRequestPortOptionVip()
            opt_map = {
                'vip_request_port': self.id,
                'optionvip': vip_port_map['options']['l7_protocol']
            }
            opt.create_v3(opt_map)

        # Deletes option by port
        VipRequestPortOptionVip.objects.filter(
            vip_request_port_id=self.id
        ).exclude(
            optionvip_id__in=[
                vip_port_map.get('options').get('l4_protocol'),
                vip_port_map.get('options').get('l7_protocol')]
        ).delete()

        # Pools
        pools = list()
        for pool in vip_port_map.get('pools'):
            pool_map = {
                'vip_request_port': self.id,
                'server_pool': pool.get('server_pool'),
                'optionvip': pool.get('l7_rule'),
                'val_optionvip': pool.get('l7_value'),
                'order': pool.get('order')
            }
            try:
                pl = VipRequestPortPool.objects.get(
                    vip_request_port=self.id,
                    id=pool.get('id'))
            except:
                pl = VipRequestPortPool()
                pl.create_v3(pool_map)
            else:
                pl.update_v3(pool_map)

            pools.append(pl.id)

        # Deletes pool by port
        VipRequestPortPool.objects.filter(
            vip_request_port=self.id
        ).exclude(
            id__in=pools
        ).delete()
    def setUp(self):
        self.equipment = Equipamento.objects.filter(id=10)[0]
        self.equipment_access = EquipamentoAcesso.objects.filter(id=1)[0]
        self.utils.set_controller_endpoint(self.equipment_access)

        self.odl = PluginFactory.factory(self.equipment)
Пример #49
0
def set_poolmember_state(pools):
    """
    Set Pool Members state

    """

    try:
        load_balance = {}

        for pool in pools:

            pools_members = []

            q_filters = []
            for pool_member in pool['server_pool_members']:

                port_real = pool_member['port_real']

                if pool_member['ipv6'] is None:
                    ip = pool_member['ip']['ip_formated']

                    ip_ft = '.'.join(str(x) for x in [
                        pool_member['ip']['oct1'],
                        pool_member['ip']['oct2'],
                        pool_member['ip']['oct3'],
                        pool_member['ip']['oct4']])

                    if ip != ip_ft:
                        raise exceptions.InvalidIpNotExist()

                    q_filters.append({
                        'ip__oct1': pool_member['ip']['oct1'],
                        'ip__oct2': pool_member['ip']['oct2'],
                        'ip__oct3': pool_member['ip']['oct3'],
                        'ip__oct4': pool_member['ip']['oct4'],
                        'port_real': port_real
                    })
                else:
                    ip = pool_member['ipv6']['ip_formated']

                    ip_ft = '.'.join(str(x) for x in [
                        pool_member['ipv6']['block1'],
                        pool_member['ipv6']['block2'],
                        pool_member['ipv6']['block3'],
                        pool_member['ipv6']['block4'],
                        pool_member['ipv6']['block5'],
                        pool_member['ipv6']['block6'],
                        pool_member['ipv6']['block7'],
                        pool_member['ipv6']['block8']])

                    if ip != ip_ft:
                        raise exceptions.InvalidIpNotExist()

                    q_filters.append({
                        'ipv6__block1': pool_member['ipv6']['block1'],
                        'ipv6__block2': pool_member['ipv6']['block2'],
                        'ipv6__block3': pool_member['ipv6']['block3'],
                        'ipv6__block4': pool_member['ipv6']['block4'],
                        'ipv6__block5': pool_member['ipv6']['block5'],
                        'ipv6__block6': pool_member['ipv6']['block6'],
                        'ipv6__block7': pool_member['ipv6']['block7'],
                        'ipv6__block8': pool_member['ipv6']['block8'],
                        'port_real': port_real
                    })

                pools_members.append({
                    'id': pool_member['id'],
                    'ip': ip,
                    'port': port_real,
                    'member_status': pool_member['member_status']
                })

            server_pool_members = ServerPoolMember.objects.filter(
                reduce(lambda x, y: x | y, [Q(**q_filter) for q_filter in q_filters]), server_pool=pool['server_pool']['id'])
            if len(server_pool_members) != len(pools_members):
                raise exceptions.PoolmemberNotExist()

            pool_name = pool['server_pool']['identifier']
            server_pools = ServerPool.objects.filter(identifier=pool_name)
            if not server_pools:
                raise exceptions.PoolNotExist()

            equips = EquipamentoAmbiente.objects.filter(
                ambiente__id=pool['server_pool']['environment']['id'],
                equipamento__tipo_equipamento__tipo_equipamento=u'Balanceador')

            equipment_list = [e.equipamento for e in equips]
            if all_equipments_are_in_maintenance(equipment_list):
                raise AllEquipmentsAreInMaintenanceException()

            for e in equips:
                eqpt_id = str(e.equipamento.id)
                equipment_access = EquipamentoAcesso.search(
                    equipamento=e.equipamento.id,
                    protocolo="https"
                ).uniqueResult()
                equipment = Equipamento.get_by_pk(e.equipamento.id)

                plugin = PluginFactory.factory(equipment)

                if not load_balance.get(eqpt_id):

                    load_balance[eqpt_id] = {
                        'plugin': plugin,
                        'fqdn': equipment_access.fqdn,
                        'user': equipment_access.user,
                        'password': equipment_access.password,
                        'pools': [],
                    }

                load_balance[eqpt_id]['pools'].append({
                    'id': pool['server_pool']['id'],
                    'nome': pool_name,
                    'pools_members': pools_members
                })

        for lb in load_balance:
            load_balance[lb]['plugin'].setStateMember(load_balance[lb])
        return {}

    except Exception, exception:
        log.error(exception)
        raise exception
Пример #50
0
def get_poolmember_state(servers_pools):
    """
    Return Pool Members State
    """
    load_balance = {}

    for server_pool in servers_pools:

        pools_members = []

        server_pool_members = ServerPoolMember.objects.filter(
            server_pool=server_pool)
        for pool_member in server_pool_members:
            if pool_member.ipv6 is None:
                ip = pool_member.ip.ip_formated
            else:
                ip = pool_member.ipv6.ip_formated

            pools_members.append({
                'id': pool_member.id,
                'ip': ip,
                'port': pool_member.port_real,
                'member_status': pool_member.member_status
            })

        if pools_members:

            # pool_name = server_pool.identifier
            pool_id = server_pool.id

            equips = EquipamentoAmbiente.objects.filter(
                ambiente__id=server_pool.environment.id,
                equipamento__tipo_equipamento__tipo_equipamento=u'Balanceador')

            equipment_list = [e.equipamento for e in equips]
            if all_equipments_are_in_maintenance(equipment_list):
                raise AllEquipmentsAreInMaintenanceException()

            for e in equips:
                eqpt_id = str(e.equipamento.id)
                equipment_access = EquipamentoAcesso.search(
                    equipamento=e.equipamento.id,
                    protocolo="https"
                ).uniqueResult()
                equipment = Equipamento.get_by_pk(e.equipamento.id)

                plugin = PluginFactory.factory(equipment)

                if not load_balance.get(eqpt_id):

                    load_balance[eqpt_id] = {
                        'plugin': plugin,
                        'fqdn': equipment_access.fqdn,
                        'user': equipment_access.user,
                        'password': equipment_access.password,
                        'pools': [],
                    }

                load_balance[eqpt_id]['pools'].append({
                    'id': server_pool.id,
                    'nome': server_pool.identifier,
                    'pools_members': pools_members
                })

    for lb in load_balance:
        ps = {}
        status = {}
        # call plugin to get state member
        states = load_balance[lb]['plugin'].getStateMember(load_balance[lb])

        for idx, state in enumerate(states):
            pool_id = load_balance[lb]['pools'][idx]['id']
            if not ps.get(pool_id):
                ps[pool_id] = {}
                status[pool_id] = {}

            # populate variable for to verify diff states
            for idx_m, st in enumerate(state):
                member_id = load_balance[lb]['pools'][idx]['pools_members'][idx_m]['id']
                if not ps[pool_id].get(member_id):
                    ps[pool_id][member_id] = []

                ps[pool_id][member_id].append(st)
                status[pool_id][member_id] = st

    # Verify diff state of pool member in eqpts
    for idx in ps:
        for idx_m in ps[idx]:
            if len(set(ps[idx][idx_m])) > 1:
                msg = 'There are states differents in equipments.'
                log.error(msg)
                raise exceptions.DiffStatesEquipament(msg)

    return status
Пример #51
0
def update_real_pool(pools, user):
    """Update real pool in Load Balancer and DB."""

    load_balance = dict()
    keys = list()

    for pool in pools:

        pool_obj = facade_v3.get_pool_by_id(pool['id'])

        healthcheck_old = serializers.HealthcheckV3Serializer(
            pool_obj.healthcheck).data

        db_members = pool_obj.serverpoolmember_set.all()
        member_ids = [spm['id'] for spm in pool['server_pool_members']
                      if spm['id']]
        db_members_remove = list(db_members.exclude(id__in=member_ids))
        db_members_id = [str(s.id) for s in db_members]

        pool_obj.update_v3(pool, user, permit_created=True)

        pools_members = list()
        for pool_member in pool['server_pool_members']:

            ip = pool_member['ip']['ip_formated'] if pool_member[
                'ip'] else pool_member['ipv6']['ip_formated']

            if pool_member.get('id', None) is not None:

                member = db_members[
                    db_members_id.index(str(pool_member['id']))]
                ip_db = member.ip.ip_formated \
                    if member.ip else member.ipv6.ip_formated

                if member.port_real == pool_member['port_real'] \
                        and ip_db == ip:
                    # update info of member
                    pools_members.append({
                        'id': pool_member['id'],
                        'identifier': member.identifier,
                        'ip': ip,
                        'port': pool_member['port_real'],
                        'limit': pool_member['limit'],
                        'priority': pool_member['priority'],
                        'member_status': pool_member['member_status'],
                        'weight': pool_member['weight'],
                    })
                else:
                    # delete member with old port and old ip
                    pools_members.append({
                        'id': None,
                        'identifier': member.identifier,
                        'ip': ip_db,
                        'port': member.port_real,
                        'remove': 1
                    })
                    # create member with new port and new ip
                    pools_members.append({
                        'id': pool_member['id'],
                        'identifier': ip,
                        'ip': ip,
                        'port': pool_member['port_real'],
                        'limit': pool_member['limit'],
                        'priority': pool_member['priority'],
                        'weight': pool_member['weight'],
                        'member_status': pool_member['member_status'],
                        'new': 1
                    })
            else:
                # create member
                pools_members.append({
                    'id': None,
                    'identifier': ip,
                    'ip': ip,
                    'port': pool_member['port_real'],
                    'limit': pool_member['limit'],
                    'priority': pool_member['priority'],
                    'weight': pool_member['weight'],
                    'member_status': pool_member['member_status'],
                    'new': 1
                })

        # delete members
        for member in db_members_remove:
            pools_members.append({
                'id': member.id,
                'identifier': member.identifier,
                'ip': member.ip.ip_formated
                if member.ip else member.ipv6.ip_formated,
                'port': member.port_real,
                'remove': 1
            })

        # get eqpts associate with pool
        equips = _validate_pool_to_apply(pool, update=True, user=user)

        keys.append(sorted([str(eqpt.id) for eqpt in equips]))

        healthcheck = copy.deepcopy(pool['healthcheck'])
        healthcheck['new'] = False
        if json_delta.diff(healthcheck_old, pool['healthcheck']):
            healthcheck['identifier'] = facade_v3.reserve_name_healthcheck(
                pool['identifier'])
            healthcheck['new'] = True

        for e in equips:
            eqpt_id = str(e.id)
            equipment_access = EquipamentoAcesso.search(
                equipamento=e.id
            )

            plugin = PluginFactory.factory(e)

            if not load_balance.get(eqpt_id):

                load_balance[eqpt_id] = {
                    'plugin': plugin,
                    'access': equipment_access,
                    'pools': [],
                }

            vips_requests = ServerPool().get_vips_related(pool['id'])

            serializer_vips = serializers_vip.VipRequestV3Serializer(
                vips_requests,
                many=True,
                include=(
                    'ipv4__details',
                    'ipv6__details',
                    'ports__identifier',
                    'ports__pools__server_pool__basic__lb_method',
                )
            )
            vips = serializer_vips.data

            load_balance[eqpt_id]['pools'].append({
                'id': pool['id'],
                'nome': pool['identifier'],
                'lb_method': pool['lb_method'],
                'healthcheck': healthcheck,
                'action': pool['servicedownaction']['name'],
                'vips': vips,
                'pools_members': pools_members
            })

    # pools are in differents load balancers
    keys = [','.join(key) for key in keys]
    if len(list(set(keys))) > 1:
        raise Exception('Pools are in differents load balancers')

    for lb in load_balance:
        load_balance[lb]['plugin'].update_pool(load_balance[lb])

    return {}
Пример #52
0
def prepare_apply(load_balance, vip, created=True, user=None):

    vip_request = copy.deepcopy(vip)

    id_vip = str(vip_request.get('id'))

    equips, conf, cluster_unit = _validate_vip_to_apply(
        vip_request, created, user)

    cache_group = OptionVip.objects.get(
        id=vip_request.get('options').get('cache_group'))
    traffic_return = OptionVip.objects.get(
        id=vip_request.get('options').get('traffic_return'))
    timeout = OptionVip.objects.get(
        id=vip_request.get('options').get('timeout'))
    persistence = OptionVip.objects.get(
        id=vip_request.get('options').get('persistence'))

    if vip_request['ipv4']:
        ipv4 = Ip.get_by_pk(
            vip_request['ipv4']) if vip_request['ipv4'] else None
        vip_request['ipv4'] = {'id': ipv4.id, 'ip_formated': ipv4.ip_formated}

    if vip_request['ipv6']:
        ipv6 = Ipv6.get_by_pk(
            vip_request['ipv6']) if vip_request['ipv6'] else None
        vip_request['ipv6'] = {'id': ipv6.id, 'ip_formated': ipv6.ip_formated}

    if conf:
        conf = json.loads(conf)

    vip_request['options'] = dict()
    vip_request['options']['cache_group'] = {
        'id': cache_group.id,
        'nome_opcao_txt': cache_group.nome_opcao_txt
    }
    vip_request['options']['traffic_return'] = {
        'id': traffic_return.id,
        'nome_opcao_txt': traffic_return.nome_opcao_txt
    }
    vip_request['options']['timeout'] = {
        'id': timeout.id,
        'nome_opcao_txt': timeout.nome_opcao_txt
    }
    vip_request['options']['persistence'] = {
        'id': persistence.id,
        'nome_opcao_txt': persistence.nome_opcao_txt
    }
    vip_request['options']['cluster_unit'] = cluster_unit

    try:
        vip_request['options']['dscp'] = VipRequestDSCP.objects.get(
            vip_request=vip_request['id']).dscp
    except:
        vip_request['options']['dscp'] = None
        pass

    for idx, port in enumerate(vip_request['ports']):
        for i, pl in enumerate(port['pools']):

            pool = get_pool_by_id(pl['server_pool'])
            pool_serializer = pool_slz.PoolV3Serializer(pool)

            l7_rule = OptionVip.objects.get(id=pl['l7_rule']).nome_opcao_txt

            healthcheck = pool_serializer.data['healthcheck']
            healthcheck['identifier'] = reserve_name_healthcheck(
                pool_serializer.data['identifier'])
            healthcheck['new'] = True
            vip_request['ports'][idx]['pools'][i]['server_pool'] = {
                'id':
                pool_serializer.data['id'],
                'nome':
                pool_serializer.data['identifier'],
                'lb_method':
                pool_serializer.data['lb_method'],
                'healthcheck':
                healthcheck,
                'action':
                pool_serializer.data['servicedownaction']['name'],
                'pool_created':
                pool_serializer.data['pool_created'],
                'pools_members':
                [{
                    'id':
                    pool_member['id'],
                    'identifier':
                    pool_member['identifier'],
                    'ip':
                    pool_member['ip']['ip_formated'] if pool_member['ip'] else
                    pool_member['ipv6']['ip_formated'],
                    'port':
                    pool_member['port_real'],
                    'member_status':
                    pool_member['member_status'],
                    'limit':
                    pool_member['limit'],
                    'priority':
                    pool_member['priority'],
                    'weight':
                    pool_member['weight']
                }
                 for pool_member in pool_serializer.data['server_pool_members']
                 ]
            }

            vip_request['ports'][idx]['pools'][i]['l7_rule'] = l7_rule
        l7_protocol = OptionVip.objects.get(id=port['options']['l7_protocol'])
        l4_protocol = OptionVip.objects.get(id=port['options']['l4_protocol'])

        vip_request['ports'][idx]['options'] = dict()
        vip_request['ports'][idx]['options']['l7_protocol'] = {
            'id': l7_protocol.id,
            'nome_opcao_txt': l7_protocol.nome_opcao_txt
        }
        vip_request['ports'][idx]['options']['l4_protocol'] = {
            'id': l4_protocol.id,
            'nome_opcao_txt': l4_protocol.nome_opcao_txt
        }

    vip_request['conf'] = conf

    if conf:
        for idx, layer in enumerate(conf['conf']['layers']):
            requiments = layer.get('requiments')
            if requiments:
                # validate for port
                for idx_port, port in enumerate(vip['ports']):
                    for requiment in requiments:
                        condicionals = requiment.get('condicionals')
                        for condicional in condicionals:

                            validated = True

                            validations = condicional.get('validations')
                            for validation in validations:
                                if validation.get('type') == 'optionvip':
                                    validated &= valid_expression(
                                        validation.get('operator'),
                                        int(vip['options'][validation.get(
                                            'variable')]),
                                        int(validation.get('value')))

                                if validation.get('type') == 'portoptionvip':
                                    validated &= valid_expression(
                                        validation.get('operator'),
                                        int(port['options'][validation.get(
                                            'variable')]),
                                        int(validation.get('value')))

                                if validation.get(
                                        'type') == 'field' and validation.get(
                                            'variable') == 'cluster_unit':
                                    validated &= valid_expression(
                                        validation.get('operator'),
                                        cluster_unit, validation.get('value'))
                            if validated:
                                use = condicional.get('use')
                                for item in use:
                                    definitions = item.get('definitions')
                                    eqpts = item.get('eqpts')
                                    if eqpts:

                                        eqpts = Equipamento.objects.filter(
                                            id__in=eqpts,
                                            maintenance=0,
                                            tipo_equipamento__tipo_equipamento=
                                            u'Balanceador').distinct()

                                        if facade_eqpt.all_equipments_are_in_maintenance(
                                                equips):
                                            raise exceptions_eqpt.AllEquipmentsAreInMaintenanceException(
                                            )

                                        if user:
                                            if not facade_eqpt.all_equipments_can_update_config(
                                                    equips, user):
                                                raise exceptions_eqpt.UserDoesNotHavePermInAllEqptException(
                                                    'User does not have permission to update conf in eqpt. \
                                                    Verify the permissions of user group with equipment group. Vip:{}'
                                                    .format(vip_request['id']))

                                        for eqpt in eqpts:
                                            eqpt_id = str(eqpt.id)

                                            if not load_balance.get(eqpt_id):
                                                equipment_access = EquipamentoAcesso.search(
                                                    equipamento=eqpt.id)

                                                plugin = PluginFactory.factory(
                                                    eqpt)

                                                load_balance[eqpt_id] = {
                                                    'plugin': plugin,
                                                    'access': equipment_access,
                                                    'vips': [],
                                                    'layers': {},
                                                }

                                            idx_layer = str(idx)
                                            idx_port_str = str(port['port'])
                                            if not load_balance[eqpt_id][
                                                    'layers'].get(id_vip):
                                                load_balance[eqpt_id][
                                                    'layers'][id_vip] = dict()

                                            if load_balance[eqpt_id]['layers'][
                                                    id_vip].get(idx_layer):
                                                if load_balance[eqpt_id][
                                                        'layers'][id_vip].get(
                                                            idx_layer).get(
                                                                'definitions'
                                                            ).get(
                                                                idx_port_str):
                                                    load_balance[eqpt_id][
                                                        'layers'][id_vip][
                                                            idx_layer][
                                                                'definitions'][
                                                                    idx_port_str] += definitions
                                                else:
                                                    load_balance[eqpt_id][
                                                        'layers'][id_vip][
                                                            idx_layer][
                                                                'definitions'][
                                                                    idx_port_str] = definitions
                                            else:
                                                load_balance[eqpt_id][
                                                    'layers'][id_vip][
                                                        idx_layer] = {
                                                            'vip_request':
                                                            vip_request,
                                                            'definitions': {
                                                                idx_port_str:
                                                                definitions
                                                            }
                                                        }
                                # In first validated==True stops conditionals.
                                # Removing this break will add a wrong
                                # conditional.
                                break

    for e in equips:
        eqpt_id = str(e.id)

        if not load_balance.get(eqpt_id):

            equipment_access = EquipamentoAcesso.search(equipamento=e.id)

            plugin = PluginFactory.factory(e)

            load_balance[eqpt_id] = {
                'plugin': plugin,
                'access': equipment_access,
                'vips': [],
                'layers': {},
            }

        load_balance[eqpt_id]['vips'].append({'vip_request': vip_request})

    return load_balance