diff --git a/reana_workflow_controller/config.py b/reana_workflow_controller/config.py index 6018fd49..c2405362 100644 --- a/reana_workflow_controller/config.py +++ b/reana_workflow_controller/config.py @@ -26,6 +26,22 @@ def _env_vars_dict_to_k8s_list(env_vars): return [{"name": name, "value": str(value)} for name, value in env_vars.items()] +def compose_reana_url(hostname: str, hostport: int) -> str: + """ + Compose a REANA API URL while omitting the default HTTPS port (443). + + Args: + hostname (str): The REANA hostname. + hostport (int): The REANA host port. + + Returns: + str: The full base URL. + """ + if hostport == 443: + return f"https://{hostname}" + return f"https://{hostname}:{hostport}" + + SECRET_KEY = os.getenv("REANA_SECRET_KEY", "CHANGE_ME") """Secret key used for the application user sessions.""" @@ -277,8 +293,14 @@ def _parse_interactive_sessions_environments(env_var): REANA_GITLAB_URL = "https://{}".format(REANA_GITLAB_HOST) """GitLab API URL""" -REANA_HOSTNAME = os.getenv("REANA_HOSTNAME", "CHANGE_ME") -"""REANA URL""" +REANA_HOSTNAME = os.getenv("REANA_HOSTNAME", "localhost") +"""REANA host name.""" + +REANA_HOSTPORT = os.getenv("REANA_HOSTPORT", "30443") +"""REANA host name port number.""" + +REANA_URL = compose_reana_url(REANA_HOSTNAME, REANA_HOSTPORT) +"""REANA URL.""" REANA_INGRESS_ANNOTATIONS = json.loads(os.getenv("REANA_INGRESS_ANNOTATIONS", "{}")) """REANA Ingress annotations defined by the administrator.""" diff --git a/reana_workflow_controller/consumer.py b/reana_workflow_controller/consumer.py index ceeb5b5d..c871934e 100644 --- a/reana_workflow_controller/consumer.py +++ b/reana_workflow_controller/consumer.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of REANA. -# Copyright (C) 2018, 2019, 2020, 2021, 2022, 2023, 2024 CERN. +# Copyright (C) 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025 CERN. # # REANA is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -40,7 +40,7 @@ ALIVE_STATUSES, PROGRESS_STATUSES, REANA_GITLAB_URL, - REANA_HOSTNAME, + REANA_URL, REANA_JOB_STATUS_CONSUMER_PREFETCH_COUNT, ) from reana_workflow_controller.errors import REANAWorkflowControllerError @@ -196,7 +196,7 @@ def _update_commit_status(workflow, status): return gitlab_access_token = gitlab_access_token_secret.value_str - target_url = f"https://{REANA_HOSTNAME}/api/workflows/{workflow.id_}/logs" + target_url = f"{REANA_URL}/api/workflows/{workflow.id_}/logs" workflow_name = urlparse.quote_plus(workflow.git_repo) system_name = "reana" commit_status_url = ( diff --git a/reana_workflow_controller/rest/utils.py b/reana_workflow_controller/rest/utils.py index d457a4d4..b7377489 100644 --- a/reana_workflow_controller/rest/utils.py +++ b/reana_workflow_controller/rest/utils.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of REANA. -# Copyright (C) 2020, 2021, 2022, 2023, 2024 CERN. +# Copyright (C) 2020, 2021, 2022, 2023, 2024, 2025 CERN. # # REANA is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. diff --git a/reana_workflow_controller/rest/workflows.py b/reana_workflow_controller/rest/workflows.py index 575a308c..f0739dd0 100644 --- a/reana_workflow_controller/rest/workflows.py +++ b/reana_workflow_controller/rest/workflows.py @@ -41,7 +41,7 @@ get_default_quota_resource, ) from reana_workflow_controller.config import ( - REANA_HOSTNAME, + REANA_URL, DEFAULT_NAME_FOR_WORKFLOWS, MAX_WORKFLOW_SHARING_MESSAGE_LENGTH, ) @@ -631,7 +631,7 @@ def create_workflow(): # noqa if requires_dask(workflow): dask_service = Service( name=get_dask_component_name(workflow.id_, "database_model_service"), - uri=f"https://{REANA_HOSTNAME}/{workflow_uuid}/dashboard/status", + uri=f"{REANA_URL}/{workflow_uuid}/dashboard/status", type_=ServiceType.dask, status=ServiceStatus.created, ) diff --git a/tests/test_utils.py b/tests/test_utils.py index 95c78da4..c281c93a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of REANA. -# Copyright (C) 2018, 2019, 2020, 2021, 2022, 2023 CERN. +# Copyright (C) 2018, 2019, 2020, 2021, 2022, 2023, 2025 CERN. # # REANA is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -31,6 +31,7 @@ update_users_disk_quota, ) from reana_workflow_controller.errors import REANAWorkflowControllerError +from reana_workflow_controller.config import compose_reana_url from reana_workflow_controller.rest.utils import ( create_workflow_workspace, delete_workflow, @@ -343,3 +344,21 @@ def test_mv_files( assert target_path.exists() if source_content: assert target_path.read_text() == source_content + + +# Note that this function is inside config.py file and not in +# `rest/utils.py file due to circular import issues.` However, +# it is a utility function so it does not hurt to test it in +# this file. +@pytest.mark.parametrize( + "hostname, hostport, expected_url", + [ + ("reana.cern.ch", 443, "https://reana.cern.ch"), + ("reana.cern.ch", 30443, "https://reana.cern.ch:30443"), + ("example.com", 443, "https://example.com"), + ("example.com", 8080, "https://example.com:8080"), + ], +) +def test_compose_reana_url(hostname, hostport, expected_url): + """Test composing base reana url.""" + assert compose_reana_url(hostname, hostport) == expected_url