Merge pull request #190 from cloud-neutral-toolkit/codex/fix-path-errors-in-render_provider_backend.py-y8nzb1

Add component-aware provider/backend rendering
This commit is contained in:
cloudneutral 2025-12-11 19:24:35 +08:00 committed by GitHub
commit 5e4bec6b74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -36,6 +36,7 @@ modules:
dev-object: dev-object:
account: dev account: dev
component_dir: s3
backend: backend:
key: "account/dev/s3/terraform.tfstate" key: "account/dev/s3/terraform.tfstate"

View File

@ -71,6 +71,17 @@ def load_account_config(account_name: str, additional_inputs: list[str] | None =
return load_merged_config(config_inputs) return load_merged_config(config_inputs)
def detect_target_component() -> str | None:
"""Return the component directory name if running inside one, otherwise None."""
try:
rel_path = Path.cwd().resolve().relative_to(ENVS_DIR)
except ValueError:
return None
return rel_path.parts[0] if rel_path.parts else None
def render_templates(): def render_templates():
provider_backend_cfg = load_merged_config(CONFIG_DIR / "provider_backend.yaml") provider_backend_cfg = load_merged_config(CONFIG_DIR / "provider_backend.yaml")
defaults = provider_backend_cfg.get("defaults", {}) defaults = provider_backend_cfg.get("defaults", {})
@ -80,11 +91,16 @@ def render_templates():
provider_template = env.get_template("provider.tf.j2") provider_template = env.get_template("provider.tf.j2")
backend_template = env.get_template("backend.tf.j2") backend_template = env.get_template("backend.tf.j2")
target_component = detect_target_component()
for module_name, module_config in modules.items(): for module_name, module_config in modules.items():
module_dir_name = module_config.get("component_dir") or module_name.split("-", 1)[ module_dir_name = module_config.get("component_dir") or module_name.split("-", 1)[
-1 -1
] ]
module_dir = ENVS_DIR / module_dir_name module_dir = ENVS_DIR / module_dir_name
if target_component and module_dir_name != target_component:
continue
if not module_dir.exists(): if not module_dir.exists():
print(f"⚠️ Skipping {module_name}: {module_dir} not found") print(f"⚠️ Skipping {module_name}: {module_dir} not found")
continue continue