From 2f22d03746f0f3423047fb49b2ad2ae722760dc6 Mon Sep 17 00:00:00 2001 From: omkarkhatavkar Date: Thu, 10 Oct 2024 13:02:53 +0530 Subject: [PATCH] fixed the dynaconf transalation for ipv4 to ipv6 --- conf/dynaconf_hooks.py | 2 +- robottelo/config/__init__.py | 28 ++++++++++++++++++ robottelo/utils/url.py | 56 ++++++++++++++++++------------------ 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/conf/dynaconf_hooks.py b/conf/dynaconf_hooks.py index 03807f5a211..8f7249b4aac 100644 --- a/conf/dynaconf_hooks.py +++ b/conf/dynaconf_hooks.py @@ -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 diff --git a/robottelo/config/__init__.py b/robottelo/config/__init__.py index 13769cdef2a..10a7d54e0f5 100644 --- a/robottelo/config/__init__.py +++ b/robottelo/config/__init__.py @@ -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 @@ -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) diff --git a/robottelo/utils/url.py b/robottelo/utils/url.py index b60fc089e6e..df692dfd53d 100644 --- a/robottelo/utils/url.py +++ b/robottelo/utils/url.py @@ -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')