Fix Dynu brownfield generator output handling

This commit is contained in:
beatz174-bit
2026-05-13 06:37:10 +10:00
parent 306e2c14db
commit 9de1bee542
2 changed files with 73 additions and 5 deletions
+37
View File
@@ -70,3 +70,40 @@ The helper script writes these files under `generated/`:
- `generated/import-dynu-dns-records.sh`
These are generated outputs meant for operator review before use in production.
## Troubleshooting
### Error: `'"'"'dynu_dns_records'"'"'`
Cause:
The helper script reads `terraform output -json` and expects an output named `dynu_dns_records`.
Fix:
```bash
cd infrastructure/terraform/dynu
terraform init
terraform apply -refresh-only
terraform output -json | jq 'keys'
```
Confirm `dynu_dns_records` appears in the key list.
If it does not, check that the Terraform config contains:
```hcl
data "dynu_dns_records" "root" {
hostname = var.dynu_root_domain
}
output "dynu_dns_records" {
value = data.dynu_dns_records.root.records
}
```
Then rerun:
```bash
python3 scripts/generate-brownfield-records.py --dry-run
```
@@ -86,8 +86,11 @@ def generate_resources(records: list[dict]) -> str:
lines.append(f" record_type = {hcl_value(rec.get('record_type'))}")
if rec.get("ttl") is not None:
lines.append(f" ttl = {hcl_value(rec.get('ttl'))}")
if rec.get("enabled") is not None:
lines.append(f" enabled = {hcl_value(rec.get('enabled'))}")
enabled = rec.get("enabled")
if enabled is None:
enabled = rec.get("state")
if enabled is not None:
lines.append(f" enabled = {hcl_value(enabled)}")
content = rec.get("content")
rtype = str(rec.get("record_type", "")).upper()
@@ -142,13 +145,41 @@ def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--dry-run", action="store_true", help="Print intended output paths without writing files.")
parser.add_argument("--overwrite", "--force", action="store_true", dest="overwrite", help="Overwrite existing generated files.")
parser.add_argument("--from-file", type=Path, help="Load inventory JSON from a file instead of calling terraform output.")
args = parser.parse_args()
try:
out = run_terraform_output()
records = out["dynu_dns_records"]["value"]
if args.from_file:
payload = json.loads(args.from_file.read_text(encoding="utf-8"))
if isinstance(payload, list):
records = payload
elif isinstance(payload, dict) and isinstance(payload.get("value"), list):
records = payload["value"]
elif isinstance(payload, dict) and isinstance(payload.get("dynu_dns_records", {}).get("value"), list):
records = payload["dynu_dns_records"]["value"]
else:
raise RuntimeError(
"--from-file JSON must be one of: a raw list of records, "
"a Terraform output wrapper with 'value' list, or full 'terraform output -json' "
"with 'dynu_dns_records.value'."
)
else:
out = run_terraform_output()
if "dynu_dns_records" not in out:
available = ", ".join(sorted(out.keys())) or "(none)"
raise RuntimeError(
"Missing Terraform output 'dynu_dns_records'. "
f"Available outputs: {available}. "
"Run 'terraform apply -refresh-only' after adding the "
"data.dynu_dns_records.root data source and dynu_dns_records output."
)
records = out["dynu_dns_records"].get("value")
if not isinstance(records, list):
raise RuntimeError("terraform output dynu_dns_records did not return a list.")
raise RuntimeError(
"Terraform output 'dynu_dns_records' did not return a list. "
f"Got: {type(records).__name__}"
)
GENERATED_DIR.mkdir(parents=True, exist_ok=True)
write_file(INVENTORY_FILE, json.dumps(records, indent=2, sort_keys=True) + "\n", args.dry_run, args.overwrite)