From 2bae5c761aa0f02b3d16763da68be824e84635a1 Mon Sep 17 00:00:00 2001 From: Hui Song Date: Tue, 15 Oct 2024 13:36:34 -0400 Subject: [PATCH] fix: update help_text for AAP credential type (#1100) Co-authored-by: Alex --- .../commands/create_initial_data.py | 29 +++-- .../0051_update_credential_type_help_text.py | 40 ++++++ .../migrations/test_update_credential_type.py | 116 ++++++++++++++++++ 3 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 src/aap_eda/core/migrations/0051_update_credential_type_help_text.py create mode 100644 tests/unit/migrations/test_update_credential_type.py diff --git a/src/aap_eda/core/management/commands/create_initial_data.py b/src/aap_eda/core/management/commands/create_initial_data.py index 359ec2193..9b56b8c7e 100644 --- a/src/aap_eda/core/management/commands/create_initial_data.py +++ b/src/aap_eda/core/management/commands/create_initial_data.py @@ -28,6 +28,16 @@ from aap_eda.core.tasking import enable_redis_prefix from aap_eda.core.utils.credentials import inputs_to_store +NEW_HELP_TEXT = ( + "Red Hat Ansible Automation Platform base URL to authenticate with.", + "For Event-Driven Ansible controller 2.5 with Ansible Automation Controller 2.4, use the following example: https://<>", # noqa E501 + "For Ansible Automation Controller 2.5, use the following example: https://<>/api/controller/", # noqa E501 +) + +ORIG_HELP_TEXT = ( + "Red Hat Ansible Automation Platform base URL to authenticate with." +) + CRUD = ["add", "view", "change", "delete"] LOGGER = logging.getLogger(__name__) AVAILABLE_ALGORITHMS = sorted(hashlib.algorithms_available) @@ -251,10 +261,7 @@ "id": "host", "label": "Red Hat Ansible Automation Platform", "type": "string", - "help_text": ( - "Red Hat Ansible Automation Platform base URL" - " to authenticate with." - ), + "help_text": NEW_HELP_TEXT, }, { "id": "username", @@ -1018,12 +1025,14 @@ def populate_credential_types( for credential_type_data in credential_types: new_type, created = models.CredentialType.objects.get_or_create( name=credential_type_data["name"], - description=credential_type_data.get("description", ""), - namespace=credential_type_data.get("namespace"), - kind=credential_type_data.get("kind", "cloud"), - inputs=credential_type_data.get("inputs", {}), - injectors=credential_type_data.get("injectors", {}), - managed=credential_type_data.get("managed", True), + defaults={ + "description": credential_type_data.get("description", ""), + "namespace": credential_type_data.get("namespace"), + "kind": credential_type_data.get("kind", "cloud"), + "inputs": credential_type_data.get("inputs", {}), + "injectors": credential_type_data.get("injectors", {}), + "managed": credential_type_data.get("managed", True), + }, ) if created: created_credential_types.append(new_type) diff --git a/src/aap_eda/core/migrations/0051_update_credential_type_help_text.py b/src/aap_eda/core/migrations/0051_update_credential_type_help_text.py new file mode 100644 index 000000000..0cc228210 --- /dev/null +++ b/src/aap_eda/core/migrations/0051_update_credential_type_help_text.py @@ -0,0 +1,40 @@ +# Generated by Django 4.2.7 on 2024-10-11 14:49 + +from django.db import migrations + +from aap_eda.core import enums +from aap_eda.core.management.commands.create_initial_data import ( + NEW_HELP_TEXT, + ORIG_HELP_TEXT, +) + + +def update_credential_type(apps, schema_editor): + _update_credential_type_help_text(apps, NEW_HELP_TEXT) + + +def backward_credential_type(apps, schema_editor): + _update_credential_type_help_text(apps, ORIG_HELP_TEXT) + + +def _update_credential_type_help_text(apps, help_text): + credential_type = apps.get_model("core", "CredentialType") # noqa: N806 + + for ctype in credential_type.objects.filter( + name=enums.DefaultCredentialType.AAP + ): + for field in ctype.inputs["fields"]: + if field["id"] == "host": + field["help_text"] = help_text + + ctype.save(update_fields=["inputs"]) + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0050_setting"), + ] + + operations = [ + migrations.RunPython(update_credential_type, backward_credential_type), + ] diff --git a/tests/unit/migrations/test_update_credential_type.py b/tests/unit/migrations/test_update_credential_type.py new file mode 100644 index 000000000..4362d54f9 --- /dev/null +++ b/tests/unit/migrations/test_update_credential_type.py @@ -0,0 +1,116 @@ +# Copyright 2024 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from typing import List, Union + +import pytest +from django.core.management import call_command + +from aap_eda.core import enums, models +from aap_eda.core.management.commands.create_initial_data import ( + NEW_HELP_TEXT, + ORIG_HELP_TEXT, +) + +INPUTS = { + "fields": [ + { + "id": "host", + "label": "Red Hat Ansible Automation Platform", + "type": "string", + "help_text": ORIG_HELP_TEXT, + }, + { + "id": "username", + "label": "Username", + "type": "string", + "help_text": ( + "Red Hat Ansible Automation Platform username id" + " to authenticate as.This should not be set if" + " an OAuth token is being used." + ), + }, + { + "id": "password", + "label": "Password", + "type": "string", + "secret": True, + }, + { + "id": "oauth_token", + "label": "OAuth Token", + "type": "string", + "secret": True, + "help_text": ( + "An OAuth token to use to authenticate with." + "This should not be set if username/password" + " are being used." + ), + }, + { + "id": "verify_ssl", + "label": "Verify SSL", + "type": "boolean", + "secret": False, + }, + ], + "required": ["host"], +} + +INJECTORS = { + "env": { + "TOWER_HOST": "{{host}}", + "TOWER_USERNAME": "{{username}}", + "TOWER_PASSWORD": "{{password}}", + "TOWER_VERIFY_SSL": "{{verify_ssl}}", + "TOWER_OAUTH_TOKEN": "{{oauth_token}}", + "CONTROLLER_HOST": "{{host}}", + "CONTROLLER_USERNAME": "{{username}}", + "CONTROLLER_PASSWORD": "{{password}}", + "CONTROLLER_VERIFY_SSL": "{{verify_ssl}}", + "CONTROLLER_OAUTH_TOKEN": "{{oauth_token}}", + } +} + + +@pytest.fixture +def rollback_migration(): + call_command("migrate", "core", "0050") + yield + call_command("migrate") + + +@pytest.mark.django_db +def test_migration(rollback_migration): + credential_type = _prepare_aap_credetial_type() + + call_command("migrate", "core", "0051") + credential_type.refresh_from_db() + assert _get_help_text(credential_type) == list(NEW_HELP_TEXT) + + +def _get_help_text( + credential_type: models.CredentialType, +) -> Union[str, List[str]]: + for field in credential_type.inputs["fields"]: + if field["id"] == "host": + return field["help_text"] + + +def _prepare_aap_credetial_type() -> models.CredentialType: + return models.CredentialType.objects.create( + name=enums.DefaultCredentialType.AAP, + inputs=INPUTS, + injectors=INJECTORS, + )