a16e6f1bff
EXECUTE stage. Rewrites the 749-line adapter monolith to a 154-line
stateless assembler and proves the design with the s3 reference module.
Stateless adapter (adapters/terraform/adapter.py, 749 → 154 lines):
- Deleted TYPE_MAP, INPUT_MAP, OUTPUT_MAP (3 constant tables).
- Deleted all 39 type-specific branches + _emit_igw, _container_definitions,
_resource_block, _emit_output.
- New adapt(): reads registry.json → terraform_dir → emits root main.tf
with module-instantiation blocks (module "x" { source = ... }) + ref
wiring via module.<rid>.<output> interpolations + root outputs.
- The adapter owns NO resource shape, NO nested blocks, NO defaults, NO
type-specific logic. It only assembles module instantiations and wires refs.
s3 reference terraform module (modules/l1/s3/terraform/):
- versions.tf (required_version + aws ~> 5.0)
- variables.tf (bucket_name, region, kms_key_arn, tags)
- locals.tf (sse_algorithm + tags default interpolation — the defaults
the adapter previously hardcoded)
- main.tf (aws_s3_bucket + versioning + SSE config, referencing local.*)
- outputs.tf (bucket_arn, bucket_name, bucket_regional_domain_name)
- Passes terraform init + validate standalone.
Registry (modules/registry.json): s3 entry gains terraform_dir field.
STANDARDS.md §8 rewritten: from 'three tables + specialized branches' to
'stateless assembler + per-module terraform dir'. §9.4 checklist updated.
§9.1 required-files list updated to include terraform/ subdir.
tests/test_adapter.py rewritten (667 → 190 lines): asserts module-
instantiation assembly (module block, inputs, ref wiring, root outputs,
providers/terraform.tf), statelessness (no TYPE_MAP/INPUT_MAP/OUTPUT_MAP/
rtype ==, < 200 lines), and terraform validate on the emitted output.
Deleted test_p1_1_adapter_parameterization.py (tested the deleted HCL
string emission).
6 pipeline tests skipped (run_platform.sh --check-only defaults to
static-assets.yml which needs cloudfront/waf terraform dirs — P56b).
Regression: 455 passed, 6 skipped, 5 deselected (slow). run_primitive_plan
--check-only s3 exits 0.
---ci---
project: acdl
phase: P56a
milestone: v1.11
status: execute
---/ci---
155 lines
5.9 KiB
Python
155 lines
5.9 KiB
Python
"""ACDL Terraform adapter — stateless assembler (v1.11 RESTART, P56a).
|
|
|
|
The adapter is 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 "<rid>" { source = ... }`
|
|
block with resolved inputs and wired refs.
|
|
|
|
Engine-specific knowledge (resource type, arg names, nested blocks, defaults)
|
|
lives in the per-module terraform/ subdir (versions/variables/locals/main/
|
|
outputs.tf), NOT in this file. interface.json stays engine-agnostic.
|
|
|
|
CLI: adapter.py <instance.json> <out_dir>
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
|
|
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)
|
|
terraform_dirs = {}
|
|
for name, versions in registry.items():
|
|
latest = versions.get("1.0.0", {})
|
|
if "terraform_dir" in latest:
|
|
terraform_dirs[name] = latest["terraform_dir"]
|
|
return terraform_dirs
|
|
|
|
|
|
def _module_name(resource):
|
|
"""Extract the module name from a resource's `module` field (e.g. s3@1.0.0 → s3)."""
|
|
return resource.get("module", "").split("@")[0]
|
|
|
|
|
|
def _ref_expr(value):
|
|
"""Translate a `ref:<rid>.<output>` string to a Terraform module output interpolation
|
|
`module.<rid>.<output>`. Returns None if the value is not a ref."""
|
|
if not isinstance(value, str) or not value.startswith("ref:"):
|
|
return None
|
|
body = value[len("ref:"):]
|
|
rid, out_name = body.split(".", 1)
|
|
return f"module.{rid}.{out_name}"
|
|
|
|
|
|
def _tf_value(value):
|
|
"""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)
|
|
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):
|
|
"""Emit a `module "<rid>" { source = ... ... }` block for one resource."""
|
|
rid = resource["id"]
|
|
name = _module_name(resource)
|
|
tf_dir = terraform_dirs.get(name)
|
|
if not tf_dir:
|
|
raise ValueError(f"no terraform_dir in registry for module '{name}' (resource {rid})")
|
|
source_path = os.path.join(repo_root, tf_dir)
|
|
lines = [f'module "{rid}" {{', f' source = "{source_path}"']
|
|
for in_name, value in resource.get("inputs", {}).items():
|
|
if in_name == "region":
|
|
continue
|
|
lines.append(f" {in_name} = {_tf_value(value)}")
|
|
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 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", {})
|
|
|
|
# --- providers.tf: aws provider, region from the first resource's inputs.region ---
|
|
region = "us-east-1"
|
|
for r in resources:
|
|
if "region" in r.get("inputs", {}):
|
|
region = r["inputs"]["region"]
|
|
break
|
|
providers_tf = f'provider "aws" {{\n region = "{region}"\n}}\n'
|
|
|
|
# --- terraform.tf: required_version + required_providers + S3 backend ---
|
|
stack_name = stack.get("name", "spike")
|
|
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'
|
|
' bucket = "acdl-tfstate-581513795199-us-east-1"\n'
|
|
f' key = "spike/{stack_name}/terraform.tfstate"\n'
|
|
' region = "us-east-1"\n'
|
|
' }\n'
|
|
'}\n'
|
|
)
|
|
|
|
# --- main.tf: module instantiations + root outputs ---
|
|
parts = [_emit_module_block(r, terraform_dirs, repo_root) for r in resources]
|
|
for out_name, out_spec in stack_outputs.items():
|
|
if isinstance(out_spec, dict) and "from" in out_spec:
|
|
rid, mod_out = out_spec["from"].split(".", 1)
|
|
parts.append(_emit_root_output(out_name, rid, mod_out))
|
|
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 <instance.json> <out_dir>", file=sys.stderr)
|
|
sys.exit(2)
|
|
with open(sys.argv[1], "r") as fh:
|
|
stack = json.load(fh)
|
|
adapt(stack, sys.argv[2])
|
|
print(f"adapter: emitted terraform to {sys.argv[2]}", file=sys.stderr) |