Skip to content

Commit

Permalink
enable or disable snat
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklaus-xie committed Feb 25, 2021
1 parent 6b5a22f commit 87b319c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
59 changes: 58 additions & 1 deletion f5_openstack_agent/lbaasv2/drivers/bigip/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ def _update_payload(self, old_listener, listener, service, **kwargs):
if old_listener['default_pool_id'] != listener['default_pool_id']:
payload['persist'] = create_payload['persist']

snat_enable_or_not = self._check_snat_changed(old_listener, listener)

LOG.debug('the create_payload:')
LOG.debug(create_payload)
if snat_enable_or_not == 'enable':
LOG.debug('inside enable')
payload['sourceAddressTranslation'] = {}
if "sourceAddressTranslation" in create_payload and \
create_payload['sourceAddressTranslation'].get('type'):
payload['sourceAddressTranslation']['type'] = \
create_payload['sourceAddressTranslation'].get('type')

if "sourceAddressTranslation" in create_payload and \
create_payload['sourceAddressTranslation'].get('pool'):
payload['sourceAddressTranslation']['pool'] = \
create_payload['sourceAddressTranslation'].get('pool')
elif snat_enable_or_not == 'disable':
LOG.debug('inside disable')
payload['sourceAddressTranslation'] = {}
payload['sourceAddressTranslation']['type'] = None
payload['sourceAddressTranslation']['pool'] = None

return super(ListenerManager, self)._update_payload(
old_listener, listener, service,
payload=payload, create_payload=create_payload
Expand Down Expand Up @@ -375,11 +397,46 @@ def _check_customized_changed(self, old_listener, listener):
return True
return False

def _check_snat_changed(self, old_listener, listener):
if not old_listener:
return ''

if 'customized' in listener and listener['customized']:
try:
new_customized_json = json.loads(listener['customized'])
new_enable_snat = new_customized_json.get('enable_snat')
except ValueError:
LOG.error('Invalid json.')
LOG.error('Due to the error, not handle snat at all.')
return ''

# might want to modify and compare with old one
# if 'customized' in old_listener and old_listener['customized']:
# try:
# old_customized_json = json.loads(old_listener['customized'])
# old_enable_snat = old_customized_json.get('enable_snat')
# except ValueError:
# LOG.error('Invalid json.')
# LOG.error('Due to the error, not handle snat at all.')

# might want to test agaist other cases as well, e.g '', str, etc
if new_enable_snat == 'True':
LOG.info('enable snat')
return 'enable'
elif new_enable_snat == 'False':
LOG.info('disable snat')
return 'disable'
else:
LOG.info('seems nothing at all')
return ''

def _check_update_needed(self, payload, old_listener, listener):
if not payload or len(payload.keys()) == 0:
if self._check_customized_changed(old_listener, listener) \
is False and \
self._check_tls_changed(old_listener, listener) is False:
self._check_tls_changed(old_listener, listener) is False and \
not self._check_snat_changed(old_listener, listener):
LOG.info('seems not needed to update')
return False
return True

Expand Down
19 changes: 19 additions & 0 deletions f5_openstack_agent/lbaasv2/drivers/bigip/service_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

import ast
import json

import hashlib
import netaddr
Expand Down Expand Up @@ -116,6 +117,24 @@ def get_virtual(self, service):

listener["use_snat"] = self.snat_mode() and not listener.get(
"transparent")

if 'customized' in listener and listener['customized']:
enable_snat_arg = None
try:
cust = json.loads(listener['customized'])
enable_snat_arg = cust.get('enable_snat')
LOG.info(enable_snat_arg)
except ValueError:
LOG.error("Skipped. Invalid json: %s", listener['customized'])

# better to do more checks agaist value, 0,1,true,false,str,etc
if enable_snat_arg == 'True':
listener["use_snat"] = True
elif enable_snat_arg == 'False':
listener["use_snat"] = False
else:
LOG.debug('nothing, skip.')

if listener["use_snat"] and self.snat_count() > 0:
listener["snat_pool_name"] = self.get_folder_name(
loadbalancer["tenant_id"])
Expand Down

0 comments on commit 87b319c

Please sign in to comment.