Skip to content

Commit

Permalink
F5 plugin: Added force delete to vips and pool
Browse files Browse the repository at this point in the history
  • Loading branch information
ederson.brilhante committed Dec 28, 2017
1 parent e5c95c0 commit d072269
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 21 deletions.
28 changes: 24 additions & 4 deletions networkapi/api_pools/facade/v3/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,35 @@ def create_real_pool(pools, user):


@commit_on_success
def delete_real_pool(pools, user):
def delete_real_pool(pools, user, cleanup='0'):
"""Delete real pool in eqpt."""

cleanup = True if cleanup == '1' else False
load_balance = _prepare_apply(pools=pools, created=True, user=user)

for lb_id in load_balance:
load_balance[lb_id]['plugin'].delete_pool(load_balance[lb_id])
id_lists = list()
if cleanup:
for lb_id in load_balance:
ids_aux = load_balance[lb_id]['plugin'].delete_pool(
load_balance[lb_id], cleanup)
id_lists.append(ids_aux)

num_list = len(id_lists)
exists = True
# ids from first list
ids = list()
for id in id_lists[0]:
for i in range(1, len(id_lists)):
if id not in id_lists[i]:
exists = False
if exists:
ids.append(id)
else:
for lb_id in load_balance:
load_balance[lb_id]['plugin'].delete_pool(
load_balance[lb_id], cleanup)
ids = [pool['id'] for pool in pools]

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

return {}
Expand Down
7 changes: 5 additions & 2 deletions networkapi/api_pools/views/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def get(self, request, *args, **kwargs):

try:

locks_list = create_lock(serializer_server_pool.data, LOCK_POOL)
locks_list = create_lock(
serializer_server_pool.data, LOCK_POOL)

mbr_state = facade_pool_deploy.get_poolmember_state(
serializer_server_pool.data)
Expand Down Expand Up @@ -177,12 +178,14 @@ def delete(self, request, *args, **kwargs):
"""

pool_ids = kwargs['obj_ids'].split(';')
cleanup = request.GET.get('cleanup') or '0'

pools = facade.get_pool_by_ids(pool_ids)
pool_serializer = serializers.PoolV3Serializer(pools, many=True)
locks_list = create_lock(pool_serializer.data, LOCK_POOL)
try:
response = facade_pool_deploy.delete_real_pool(
pool_serializer.data, request.user)
pool_serializer.data, request.user, cleanup)
except Exception, exception:
log.error(exception)
raise rest_exceptions.NetworkAPIException(exception)
Expand Down
5 changes: 3 additions & 2 deletions networkapi/api_vip_request/facade/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,9 @@ def patch_real_vip_request(vip_requests, user):


@commit_on_success
def delete_real_vip_request(vip_requests, user):
def delete_real_vip_request(vip_requests, user, cleanup='0'):
load_balance = dict()
cleanup = True if cleanup == '1' else False

keys = list()
for vip in vip_requests:
Expand All @@ -633,7 +634,7 @@ def delete_real_vip_request(vip_requests, user):
for lb in load_balance:
inst = copy.deepcopy(load_balance.get(lb))
log.info('started call:%s' % lb)
pool_del = inst.get('plugin').delete_vip(inst)
pool_del = inst.get('plugin').delete_vip(inst, cleanup)
log.info('ended call')
pools_ids += pool_del

Expand Down
6 changes: 5 additions & 1 deletion networkapi/api_vip_request/views/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ def delete(self, request, *args, **kwargs):
Deletes list of vip request in equipments
:url /api/v3/vip-request/deploy/<vip_request_ids>/
:param vip_request_ids=<vip_request_ids>
cleanup = request.GET.get('cleanup', '0')
"""

vip_request_ids = kwargs['obj_ids'].split(';')
cleanup = request.GET.get('cleanup', '0')

vips = facade.get_vip_request_by_ids(vip_request_ids)
vip_serializer = VipRequestV3Serializer(
vips, many=True, include=('ports__identifier',))

locks_list = create_lock(vip_serializer.data, LOCK_VIP)
try:
response = facade.delete_real_vip_request(
vip_serializer.data, request.user)
vip_serializer.data, request.user, cleanup)
except Exception, exception:
log.error(exception)
raise api_exceptions.NetworkAPIException(exception)
Expand Down
4 changes: 2 additions & 2 deletions networkapi/plugins/Brocade/Generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def create_pool(self, pools):

@util.connection
@util.logger
def delete_pool(self, pools):
def delete_pool(self, pools, cleanup=False):

for pool in pools['pools']:

Expand Down Expand Up @@ -234,7 +234,7 @@ def update_vip(self, vips):

@util.connection
@util.logger
def delete_vip(self, vips):
def delete_vip(self, vips, cleanup=False):

