Skip to content

Commit

Permalink
Removing only not used server pools when removing Vip Request
Browse files Browse the repository at this point in the history
Configuring pool created flag when creating or deleting a vip request
Choose unique name for server pool when editing vip request
Removing not used server pool when editing vip request
  • Loading branch information
Marcus Vinicius G. Cesario committed May 7, 2015
1 parent e9be5a0 commit cbfd24b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
41 changes: 30 additions & 11 deletions networkapi/requisicaovips/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion networkapi/requisicaovips/resource/CreateVipResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions networkapi/requisicaovips/resource/RemoveVipResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cbfd24b

Please sign in to comment.