Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to skip overrides by provider alias #60

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading