From f506879a32c302b81de93c9a8a292094133a5483 Mon Sep 17 00:00:00 2001 From: vavuthu Date: Wed, 13 Nov 2024 16:40:57 +0530 Subject: [PATCH] pass vm index for changing tfstate file Signed-off-by: vavuthu --- ocs_ci/deployment/terraform.py | 15 +++++-------- ocs_ci/ocs/exceptions.py | 4 ++++ ocs_ci/ocs/platform_nodes.py | 39 +++++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/ocs_ci/deployment/terraform.py b/ocs_ci/deployment/terraform.py index 398fde13eab..a12a3b04b7a 100644 --- a/ocs_ci/deployment/terraform.py +++ b/ocs_ci/deployment/terraform.py @@ -188,7 +188,7 @@ def destroy_module(self, tfvars, module): ) run_cmd(cmd, timeout=1200) - def change_statefile(self, module, resource_type, resource_name, instance): + def change_statefile(self, module, vm_index): """ Remove the records from the state file so that terraform will no longer be tracking the corresponding remote objects. @@ -199,25 +199,20 @@ def change_statefile(self, module, resource_type, resource_name, instance): Args: module (str): Name of the module e.g: compute_vm, module.control_plane_vm etc. - resource_type (str): Resource type - e.g: vsphere_virtual_machine, vsphere_compute_cluster etc. - resource_name (str): Name of the resource - e.g: vm - instance (str): Name of the instance - e.g: compute-0.j-056vu1cs33l-a.qe.rh-ocs.com + vm_index (int): VM index. If the VM is compute-1, index is 1 and if the VM is + compute-2, then index is 2 Examples:: terraform = Terraform(os.path.join(upi_repo_path, "upi/vsphere/")) terraform.change_statefile( - module="compute_vm", resource_type="vsphere_virtual_machine", - resource_name="vm", instance="compute-0.j-056vu1cs33l-a.qe.rh-ocs.com" + module="compute_vm", vm_index=2 ) """ logger.info("Modifying terraform state file") cmd = ( f"{self.terraform_installer} state rm {self.state_file_param} " - f"'module.{module}.{resource_type}.{resource_name}[\"{instance}\"]'" + f"'module.{module}[{vm_index}]'" ) run_cmd(cmd) diff --git a/ocs_ci/ocs/exceptions.py b/ocs_ci/ocs/exceptions.py index 03864524e02..d0c1efe6918 100644 --- a/ocs_ci/ocs/exceptions.py +++ b/ocs_ci/ocs/exceptions.py @@ -726,3 +726,7 @@ class ProviderModeNotFoundException(Exception): class TolerationNotFoundException(Exception): pass + + +class VMIndexNotFoundException(Exception): + pass diff --git a/ocs_ci/ocs/platform_nodes.py b/ocs_ci/ocs/platform_nodes.py index 2a7dc2a5937..a6011b88e3a 100644 --- a/ocs_ci/ocs/platform_nodes.py +++ b/ocs_ci/ocs/platform_nodes.py @@ -24,6 +24,7 @@ NotAllNodesCreated, RebootEventNotFoundException, ResourceWrongStatusException, + VMIndexNotFoundException, VolumePathNotFoundException, ) from ocs_ci.framework import config, merge_dict @@ -2009,22 +2010,40 @@ def change_terraform_statefile_after_remove_vm(self, vm_name): Args: vm_name (str): The VM name + """ + module, vm_index = self.get_vm_module_and_index(vm_name) + + os.chdir(self.terraform_data_dir) + logger.info(f"Modifying terraform state file of the removed vm {vm_name}") + self.terraform.change_statefile(module=module, vm_index=vm_index) + os.chdir(self.previous_dir) + + def get_vm_module_and_index(self, vm_name): + """ + Gets the VM module and index for the node + + Args: + vm_name (str): VM name + + Returns: + tuple: which contains module and vm_index + """ if vm_name.startswith("compute-"): module = "compute_vm" + search_str = "compute-" else: module = "control_plane_vm" - instance = f"{vm_name}.{config.ENV_DATA.get('cluster_name')}.{config.ENV_DATA.get('base_domain')}" + search_str = "control-plane-" - os.chdir(self.terraform_data_dir) - logger.info(f"Modifying terraform state file of the removed vm {vm_name}") - self.terraform.change_statefile( - module=module, - resource_type="vsphere_virtual_machine", - resource_name="vm", - instance=instance, - ) - os.chdir(self.previous_dir) + # get the VM index + pattern = rf"{search_str}(\d+)" + match = re.search(pattern, vm_name) + if match: + vm_index = match.group(1) + else: + raise VMIndexNotFoundException + return module, vm_index def change_terraform_tfvars_after_remove_vm(self, num_nodes_removed=1): """