From efb3035abd792a0fd0f353724cb98ccaa712ebec Mon Sep 17 00:00:00 2001 From: MUTHU-RAKESH-27 <19cs127@psgitech.ac.in> Date: Mon, 6 Jan 2025 15:22:38 +0530 Subject: [PATCH] Provided the bulk input to the add and update fabric devices APIs in a batch of 40 --- .../sda_fabric_devices_workflow_manager.py | 198 +++++++++++++----- 1 file changed, 144 insertions(+), 54 deletions(-) diff --git a/plugins/modules/sda_fabric_devices_workflow_manager.py b/plugins/modules/sda_fabric_devices_workflow_manager.py index 1e4ee0cc4..266a75517 100644 --- a/plugins/modules/sda_fabric_devices_workflow_manager.py +++ b/plugins/modules/sda_fabric_devices_workflow_manager.py @@ -3550,6 +3550,106 @@ def get_want(self, config): self.status = "success" return self + def bulk_add_fabric_devices(self, create_fabric_devices, fabric_name): + """ + Add the SDA fabric devices with the given payload under the fabric. + + Parameters: + create_fabric_devices (list): The payload for adding the fabric devices in bulk. + fabric_name (str): The name of the fabric site or zone. + Returns: + self (object): The current object with adding SDA fabric device information. + Description: + Find the length of the payload, if it is greater than 40, seperate it out into batches of 40. + Call the add fabric devices API with the bulk payload. Check the task status from the task ID. + """ + + try: + len_create_fabric_devices = len(create_fabric_devices) + for item in range(0, len_create_fabric_devices, 40): + payload = {"payload": create_fabric_devices[item:item + 40]} + task_name = "add_fabric_devices" + task_id = self.get_taskid_post_api_call("sda", task_name, payload) + if not task_id: + self.msg = ( + "Unable to retrive the task_id for the task '{task_name}'." + .format(task_name=task_name) + ) + self.set_operation_result("failed", False, self.msg, "ERROR") + return self + + success_msg = ( + "Successfully added the fabric device with details '{device_details}'." + .format(device_details=create_fabric_devices[item:item + 40]) + ) + self.get_task_status_from_tasks_by_id(task_id, task_name, success_msg).check_return_status() + except Exception as msg: + self.msg = ( + "Exception occurred while updating the fabric devices with the payload '{payload}': {msg}" + .format(payload=create_fabric_devices, msg=msg) + ) + self.status = "failed" + return self + + self.msg = ( + "Successfully created the fabric devices with the payload to the fabric site '{fabric_site}': {payload}" + .format(fabric_site=fabric_name, payload=create_fabric_devices) + ) + self.log(self.msg, "INFO") + self.status = "success" + return self + + def bulk_update_fabric_devices(self, update_fabric_devices, fabric_name): + """ + Update the SDA fabric devices with the given payload under the fabric. + + Parameters: + update_fabric_devices (list): The payload for updating the fabric devices in bulk. + fabric_name (str): The name of the fabric site or zone. + Returns: + self (object): The current object with updated SDA fabric device information. + Description: + Find the length of the payload, if it is greater than 40, seperate it out into batches of 40. + Call the update fabric devices API with the bulk payload. Check the task status from the task ID. + """ + + try: + len_update_fabric_devices = len(update_fabric_devices) + for item in range(0, len_update_fabric_devices, 40): + payload = {"payload": update_fabric_devices[item:item + 40]} + task_name = "update_fabric_devices" + task_id = self.get_taskid_post_api_call("sda", task_name, payload) + if not task_id: + self.msg = ( + "Unable to retrive the task_id for the task '{task_name}'." + .format(task_name=task_name) + ) + self.set_operation_result("failed", False, self.msg, "ERROR") + return self + + success_msg = ( + "Successfully updated the fabric device with details '{device_details}'." + .format(device_details=update_fabric_devices[item:item + 40]) + ) + self.get_task_status_from_tasks_by_id(task_id, task_name, success_msg).check_return_status() + + except Exception as msg: + self.msg = ( + "Exception occurred while updating the fabric devices for the payload '{payload}': {msg}" + .format(payload=update_fabric_devices, msg=msg) + ) + self.log(self.msg, "ERROR") + self.status = "failed" + return self + + self.msg = ( + "Successfully updated the device with payload '{payload}' to the fabric site '{fabric_site}'." + .format(payload=update_fabric_devices, fabric_site=fabric_name), "INFO" + ) + self.log(self.msg, "INFO") + self.status = "success" + return self + def update_l2_handoff(self, have_l2_handoff, want_l2_handoff, device_ip, result_fabric_device_response, result_fabric_device_msg): @@ -3998,6 +4098,8 @@ def update_fabric_devices(self, fabric_devices): self.response.append({"response": {}, "msg": {}}) self.response[0].get("response").update({fabric_name: {}}) self.response[0].get("msg").update({fabric_name: {}}) + to_create = [] + to_update = [] for item in device_config: fabric_device_index += 1 device_ip = item.get("device_ip") @@ -4022,7 +4124,6 @@ def update_fabric_devices(self, fabric_devices): device_ip: {} }) result_fabric_device_response = self.response[0].get("response").get(fabric_name).get(device_ip) - self.log("hi") result_fabric_device_msg = self.response[0].get("msg").get(fabric_name).get(device_ip) have_fabric_device = self.have.get("fabric_devices")[fabric_device_index] want_fabric_device = self.want.get("fabric_devices")[fabric_device_index] @@ -4103,40 +4204,45 @@ def update_fabric_devices(self, fabric_devices): task_name=task_name, task_id=task_id ), "INFO" ) + if not task_id: + self.msg = ( + "Unable to retrive the task_id for the task '{task_name}'." + .format(task_name=task_name) + ) + self.set_operation_result("failed", False, self.msg, "ERROR") + return self + self.log( "Task ID received from API call to Function: 'add_control_plane_device': {task_id}" .format(task_id=task_id), "INFO" ) - else: - payload = {"payload": [want_device_details]} - task_name = "add_fabric_devices" - task_id = self.get_taskid_post_api_call("sda", task_name, payload) - - if not task_id: - self.msg = ( - "Unable to retrive the task_id for the task '{task_name}'." - .format(task_name=task_name) + success_msg = ( + "Successfully added the fabric device (Control Node) with details '{device_details}'." + .format(device_details=payload) ) - self.set_operation_result("failed", False, self.msg, "ERROR") - return self + self.get_task_status_from_tasks_by_id(task_id, task_name, success_msg).check_return_status() + self.log( + "Successfully added the device (Control Node) with IP '{ip}' to the fabric site '{fabric_site}'." + .format(fabric_site=fabric_name, ip=device_ip), "INFO" + ) + result_fabric_device_response.update({ + "device_details": want_device_details + }) + result_fabric_device_msg.update({ + "device_details": "SDA fabric device (Control Node) details added successfully." + }) + else: + to_create.append(want_device_details) - success_msg = ( - "Successfully added the fabric device with details '{device_details}'." - .format(device_details=want_device_details) - ) - self.get_task_status_from_tasks_by_id(task_id, task_name, success_msg).check_return_status() except Exception as msg: self.msg = ( - "Exception occurred while adding the device '{ip}' to the fabric site '{site}: {msg}" + "Exception occurred while adding the control plane device '{ip}' " + "to the fabric site '{site}: {msg}" .format(ip=device_ip, site=fabric_name, msg=msg) ) self.status = "failed" return self - self.log( - "Successfully added the device with IP '{ip}' to the fabric site '{fabric_site}'." - .format(fabric_site=fabric_name, ip=device_ip), "INFO" - ) result_fabric_device_response.update({ "device_details": want_device_details }) @@ -4165,7 +4271,6 @@ def update_fabric_devices(self, fabric_devices): "device_details": "SDA fabric device details doesn't require an update." }) else: - self.log("Updating SDA fabric device '{ip}'.".format(ip=device_ip), "DEBUG") # Device Details Exists self.log( @@ -4177,36 +4282,7 @@ def update_fabric_devices(self, fabric_devices): .format(ip=device_ip, requested_state=want_device_details), "DEBUG" ) want_device_details.update({"id": self.have.get("fabric_devices")[fabric_device_index].get("id")}) - try: - payload = {"payload": [want_device_details]} - task_name = "update_fabric_devices" - task_id = self.get_taskid_post_api_call("sda", task_name, payload) - if not task_id: - self.msg = ( - "Unable to retrive the task_id for the task '{task_name}'." - .format(task_name=task_name) - ) - self.set_operation_result("failed", False, self.msg, "ERROR") - return self - - success_msg = ( - "Successfully updated the fabric device with details '{device_details}'." - .format(device_details=want_device_details) - ) - self.get_task_status_from_tasks_by_id(task_id, task_name, success_msg).check_return_status() - except Exception as msg: - self.msg = ( - "Exception occurred while updating the fabric device with IP '{ip}': {msg}" - .format(ip=device_ip, msg=msg) - ) - self.log(self.msg, "ERROR") - self.status = "failed" - return self - - self.log( - "Successfully updated the device with IP '{ip}' to the fabric site '{fabric_site}'." - .format(fabric_site=fabric_name, ip=device_ip), "INFO" - ) + to_update.append(want_device_details) result_fabric_device_response.update({ "device_details": want_device_details }) @@ -4214,6 +4290,20 @@ def update_fabric_devices(self, fabric_devices): "device_details": "SDA fabric device details updated successfully." }) + if to_create: + self.bulk_add_fabric_devices(to_create, fabric_name).check_return_status() + + if to_update: + self.bulk_update_fabric_devices(to_update, fabric_name).check_return_status() + + fabric_device_index = -1 + for item in device_config: + fabric_device_index += 1 + result_fabric_device_response = self.response[0].get("response").get(fabric_name).get(device_ip) + result_fabric_device_msg = self.response[0].get("msg").get(fabric_name).get(device_ip) + have_fabric_device = self.have.get("fabric_devices")[fabric_device_index] + want_fabric_device = self.want.get("fabric_devices")[fabric_device_index] + device_ip = item.get("device_ip") device_roles = want_device_details.get("deviceRoles") if "BORDER_NODE" not in device_roles: continue @@ -4277,7 +4367,7 @@ def get_diff_merged(self, config): if fabric_devices is not None: self.log("Updating fabric devices: {devices}".format(devices=fabric_devices), "DEBUG") try: - self.update_fabric_devices(fabric_devices) + self.update_fabric_devices(fabric_devices).check_return_status() self.log("Successfully updated fabric devices.", "INFO") except Exception as e: self.log("Error while updating fabric devices: {error}".format(error=str(e)), "ERROR")