Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass vm index for changing tfstate file #10847

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions ocs_ci/deployment/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
4 changes: 4 additions & 0 deletions ocs_ci/ocs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,3 +726,7 @@ class ProviderModeNotFoundException(Exception):

class TolerationNotFoundException(Exception):
pass


class VMIndexNotFoundException(Exception):
pass
39 changes: 29 additions & 10 deletions ocs_ci/ocs/platform_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
NotAllNodesCreated,
RebootEventNotFoundException,
ResourceWrongStatusException,
VMIndexNotFoundException,
VolumePathNotFoundException,
)
from ocs_ci.framework import config, merge_dict
Expand Down Expand Up @@ -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):
"""
Expand Down
Loading