From 3fd4a72d3d0741438afdee916217e67e63d401cb Mon Sep 17 00:00:00 2001 From: bom Date: Fri, 24 Nov 2023 22:08:38 +0100 Subject: [PATCH] Adjust logic for plan files (version > 1.0.0) We will no longer try to chdir into files and instead add them as subcommands. Includes regression test Resolves #3 (https://gitlab.com/domaindrivenarchitecture/dda-python-terraform/-/issues/3) --- dda_python_terraform/terraform.py | 10 +++++++++- test/test_terraform.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/dda_python_terraform/terraform.py b/dda_python_terraform/terraform.py index a216671..238b255 100644 --- a/dda_python_terraform/terraform.py +++ b/dda_python_terraform/terraform.py @@ -464,12 +464,20 @@ class Terraform: def _generate_default_args(self, dir_or_plan: Optional[str]) -> Sequence[str]: if (version.parse(self.terraform_semantic_version) < version.parse("1.0.0") and dir_or_plan): return [dir_or_plan] + elif (version.parse(self.terraform_semantic_version) >= version.parse("1.0.0") and dir_or_plan and os.path.isfile(f'{self.working_dir}/{dir_or_plan}')): + plan = dir_or_plan.split('/')[-1] + return [plan] else: return [] def _generate_default_general_options(self, dir_or_plan: Optional[str]) -> Dict[str, Any]: if (version.parse(self.terraform_semantic_version) >= version.parse("1.0.0") and dir_or_plan): - return {"chdir": dir_or_plan} + if os.path.isdir(self.working_dir + '/' + dir_or_plan): + return {"chdir": dir_or_plan} + else: + plan_path = dir_or_plan.split('/') + dir_to_plan_path = "/".join(plan_path[:-1]) + return {"chdir": dir_to_plan_path} else: return {} diff --git a/test/test_terraform.py b/test/test_terraform.py index becdf5c..e9329f2 100644 --- a/test/test_terraform.py +++ b/test/test_terraform.py @@ -316,6 +316,26 @@ class TestTerraform: assert expected_output in out.replace("\n", "").replace(" ", "") assert err == "" + def test_apply_plan(self): + # test is only applicable to version > 1.0.0 + if version.parse(semantic_version) < version.parse("1.0.0"): + return + + tf = Terraform( + working_dir=current_path, terraform_semantic_version=semantic_version + ) + out_folder = 'var_to_output' + out_file_name = 'test.out' + out_file_path = f'{out_folder}/{out_file_name}' + tf.init(out_folder) + ret, _, err = tf.plan(out_folder, detailed_exitcode=IsNotFlagged, out=out_file_name) + assert ret == 0 + assert err == "" + + ret, _, err = tf.apply(out_file_path, skip_plan=True) + assert ret == 0 + assert err == "" + def test_apply_with_var_file(self, caplog: LogCaptureFixture): with caplog.at_level(logging.INFO): tf = Terraform(working_dir=current_path, terraform_semantic_version=semantic_version)