From 306ef439ca6691edec5fc41968d6e7a48b223aaa Mon Sep 17 00:00:00 2001 From: Nicolas Marcq Date: Wed, 20 Nov 2024 15:48:18 +0100 Subject: [PATCH] Fix: Credential id set in operation was not sent to AWX Closes #790 --- service_catalog/models/request.py | 19 +++++++++ .../test_models/test_request.py | 39 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/service_catalog/models/request.py b/service_catalog/models/request.py index 5b090d7b4..1f0b508b8 100644 --- a/service_catalog/models/request.py +++ b/service_catalog/models/request.py @@ -233,6 +233,25 @@ def perform_processing(self, inventory_override=None, credentials_override=None, "squest_host": settings.SQUEST_HOST, "request": AdminRequestSerializer(self).data } + + # load default override from the operation + if inventory_override is None: + inventory_override = self.operation.default_inventory_id + if credentials_override is None: + credentials_override = self.operation.default_credentials_ids.split(",") + if tags_override is None: + tags_override = self.operation.default_tags.split(",") + if skip_tags_override is None: + skip_tags_override = self.operation.default_skip_tags.split(",") + if limit_override is None: + limit_override = self.operation.default_limits.split(",") + if verbosity_override is None: + verbosity_override = self.operation.default_verbosity + if job_type_override is None: + job_type_override = self.operation.default_job_type + if diff_mode_override is None: + diff_mode_override = self.operation.default_diff_mode + tower_job_id = None try: tower_job_id, error_message = self.operation.job_template.execute(extra_vars=tower_extra_vars, diff --git a/tests/test_service_catalog/test_models/test_request.py b/tests/test_service_catalog/test_models/test_request.py index b3dbaaa38..2f80e7280 100644 --- a/tests/test_service_catalog/test_models/test_request.py +++ b/tests/test_service_catalog/test_models/test_request.py @@ -537,3 +537,42 @@ def test_date_submitted_not_update_on_save(self): old_submitted_date = new_request.date_submitted new_request.save() self.assertEqual(old_submitted_date, new_request.date_submitted) + + def setup_default_on_operation(self): + self.create_operation_test.default_inventory_id = 1 + self.create_operation_test.default_limits = "machine_one,machine_two" + self.create_operation_test.default_tags = "tag1,tag2" + self.create_operation_test.default_skip_tags = "skipped_tag1,skipped_tag2" + self.create_operation_test.default_credentials_ids = "4,5" + self.create_operation_test.default_verbosity = "3" + self.create_operation_test.default_diff_mode = "new_diff" + self.create_operation_test.default_job_type = "new_job" + self.create_operation_test.save() + + def test_perform_processing_uses_default_from_operation(self): + self.create_operation_test.auto_process = True + self.setup_default_on_operation() + + with mock.patch("service_catalog.models.job_templates.JobTemplate.execute") as mock_job_execute: + mock_job_execute.return_value = 10, "" + self.test_request.accept(user=self.superuser) + args, called_args = mock_job_execute.call_args + self.assertEqual(called_args["inventory_override"],1) + self.assertEqual(called_args["credentials_override"],['4','5']) + self.assertEqual(called_args["tags_override"],["tag1", "tag2"]) + self.assertEqual(called_args["skip_tags_override"],["skipped_tag1","skipped_tag2"]) + self.assertEqual(called_args["limit_override"],["machine_one", "machine_two"]) + self.assertEqual(called_args["verbosity_override"],"3") + self.assertEqual(called_args["job_type_override"],"new_job") + self.assertEqual(called_args["diff_mode_override"],"new_diff") + + def test_perform_processing_uses_default_from_operation_override_on_accept(self): + # in this test we've set default credentials on the operation, but we override them on the process request page + self.setup_default_on_operation() + self.test_request.state = RequestState.PROCESSING + self.test_request.save() + with mock.patch("service_catalog.models.job_templates.JobTemplate.execute") as mock_job_execute: + mock_job_execute.return_value = 10, "" + self.test_request.perform_processing(inventory_override=3) + args, called_args = mock_job_execute.call_args + self.assertEqual(called_args["inventory_override"], 3)