From 62b5cc13e40dce2b235bd4d0066d0e51f5edcd7c Mon Sep 17 00:00:00 2001 From: Ignas <5737899+Hoffs@users.noreply.github.com> Date: Fri, 9 Aug 2024 10:31:46 +0300 Subject: [PATCH] allow to skip overrides by provider alias (#60) --- README.md | 2 ++ bin/tflocal | 3 ++- setup.cfg | 2 +- tests/test_apply.py | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e06d99d..6b7886c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ The following environment variables can be configured: * `default` profile's credentials are configured * falls back to the default `AWS_ACCESS_KEY_ID` mock value * `AWS_ACCESS_KEY_ID`: AWS Access Key ID to use for multi account setups (default: `test` -> account ID: `000000000000`) +* `SKIP_ALIASES`: Allows to skip generating AWS provider overrides for specified aliased providers, e.g. `SKIP_ALIASES=aws_secrets,real_aws` ## Usage @@ -49,6 +50,7 @@ please refer to the man pages of `terraform --help`. ## Change Log +* v0.19.0: Add `SKIP_ALIASES` configuration environment variable * v0.18.2: Fix warning on aliased custom endpoint names * v0.18.1: Fix issue with not proxied commands * v0.18.0: Add `DRY_RUN` and patch S3 backend entrypoints diff --git a/bin/tflocal b/bin/tflocal index 32ccd26..5c57489 100755 --- a/bin/tflocal +++ b/bin/tflocal @@ -221,12 +221,13 @@ def get_providers_file_path() -> str: def determine_provider_aliases() -> list: """Return a list of providers (and aliases) configured in the *.tf files (if any)""" + skipped = str(os.environ.get("SKIP_ALIASES") or "").strip().split(",") result = [] tf_files = parse_tf_files() for _file, obj in tf_files.items(): try: providers = ensure_list(obj.get("provider", [])) - aws_providers = [prov["aws"] for prov in providers if prov.get("aws")] + aws_providers = [prov["aws"] for prov in providers if prov.get("aws") and prov.get("aws").get("alias") not in skipped] result.extend(aws_providers) except Exception as e: print(f"Warning: Unable to extract providers from {_file}:", e) diff --git a/setup.cfg b/setup.cfg index 7ec32a5..a158c0a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = terraform-local -version = 0.18.2 +version = 0.19.0 url = https://github.com/localstack/terraform-local author = LocalStack Team author_email = info@localstack.cloud diff --git a/tests/test_apply.py b/tests/test_apply.py index 7e3c16c..238da97 100644 --- a/tests/test_apply.py +++ b/tests/test_apply.py @@ -353,6 +353,39 @@ def check_override_file_backend_content(override_file, is_legacy: bool = False): return new_options_check and not legacy_options_check +def test_provider_aliases_ignored(monkeypatch): + monkeypatch.setenv("DRY_RUN", "1") + config = """ + provider "aws" { + region = "eu-west-1" + } + provider "aws" { + alias = "us_east_2" + region = "us-east-2" + secret_key = "not-overriden" + } + """ + + temp_dir = deploy_tf_script(config, cleanup=False, env_vars={"SKIP_ALIASES": "us_east_2"}, user_input="yes") + override_file = os.path.join(temp_dir, "localstack_providers_override.tf") + assert check_override_file_content_for_alias(override_file) + rmtree(temp_dir) + + +def check_override_file_content_for_alias(override_file): + try: + with open(override_file, "r") as fp: + result = hcl2.load(fp) + result = result["provider"] + except Exception as e: + raise Exception(f'Unable to parse "{override_file}" as HCL file: {e}') + + for p in result: + if "aws" in p and "alias" in p["aws"] and p["aws"]["alias"] == "us_east_2": + return False + return True + + ### # UTIL FUNCTIONS ###