"""Nova Terraform adapter — stateless assembler (v1.11 RESTART, P56a). A STATELESS ASSEMBLER. It owns no module content — no resource shape, no nested HCL blocks, no defaults, no type-specific logic. It reads the registry to find each L1 module's terraform/ dir, then emits a root main.tf that instantiates each resource as a `module "" { source }` block with resolved inputs and wired refs. Engine-specific knowledge lives in the per-module terraform/ subdir, NOT in this file. CLI: adapter.py """ import json, os, sys _R = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, _R) if _R not in sys.path else None from core import env def _load_registry(repo_root): """Load registry.json → {module_name: terraform_dir}.""" with open(os.path.join(repo_root, "modules", "registry.json")) as fh: registry = json.load(fh) return {n: v.get("1.0.0", {}).get("terraform_dir") for n, v in registry.items() if v.get("1.0.0", {}).get("terraform_dir")} def _module_name(resource): """Extract the module name from a resource's `module` field (s3@1.0.0 → s3).""" return resource.get("module", "").split("@")[0] def _load_env_json(env_name, repo_root): """Load core/environments/.json → dict (P03 W3, REQ-319). Returns {} if the file is absent (the adapter falls back to the computed state-bucket name). Sources env.state_backend.bucket + env.account_id + env.region for the S3 backend block. """ env_path = os.path.join(repo_root, "core", "environments", f"{env_name}.json") if not os.path.isfile(env_path): return {} with open(env_path, "r") as fh: return json.load(fh) def _resolve_state_bucket(env_json, region): """Resolve the S3 state-backend bucket name (P03 W3, REQ-319). Precedence: (1) env.state_backend.bucket when present + non-empty; (2) nova-tfstate-{account_id}-{region} from env.account_id + region (backwards-compat); (3) nova-tfstate-581513795199-{region} when account_id is absent (the only real account — bootstrap bucket). The env JSON is authoritative; NOVA_AWS_ACCOUNT_ID is no longer consulted for the bucket name. """ bucket = (env_json.get("state_backend") or {}).get("bucket") if bucket: return bucket account_id = env_json.get("account_id") or "581513795199" return f"nova-tfstate-{account_id}-{region}" def _ref_expr(value, data_source_names=None, id_remap=None): """Translate `ref:.` → `module..` (or `data.terraform_remote_state.platform.outputs.` for data sources). Returns None if not a ref. id_remap rewrites expanded multi-resource L1 sub-ids (e.g. alb-targetgroup → alb). CAP-013.""" if not isinstance(value, str) or not value.startswith("ref:"): return None rid, out_name = value[len("ref:"):].split(".", 1) if data_source_names and rid in data_source_names: return f"data.terraform_remote_state.platform.outputs.{out_name}" if id_remap: rid = id_remap.get(rid, rid) return f"module.{rid}.{out_name}" def _tf_value(value, data_source_names=None, id_remap=None): """Render a Python value as a Terraform expression fragment.""" if isinstance(value, bool): return "true" if value else "false" if isinstance(value, (int, float)) and not isinstance(value, bool): return str(value) if isinstance(value, str): ref = _ref_expr(value, data_source_names, id_remap) if ref is not None: return ref stripped = value.lstrip() if stripped and stripped[0] in "{[": try: parsed = json.loads(value) if isinstance(parsed, (dict, list)): return f"jsonencode({json.dumps(parsed, sort_keys=True)})" except json.JSONDecodeError: pass return f'"{value}"' if isinstance(value, (dict, list)): return f"jsonencode({json.dumps(value, sort_keys=True)})" raise ValueError(f"unsupported input value type {type(value).__name__}") def _emit_module_block(resource, terraform_dirs, repo_root, data_source_names=None, id_remap=None): """Emit a `module "" { source = ... ... }` block.""" rid = resource["id"] tf_dir = terraform_dirs.get(_module_name(resource)) if not tf_dir: raise ValueError(f"no terraform_dir for module '{_module_name(resource)}' (resource {rid})") lines = [f'module "{rid}" {{', f' source = "{os.path.join(repo_root, tf_dir)}"'] for in_name, value in resource.get("inputs", {}).items(): if in_name != "region": lines.append(f" {in_name} = {_tf_value(value, data_source_names, id_remap)}") lines.append("}") return "\n".join(lines) def _emit_root_output(out_name, rid, module_output_name): """Emit a root output wiring a module output to a stack output.""" return f'output "{out_name}" {{\n value = module.{rid}.{module_output_name}\n}}' def _child_id(group_ids): """Composition child id for resource ids sharing one terraform dir. Multi-resource L1s expand a child to `-` ids; the common-prefix (trailing `-` stripped) is the child id. Single-resource L1s: the id IS the child id.""" if len(group_ids) == 1: return group_ids[0] return os.path.commonprefix([i + "-" for i in group_ids]).rstrip("-") or group_ids[0] def adapt(stack_instance, out_dir): """Emit main.tf + terraform.tf + providers.tf to out_dir for the stack instance.""" os.makedirs(out_dir, exist_ok=True) repo_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) terraform_dirs = _load_registry(repo_root) stack = stack_instance.get("stack", {}) resources = stack_instance.get("resources", []) stack_outputs = stack_instance.get("outputs", {}) stack_name = stack.get("name", "spike") environment = stack.get("environment", "dev") # P03 W3 (REQ-319): state backend bucket + account_id + region come # from the env onboarding JSON (source of truth post-REQ-319). Bucket # = env.state_backend.bucket when present (fallback to the computed # nova-tfstate-{account_id}-{region} pattern for backwards compat). env_json = _load_env_json(environment, repo_root) region = env_json.get("region") or next( (r["inputs"]["region"] for r in resources if "region" in r.get("inputs", {})), "us-east-1", ) state_bucket = _resolve_state_bucket(env_json, region) providers_tf = f'provider "aws" {{\n region = "{region}"\n}}\n' # State key is env-scoped (v1.24 REQ-287): the {environment} segment lets # the env-transition detect-and-destroy step target the PRIOR env's state # without affecting the new env. No orphan path on environment promotion. terraform_tf = ( 'terraform {\n' ' required_version = ">= 1.9, < 1.10"\n' ' required_providers {\n' ' aws = {\n' ' source = "hashicorp/aws"\n' ' version = "~> 5.0"\n' ' }\n' ' }\n' ' backend "s3" {\n' f' bucket = "{state_bucket}"\n' f' key = "spike/{stack_name}/{environment}/terraform.tfstate"\n' f' region = "{region}"\n' ' }\n' '}\n' ) data_source_names = stack_instance.get("data_sources", []) parts = [] if data_source_names: remote_state_key = env.get_env("REMOTE_STATE_KEY", "platform/terraform.tfstate") parts.append( 'data "terraform_remote_state" "platform" {\n' ' backend = "s3"\n' ' config = {\n' f' bucket = "{state_bucket}"\n' f' key = "{remote_state_key}"\n' f' region = "{region}"\n' ' }\n' '}\n' ) # Deduplicate multi-resource L1s (ecs-service, alb, ...) to ONE module # block per terraform dir, named by the composition child id (common # prefix), NOT the first sub-resource id. Stack outputs + cross-module # refs reference expanded sub-ids, rewritten via id_remap. CAP-013. groups = {} # terraform_dir → {"ids": [...], "inputs": {}, "module": ""} for r in resources: tf_dir = terraform_dirs.get(_module_name(r)) if not tf_dir: raise ValueError(f"no terraform_dir for module '{_module_name(r)}' (resource {r['id']})") grp = groups.setdefault(tf_dir, {"ids": [], "inputs": {}, "module": r["module"]}) grp["ids"].append(r["id"]) for k, v in r.get("inputs", {}).items(): if k != "region": grp["inputs"].setdefault(k, v) id_remap = {} merged_resources = [] for tf_dir, grp in groups.items(): child_id = _child_id(grp["ids"]) for sub_id in grp["ids"]: id_remap[sub_id] = child_id merged_resources.append({"id": child_id, "module": grp["module"], "inputs": grp["inputs"]}) parts.extend(_emit_module_block(r, terraform_dirs, repo_root, set(data_source_names), id_remap) for r in merged_resources) for out_name, out_spec in stack_outputs.items(): if isinstance(out_spec, dict) and "from" in out_spec: rid = id_remap.get(out_spec["from"], out_spec["from"]) parts.append(_emit_root_output(out_name, rid, out_spec.get("output", out_name))) main_tf = "\n\n".join(parts) + "\n" with open(os.path.join(out_dir, "main.tf"), "w") as fh: fh.write(main_tf) with open(os.path.join(out_dir, "terraform.tf"), "w") as fh: fh.write(terraform_tf) with open(os.path.join(out_dir, "providers.tf"), "w") as fh: fh.write(providers_tf) return out_dir if __name__ == "__main__": if len(sys.argv) != 3: print("usage: adapter.py ", file=sys.stderr) sys.exit(2) with open(sys.argv[1], "r") as fh: adapt(json.load(fh), sys.argv[2]) print(f"adapter: emitted terraform to {sys.argv[2]}", file=sys.stderr)