Skip to content

Commit 62b5cc1

Browse files
authoredAug 9, 2024
allow to skip overrides by provider alias (#60)
1 parent 27a834d commit 62b5cc1

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The following environment variables can be configured:
4141
* `default` profile's credentials are configured
4242
* falls back to the default `AWS_ACCESS_KEY_ID` mock value
4343
* `AWS_ACCESS_KEY_ID`: AWS Access Key ID to use for multi account setups (default: `test` -> account ID: `000000000000`)
44+
* `SKIP_ALIASES`: Allows to skip generating AWS provider overrides for specified aliased providers, e.g. `SKIP_ALIASES=aws_secrets,real_aws`
4445

4546
## Usage
4647

@@ -49,6 +50,7 @@ please refer to the man pages of `terraform --help`.
4950

5051
## Change Log
5152

53+
* v0.19.0: Add `SKIP_ALIASES` configuration environment variable
5254
* v0.18.2: Fix warning on aliased custom endpoint names
5355
* v0.18.1: Fix issue with not proxied commands
5456
* v0.18.0: Add `DRY_RUN` and patch S3 backend entrypoints

‎bin/tflocal

+2-1
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,13 @@ def get_providers_file_path() -> str:
221221

222222
def determine_provider_aliases() -> list:
223223
"""Return a list of providers (and aliases) configured in the *.tf files (if any)"""
224+
skipped = str(os.environ.get("SKIP_ALIASES") or "").strip().split(",")
224225
result = []
225226
tf_files = parse_tf_files()
226227
for _file, obj in tf_files.items():
227228
try:
228229
providers = ensure_list(obj.get("provider", []))
229-
aws_providers = [prov["aws"] for prov in providers if prov.get("aws")]
230+
aws_providers = [prov["aws"] for prov in providers if prov.get("aws") and prov.get("aws").get("alias") not in skipped]
230231
result.extend(aws_providers)
231232
except Exception as e:
232233
print(f"Warning: Unable to extract providers from {_file}:", e)

‎setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = terraform-local
3-
version = 0.18.2
3+
version = 0.19.0
44
url = https://github.com/localstack/terraform-local
55
author = LocalStack Team
66
author_email = info@localstack.cloud

‎tests/test_apply.py

+33
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,39 @@ def check_override_file_backend_content(override_file, is_legacy: bool = False):
353353
return new_options_check and not legacy_options_check
354354

355355

356+
def test_provider_aliases_ignored(monkeypatch):
357+
monkeypatch.setenv("DRY_RUN", "1")
358+
config = """
359+
provider "aws" {
360+
region = "eu-west-1"
361+
}
362+
provider "aws" {
363+
alias = "us_east_2"
364+
region = "us-east-2"
365+
secret_key = "not-overriden"
366+
}
367+
"""
368+
369+
temp_dir = deploy_tf_script(config, cleanup=False, env_vars={"SKIP_ALIASES": "us_east_2"}, user_input="yes")
370+
override_file = os.path.join(temp_dir, "localstack_providers_override.tf")
371+
assert check_override_file_content_for_alias(override_file)
372+
rmtree(temp_dir)
373+
374+
375+
def check_override_file_content_for_alias(override_file):
376+
try:
377+
with open(override_file, "r") as fp:
378+
result = hcl2.load(fp)
379+
result = result["provider"]
380+
except Exception as e:
381+
raise Exception(f'Unable to parse "{override_file}" as HCL file: {e}')
382+
383+
for p in result:
384+
if "aws" in p and "alias" in p["aws"] and p["aws"]["alias"] == "us_east_2":
385+
return False
386+
return True
387+
388+
356389
###
357390
# UTIL FUNCTIONS
358391
###

0 commit comments

Comments
 (0)