Skip to content

Commit 74122da

Browse files
authored
Merge pull request #302 from veeravallib/master
Update resource gitlab_group_label to read labels from all pages
2 parents 0979824 + a6bd59f commit 74122da

File tree

2 files changed

+85
-18
lines changed

2 files changed

+85
-18
lines changed

gitlab/resource_gitlab_group_label.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package gitlab
22

33
import (
4-
"errors"
5-
"fmt"
64
"log"
75

86
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -68,24 +66,27 @@ func resourceGitlabGroupLabelRead(d *schema.ResourceData, meta interface{}) erro
6866
labelName := d.Id()
6967
log.Printf("[DEBUG] read gitlab group label %s/%s", group, labelName)
7068

71-
labels, _, err := client.GroupLabels.ListGroupLabels(group, nil)
72-
if err != nil {
73-
return err
74-
}
75-
found := false
76-
for _, label := range labels {
77-
if label.Name == labelName {
78-
d.Set("description", label.Description)
79-
d.Set("color", label.Color)
80-
d.Set("name", label.Name)
81-
found = true
82-
break
69+
page := 1
70+
labelsLen := 0
71+
for page == 1 || labelsLen != 0 {
72+
labels, _, err := client.GroupLabels.ListGroupLabels(group, &gitlab.ListGroupLabelsOptions{Page: page})
73+
if err != nil {
74+
return err
8375
}
84-
}
85-
if !found {
86-
return errors.New(fmt.Sprintf("label %s does not exist or the user does not have permissions to see it", labelName))
76+
for _, label := range labels {
77+
if label.Name == labelName {
78+
d.Set("description", label.Description)
79+
d.Set("color", label.Color)
80+
d.Set("name", label.Name)
81+
return nil
82+
}
83+
}
84+
labelsLen = len(labels)
85+
page = page + 1
8786
}
8887

88+
log.Printf("[DEBUG] failed to read gitlab label %s/%s", group, labelName)
89+
d.SetId("")
8990
return nil
9091
}
9192

gitlab/resource_gitlab_group_label_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,34 @@ func TestAccGitlabGroupLabel_basic(t *testing.T) {
5252
}),
5353
),
5454
},
55+
{
56+
Config: testAccGitlabGroupLabelLargeConfig(rInt),
57+
Check: resource.ComposeTestCheckFunc(
58+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.20", &label),
59+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.30", &label),
60+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.40", &label),
61+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.10", &label),
62+
testAccCheckGitlabGroupLabelAttributes(&label, &testAccGitlabGroupLabelExpectedAttributes{
63+
Name: "FIXME11",
64+
Color: "#ffcc00",
65+
Description: "fix this test",
66+
}),
67+
),
68+
},
69+
{
70+
Config: testAccGitlabGroupLabelUpdateLargeConfig(rInt),
71+
Check: resource.ComposeTestCheckFunc(
72+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.20", &label),
73+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.30", &label),
74+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.40", &label),
75+
testAccCheckGitlabGroupLabelExists("gitlab_group_label.fixme.10", &label),
76+
testAccCheckGitlabGroupLabelAttributes(&label, &testAccGitlabGroupLabelExpectedAttributes{
77+
Name: "FIXME11",
78+
Color: "#ff0000",
79+
Description: "red label",
80+
}),
81+
),
82+
},
5583
},
5684
})
5785
}
@@ -70,7 +98,7 @@ func testAccCheckGitlabGroupLabelExists(n string, label *gitlab.GroupLabel) reso
7098
}
7199
conn := testAccProvider.Meta().(*gitlab.Client)
72100

73-
labels, _, err := conn.GroupLabels.ListGroupLabels(groupName, nil)
101+
labels, _, err := conn.GroupLabels.ListGroupLabels(groupName, &gitlab.ListGroupLabelsOptions{PerPage: 1000})
74102
if err != nil {
75103
return err
76104
}
@@ -167,3 +195,41 @@ resource "gitlab_group_label" "fixme" {
167195
}
168196
`, rInt, rInt, rInt)
169197
}
198+
199+
func testAccGitlabGroupLabelLargeConfig(rInt int) string {
200+
return fmt.Sprintf(`
201+
resource "gitlab_group" "foo" {
202+
name = "foo-%d"
203+
path = "foo-%d"
204+
description = "Terraform acceptance tests"
205+
visibility_level = "public"
206+
}
207+
208+
resource "gitlab_group_label" "fixme" {
209+
group = "${gitlab_group.foo.id}"
210+
name = format("FIXME%%02d", count.index+1)
211+
count = 100
212+
color = "#ffcc00"
213+
description = "fix this test"
214+
}
215+
`, rInt, rInt)
216+
}
217+
218+
func testAccGitlabGroupLabelUpdateLargeConfig(rInt int) string {
219+
return fmt.Sprintf(`
220+
resource "gitlab_group" "foo" {
221+
name = "foo-%d"
222+
path = "foo-%d"
223+
description = "Terraform acceptance tests"
224+
visibility_level = "public"
225+
}
226+
227+
resource "gitlab_group_label" "fixme" {
228+
group = "${gitlab_group.foo.id}"
229+
name = format("FIXME%%02d", count.index+1)
230+
count = 99
231+
color = "#ff0000"
232+
description = "red label"
233+
}
234+
`, rInt, rInt)
235+
}

0 commit comments

Comments
 (0)