Skip to content

Commit 24afbe1

Browse files
committed
allow to skip overrides by provider alias
1 parent 27a834d commit 24afbe1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

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)

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)