Skip to main content

tfvars_editor.py

Source: sunholo/terraform/tfvars_editor.py

Functions

setup_tfvarseditor_subparser(subparsers)

Sets up an argparse subparser for the 'tfvars' command.

Args: subparsers: The subparsers object from argparse.ArgumentParser().

tfvars_command(args)

Executes the tfvars command based on parsed arguments.

Args: args: The parsed command-line arguments.

Classes

TerraformVarsEditor

A class to manage and safely edit Terraform .tfvars files.

This class allows you to update specific keys in a .tfvars file with new data and ensures that the changes only take effect if Terraform validation passes.

Attributes:

tfvars_file : str The path to the .tfvars file to be edited. terraform_dir : str The directory where Terraform commands will be executed (default is current directory). tfvars_data : dict The content of the .tfvars file loaded into a dictionary.

Methods:

_load_tfvars() -> Dict[str, Any] Loads the .tfvars file into a dictionary. _save_tfvars() -> None Saves the current state of the dictionary back to the .tfvars file. _backup_tfvars() -> str Creates a backup of the current .tfvars file. _restore_tfvars(backup_file: str) -> None Restores the .tfvars file from the backup. update_or_add_instance(main_key: str, instance_name: str, instance_data: Dict[str, Any]) -> None Adds or updates an instance under a specified top-level key in the .tfvars file. validate_terraform() -> bool Runs terraform validate in the specified directory. update_from_json(json_file: str, main_key: str) -> None Updates the .tfvars file based on the content of a JSON file and validates the changes.

  • init(self, tfvars_file: str, terraform_dir: str = '.') -> None
    • Initializes the TerraformVarsEditor with the given .tfvars file and Terraform directory.

Parameters:

tfvars_file : str The path to the .tfvars file to be edited. terraform_dir : str The directory where Terraform commands will be executed (default is current directory). Will use MULTIVAC_TERRAFORM_DIR env var if present.

Example:

editor = TerraformVarsEditor('example.tfvars', '/path/to/terraform/config')

  • _backup_tfvars(self) -> str
    • Creates a backup of the current .tfvars file.

Returns:

str The path to the backup file.

Example:

backup_file = self._backup_tfvars()

  • _load_tfvars(self) -> Dict[str, Any]
    • Loads the .tfvars file into a dictionary.

Returns:

dict The content of the .tfvars file.

Example:

data = self._load_tfvars()

  • _restore_tfvars(self, backup_file: str) -> None
    • Restores the .tfvars file from the backup.

Parameters:

backup_file : str The path to the backup file to restore from.

Example:

self._restore_tfvars('example.tfvars.bak')

  • _save_tfvars(self) -> None
    • Saves the current state of the dictionary back to the .tfvars file.

Example:

self._save_tfvars()

  • update_from_dict(self, data: Dict[str, Any], main_key: str) -> None
    • Updates the .tfvars file based on the content of a Python dictionary and validates the changes.

Parameters:

data : dict The dictionary with the new instance data. main_key : str The top-level key under which the instance is added (e.g., "cloud_run").

Example:

editor.update_from_dict(data, 'cloud_run')

  • update_from_json(self, json_file: str, main_key: str) -> None
    • Updates the .tfvars file based on the content of a JSON file and validates the changes.

Parameters:

json_file : str The path to the JSON file with the new instance data. main_key : str The top-level key in the .tfvars file (e.g., "cloud_run").

Example:

editor.update_from_json('update.json', 'cloud_run')

  • update_or_add_instance(self, main_key: str, instance_name: str, instance_data: Dict[str, Any]) -> None
    • Adds or updates an instance under a specified top-level key in the .tfvars file.

Parameters:

main_key : str The top-level key in the .tfvars file (e.g., "cloud_run"). instance_name : str The name of the instance to add or update. instance_data : dict The dictionary containing the instance data.

Example:

editor.update_or_add_instance('cloud_run', 'new_service', (your dict))
  • validate_terraform(self) -> bool
    • Runs terraform init followed by terraform validate in the specified directory.

Returns:

bool True if validation passes, False otherwise.

Example:

if self.validate_terraform():
print("Validation passed.")
Sunholo Multivac

Get in touch to see if we can help with your GenAI project.

Contact us

Other Links

Sunholo Multivac - GenAIOps

Copyright ©

Holosun ApS 2024