Skip to content

Commit

Permalink
fixed the dynaconf transalation for ipv4 to ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarkhatavkar committed Oct 10, 2024
1 parent b56eacd commit 2f22d03
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
2 changes: 1 addition & 1 deletion conf/dynaconf_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def post(settings):
)
data = get_repos_config(settings)
write_cache(settings_cache_path, data)
ipv6_hostname_translation(settings, data)
ipv6_hostname_translation(settings)
config_migrations(settings, data)
data['dynaconf_merge'] = True
return data
Expand Down
28 changes: 28 additions & 0 deletions robottelo/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@
os.environ['ROBOTTELO_DIR'] = str(robottelo_root_dir)


# Define the function to replace 'ipv4' with 'ipv6' in URLs
def replace_ipv4_with_ipv6_in_urls(settings):
def replace_in_value(value):
# Only replace 'ipv4' with 'ipv6' in strings that contain 'redhat.com'
if isinstance(value, str) and 'ipv4' in value and 'redhat.com' in value:
return value.replace('ipv4', 'ipv6')
return value

def resolve_and_replace(settings_dict):
resolved = {}
for key, value in settings_dict.items():
if isinstance(value, str):
resolved[key] = replace_in_value(value)
elif isinstance(value, dict):
# Recursively handle nested dictionaries
resolved[key] = resolve_and_replace(value)
else:
resolved[key] = value
return resolved

# Update the settings in-place with resolved and replaced values
updated_settings = resolve_and_replace(settings.as_dict())
for key, value in updated_settings.items():
settings.set(key, value)


def get_settings():
"""Return Lazy settings object after validating
Expand Down Expand Up @@ -49,6 +75,8 @@ def get_settings():


settings = get_settings()
if settings.server.is_ipv6:
replace_ipv4_with_ipv6_in_urls(settings)
robottelo_tmp_dir = Path(settings.robottelo.tmp_dir)
robottelo_tmp_dir.mkdir(parents=True, exist_ok=True)

Expand Down
56 changes: 28 additions & 28 deletions robottelo/utils/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@ def is_ipv4_url(text):
return isinstance(text, str) and 'ipv4' in text and 'redhat.com' in text


def ipv6_translator(settings_list, setting_major, data):
def ipv6_translator(settings):
"""Translates the hostname containing IPv4 to IPv6 and updates the settings object"""
setting_major = list(map(str, setting_major))
dotted_settings = '.'.join(setting_major)
for _key, _val in settings_list.items():
if is_ipv4_url(_val):
data[f'{dotted_settings}.{_key}'] = str(_val).replace('ipv4', 'ipv6')
logger.debug(f'Setting translated to IPv6, Path: {dotted_settings}.{_key}')
elif isinstance(_val, list):
updated = False
new_list = _val
for i in range(len(new_list)):
if is_ipv4_url(new_list[i]):
new_list[i] = new_list[i].replace('ipv4', 'ipv6')
updated = True
if updated:
data[f'{dotted_settings}.{_key}'] = new_list
logger.debug(f'Setting translated to IPv6, Path: {dotted_settings}.{_key}')
elif isinstance(_val, dict):
new_setting_major = setting_major + [_key]
ipv6_translator(settings_list=_val, setting_major=new_setting_major, data=data)


def ipv6_hostname_translation(settings, data):

def replace_in_value(value):
# Only replace 'ipv4' with 'ipv6' in strings that contain 'redhat.com'
if isinstance(value, str) and 'ipv4' in value and 'redhat.com' in value:
return value.replace('ipv4', 'ipv6')
return value

def resolve_and_replace(settings_dict):
resolved = {}
for key, value in settings_dict.items():
if isinstance(value, str):
resolved[key] = replace_in_value(value)
elif isinstance(value, dict):
# Recursively handle nested dictionaries
resolved[key] = resolve_and_replace(value)
else:
resolved[key] = value
return resolved

# Update the settings in-place with resolved and replaced values
updated_settings = resolve_and_replace(settings.as_dict())
for key, value in updated_settings.items():
settings.set(key, value)


def ipv6_hostname_translation(settings):
"""Migrates any IPv4 containing hostname in conf to IPv6 hostname"""
settings_path = []
if settings.server.is_ipv6:
all_settings = settings.loaded_by_loaders.items()
for loader_name, loader_settings in tuple(all_settings):
if loader_name.loader == 'yaml':
ipv6_translator(loader_settings, settings_path, data)
ipv6_translator(settings)
else:
logger.debug('IPv6 Hostname dynaconf migration hook is skipped for IPv4 testing')

0 comments on commit 2f22d03

Please sign in to comment.