def test_create_vip_with_pools(self): server_pools = [ServerPool(id=1), ServerPool(id=2)] self.vip.validado = True self.vip.vip_criado = False self.mock_user_has_permission(True) self.mock_requisicao_vip_get_by_pk(self.vip) exec_script_mock = self.mock_exec_script( (0, 'load balancer output', '')) vip_save_mock = self.mock_vip_save() find_pools_mock = self.mock_find_pools(server_pools) pool_save_mock = self.mock_pool_save() response = self.client.post( '/vip/create/', VIP_XML, content_type='text/xml') xml_map = self.parse_response(response) self.assertEquals('load balancer output', xml_map.get( 'networkapi').get('sucesso').get('descricao').get('stdout')) self.assertEquals('0000', xml_map.get( 'networkapi').get('sucesso').get('codigo')) self.assertEquals(200, response.status_code) exec_script_mock.assert_called_with(VIP_CREATE % 1) self.assertTrue(vip_save_mock.called) self.assertTrue(find_pools_mock.called) self.assertTrue(pool_save_mock.called) self.assertTrue(server_pools[0].pool_created) self.assertTrue(server_pools[1].pool_created) self.assertTrue(self.vip.vip_criado)
def create_pool(pool, user): """Creates pool""" try: sp = ServerPool() sp.create_v3(pool, user) except exceptions.InvalidIdentifierAlreadyPoolException, e: raise ValidationAPIException(e.detail)
def test_update_pool_member(self): save_member_mock = patch( 'networkapi.requisicaovips.models.ServerPoolMember.save').start() pool = ServerPool(id=1, default_limit=1) pool_member = ServerPoolMember() dict = { 'nome_equips': 'equip_name', 'weight': 1, 'priority': 1, 'port_real': 80 } ip = Ip(id=1) ipv6 = Ipv6(id=1) update_pool_member(pool, pool_member, dict, ip, ipv6, self.user) self.assertEquals(pool, pool_member.server_pool) self.assertEquals(1, pool_member.limit) self.assertEquals(ip, pool_member.ip) self.assertEquals(ipv6, pool_member.ipv6) self.assertEquals('equip_name', pool_member.identifier) self.assertEquals(1, pool_member.weight) self.assertEquals(1, pool_member.priority) self.assertEquals(80, pool_member.port_real) self.assertTrue(save_member_mock.called)
def create_server_pool_model(self): return ServerPool( id=1, default_port=8080, identifier='pool_1', environment=Ambiente(id=1) )
def test_task_id_create_in_delete_deploy_one_server_pool_success( self, *args): """Test success of id task generate for server pool delete deploy success.""" mock_get_user = args[1] mock_get_pool = args[2] mock_delete_real_pool = args[3] user = Usuario(id=1, nome='test') divisaodc = DivisaoDc(id=1, nome='test') amblog = AmbienteLogico(id=1, nome='test') gl3 = GrupoL3(id=1, nome='test') ambiente = Ambiente(id=1, grupo_l3=gl3, divisao_dc=divisaodc, ambiente_logico=amblog) optionpool = OptionPool(id=1, type='test', name='test') healthcheck = Healthcheck(id=1) pool = ServerPool(id=1, identifier='test', environment=ambiente, servicedownaction=optionpool, healthcheck=healthcheck) pool_serializer = serializers.PoolV3Serializer(pool) mock_delete_real_pool.return_value = pool mock_get_pool.return_value = pool mock_get_user.return_value = user undeploy(pool.id, user.id) mock_delete_real_pool.assert_called_with([pool_serializer.data], user)
def test_task_id_create_in_put_deploy_one_server_pool_success(self, *args): """Test success of id task generate for server pool put deploy success.""" mock_get_user = args[1] mock_get_pool = args[2] mock_update_real_pool = args[3] user = Usuario(id=1, nome='test') divisaodc = DivisaoDc(id=1, nome='test') amblog = AmbienteLogico(id=1, nome='test') gl3 = GrupoL3(id=1, nome='test') ambiente = Ambiente(id=1, grupo_l3=gl3, divisao_dc=divisaodc, ambiente_logico=amblog) pool = ServerPool(id=1, identifier='test', environment=ambiente) mock_update_real_pool.return_value = pool mock_get_pool.return_value = pool mock_get_user.return_value = user redeploy({'id': pool.id}, user.id) mock_update_real_pool.assert_called_with([{'id': pool.id}], user)
def save_server_pool(user, id, identifier, default_port, hc, env, balancing, maxconn, id_pool_member, servicedownaction): # Save Server pool old_healthcheck = None if id: sp = ServerPool.objects.get(id=id) # store old healthcheck,lb method and service-down-action old_servicedownaction = sp.servicedownaction old_identifier = sp.identifier old_healthcheck = Healthcheck.objects.get(id=sp.healthcheck.id) old_lb_method = sp.lb_method old_maxconn = sp.default_limit # validate change of environment if sp.environment and sp.environment.id != env.id: validate_change_of_environment(id_pool_member, sp) # Pool already created, it is not possible to change Pool Identifier if (old_identifier != identifier and sp.pool_created): raise exceptions.CreatedPoolIdentifierException() update_pool_fields(default_port, env, identifier, old_healthcheck, old_lb_method, old_maxconn, sp, user) update_pool_maxconn(maxconn, old_maxconn, sp, user) apply_health_check(hc, old_healthcheck, sp, user) update_load_balancing_method(balancing, old_lb_method, sp, user) apply_service_down_action(old_servicedownaction, servicedownaction, sp, user) else: sp = ServerPool(identifier=identifier, default_port=default_port, healthcheck=hc, environment=env, pool_created=False, lb_method=balancing, default_limit=maxconn, servicedownaction=servicedownaction) sp.save() return sp, (old_healthcheck.id if old_healthcheck else None)
def test_save_pool_given_duplicate_identifier(self): self.mock_get_service_down_action_by_environment( OptionPool(name='none')) self.mock_filter_server_pool_by_identifier( ServerPool(identifier='pool_1')) response = self.client.post( '/api/pools/save/', self.load_pool_json('pool.json'), format='json') self.assertEquals(400, response.status_code) self.assertEquals('Identifier already exists.', response.data.get('detail'))
def save_server_pool(user, id, identifier, default_port, hc, env, balancing, maxconn, id_pool_member, servicedownaction): # Save Server pool old_healthcheck = None if id: sp = ServerPool.objects.get(id=id) # store old healthcheck,lb method and service-down-action old_servicedownaction = sp.servicedownaction old_identifier = sp.identifier old_healthcheck = Healthcheck.objects.get(id=sp.healthcheck.id) old_lb_method = sp.lb_method old_maxconn = sp.default_limit # validate change of environment if sp.environment and sp.environment.id != env.id: validate_change_of_environment(id_pool_member, sp) # Pool already created, it is not possible to change Pool Identifier if(old_identifier != identifier and sp.pool_created): raise exceptions.CreatedPoolIdentifierException() update_pool_fields(default_port, env, identifier, old_healthcheck, old_lb_method, old_maxconn, sp, user) update_pool_maxconn(maxconn, old_maxconn, sp, user) apply_health_check(hc, old_healthcheck, sp, user) update_load_balancing_method(balancing, old_lb_method, sp, user) apply_service_down_action( old_servicedownaction, servicedownaction, sp, user) else: sp = ServerPool(identifier=identifier, default_port=default_port, healthcheck=hc, environment=env, pool_created=False, lb_method=balancing, default_limit=maxconn, servicedownaction=servicedownaction) sp.save() return sp, (old_healthcheck.id if old_healthcheck else None)
def test_update_pool_fields(self): pool = ServerPool() default_port = 8080 env = Ambiente(id=1) identifier = 'pool_1' old_healthcheck = Healthcheck() old_lb_method = 'round-robin' old_maxconn = 50 save_pool_mock = self.mock_save_pool() update_pool_fields(default_port, env, identifier, old_healthcheck, old_lb_method, old_maxconn, pool, self.user) save_pool_mock.assert_calls(call(), call()) self.assertEquals(default_port, pool.default_port) self.assertEquals(env, pool.environment) self.assertEquals(identifier, pool.identifier) self.assertEquals(old_healthcheck, pool.healthcheck) self.assertEquals(old_lb_method, pool.lb_method)
def create_pool(pool, user): """Creates pool""" sp = ServerPool() sp.identifier = pool.get('identifier') sp.default_port = pool.get('default_port') sp.environment_id = pool.get('environment') sp.servicedownaction_id = _get_option_pool( pool.get('servicedownaction').get('name'), 'ServiceDownAction') sp.lb_method = pool.get('lb_method') sp.default_limit = pool.get('default_limit') healthcheck = _get_healthcheck(pool.get('healthcheck')) sp.healthcheck = healthcheck sp.pool_created = False sp.save() _create_pool_member(pool['server_pool_members'], sp) # perms groups_perm = pool.get('groups_permissions', []) groups_perm += facade_usr.get_groups(pool.get('users_permissions', [])) groups = facade_usr.reduce_groups(groups_perm) create_groups_permissions(groups, sp.id, user) return sp
def save_server_pool(user, id, identifier, default_port, hc, env, balancing, maxconn, id_pool_member, servicedownaction): # Save Server pool old_healthcheck = None if id: sp = ServerPool.objects.get(id=id) # storage old healthcheck , lb method and service-down-action old_servicedownaction = sp.servicedownaction old_identifier = sp.identifier old_healthcheck = Healthcheck.objects.get(id=sp.healthcheck.id) old_lb_method = sp.lb_method old_maxconn = sp.default_limit #valid change environment if sp.environment and sp.environment.id != env.id: if sp.pool_created: raise exceptions.UpdateEnvironmentPoolCreatedException() del_smp = sp.serverpoolmember_set.exclude(id__in=id_pool_member) vip = sp.vipporttopool_set.count() if vip > 0: raise exceptions.UpdateEnvironmentVIPException() if len(del_smp) > 0: raise exceptions.UpdateEnvironmentServerPoolMemberException() #Pool already created, it is not possible to change Pool Identifier if(old_identifier != identifier and sp.pool_created): raise exceptions.CreatedPoolIdentifierException() sp.default_port = default_port sp.environment = env sp.default_limit = old_maxconn sp.healthcheck = old_healthcheck sp.lb_method = old_lb_method sp.identifier = identifier sp.save(user) sp.default_limit = maxconn sp.save(user) #If exists pool member, checks if all them have the same maxconn #before changing default maxconn of pool if(len(sp.serverpoolmember_set.all()) > 0): if(old_maxconn != sp.default_limit and sp.pool_created): for serverpoolmember in sp.serverpoolmember_set.all(): if serverpoolmember.limit != old_maxconn: raise exceptions.ScriptAlterLimitPoolDiffMembersException() else: serverpoolmember.limit = maxconn serverpoolmember.save(user) transaction.commit() command = settings.POOL_MANAGEMENT_LIMITS % (sp.id) code, _, _ = exec_script(command) if code != 0: sp.default_limit = old_maxconn for serverpoolmember in sp.serverpoolmember_set.all(): serverpoolmember.limit = old_maxconn serverpoolmember.save(user) sp.save(user) transaction.commit() raise exceptions.ScriptAlterLimitPoolException() #Applies new healthcheck in pool #Todo - new method sp.healthcheck = hc sp.save(user) if(old_healthcheck.id != hc.id and sp.pool_created): transaction.commit() command = settings.POOL_HEALTHCHECK % (sp.id) code, _, _ = exec_script(command) if code != 0: sp.healthcheck = old_healthcheck sp.save(user) transaction.commit() raise exceptions.ScriptCreatePoolException() #Applies new lb method in pool #Todo - new method sp.lb_method = balancing sp.save(user) if(old_lb_method != sp.lb_method and sp.pool_created): transaction.commit() command = settings.POOL_MANAGEMENT_LB_METHOD % (sp.id) code, _, _ = exec_script(command) if code != 0: sp.lb_method = old_lb_method sp.save(user) transaction.commit() raise exceptions.ScriptCreatePoolException() #Applies new service-down-action in pool #Todo - new method sp.servicedownaction = servicedownaction sp.save(user) if(old_servicedownaction != sp.servicedownaction and sp.pool_created): transaction.commit() command = settings.POOL_SERVICEDOWNACTION % (sp.id) code, _, _ = exec_script(command) if code != 0: sp.servicedownaction = old_servicedownaction sp.save(user) transaction.commit() raise exceptions.ScriptAlterServiceDownActionException() else: sp = ServerPool(identifier=identifier, default_port=default_port, healthcheck=hc, environment=env, pool_created=False, lb_method=balancing, default_limit=maxconn, servicedownaction=servicedownaction) sp.save(user) return sp, (old_healthcheck.id if old_healthcheck else None)
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
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 {}
def update(request, pk): """ Update Vip Request :param request: :param pk: Identifier Vip Request :return: Data Serialized Post Update """ data = request.DATA user = request.user if not is_valid_int_greater_zero_param(pk): raise exceptions.InvalidIdVipRequestException() vip_ports = data.get('vip_ports_to_pools') req_vip_serializer = RequestVipSerializer( data=data ) if not req_vip_serializer.is_valid(): log.error(req_vip_serializer.errors) raise api_exceptions.ValidationException() # test if request exists RequisicaoVips.objects.get(pk=pk) with distributedlock(LOCK_VIP % pk): obj_req_vip = req_vip_serializer.object # compatibility issues if obj_req_vip.trafficreturn is None: obj_req_vip.trafficreturn = RequisicaoVips.objects.get( pk=pk).trafficreturn obj_req_vip.id = int(pk) obj_req_vip.filter_valid = True obj_req_vip.validado = False set_l7_filter_for_vip(obj_req_vip) obj_req_vip.set_new_variables(data) old_trafficreturn = RequisicaoVips.objects.get(pk=pk).trafficreturn if old_trafficreturn.id != obj_req_vip.trafficreturn.id: if obj_req_vip.trafficreturn.nome_opcao_txt == 'DSRL3': dsrl3_to_vip_obj = DsrL3_to_Vip() dsrl3_to_vip_obj.get_dsrl3(obj_req_vip, user) else: try: dsrl3_to_vip_obj = DsrL3_to_Vip.get_by_vip_id( obj_req_vip.id) dsrl3_to_vip_obj.delete(user) except ObjectDoesNotExist: pass obj_req_vip.save() vip_port_serializer = VipPortToPoolSerializer( data=vip_ports, many=True) if not vip_port_serializer.is_valid(): raise api_exceptions.ValidationException( 'Invalid Port Vip To Pool') vip_port_to_pool_pks = [port['id'] for port in vip_ports if port.get('id')] vip_port_to_pool_to_remove = VipPortToPool.objects.filter( requisicao_vip=obj_req_vip ).exclude( id__in=vip_port_to_pool_pks ) # valid if pools member can linked by environment/environment vip # relationship rule server_pool_ips_can_associate_with_vip_request( obj_req_vip, vip_port_to_pool_to_remove) for v_port_to_del in vip_port_to_pool_to_remove: v_port_to_del.delete() for vip_port in vip_ports: vip_port_obj = VipPortToPool() vip_port_obj.id = vip_port.get('id') vip_port_obj.server_pool = ServerPool( id=vip_port.get('server_pool')) vip_port_obj.port_vip = vip_port.get('port_vip') vip_port_obj.requisicao_vip = obj_req_vip vip_port_obj.save() # SYNC_VIP syncs.old_to_new(obj_req_vip) return req_vip_serializer.data