fix: handle pagination for service account tokens
Closes #6708 (closed)
Closes #6709 (closed)
Changes
Fixed Pagination in Resource Read Function
-
File:
internal/provider/resource_gitlab_group_service_account_access_token.go - Modified the
Readfunction (lines 539-557) to implement proper pagination when listing service account access tokens - The fix follows the exact pattern used in the datasource:
- Initialized pagination options with
Page: 1andPerPage: 20 - Created an empty slice to accumulate results
- Implemented a pagination loop that continues while
options.Page != 0 - Appended paginated results to the accumulator
- Updated
options.PagefromapiResp.NextPageafter each iteration
- Initialized pagination options with
- This ensures that when a service account has more than 20 access tokens, all tokens are retrieved and the resource can correctly find its token even if it's not in the first page of results
Added Comprehensive Pagination Test
-
File:
internal/provider/resource_gitlab_group_service_account_access_token_test.go -
Test Function:
TestAccGitlabGroupServiceAccountAccessToken_paginationHandling -
Test Coverage:
- Step 1: Creates a token with basic configuration and stores the original token value
-
Step 2: Uses
PreConfigto create 30 additional tokens (triggering pagination since >20 tokens exist), then re-runs the same config to verify the original token is NOT destroyed and recreated by checking that the token value hasn't changed - Step 3: Verifies the resource can be imported correctly
- The test validates that the pagination fix works correctly by ensuring the resource doesn't incorrectly detect the token as deleted when there are more than 20 tokens
Fixed URL-encoded name issue
- Removed the need to ParseInt on the Group before passing it to
client-gosince the Group is a ID-or-name input option
Updated the Basic Test to use URL-encoded input for Group instead of ID
- This validates that in the future, we ensure url-encoded inputs work properly
Summary
The implementation:
- Fixes the root cause - The resource now properly handles pagination when listing tokens
- Follows existing patterns - Uses the same pagination approach as the datasource
- Includes comprehensive testing - New test specifically validates pagination behavior with >20 tokens
- Maintains backward compatibility - No breaking changes to the resource API
The bug where tokens were incorrectly detected as deleted when a service account had more than 20 access tokens is now resolved.
Edited by Patrick Rice