From 873cab237170555f428f8312fccfba417b3ccb45 Mon Sep 17 00:00:00 2001 From: Allen Robel Date: Tue, 13 Feb 2024 11:07:45 -1000 Subject: [PATCH] Fix pylint issues, more... ImagePolicyCreateCommon(): set self.response_current and self.result_current directly from dcnm_send() and _handle_response() respectively. --- plugins/module_utils/image_policy/create.py | 31 ++++++++++++------- .../test_image_policy_create.py | 5 ++- .../test_image_policy_create_bulk.py | 5 +-- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/plugins/module_utils/image_policy/create.py b/plugins/module_utils/image_policy/create.py index 355f133ce..0771b69ef 100644 --- a/plugins/module_utils/image_policy/create.py +++ b/plugins/module_utils/image_policy/create.py @@ -133,18 +133,20 @@ def _send_payloads(self): self.result_nok = [] self.diff_nok = [] for payload in self._payloads_to_commit: - response = dcnm_send( + self.response_current = dcnm_send( self.ansible_module, self.verb, self.path, data=json.dumps(payload) ) - result = self._handle_response(response, self.verb) + self.result_current = self._handle_response( + self.response_current, self.verb + ) - if result["success"]: - self.response_ok.append(response) - self.result_ok.append(result) + if self.result_current["success"]: + self.response_ok.append(self.response_current) + self.result_ok.append(self.result_current) self.diff_ok.append(payload) else: - self.response_nok.append(response) - self.result_nok.append(result) + self.response_nok.append(self.response_current) + self.result_nok.append(self.result_current) self.diff_nok.append(payload) msg = f"self.response_ok: {json.dumps(self.response_ok, indent=4, sort_keys=True)}" @@ -157,7 +159,9 @@ def _send_payloads(self): self.log.debug(msg) msg = f"self.result_nok: {json.dumps(self.result_nok, indent=4, sort_keys=True)}" self.log.debug(msg) - msg = f"self.diff_nok: {json.dumps(self.diff_nok, indent=4, sort_keys=True)}" + msg = ( + f"self.diff_nok: {json.dumps(self.diff_nok, indent=4, sort_keys=True)}" + ) self.log.debug(msg) def _process_responses(self): @@ -179,6 +183,7 @@ def _process_responses(self): self.response_current = copy.deepcopy(response) return + self.failed = True self.changed = False # at least one request succeeded, so set changed to True if len(self.result_nok) != len(self._payloads_to_commit): @@ -195,7 +200,6 @@ def _process_responses(self): for response in self.response_ok: self.response = copy.deepcopy(response) self.response_current = copy.deepcopy(response) - self.failed = True result = {} result["failed"] = self.failed @@ -235,7 +239,7 @@ def payloads(self, value): class ImagePolicyCreateBulk(ImagePolicyCreateCommon): """ - Given a property-constructed list of payloads, bulk-create the + Given a properly-constructed list of payloads, bulk-create the image policies therein. The payload format is given below. Payload format: @@ -307,6 +311,8 @@ def commit(self): class ImagePolicyCreate(ImagePolicyCreateCommon): """ + NOTE: This class is not being used currently. + Given a properly-constructed image policy payload (python dict), send an image policy create request to the controller. The payload format is given below. @@ -364,6 +370,7 @@ def payload(self): @payload.setter def payload(self, value): self._verify_payload(value) + self.properties["payloads"] = [value] self.properties["payload"] = value def commit(self): @@ -377,8 +384,8 @@ def commit(self): msg += "payload must be set prior to calling commit." self.ansible_module.fail_json(msg, **self.failed_result) - # ImagePolicyCreateCommon expects a list of payloads - self.payloads = [self.payload] + # # ImagePolicyCreateCommon expects a list of payloads + # self.payloads = [self.payload] self._build_payloads_to_commit() if not self._payloads_to_commit: diff --git a/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create.py b/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create.py index 3edff9b68..6fc0614fc 100644 --- a/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create.py +++ b/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create.py @@ -15,9 +15,12 @@ # See the following regarding *_fixture imports # https://pylint.pycqa.org/en/latest/user_guide/messages/warning/redefined-outer-name.html # Due to the above, we also need to disable unused-import +# Also, fixtures need to use *args to match the signature of the function they are mocking # pylint: disable=unused-import -# Some fixtures need to use *args to match the signature of the function they are mocking +# pylint: disable=redefined-outer-name +# pylint: disable=protected-access # pylint: disable=unused-argument +# pylint: disable=invalid-name from __future__ import absolute_import, division, print_function diff --git a/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create_bulk.py b/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create_bulk.py index 3f5eb4536..1a43633a4 100644 --- a/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create_bulk.py +++ b/tests/unit/modules/dcnm/dcnm_image_policy/test_image_policy_create_bulk.py @@ -21,6 +21,7 @@ # pylint: disable=protected-access # pylint: disable=unused-argument # pylint: disable=invalid-name + from __future__ import absolute_import, division, print_function __metaclass__ = type @@ -385,8 +386,8 @@ def mock_dcnm_send(*args, **kwargs): assert instance.result_ok == [] assert instance.diff_ok == [] assert instance.response_nok[0]["RETURN_CODE"] == 500 - assert instance.result_nok[0]["changed"] == False - assert instance.result_nok[0]["success"] == False + assert instance.result_nok[0]["changed"] is False + assert instance.result_nok[0]["success"] is False assert instance.diff_nok[0]["agnostic"] is False assert instance.diff_nok[0]["policyName"] == "FOO"