Skip to content

Commit 39b0b6d

Browse files
authored
Merge pull request #673 from zbindenren/group_badges
add support for gitlab group badges
2 parents d7059cf + 5996d8a commit 39b0b6d

File tree

4 files changed

+348
-0
lines changed

4 files changed

+348
-0
lines changed

docs/resources/group_badge.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# gitlab\_group\_badge
2+
3+
This resource allows you to create and manage badges for your GitLab groups.
4+
For further information, consult the [gitlab
5+
documentation](https://docs.gitlab.com/ee/user/project/badges.html#group-badges).
6+
7+
## Example Usage
8+
9+
```hcl
10+
resource "gitlab_group" "foo" {
11+
name = "foo-group"
12+
}
13+
14+
resource "gitlab_group_badge" "example" {
15+
group = gitlab_group.foo.id
16+
link_url = "https://example.com/badge-123"
17+
image_url = "https://example.com/badge-123.svg"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `group` - (Required) The id of the group to add the badge to.
26+
27+
* `link_url` - (Required) The url linked with the badge.
28+
29+
* `image_url` - (Required) The image url which will be presented on group overview.
30+
31+
## Attributes Reference
32+
33+
The resource exports the following attributes:
34+
35+
* `rendered_link_url` - The link_url argument rendered (in case of use of placeholders).
36+
37+
* `rendered_image_url` - The image_url argument rendered (in case of use of placeholders).
38+
39+
## Import
40+
41+
GitLab group badges can be imported using an id made up of `{group_id}:{badge_id}`,
42+
e.g.
43+
44+
```bash
45+
terraform import gitlab_group_badge.foo 1:3
46+
```

gitlab/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func Provider() terraform.ResourceProvider {
9999
"gitlab_project_freeze_period": resourceGitlabProjectFreezePeriod(),
100100
"gitlab_group_share_group": resourceGitlabGroupShareGroup(),
101101
"gitlab_project_badge": resourceGitlabProjectBadge(),
102+
"gitlab_group_badge": resourceGitlabGroupBadge(),
102103
},
103104
}
104105

gitlab/resource_gitlab_group_badge.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package gitlab
2+
3+
import (
4+
"log"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
gitlab "github.com/xanzy/go-gitlab"
10+
)
11+
12+
func resourceGitlabGroupBadge() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceGitlabGroupBadgeCreate,
15+
Read: resourceGitlabGroupBadgeRead,
16+
Update: resourceGitlabGroupBadgeUpdate,
17+
Delete: resourceGitlabGroupBadgeDelete,
18+
Importer: &schema.ResourceImporter{
19+
State: schema.ImportStatePassthrough,
20+
},
21+
22+
Schema: map[string]*schema.Schema{
23+
"group": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"link_url": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
},
31+
"image_url": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
},
35+
"rendered_link_url": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"rendered_image_url": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
},
44+
}
45+
}
46+
47+
func resourceGitlabGroupBadgeCreate(d *schema.ResourceData, meta interface{}) error {
48+
client := meta.(*gitlab.Client)
49+
groupID := d.Get("group").(string)
50+
options := &gitlab.AddGroupBadgeOptions{
51+
LinkURL: gitlab.String(d.Get("link_url").(string)),
52+
ImageURL: gitlab.String(d.Get("image_url").(string)),
53+
}
54+
55+
log.Printf("[DEBUG] create gitlab group variable %s/%s", *options.LinkURL, *options.ImageURL)
56+
57+
badge, _, err := client.GroupBadges.AddGroupBadge(groupID, options)
58+
if err != nil {
59+
return err
60+
}
61+
62+
badgeID := strconv.Itoa(badge.ID)
63+
64+
d.SetId(buildTwoPartID(&groupID, &badgeID))
65+
66+
return resourceGitlabGroupBadgeRead(d, meta)
67+
}
68+
69+
func resourceGitlabGroupBadgeRead(d *schema.ResourceData, meta interface{}) error {
70+
client := meta.(*gitlab.Client)
71+
ids := strings.Split(d.Id(), ":")
72+
groupID := ids[0]
73+
badgeID, err := strconv.Atoi(ids[1])
74+
if err != nil {
75+
return err
76+
}
77+
78+
log.Printf("[DEBUG] read gitlab group badge %s/%d", groupID, badgeID)
79+
80+
badge, _, err := client.GroupBadges.GetGroupBadge(groupID, badgeID)
81+
if err != nil {
82+
return err
83+
}
84+
85+
resourceGitlabGroupBadgeSetToState(d, badge, &groupID)
86+
return nil
87+
}
88+
89+
func resourceGitlabGroupBadgeUpdate(d *schema.ResourceData, meta interface{}) error {
90+
client := meta.(*gitlab.Client)
91+
ids := strings.Split(d.Id(), ":")
92+
groupID := ids[0]
93+
badgeID, err := strconv.Atoi(ids[1])
94+
if err != nil {
95+
return err
96+
}
97+
98+
options := &gitlab.EditGroupBadgeOptions{
99+
LinkURL: gitlab.String(d.Get("link_url").(string)),
100+
ImageURL: gitlab.String(d.Get("image_url").(string)),
101+
}
102+
103+
log.Printf("[DEBUG] update gitlab group badge %s/%d", groupID, badgeID)
104+
105+
_, _, err = client.GroupBadges.EditGroupBadge(groupID, badgeID, options)
106+
if err != nil {
107+
return err
108+
}
109+
110+
return resourceGitlabGroupBadgeRead(d, meta)
111+
}
112+
113+
func resourceGitlabGroupBadgeDelete(d *schema.ResourceData, meta interface{}) error {
114+
client := meta.(*gitlab.Client)
115+
ids := strings.Split(d.Id(), ":")
116+
groupID := ids[0]
117+
badgeID, err := strconv.Atoi(ids[1])
118+
if err != nil {
119+
return err
120+
}
121+
122+
log.Printf("[DEBUG] Delete gitlab group badge %s/%d", groupID, badgeID)
123+
124+
_, err = client.GroupBadges.DeleteGroupBadge(groupID, badgeID)
125+
return err
126+
}
127+
128+
func resourceGitlabGroupBadgeSetToState(d *schema.ResourceData, badge *gitlab.GroupBadge, groupID *string) {
129+
d.Set("link_url", badge.LinkURL)
130+
d.Set("image_url", badge.ImageURL)
131+
d.Set("rendered_link_url", badge.RenderedLinkURL)
132+
d.Set("rendered_image_url", badge.RenderedImageURL)
133+
d.Set("group", groupID)
134+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
12+
"github.com/xanzy/go-gitlab"
13+
)
14+
15+
func TestAccGitlabGroupBadge_basic(t *testing.T) {
16+
var badge gitlab.GroupBadge
17+
rInt := acctest.RandInt()
18+
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() { testAccPreCheck(t) },
21+
Providers: testAccProviders,
22+
CheckDestroy: testAccCheckGitlabGroupBadgeDestroy,
23+
Steps: []resource.TestStep{
24+
// Create a group and badge
25+
{
26+
Config: testAccGitlabGroupBadgeConfig(rInt),
27+
Check: resource.ComposeTestCheckFunc(
28+
testAccCheckGitlabGroupBadgeExists("gitlab_group_badge.foo", &badge),
29+
testAccCheckGitlabGroupBadgeAttributes(&badge, &testAccGitlabGroupBadgeExpectedAttributes{
30+
LinkURL: fmt.Sprintf("https://example.com/badge-%d", rInt),
31+
ImageURL: fmt.Sprintf("https://example.com/badge-%d.svg", rInt),
32+
}),
33+
),
34+
},
35+
// Test ImportState
36+
{
37+
ResourceName: "gitlab_group_badge.foo",
38+
ImportState: true,
39+
ImportStateVerify: true,
40+
},
41+
// Update the group badge
42+
{
43+
Config: testAccGitlabGroupBadgeUpdateConfig(rInt),
44+
Check: resource.ComposeTestCheckFunc(
45+
testAccCheckGitlabGroupBadgeExists("gitlab_group_badge.foo", &badge),
46+
testAccCheckGitlabGroupBadgeAttributes(&badge, &testAccGitlabGroupBadgeExpectedAttributes{
47+
LinkURL: fmt.Sprintf("https://example.com/new-badge-%d", rInt),
48+
ImageURL: fmt.Sprintf("https://example.com/new-badge-%d.svg", rInt),
49+
}),
50+
),
51+
},
52+
},
53+
})
54+
}
55+
56+
func testAccCheckGitlabGroupBadgeExists(n string, badge *gitlab.GroupBadge) resource.TestCheckFunc {
57+
return func(s *terraform.State) error {
58+
rs, ok := s.RootModule().Resources[n]
59+
if !ok {
60+
return fmt.Errorf("Not Found: %s", n)
61+
}
62+
63+
splitID := strings.Split(rs.Primary.ID, ":")
64+
65+
badgeID, err := strconv.Atoi(splitID[len(splitID)-1])
66+
if err != nil {
67+
return err
68+
}
69+
groupID := rs.Primary.Attributes["group"]
70+
if groupID == "" {
71+
return fmt.Errorf("No group ID is set")
72+
}
73+
74+
conn := testAccProvider.Meta().(*gitlab.Client)
75+
76+
gotBadge, _, err := conn.GroupBadges.GetGroupBadge(groupID, badgeID)
77+
if err != nil {
78+
return err
79+
}
80+
*badge = *gotBadge
81+
return nil
82+
}
83+
}
84+
85+
type testAccGitlabGroupBadgeExpectedAttributes struct {
86+
LinkURL string
87+
ImageURL string
88+
}
89+
90+
func testAccCheckGitlabGroupBadgeAttributes(badge *gitlab.GroupBadge, want *testAccGitlabGroupBadgeExpectedAttributes) resource.TestCheckFunc {
91+
return func(s *terraform.State) error {
92+
if badge.LinkURL != want.LinkURL {
93+
return fmt.Errorf("got link_url %q; want %q", badge.LinkURL, want.LinkURL)
94+
}
95+
96+
if badge.ImageURL != want.ImageURL {
97+
return fmt.Errorf("got image_url %s; want %s", badge.ImageURL, want.ImageURL)
98+
}
99+
100+
return nil
101+
}
102+
}
103+
104+
func testAccCheckGitlabGroupBadgeDestroy(s *terraform.State) error {
105+
conn := testAccProvider.Meta().(*gitlab.Client)
106+
107+
for _, rs := range s.RootModule().Resources {
108+
if rs.Type != "gitlab_group" {
109+
continue
110+
}
111+
112+
group, resp, err := conn.Groups.GetGroup(rs.Primary.ID, nil)
113+
if err == nil {
114+
if group != nil && fmt.Sprintf("%d", group.ID) == rs.Primary.ID {
115+
if group.MarkedForDeletionOn == nil {
116+
return fmt.Errorf("Group still exists")
117+
}
118+
}
119+
}
120+
if resp.StatusCode != 404 {
121+
return err
122+
}
123+
return nil
124+
}
125+
return nil
126+
}
127+
128+
func testAccGitlabGroupBadgeConfig(rInt int) string {
129+
return fmt.Sprintf(`
130+
resource "gitlab_group" "foo" {
131+
name = "foo-%d"
132+
path = "foo-%d"
133+
description = "Terraform acceptance tests"
134+
135+
# So that acceptance tests can be run in a gitlab organization
136+
# with no billing
137+
visibility_level = "public"
138+
}
139+
140+
resource "gitlab_group_badge" "foo" {
141+
group = "${gitlab_group.foo.id}"
142+
link_url = "https://example.com/badge-%d"
143+
image_url = "https://example.com/badge-%d.svg"
144+
}
145+
`, rInt, rInt, rInt, rInt)
146+
}
147+
148+
func testAccGitlabGroupBadgeUpdateConfig(rInt int) string {
149+
return fmt.Sprintf(`
150+
resource "gitlab_group" "foo" {
151+
name = "foo-%d"
152+
path = "foo-%d"
153+
description = "Terraform acceptance tests"
154+
155+
# So that acceptance tests can be run in a gitlab organization
156+
# with no billing
157+
visibility_level = "public"
158+
}
159+
160+
# change link and image url
161+
resource "gitlab_group_badge" "foo" {
162+
group = "${gitlab_group.foo.id}"
163+
link_url = "https://example.com/new-badge-%d"
164+
image_url = "https://example.com/new-badge-%d.svg"
165+
}
166+
`, rInt, rInt, rInt, rInt)
167+
}

0 commit comments

Comments
 (0)