tags: detect out-of-band deletion of google_tags_location_tag_binding (#24408)#77
Open
jbbqqf wants to merge 11 commits into
Open
tags: detect out-of-band deletion of google_tags_location_tag_binding (#24408)#77jbbqqf wants to merge 11 commits into
jbbqqf wants to merge 11 commits into
Conversation
… (#24408)
resourceTagsLocationTagBindingRead listed bindings under the parent and
searched for the one matching the resource's name. When the binding had
been deleted outside of Terraform (e.g. via gcloud, the console, or
another tool), the listing returned without the matching item. The
existing code returned nil in that path without clearing d.SetId(),
so Terraform kept the resource in state and never offered to recreate it.
This change clears the resource ID and returns nil in three paths:
1. The parent has no tag bindings at all (tagBindings key missing or nil).
2. flattenNestedTagsLocationTagBinding returns nil (no matching item
found across paginated results).
3. The pagination break condition no longer abandons accumulated
results when a subsequent page is empty — fixed by switching that
return nil to break.
This matches the behaviour of the sibling google_tags_tag_binding
resource and lets terraform plan / apply correctly propose recreation
when a binding has been deleted out of band.
Fixes hashicorp/terraform-provider-google#24408
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
google_tags_location_tag_binding.Readdid not detect that a tag bindinghad been deleted out-of-band (gcloud / console / another tool). When the
listing under the parent returned no matching binding, the function
returned
nilwithout clearingd.SetId(""), so Terraform kept theresource in state and never offered to recreate it on the next plan.
Fixes hashicorp/terraform-provider-google#24408 — see hashicorp/terraform-provider-google#24408
Why
resourceTagsLocationTagBindingRead(inmmv1/third_party/terraform/services/tags/resource_tags_location_tag_binding.go.tmpl)hits the regional
tagBindings.list?parent=...&pageSize=300endpointand then walks the page to find the entry whose
namematches theresource's stored ID. Three paths in that walk currently mishandle the
"binding not found" outcome:
tagBindingskey in the response (parent has zero bindings)— the code returned
nildirectly, leaving Terraform's state intact.tagBindings, the existing code didreturn nil, abandoning anybindings already accumulated from prior pages.
flattenNestedTagsLocationTagBindingreturns nil (the matchingitem was not found in any page) — the caller called
d.Set("name", ...)on the nil map and continued, never clearing the ID.
In all three cases the resource quietly stayed in state. A subsequent
terraform planthen either succeeded with no diff (silently maskingthe deletion) or failed later with a confusing "tag binding not found"
on update or destroy.
The sibling resource
google_tags_tag_bindingalready handles thiscorrectly:
This change ports the same pattern to the location-aware variant.
GCP API reference:
string when the resource is no longer found" — https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/read
What changed
Single hand-written template:
Three behavior changes inside
Read:res["tagBindings"]is missing/nil, log +d.SetId("")+ return.tagBindings,breakout of thepagination loop instead of dropping the partial result.
flattenNestedTagsLocationTagBinding, if the result is nil,log +
d.SetId("")+ return, before anyd.Set(...)call.The fix matches the structure of
resource_tags_tag_binding.goso thetwo resources behave consistently from a state-drift perspective.
Edge cases tested
d.SetId(""), log message, return; next plan proposes recreation.gofile inspectionflattenNestedTagsLocationTagBindingreturns nil, the newif res == nilbranch fires, ID cleared.gofile shows the new branch atReadline ~266tagBindingsbreak; subsequent search either finds the binding (state preserved) or doesn't (case 2 fires)breakreplaces the earlyreturn nilthat previously discardedpViewTest protocol
make build OUTPUT_PATH=... VERSION=ga(mmv1 regen)go build ./google/services/tags/...on regenerated TPGgo build ./...on full regenerated TPGgcloud resource-manager tags bindings deletebetween two plans on the same project resource. The test is a faithful port of the working pattern inresource_tags_tag_binding.go; live verification is best done as part of the existingTestAccTagsLocationTagBinding_*acceptance suite, which already exercises real bindings against the API.I considered adding a dedicated drift acceptance test
(
TestAccTagsLocationTagBinding_outOfBandDelete) but decided againstit in this PR to keep the diff minimal and reviewer-friendly. The
maintainers may want to add one in the existing
resource_tags_test.go(the file is shared by both binding variantsin
mmv1/third_party/terraform/services/tags/).Resources
google_tags_location_tag_bindingdoes not detect when tag binding is deleted outside of Terraform hashicorp/terraform-provider-google#24408google/services/tags/resource_tags_tag_binding.go::resourceTagsTagBindingReadDisclosure
This PR was drafted with assistance from Claude Code as part of a focused
contribution batch on hand-written TPG resources. The fix was reviewed
manually against the sibling
google_tags_tag_bindingresource pattern.The mmv1 generation was run locally and the regenerated provider
compiled clean.
The author (a human) reviewed the diff and the regenerated
.gofilebefore opening this PR.