diff --git a/networkapi/requisicaovips/models.py b/networkapi/requisicaovips/models.py index 96f6034da..bdc620acf 100644 --- a/networkapi/requisicaovips/models.py +++ b/networkapi/requisicaovips/models.py @@ -1521,9 +1521,6 @@ def save_vips_and_ports(self, vip_map, user): raise RequestVipServerPoolConstraintError( None, 'ServerPool %s ja esta indicado como criado no equipamento nao pode ser alterado.' % server_pool.identifier) - #raise Exception( - #e, u'The pool is created and thus cannot be edited.') - vip_port_list = list() pool_member_pks_removed = list() @@ -1605,7 +1602,18 @@ def save_vips_and_ports(self, vip_map, user): if server_pools.count() == 0: server_pool = ServerPool() + + #Try to get a unique identifier for server pool server_pool.identifier = 'VIP' + str(self.id) + '_pool_' + vip_port + if ServerPool.objects.filter(identifier = server_pool.identifier): + name_not_found = 1 + retry = 2 + while name_not_found: + server_pool.identifier = 'VIP' + str(self.id) + '_pool_' + str(retry) + '_' + vip_port + retry += 1 + if not ServerPool.objects.filter(identifier = server_pool.identifier): + name_not_found=0 + server_pool.environment = environment_obj server_pool.pool_created = False @@ -1650,8 +1658,14 @@ def save_vips_and_ports(self, vip_map, user): vip_port_to_pool.delete(user) - #TODO: remove se nao estiver em outra requisicaos - + #Removing unused ServerPool + #Safe to remove because server pools are not created (tested earlier) + vip_port_to_pool = VipPortToPool.objects.filter( + server_pool=sp) + if not vip_port_to_pool: + self.log.info("Removing unused ServerPool %s %s" % (sp.id, sp.identifier) ) + sp.delete(user) + # save ServerPoolMember server_pool_member_pks = [] for i in range(0, len(reals)): @@ -1770,13 +1784,18 @@ def delete_vips_and_reals(self, user): vip_ports = VipPortToPool.objects.filter(requisicao_vip=self) server_pool_list = list() + for vip_port in vip_ports: - server_pool_members = ServerPoolMember.objects.filter( - server_pool=vip_port.server_pool) - for server_pool_member in server_pool_members: - server_pool_member.delete(user) - vip_port.delete(user) - server_pool_list.append(vip_port.server_pool) + #Only deletes pool if it is not in use in any other vip request + server_pools_still_used = VipPortToPool.objects.filter(server_pool=vip_port.server_pool).exclude(requisicao_vip=self) + + if not server_pools_still_used: + server_pool_members = ServerPoolMember.objects.filter( + server_pool=vip_port.server_pool) + for server_pool_member in server_pool_members: + server_pool_member.delete(user) + vip_port.delete(user) + server_pool_list.append(vip_port.server_pool) for server_pool in server_pool_list: server_pool.delete(user) diff --git a/networkapi/requisicaovips/resource/CreateVipResource.py b/networkapi/requisicaovips/resource/CreateVipResource.py index 7748944a0..a634f09e7 100644 --- a/networkapi/requisicaovips/resource/CreateVipResource.py +++ b/networkapi/requisicaovips/resource/CreateVipResource.py @@ -28,7 +28,7 @@ from networkapi.equipamento.models import EquipamentoError, EquipamentoNotFoundError from networkapi.settings import VIP_CREATE from networkapi.infrastructure.script_utils import exec_script, ScriptError -from networkapi.requisicaovips.models import RequisicaoVipsNotFoundError, RequisicaoVipsError, RequisicaoVips +from networkapi.requisicaovips.models import RequisicaoVipsNotFoundError, RequisicaoVipsError, RequisicaoVips, ServerPool from networkapi.healthcheckexpect.models import HealthcheckExpectError from networkapi.distributedlock import distributedlock, LOCK_VIP @@ -143,6 +143,13 @@ def __create_vip(self, vip_id, user): vip.vip_criado = 1 vip.save(user) + server_pools = ServerPool.objects.filter(vipporttopool__requisicao_vip=vip.id) + + for server_pool in server_pools: + if not server_pool.pool_created: + server_pool.pool_created = 1 + server_pool.save(user) + map = dict() map['sucesso'] = success_map diff --git a/networkapi/requisicaovips/resource/RemoveVipResource.py b/networkapi/requisicaovips/resource/RemoveVipResource.py index 5e760f051..b705868e4 100644 --- a/networkapi/requisicaovips/resource/RemoveVipResource.py +++ b/networkapi/requisicaovips/resource/RemoveVipResource.py @@ -28,7 +28,7 @@ from networkapi.equipamento.models import EquipamentoError, EquipamentoNotFoundError from networkapi.settings import VIP_REMOVE from networkapi.infrastructure.script_utils import exec_script, ScriptError -from networkapi.requisicaovips.models import RequisicaoVipsNotFoundError, RequisicaoVipsError, RequisicaoVips +from networkapi.requisicaovips.models import RequisicaoVipsNotFoundError, RequisicaoVipsError, RequisicaoVips, ServerPool, VipPortToPool from networkapi.healthcheckexpect.models import HealthcheckExpectError from networkapi.distributedlock import distributedlock, LOCK_VIP @@ -120,8 +120,24 @@ def handle_post(self, request, user, *args, **kwargs): vip.vip_criado = 0 vip.save(user) - map = dict() - map['sucesso'] = success_map + #Marks the server pool as not created if the + # server pool is not used in another already created vip request + server_pools = ServerPool.objects.filter(vipporttopool__requisicao_vip=vip.id) + + for server_pool in server_pools: + #Checks if server pool is still used in another created vip request + server_pools_still_used = VipPortToPool.objects.filter(server_pool=server_pool).exclude(requisicao_vip=vip.id) + vip_with_server_pool_is_created = 0 + for server_pool_still_used in server_pools_still_used: + if server_pool_still_used.requisicao_vip.vip_criado: + vip_with_server_pool_is_created = 1 + + if not vip_with_server_pool_is_created and server_pool.pool_created: + server_pool.pool_created = 0 + server_pool.save(user) + + map = dict() + map['sucesso'] = success_map else: return self.response_error(2, stdout + stderr)