fix(zone): prevent data source .zone_id from being mangled to .name_id#305
Open
koslib wants to merge 2 commits into
Open
fix(zone): prevent data source .zone_id from being mangled to .name_id#305koslib wants to merge 2 commits into
koslib wants to merge 2 commits into
Conversation
The computed-attribute mapping introduced for the cloudflare_zone resource (rewriting .zone → .name) used an unanchored regex, causing two problems: 1. .zone matched the prefix of .zone_id, turning it into .name_id — an attribute that does not exist on the v5 zone data source. 2. cloudflare_zone also matched inside data.cloudflare_zone, so the resource rule leaked onto data source references. Fix by adding regexp.QuoteMeta and a trailing word boundary (\b) to both the computed-attribute-mapping and attribute-rename regex builders in applyGlobalPostprocessing. Add a ComputedAttributeMapping to the data.cloudflare_zone datasource migrator so that cross-file references to the v4 zone_id output attribute are correctly rewritten to the v5 id attribute.
Add TestComputedAttrMappingBoundary to verify that: - The .zone → .name resource rule does not corrupt .zone_id references. - The data.cloudflare_zone .zone_id → .id datasource rule fires correctly. - Valid .name references on the data source are left untouched. Extend the zone_datasource integration fixture with Scenario 8: an import block and output using data.cloudflare_zone.by_id.zone_id, which must become .id after migration.
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.
Problem
When
tf-migrate migrateprocesses configs that reference the zone data source'szone_idoutput (e.g. inimportblocks or string interpolations), the migration produces an invalid reference:The correct v5 output is
.id:Root cause
Two compounding issues introduced in #295:
The regex built in
applyGlobalPostprocessingfor computed-attribute mappings had no trailing word boundary. Thecloudflare_zoneresource rule (\.zone→.name) matched the prefix of.zone_id, silently rewriting it to.name_id.Because
cloudflare_zoneis an unanchored substring ofdata.cloudflare_zone, the resource rule also leaked onto data source references. Thedata.cloudflare_zonemigrator had no mapping of its own to correctly handle itszone_idoutput attribute.Fix
cmd/tf-migrate/main.go— addregexp.QuoteMetaand a trailing\bword boundary to both the computed-attribute-mapping and attribute-rename regex builders inapplyGlobalPostprocessing. This prevents any attribute name from being matched as a prefix of a longer one, and ensures resource-type strings containing.are treated as literals.internal/datasources/zone/v4_to_v5.go— addGetComputedAttributeMappings()to the data source migrator so cross-filezone_idoutput references are explicitly rewritten toid.Tests
TestComputedAttrMappingBoundary(unit) — verifies the boundary fix:.zone_idis not touched by the resource rule,.zone_idis correctly rewritten to.idby the datasource rule, and valid.namereferences are preserved.zone_datasourceintegration fixture — covers animportblock andoutputusing.zone_id, asserting the output is.id.Test layers run:
make test(unit + integration) — all pass.Related
Regression from #295.