From 78251f3e489cea4c59395fa780c35dcf9118da6e Mon Sep 17 00:00:00 2001 From: Osvaldo Demo Date: Thu, 11 Apr 2024 16:57:25 -0300 Subject: [PATCH] [159] Improve apply arguments parsing logic --- .github/workflows/tests-integration.yaml | 2 +- leverage/modules/terraform.py | 49 +++++++++++++++++-- tests/test_modules/terraform/__init__.py | 0 .../test_handle_apply_arguments_parsing.py | 35 +++++++++++++ 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 tests/test_modules/terraform/__init__.py create mode 100644 tests/test_modules/terraform/test_handle_apply_arguments_parsing.py diff --git a/.github/workflows/tests-integration.yaml b/.github/workflows/tests-integration.yaml index 575ca6a6..a5e97237 100644 --- a/.github/workflows/tests-integration.yaml +++ b/.github/workflows/tests-integration.yaml @@ -175,7 +175,7 @@ jobs: EOF echo "[INFO] Disable MFA\n" sed -i "s/^\(MFA_ENABLED=\)true/\1false/" build.env - sed -E -i 's/^TERRAFORM_IMAGE_TAG=.+$/TERRAFORM_IMAGE_TAG=1.2.7-0.0.5/' build.env; + sed -E -i 's/^TERRAFORM_IMAGE_TAG=.+$/TERRAFORM_IMAGE_TAG=1.3.5-0.1.15/' build.env; working-directory: ../theblairwitchproject - name: Test Testing Reference Architecture diff --git a/leverage/modules/terraform.py b/leverage/modules/terraform.py index e4ac10d8..359cc44e 100644 --- a/leverage/modules/terraform.py +++ b/leverage/modules/terraform.py @@ -1,17 +1,17 @@ import os import re +import click import dockerpty import hcl2 -import click from click.exceptions import Exit from leverage import logger -from leverage._internals import pass_state from leverage._internals import pass_container +from leverage._internals import pass_state from leverage._utils import tar_directory, AwsCredsContainer, LiveContainer, ExitError -from leverage.container import get_docker_client from leverage.container import TerraformContainer +from leverage.container import get_docker_client from leverage.modules.utils import env_var_option, mount_option, auth_mfa, auth_sso REGION = ( @@ -351,18 +351,57 @@ def _plan(tf, args): raise Exit(exit_code) +def handle_apply_arguments_parsing(args): + """Parse and process the arguments for the 'apply' command.""" + # Initialize new_args to handle both '-key=value' and '-key value' + new_args = [] + skip_next = False # Flag to skip the next argument if it's part of '-key value' + + for i, arg in enumerate(args): + if skip_next: + skip_next = False # Reset flag and skip this iteration + continue + + if arg.startswith("-") and not arg.startswith("-var"): + if i + 1 < len(args) and not args[i + 1].startswith("-"): + # Detected '-key value' pair; append them without merging + new_args.append(arg) + new_args.append(args[i + 1]) + skip_next = True # Mark to skip the next item as it's already processed + logger.debug(f"Detected '-key value' pair: {arg}, {args[i + 1]}") + else: + # Either '-key=value' or a standalone '-key'; just append + new_args.append(arg) + logger.debug(f"Appending standard -key=value or standalone argument: {arg}") + else: + # Handles '-var' and non '-' starting arguments + new_args.append(arg) + logger.debug(f"Appending argument (non '-' or '-var'): {arg}") + + return new_args + + @pass_container def _apply(tf, args): """Build or change the infrastructure in this layer.""" - # if there is a plan, remove all "-var" from the default args + # Preserve the original `-var` removal logic and modify tf_default_args if necessary tf_default_args = tf.tf_default_args for arg in args: if not arg.startswith("-"): tf_default_args = [arg for index, arg in enumerate(tf_default_args) if not arg.startswith("-var")] break - exit_code = tf.start_in_layer("apply", *tf_default_args, *args) + + # Process arguments using the new parsing logic + processed_args = handle_apply_arguments_parsing(args) + + logger.debug(f"Original tf_default_args: {tf.tf_default_args}") + logger.debug(f"Processed argument list for execution: {processed_args}") + + # Execute the command with the modified arguments list + exit_code = tf.start_in_layer("apply", *tf_default_args, *processed_args) if exit_code: + logger.error(f"Command execution failed with exit code: {exit_code}") raise Exit(exit_code) diff --git a/tests/test_modules/terraform/__init__.py b/tests/test_modules/terraform/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_modules/terraform/test_handle_apply_arguments_parsing.py b/tests/test_modules/terraform/test_handle_apply_arguments_parsing.py new file mode 100644 index 00000000..e61dd1d3 --- /dev/null +++ b/tests/test_modules/terraform/test_handle_apply_arguments_parsing.py @@ -0,0 +1,35 @@ +import pytest + +from leverage.modules.terraform import handle_apply_arguments_parsing + + +class TestHandleApplyArgumentsParsing: + @pytest.mark.parametrize( + "input_args,expected_output", + [ + # Test case: Single '-key=value' + (["-target=kubernetes_manifest.irfq"], ["-target=kubernetes_manifest.irfq"]), + # Test case: '-key value' + (["-target", "kubernetes_manifest.irfq"], ["-target", "kubernetes_manifest.irfq"]), + # Test case: Multiple mixed arguments + ( + ["-target", "kubernetes_manifest.irfq", "-lock=false"], + ["-target", "kubernetes_manifest.irfq", "-lock=false"], + ), + # Test case: '-var' arguments should be included as is + (["-var", "name=value"], ["-var", "name=value"]), + # Test case: Non-flag argument + (["some_value"], ["some_value"]), + # Test case: Mixed '-key=value' and '-key value' with '-var' + ( + ["-var", "name=value", "-target", "kubernetes_manifest.irfq", "-lock=false"], + ["-var", "name=value", "-target", "kubernetes_manifest.irfq", "-lock=false"], + ), + # Test case: No arguments + ([], []), + # Test case: '-key=value' format with '-var' + (["-var", "name=value", "-lock=false"], ["-var", "name=value", "-lock=false"]), + ], + ) + def test_handle_apply_arguments_parsing(self, input_args, expected_output): + assert handle_apply_arguments_parsing(input_args) == expected_output