Skip to content

Commit

Permalink
Consistent diff between ImageStage and ImageValidate
Browse files Browse the repository at this point in the history
1. Align the diff returned by ImageStage and ImageValidate

2. ImageStage, improve the response when no serial numbers need to be processed.

3. ImageValidate, return a response when no serial numbers need to be processed (similar code to 2, above).

4. ImageValidate, update unit test to reflect different response from ImageValidate in the case where no serial_numbers need to be processed.
  • Loading branch information
allenrobel committed Jan 12, 2024
1 parent aeb7878 commit 6d2957c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
23 changes: 14 additions & 9 deletions plugins/module_utils/image_mgmt/image_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ def commit(self):

msg = f"self.serial_numbers: {self.serial_numbers}"
self.log.debug(msg)

if len(self.serial_numbers) == 0:
self.properties["response"] = {"response": "No serial numbers to stage."}
self.properties["response_data"] = {
"response": "No serial numbers to stage."
}
msg = "No serial numbers to stage."
self.properties["response"] = {"response": msg}
self.properties["response_data"] = {"response": msg}
self.properties["result"] = {"success": True}
return

Expand All @@ -215,25 +215,30 @@ def commit(self):
)
self.properties["result"] = self._handle_response(self.response, self.verb)

msg = f"payload: {self.payload}"
self.log.debug(msg)
msg = f"response: {self.response}"
self.log.debug(msg)
msg = f"result: {self.result}"
self.log.debug(msg)

if not self.result["success"]:
msg = f"{self.class_name}.{method_name}: "
msg = f"failed: {self.result}. "
msg += f"Controller response: {self.response}"
self.module.fail_json(msg, **self.failed_result)

msg = f"self.response: {self.response}"
self.log.debug(msg)
self.properties["response_data"] = self.response.get("DATA", "No Stage DATA")
self._wait_for_image_stage_to_complete()

for serial_number in self.serial_numbers_done:
self.issu_detail.filter = serial_number
self.issu_detail.refresh()
diff = {}
diff["serial_number"] = serial_number
diff["action"] = "stage"
diff["logical_name"] = self.issu_detail.device_name
diff["ip_address"] = self.issu_detail.ip_address
diff["logical_name"] = self.issu_detail.device_name
diff["policy"] = self.issu_detail.policy
diff["serial_number"] = serial_number
self.diff = copy.deepcopy(diff)

def _wait_for_current_actions_to_complete(self):
Expand Down
30 changes: 25 additions & 5 deletions plugins/module_utils/image_mgmt/image_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,25 @@ def commit(self) -> None:
Commit the image validation request to the controller and wait
for the images to be validated.
"""
self.method_name = inspect.stack()[0][3]
method_name = inspect.stack()[0][3]

msg = "ENTERED commit()"
self.log.debug(msg)

if self.serial_numbers is None:
msg = f"{self.class_name}.{self.method_name}: "
msg += "call instance.serial_numbers before "
msg += "calling commit."
msg = f"{self.class_name}.{method_name}: "
msg += "call instance.serial_numbers "
msg += "before calling commit."
self.module.fail_json(msg, **self.failed_result)

msg = f"self.serial_numbers: {self.serial_numbers}"
self.log.debug(msg)

if len(self.serial_numbers) == 0:
msg = "No serial numbers to validate."
self.properties["response"] = {"response": msg}
self.properties["response_data"] = {"response": msg}
self.properties["result"] = {"success": True}
return

self.prune_serial_numbers()
Expand All @@ -183,14 +193,24 @@ def commit(self) -> None:
self.log.debug(msg)

if not self.result["success"]:
msg = f"{self.class_name}.{self.method_name}: "
msg = f"{self.class_name}.{method_name}: "
msg = f"failed: {self.result}. "
msg += f"Controller response: {self.response}"
self.module.fail_json(msg, **self.failed_result)

self.properties["response_data"] = self.response
self._wait_for_image_validate_to_complete()

for serial_number in self.serial_numbers_done:
self.issu_detail.filter = serial_number
diff = {}
diff["action"] = "validate"
diff["ip_address"] = self.issu_detail.ip_address
diff["logical_name"] = self.issu_detail.device_name
diff["policy"] = self.issu_detail.policy
diff["serial_number"] = serial_number
self.diff = copy.deepcopy(diff)

def _wait_for_current_actions_to_complete(self) -> None:
"""
The controller will not validate an image if there are any actions in
Expand Down
6 changes: 4 additions & 2 deletions plugins/modules/dcnm_image_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,8 @@ def _stage_images(self, serial_numbers) -> None:
instance = ImageStage(self.module)
instance.serial_numbers = serial_numbers
instance.commit()
self.result.diff_stage = instance.response
for diff in instance.diff:
self.result.diff_stage = copy.deepcopy(diff)
instance.response.pop("DATA", None)
self.result.response_stage = instance.response

Expand All @@ -1093,7 +1094,8 @@ def _validate_images(self, serial_numbers) -> None:
instance = ImageValidate(self.module)
instance.serial_numbers = serial_numbers
instance.commit()
self.result.diff_validate = instance.payload
for diff in instance.diff:
self.result.diff_validate = copy.deepcopy(diff)
self.result.response_validate = instance.response

def _verify_install_options(self, devices) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ def test_image_mgmt_validate_00022(image_validate) -> None:
instance = image_validate
instance.serial_numbers = []
instance.commit()
assert instance.response == {}
assert instance.result == {}
assert instance.response == {'response': 'No serial numbers to validate.'}
assert instance.result == {'success': True}


def test_image_mgmt_validate_00023(monkeypatch, image_validate) -> None:
Expand Down

0 comments on commit 6d2957c

Please sign in to comment.