From 3c3493effb06090c23d227f07751914f33ad7b20 Mon Sep 17 00:00:00 2001 From: Jennifer-John Date: Tue, 10 Oct 2023 03:26:08 +0530 Subject: [PATCH] changes --- .../modules/idrac_server_config_profile.py | 39 ++++++++++++------- .../test_idrac_server_config_profile.py | 12 +++--- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/plugins/modules/idrac_server_config_profile.py b/plugins/modules/idrac_server_config_profile.py index 9b18484d4..a5b84987e 100644 --- a/plugins/modules/idrac_server_config_profile.py +++ b/plugins/modules/idrac_server_config_profile.py @@ -512,6 +512,7 @@ ''' import os +import logging import json import copy from datetime import datetime @@ -525,6 +526,7 @@ from ansible.module_utils.six.moves.urllib.parse import urlparse +logging.basicConfig(filename='example.log', level=logging.INFO) REDFISH_SCP_BASE_URI = "/redfish/v1/Managers/iDRAC.Embedded.1" CHANGES_FOUND = "Changes found to be applied." NO_CHANGES_FOUND = "No changes found to be applied." @@ -655,8 +657,7 @@ def run_export_import_scp_http(idrac, module): job_wait=module.params["job_wait"], share=share, include_in_export=include_in_export) scp_response = response_format_change(scp_response, module.params, scp_file_name_format) - if isinstance(scp_response, dict) and scp_response.get("TaskStatus") == "Critical": - module.fail_json(msg=FAIL_MSG.format(command), scp_status=scp_response) + exit_on_failure(module, scp_response, command) return scp_response @@ -703,10 +704,12 @@ def export_scp_redfish(module, idrac): scp_components = ",".join(module.params["scp_components"]) include_in_export = IN_EXPORTS[module.params["include_in_export"]] if share["share_type"] == "LOCAL": + logging.info("11") scp_response = idrac.export_scp(export_format=module.params["export_format"], export_use=module.params["export_use"], target=scp_components, include_in_export=include_in_export, job_wait=False, share=share, ) + logging.info("12") scp_response = wait_for_response(scp_response, module, share, idrac) else: scp_response = idrac.export_scp(export_format=module.params["export_format"], @@ -714,8 +717,7 @@ def export_scp_redfish(module, idrac): target=scp_components, include_in_export=include_in_export, job_wait=module.params["job_wait"], share=share, ) scp_response = response_format_change(scp_response, module.params, scp_file_name_format) - if isinstance(scp_response, dict) and scp_response.get("TaskStatus") == "Critical": - module.fail_json(msg=FAIL_MSG.format(command), scp_status=scp_response) + exit_on_failure(module, scp_response, command) return scp_response @@ -760,11 +762,16 @@ def preview_scp_redfish(module, idrac, http_share, import_job_wait=False): else: scp_response = idrac.import_preview(import_buffer=import_buffer, target=scp_targets, job_wait=job_wait_option) scp_response = response_format_change(scp_response, module.params, share.get("file_name")) - if isinstance(scp_response, dict) and scp_response.get("TaskStatus") == "Critical": - module.fail_json(msg=FAIL_MSG.format(command), scp_status=scp_response) + exit_on_failure(module, scp_response, command) return scp_response +def exit_on_failure(module, scp_response, command): + if isinstance(scp_response, dict) and (scp_response.get("TaskStatus") == "Critical" or + scp_response.get("JobState") == "Failed"): + module.fail_json(msg=FAIL_MSG.format(command), scp_status=scp_response) + + def get_buffer_text(module, share): buffer_text = None if share["share_type"] == "LOCAL": @@ -799,8 +806,7 @@ def import_scp_redfish(module, idrac, http_share): else: scp_response = idrac.import_scp(import_buffer=import_buffer, target=scp_targets, job_wait=module.params["job_wait"]) scp_response = response_format_change(scp_response, module.params, share.get("file_name")) - if isinstance(scp_response, dict) and scp_response.get("TaskStatus") == "Critical": - module.fail_json(msg=FAIL_MSG.format(command), scp_status=scp_response) + exit_on_failure(module, scp_response, command) return scp_response @@ -817,14 +823,14 @@ def idrac_import_scp_share(module, idrac, job_params): def validate_input(module, scp_components): - if len(scp_components) != 1 and "ALL" in scp_components: + if len(scp_components) != 1 and "ALL" in scp_components: module.fail_json(msg=SCP_ALL_ERR_MSG) - if module.params["command"] in ["import", "preview"]: - if module.params.get("import_buffer") is not None: - if module.params.get("scp_file") is not None: - module.fail_json(msg=MUTUALLY_EXCLUSIVE.format("scp_file")) - if module.params.get("share_name") is not None: - module.fail_json(msg=MUTUALLY_EXCLUSIVE.format("share_name")) + if module.params["command"] in ["import", "preview"]: + if module.params.get("import_buffer") is not None: + if module.params.get("scp_file") is not None: + module.fail_json(msg=MUTUALLY_EXCLUSIVE.format("scp_file")) + if module.params.get("share_name") is not None: + module.fail_json(msg=MUTUALLY_EXCLUSIVE.format("share_name")) class ImportCommand(): @@ -892,12 +898,15 @@ def main(): http_share = False if module.params.get("share_name") is not None: http_share = module.params["share_name"].lower().startswith(('http://', 'https://')) + logging.info("0") with iDRACRedfishAPI(module.params) as idrac: + logging.info("1") command = module.params['command'] if command == 'import': command_obj = ImportCommand(idrac, http_share, module) elif command == 'export': command_obj = ExportCommand(idrac, http_share, module) + logging.info("1") else: command_obj = PreviewCommand(idrac, http_share, module) scp_status, changed = command_obj.execute() diff --git a/tests/unit/plugins/modules/test_idrac_server_config_profile.py b/tests/unit/plugins/modules/test_idrac_server_config_profile.py index 24d43614c..e7eba8608 100644 --- a/tests/unit/plugins/modules/test_idrac_server_config_profile.py +++ b/tests/unit/plugins/modules/test_idrac_server_config_profile.py @@ -81,7 +81,7 @@ def test_run_export_scp(self, params, idrac_scp_redfish_mock, idrac_default_args "share_password": "sharepswd", "job_wait": False, "scp_components": "IDRAC", "scp_file": "scp_file1.xml", "end_host_power_state": "On", "shutdown_type": "Graceful"}}, - {"message": SUCCESS_MSG.format("import"), "success": True, + {"message": SUCCESS_MSG.format("import"), "json_data": {"Id": "JID_932024672685", "Message": NO_CHANGES_FOUND, "MessageId": "SYS043", "PercentComplete": 100, "file": "http://{SCP SHARE PATH}/{SCP FILE NAME}.json"}, "mparams": {"share_name": "/share", "share_user": "sharename", @@ -163,26 +163,26 @@ def test_import_scp_http_throws_exception(self, idrac_scp_redfish_mock, idrac_de assert result['failed'] @pytest.mark.parametrize("params", [ - {"message": "Invalid file path provided.", "success": False, + {"message": "Invalid file path provided.", "mparams": {"share_name": "/share/", "share_user": "sharename", "share_password": "sharepswd", "command": "import", "job_wait": False, "scp_components": "IDRAC", "scp_file": "scp_file3.xml", "end_host_power_state": "On", "shutdown_type": "Graceful"}}, - {"message": "proxy_support is enabled but all of the following are missing: proxy_server", "success": False, + {"message": "proxy_support is enabled but all of the following are missing: proxy_server", "mparams": {"share_name": "http://10.10.10.10/myshare/", "proxy_type": "http", "proxy_support": True, "job_wait": True, "scp_components": "IDRAC", "proxy_port": 80, "export_format": "JSON", "proxy_username": "proxy_username", "proxy_password": "proxy_password"}}, - {"message": "import_buffer is mutually exclusive with share_name", "success": False, + {"message": "import_buffer is mutually exclusive with share_name", "mparams": {"share_name": "192.168.0.1:/nfsshare", "command": "preview", "job_wait": False, "import_buffer": " \ Disabled"}}, - {"message": "import_buffer is mutually exclusive with scp_file", "success": False, + {"message": "import_buffer is mutually exclusive with scp_file", "mparams": {"scp_file": "example.json", "job_wait": False, "command": "import", "import_buffer": " \ Disabled"}}, - {"message": "The option ALL cannot be used with options IDRAC, BIOS, NIC, or RAID.", "success": False, + {"message": "The option ALL cannot be used with options IDRAC, BIOS, NIC, or RAID.", "mparams": {"share_name": "http://100.96.20.175/myshare/", "share_user": "sharename", "share_password": "sharepswd", "command": "import", "job_wait": True, "scp_components": ["IDRAC", "ALL"],