Compare commits

..
Author SHA1 Message Date
claude-bot 853928b6b1 docs(opentofu): record the hairpin _id diff and retract the PUT 400 warning
ci/woodpecker/pr/ci-scripts-test Pipeline was successful
ci/woodpecker/pr/pr-review Pipeline was successful
Both of these were learned the expensive way today and existed only in commit
messages and PR descriptions, which is not where anyone looks.

providers.tf carried a warning that a UniFi PUT may require _id in the body,
which restapi does not send, and that a 400 should be answered by tainting the
object so CI recreates it. That is wrong on this controller. Tested against a
live rule with exactly the body tofu sends: HTTP 200, object returned intact,
rule byte-identical before and after, 60 rules before and after. Confirmed at
scale by apply 5675 — all 60 modified, no 400s, ci.renner.uno and
immich.renner.uno still 200.

That wrong warning had a cost. A note saying an apply might break firewall
rules is one nobody argues with, so the apply did not get run, and the
renner-uno-k8s-mariadb bucket added in b976a67b sat uncreated for three days
while the WordPress backup failed nightly with "The specified bucket does not
exist". The warning is replaced with the evidence that disproves it.

unifi_nat_hairpin.tf now records that the 60-rule `- _id` diff is permanent and
cosmetic, and lists the four attempts to remove it with the plan number that
disproved each: ignore_changes_to (5661), ignore_all_server_changes (5662),
restapi 3.x ignore_server_additions (5667), and create_returns_object (5677).

The first three cannot work in principle — `data` is a configured attribute, so
Terraform core diffs it against state before provider-side ignore logic runs.
The fourth was the better theory, that PUT responses re-polluted `data`, and it
is wrong too: the diff returned on the plan immediately after the apply. That
means the response is written into `data` on the read path, and settling it
needs the provider source rather than more guesses at its schema.

No behaviour change. Documentation only, so the next person inherits the
findings instead of the dead ends.

Co-Authored-By: Claude Bot <claude-bot@renner.uno>
2026-08-19 08:34:42 +02:00
k.renner 3fac750430 Merge pull request 'fix(opentofu): make apply safe — no container replacement, no permanent NAT diff' (#513) from fix-rustfs-shim-replace into master
ci/woodpecker/push/deploy-infrastructure Pipeline was successful
ci/woodpecker/manual/deploy-infrastructure Pipeline was successful
ci/woodpecker/cron/issue-fix Pipeline was successful
ci/woodpecker/cron/alert-monitor Pipeline was successful
Reviewed-on: k.renner/homelab#513
2026-08-19 06:15:35 +00:00
2 changed files with 63 additions and 12 deletions
+19 -2
View File
@@ -59,8 +59,25 @@ provider "unifi" {
# UniFi Network v2 API (policy-table NAT rules). Same controller and API key
# as the unifi provider above; the v2 endpoints are simply not covered by it.
# UniFi PUT quirk: updates may require _id in the body, which restapi does
# not send — if an in-place update 400s, taint the object so CI recreates it.
# UniFi PUT quirk — DISPROVEN 2026-08-19, left here because it was wrong for
# long enough to do damage. The claim was that a UniFi PUT may require _id in
# the body, which restapi does not send, and that a 400 should be answered by
# tainting the object so CI recreates it. Tested directly against a live rule
# with exactly the body tofu sends:
#
# PUT /proxy/network/v2/api/site/default/nat/6a61c7d7bbd7b2ca90981f51
# body: the object, _id removed -> HTTP 200, object returned intact
# rule re-read and diffed before/after -> byte-identical
# rule count -> 60 before, 60 after
#
# Confirmed again at scale by apply 5675: all 60 rules "Modifications complete
# after 0s", no 400s, ci.renner.uno and immich.renner.uno both still 200.
#
# This mattered. A warning that applying might break firewall rules is a
# warning nobody argues with, so the apply did not get run — and the
# renner-uno-k8s-mariadb bucket added in b976a67b sat uncreated for three days
# while the WordPress backup failed nightly with "The specified bucket does not
# exist". Applying these NAT rules is safe. Do not taint them.
#
# create_returns_object, NOT write_returns_object. Both make the provider store
# the API response into `data`; write_returns_object does it for PUT as well as
+44 -10
View File
@@ -107,17 +107,51 @@ resource "restapi_object" "hairpin" {
for_each = local.hairpin_rules
path = "/nat"
# KNOWN PERMANENT DIFF — cosmetic. Do not try to fix it with a provider
# setting; four have been tried and none work. Recorded here so the next
# person does not spend a day rediscovering that.
#
# Every plan shows all 60 of these as "will be updated in-place" with the
# entire diff being one line:
#
# ~ data = jsonencode( ~ { - _id = "6a61c7d7bbd7b2ca90981f51"
# # (15 unchanged attributes hidden) } )
#
# The UDM assigns _id and returns it; the provider puts the response into
# `data`; the `data` below never sets it. So state carries a field the config
# does not. It survives an apply: 5675 rewrote all 60, and the very next plan
# (5677) showed the same 60 again.
#
# Tried, with the plan that disproved each — do not repeat these:
#
# ignore_changes_to = ["_id"] 5661 60 remained
# ignore_all_server_changes = true 5662 60 remained
# restapi 3.x + ignore_server_additions = true 5667 60 remained, and
# added an api_data diff
# write_returns_object -> create_returns_object 5677 60 remained
#
# The first three cannot work in principle: `data` is a configured attribute,
# so Terraform core diffs config against state before any provider-side ignore
# logic runs. The fourth was the better theory — that PUT responses were
# re-polluting `data` — and it is wrong too, which means the response is
# written into `data` on the READ path. Settling it properly needs the
# provider source, not more guesses at the schema.
#
# It is safe to apply through. Verified twice: a single PUT without _id
# returns 200 and leaves the rule byte-identical (see providers.tf), and
# apply 5675 modified all 60 with no errors and no service impact.
data = jsonencode({
description = "Hairpin ${each.key}"
type = "DNAT"
enabled = true
exclude = false
in_interface = each.value.network_id
ip_address = each.value.endpoint.target
ip_version = each.value.endpoint.ip_version
protocol = "tcp_udp"
rule_index = each.value.rule_index
logging = false
description = "Hairpin ${each.key}"
type = "DNAT"
enabled = true
exclude = false
in_interface = each.value.network_id
ip_address = each.value.endpoint.target
ip_version = each.value.endpoint.ip_version
protocol = "tcp_udp"
rule_index = each.value.rule_index
logging = false
is_predefined = false
pppoe_use_base_interface = false
setting_preference = "manual"