fix(api_token): migrate permission_groups data source references instead of extracting map keys#314
Open
rotem-cloud wants to merge 1 commit into
Open
Conversation
…ead of extracting map keys
The cloudflare_api_token migrator incorrectly extracted map key strings
(e.g. "DNS Read") from data source index access expressions like
data.cloudflare_api_token_permission_groups.all.permissions["DNS Read"]
and used them as permission group IDs. This produced invalid output like
id = "DNS Read" instead of the actual permission group UUID.
Changes:
- Fix transformPermissionGroups to use bracket-depth tracking so that
TokenQuotedLit tokens inside index access (depth 2) are not collected
as permission group IDs
- When expression references are detected, parse them as
cloudflare_api_token_permission_groups data source refs and convert to
for-expressions against the v5 replacement data source:
[for pg in data.cloudflare_api_token_permission_groups_list.<label>.result : pg.id if pg.name == "<name>"][0]
- Add datasource migrator that renames cloudflare_api_token_permission_groups
to cloudflare_api_token_permission_groups_list (instead of silently
removing via Preprocess regex)
- Unrecognized expressions fall back to { id = <expr> } wrapping with a
diagnostic warning
Closes #298
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
The
cloudflare_api_tokenmigrator incorrectly handledpermission_groupsthat referenced thecloudflare_api_token_permission_groupsdata source. Expressions like:had the map key string (
"DNS Read") extracted as the permission group ID, producing invalid output:Additionally, the data source block was silently removed via a
Preprocessregex with no warning.Fix
1. Bracket-depth tracking in
transformPermissionGroupsReplaced the boolean
inListflag with an integerbracketDepthcounter.TokenQuotedLittokens are only collected at depth 1 (direct list elements), not at depth 2+ (index access keys like["DNS Read"]).2. Data source reference → for-expression conversion
When expression elements are detected, each is parsed to check if it matches
data.cloudflare_api_token_permission_groups.<label>.<category>["<name>"]. If so, it's converted to a for-expression against the v5 replacement data source:3. Datasource migrator (new)
Added
internal/datasources/api_token_permission_groups/v4_to_v5.gothat renames the data source block fromcloudflare_api_token_permission_groupstocloudflare_api_token_permission_groups_listinstead of silently removing it.4. Fallback for unrecognized expressions
Expressions that don't match the data source pattern are wrapped as
{ id = <expr> }with a diagnostic warning.Testing
tf-migrate→ v5 config with for-expressionsterraform applywith v5 provider succeedsterraform planshows no driftFiles changed
internal/resources/api_token/v4_to_v5.gointernal/datasources/api_token_permission_groups/v4_to_v5.gointernal/registry/registry.gointernal/resources/api_token/v4_to_v5_test.gointegration/.../api_token/expected/api_token.tfCloses #298