Skip to content

Commit 1fcf6cf

Browse files
authored
Merge pull request #941 from timofurrer/bugfix/deploy-tokens-pagination
resource/gitlab_deploy_token: Implement pagination when reading tokens
2 parents 582968c + 7547989 commit 1fcf6cf

File tree

2 files changed

+90
-23
lines changed

2 files changed

+90
-23
lines changed

internal/provider/resource_gitlab_deploy_token.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,63 @@ func resourceGitlabDeployTokenRead(ctx context.Context, d *schema.ResourceData,
165165
return diag.FromErr(err)
166166
}
167167

168-
var deployTokens []*gitlab.DeployToken
168+
var deployToken *gitlab.DeployToken
169+
170+
extractDeployToken := func(paginatedDeployTokens []*gitlab.DeployToken) *gitlab.DeployToken {
171+
for _, token := range paginatedDeployTokens {
172+
if token.ID == deployTokenID {
173+
return token
174+
}
175+
}
176+
return nil
177+
}
169178

170179
if isProject {
171180
log.Printf("[DEBUG] Read GitLab deploy token %d in project %s", deployTokenID, project.(string))
172-
deployTokens, _, err = client.DeployTokens.ListProjectDeployTokens(project, nil, gitlab.WithContext(ctx))
173-
181+
options := gitlab.ListProjectDeployTokensOptions{
182+
Page: 1,
183+
PerPage: 20,
184+
}
185+
for options.Page != 0 && deployToken == nil {
186+
paginatedDeployTokens, resp, err := client.DeployTokens.ListProjectDeployTokens(project, &options, gitlab.WithContext(ctx))
187+
if err != nil {
188+
return diag.FromErr(err)
189+
}
190+
deployToken = extractDeployToken(paginatedDeployTokens)
191+
options.Page = resp.NextPage
192+
}
174193
} else if isGroup {
175194
log.Printf("[DEBUG] Read GitLab deploy token %d in group %s", deployTokenID, group.(string))
176-
deployTokens, _, err = client.DeployTokens.ListGroupDeployTokens(group, nil, gitlab.WithContext(ctx))
177-
}
178-
if err != nil {
179-
return diag.FromErr(err)
180-
}
181-
182-
for _, token := range deployTokens {
183-
if token.ID == deployTokenID {
184-
d.Set("name", token.Name)
185-
d.Set("username", token.Username)
186-
187-
if token.ExpiresAt != nil {
188-
d.Set("expires_at", token.ExpiresAt.Format(time.RFC3339))
189-
}
190-
191-
if err := d.Set("scopes", token.Scopes); err != nil {
195+
options := gitlab.ListGroupDeployTokensOptions{
196+
Page: 1,
197+
PerPage: 20,
198+
}
199+
for options.Page != 0 && deployToken == nil {
200+
paginatedDeployTokens, resp, err := client.DeployTokens.ListGroupDeployTokens(group, &options, gitlab.WithContext(ctx))
201+
if err != nil {
192202
return diag.FromErr(err)
193203
}
194-
195-
return nil
204+
deployToken = extractDeployToken(paginatedDeployTokens)
205+
options.Page = resp.NextPage
196206
}
197207
}
198208

199-
log.Printf("[DEBUG] GitLab deploy token %d in group %s was not found", deployTokenID, group.(string))
209+
if deployToken == nil {
210+
log.Printf("[DEBUG] GitLab deploy token %d in was not found, removing from state", deployTokenID)
211+
d.SetId("")
212+
return nil
213+
}
200214

201-
d.SetId("")
215+
d.Set("name", deployToken.Name)
216+
d.Set("username", deployToken.Username)
217+
218+
if deployToken.ExpiresAt != nil {
219+
d.Set("expires_at", deployToken.ExpiresAt.Format(time.RFC3339))
220+
}
221+
222+
if err := d.Set("scopes", deployToken.Scopes); err != nil {
223+
return diag.FromErr(err)
224+
}
202225

203226
return nil
204227
}

internal/provider/resource_gitlab_deploy_token_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ func TestAccGitlabDeployToken_basic(t *testing.T) {
3333
},
3434
})
3535
}
36+
func TestAccGitlabDeployToken_pagination(t *testing.T) {
37+
testAccCheck(t)
38+
39+
testGroup := testAccCreateGroups(t, 1)[0]
40+
testProject := testAccCreateProject(t)
41+
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
ProviderFactories: providerFactories,
45+
CheckDestroy: testAccCheckGitlabDeployTokenDestroy,
46+
Steps: []resource.TestStep{
47+
{
48+
Config: testAccGitlabDeployTokenPaginationConfig(25, testGroup.ID, testProject.ID),
49+
},
50+
// In case pagination wouldn't properly work, we would get that the plan isn't empty,
51+
// because some of the deploy tokens wouldn't be in the first page and therefore
52+
// considered non-existing, ...
53+
{
54+
Config: testAccGitlabDeployTokenPaginationConfig(25, testGroup.ID, testProject.ID),
55+
PlanOnly: true,
56+
},
57+
},
58+
})
59+
}
3660

3761
func testAccCheckGitlabDeployTokenExists(n string, deployToken *gitlab.DeployToken) resource.TestCheckFunc {
3862
return func(s *terraform.State) error {
@@ -163,6 +187,26 @@ resource "gitlab_deploy_token" "foo" {
163187
`, rInt, rInt)
164188
}
165189

190+
func testAccGitlabDeployTokenPaginationConfig(numberOfTokens int, groupID int, projectID int) string {
191+
return fmt.Sprintf(`
192+
resource "gitlab_deploy_token" "example_group" {
193+
group = %d
194+
name = "deploy-token-${count.index}"
195+
scopes = ["read_registry"]
196+
197+
count = %d
198+
}
199+
200+
resource "gitlab_deploy_token" "example_project" {
201+
project = %d
202+
name = "deploy-token-${count.index}"
203+
scopes = ["read_registry"]
204+
205+
count = %d
206+
}
207+
`, groupID, numberOfTokens, projectID, numberOfTokens)
208+
}
209+
166210
type expiresAtSuppressFuncTest struct {
167211
description string
168212
old string

0 commit comments

Comments
 (0)