pools_del = list()
for vip in vips['vips']:
Expand Down
107 changes: 97 additions & 10 deletions networkapi/plugins/F5/Generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ def get_name_eqpt(self, obj, port):

@logger
@util.connection
def delete_vip(self, vips):
def delete_vip(self, vips, cleanup=False):
tratado = util.trata_param_vip(vips)
dict_vip = {
'vips': tratado.get('vips_filter'),
'vips_cache': tratado.get('vips_cache_filter'),
'pool_created': tratado.get('pool_filter_created')
}
pools_del = self._delete_vip(dict_vip)

return pools_del
if cleanup:
self._cleanup_vip(dict_vip)
return []
else:
pools_del = self._delete_vip(dict_vip)
return pools_del

@logger
def _delete_vip(self, tratado):
Expand Down Expand Up @@ -81,6 +84,30 @@ def _delete_vip(self, tratado):

return pools_del

@logger
def _cleanup_vip(self, tratado):

pools_del = list()
vts = virtualserver.VirtualServer(self._lb)
if tratado.get('vips'):
for vip in tratado.get('vips'):
try:
vts.delete(vps_names=[vip['name']])
except:
pass

try:
if vip.get('pool_l7'):
rl = rule.Rule(self._lb)
rl.delete(rule_names='{}_RULE_L7'.format(vip['name']))
except:
pass

# CACHE
if tratado.get('vips_cache'):
for vip in tratado.get('vips_cache'):
vts.delete(vps_names=[vip['name']])

@logger
@util.connection
def create_vip(self, vips):
Expand Down Expand Up @@ -109,7 +136,7 @@ def _create_vip(self, tratado):
log.error(e)

if tratado.get('pool'):
self.__delete_pool({'pools': tratado.get('pool')})
self._delete_pool({'pools': tratado.get('pool')})
raise base_exceptions.CommandErrorException(e)

else:
Expand All @@ -131,7 +158,7 @@ def _create_vip(self, tratado):
raise base_exceptions.CommandErrorException(e)
else:
if tratado.get('pool'):
self.__delete_pool({'pools': tratado.get('pool')})
self._delete_pool({'pools': tratado.get('pool')})

raise base_exceptions.CommandErrorException(e)
return pools_ins
Expand Down Expand Up @@ -257,7 +284,7 @@ def partial_update_vip(self, vips):
@logger
def _delete_pool_by_pass(self, server_pool):
try:
self.__delete_pool({'pools': [server_pool]})
self._delete_pool({'pools': [server_pool]})
except Exception, e:
if 'cannot be deleted because it is in use by a Virtual Server' in str(e.message):
log.warning(
Expand Down Expand Up @@ -554,11 +581,15 @@ def __update_pool(self, pools):

@logger
@util.connection
def delete_pool(self, pools):
self.__delete_pool(pools)
def delete_pool(self, pools, cleanup=False):

if cleanup:
return self._cleanup_pool(pools)
else:
self._delete_pool(pools)

@logger
def __delete_pool(self, pools):
def _delete_pool(self, pools):

pls = util.trata_param_pool(pools)

Expand Down Expand Up @@ -590,3 +621,59 @@ def __delete_pool(self, pools):
mon.delete_template(template_names=template_names)
except bigsuds.OperationFailed:
pass

@logger
def _cleanup_pool(self, pools):

pls = util.trata_param_pool(pools)

pl = pool.Pool(self._lb)
mon = monitor.Monitor(self._lb)

ids = list()

for idx, pool_name in enumerate(pls['pools_names']):

try:
monitor_associations = pl.get_monitor_association(
names=[pool_name])
except:
pass

can_delete = True
try:
pl.delete(names=[pool_name])
except Exception, e:
can_delete = False
if 'cannot be deleted because it is in use by a Virtual Server' in str(e.message):
log.warning(
'Pool cannot be deleted because it is in use by a Virtual Server')
pass
elif 'is referenced by one or more virtual servers' in str(e.message):
log.warning(
'Pool cannot be deleted because it is referenced by one or more virtual servers')
pass
elif 'is referenced by one or more rules' in str(e.message):
log.warning(
'Pool cannot be deleted because is referenced by one or more rules')
pass
elif 'was not found.' in str(e.message):
log.warning('Pool already deleted')
can_delete = True
pass
else:
raise e

if can_delete:
ids.append(pools['pools'][idx]['id'])

try:
template_names = [m for m in list(
itertools.chain(
*[m['monitor_rule']['monitor_templates'] for m in monitor_associations])) if 'MONITOR' in m]
if template_names:
mon.delete_template(template_names=template_names)
except:
pass

return ids

0 comments on commit d072269

Please sign in to comment.