Skip to content

Commit

Permalink
allow to skip overrides by provider alias (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoffs authored Aug 9, 2024
1 parent 27a834d commit 62b5cc1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion bin/tflocal
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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 = [email protected]
Expand Down
33 changes: 33 additions & 0 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
###
Expand Down

0 comments on commit 62b5cc1

Please sign in to comment.