Skip to content

Commit 7235477

Browse files
authored
Merge pull request #22 from roidelapluie/v4-labels
new resource: gitlab_labels
2 parents 112aca4 + 7c3aa5f commit 7235477

File tree

5 files changed

+350
-0
lines changed

5 files changed

+350
-0
lines changed

gitlab/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Provider() terraform.ResourceProvider {
4343
ResourcesMap: map[string]*schema.Resource{
4444
"gitlab_group": resourceGitlabGroup(),
4545
"gitlab_project": resourceGitlabProject(),
46+
"gitlab_label": resourceGitlabLabel(),
4647
"gitlab_project_hook": resourceGitlabProjectHook(),
4748
"gitlab_deploy_key": resourceGitlabDeployKey(),
4849
},

gitlab/resource_gitlab_label.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package gitlab
2+
3+
import (
4+
"log"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
gitlab "github.com/xanzy/go-gitlab"
8+
)
9+
10+
func resourceGitlabLabel() *schema.Resource {
11+
return &schema.Resource{
12+
Create: resourceGitlabLabelCreate,
13+
Read: resourceGitlabLabelRead,
14+
Update: resourceGitlabLabelUpdate,
15+
Delete: resourceGitlabLabelDelete,
16+
17+
Schema: map[string]*schema.Schema{
18+
"project": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
},
22+
"name": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
ForceNew: true,
26+
},
27+
"color": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
},
31+
"description": {
32+
Type: schema.TypeString,
33+
Optional: true,
34+
},
35+
},
36+
}
37+
}
38+
39+
func resourceGitlabLabelCreate(d *schema.ResourceData, meta interface{}) error {
40+
client := meta.(*gitlab.Client)
41+
project := d.Get("project").(string)
42+
options := &gitlab.CreateLabelOptions{
43+
Name: gitlab.String(d.Get("name").(string)),
44+
Color: gitlab.String(d.Get("color").(string)),
45+
}
46+
47+
if v, ok := d.GetOk("description"); ok {
48+
options.Description = gitlab.String(v.(string))
49+
}
50+
51+
log.Printf("[DEBUG] create gitlab label %s", *options.Name)
52+
53+
label, _, err := client.Labels.CreateLabel(project, options)
54+
if err != nil {
55+
return err
56+
}
57+
58+
d.SetId(label.Name)
59+
60+
return resourceGitlabLabelRead(d, meta)
61+
}
62+
63+
func resourceGitlabLabelRead(d *schema.ResourceData, meta interface{}) error {
64+
client := meta.(*gitlab.Client)
65+
project := d.Get("project").(string)
66+
labelName := d.Id()
67+
log.Printf("[DEBUG] read gitlab label %s/%s", project, labelName)
68+
69+
labels, response, err := client.Labels.ListLabels(project)
70+
if err != nil {
71+
if response.StatusCode == 404 {
72+
log.Printf("[WARN] removing label %s from state because it no longer exists in gitlab", labelName)
73+
d.SetId("")
74+
return nil
75+
}
76+
77+
return err
78+
}
79+
found := false
80+
for _, label := range labels {
81+
if label.Name == labelName {
82+
d.Set("description", label.Description)
83+
d.Set("color", label.Color)
84+
d.Set("name", label.Name)
85+
found = true
86+
break
87+
}
88+
}
89+
if !found {
90+
log.Printf("[WARN] removing label %s from state because it no longer exists in gitlab", labelName)
91+
d.SetId("")
92+
}
93+
94+
return nil
95+
}
96+
97+
func resourceGitlabLabelUpdate(d *schema.ResourceData, meta interface{}) error {
98+
client := meta.(*gitlab.Client)
99+
project := d.Get("project").(string)
100+
options := &gitlab.UpdateLabelOptions{
101+
Name: gitlab.String(d.Get("name").(string)),
102+
Color: gitlab.String(d.Get("color").(string)),
103+
}
104+
105+
if d.HasChange("description") {
106+
options.Description = gitlab.String(d.Get("description").(string))
107+
}
108+
109+
log.Printf("[DEBUG] update gitlab label %s", d.Id())
110+
111+
_, _, err := client.Labels.UpdateLabel(project, options)
112+
if err != nil {
113+
return err
114+
}
115+
116+
return resourceGitlabLabelRead(d, meta)
117+
}
118+
119+
func resourceGitlabLabelDelete(d *schema.ResourceData, meta interface{}) error {
120+
client := meta.(*gitlab.Client)
121+
project := d.Get("project").(string)
122+
log.Printf("[DEBUG] Delete gitlab label %s", d.Id())
123+
options := &gitlab.DeleteLabelOptions{
124+
Name: gitlab.String(d.Id()),
125+
}
126+
127+
_, err := client.Labels.DeleteLabel(project, options)
128+
return err
129+
}

gitlab/resource_gitlab_label_test.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
"github.com/xanzy/go-gitlab"
11+
)
12+
13+
func TestAccGitlabLabel_basic(t *testing.T) {
14+
var label gitlab.Label
15+
rInt := acctest.RandInt()
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testAccCheckGitlabLabelDestroy,
21+
Steps: []resource.TestStep{
22+
// Create a project and label with default options
23+
{
24+
Config: testAccGitlabLabelConfig(rInt),
25+
Check: resource.ComposeTestCheckFunc(
26+
testAccCheckGitlabLabelExists("gitlab_label.fixme", &label),
27+
testAccCheckGitlabLabelAttributes(&label, &testAccGitlabLabelExpectedAttributes{
28+
Name: fmt.Sprintf("FIXME-%d", rInt),
29+
Color: "#ffcc00",
30+
Description: "fix this test",
31+
}),
32+
),
33+
},
34+
// Update the label to change the parameters
35+
{
36+
Config: testAccGitlabLabelUpdateConfig(rInt),
37+
Check: resource.ComposeTestCheckFunc(
38+
testAccCheckGitlabLabelExists("gitlab_label.fixme", &label),
39+
testAccCheckGitlabLabelAttributes(&label, &testAccGitlabLabelExpectedAttributes{
40+
Name: fmt.Sprintf("FIXME-%d", rInt),
41+
Color: "#ff0000",
42+
Description: "red label",
43+
}),
44+
),
45+
},
46+
// Update the label to get back to initial settings
47+
{
48+
Config: testAccGitlabLabelConfig(rInt),
49+
Check: resource.ComposeTestCheckFunc(
50+
testAccCheckGitlabLabelExists("gitlab_label.fixme", &label),
51+
testAccCheckGitlabLabelAttributes(&label, &testAccGitlabLabelExpectedAttributes{
52+
Name: fmt.Sprintf("FIXME-%d", rInt),
53+
Color: "#ffcc00",
54+
Description: "fix this test",
55+
}),
56+
),
57+
},
58+
},
59+
})
60+
}
61+
62+
func testAccCheckGitlabLabelExists(n string, label *gitlab.Label) resource.TestCheckFunc {
63+
return func(s *terraform.State) error {
64+
rs, ok := s.RootModule().Resources[n]
65+
if !ok {
66+
return fmt.Errorf("Not Found: %s", n)
67+
}
68+
69+
labelName := rs.Primary.ID
70+
repoName := rs.Primary.Attributes["project"]
71+
if repoName == "" {
72+
return fmt.Errorf("No project ID is set")
73+
}
74+
conn := testAccProvider.Meta().(*gitlab.Client)
75+
76+
labels, _, err := conn.Labels.ListLabels(repoName)
77+
if err != nil {
78+
return err
79+
}
80+
for _, gotLabel := range labels {
81+
if gotLabel.Name == labelName {
82+
*label = *gotLabel
83+
return nil
84+
}
85+
}
86+
return fmt.Errorf("Label does not exist")
87+
}
88+
}
89+
90+
type testAccGitlabLabelExpectedAttributes struct {
91+
Name string
92+
Color string
93+
Description string
94+
}
95+
96+
func testAccCheckGitlabLabelAttributes(label *gitlab.Label, want *testAccGitlabLabelExpectedAttributes) resource.TestCheckFunc {
97+
return func(s *terraform.State) error {
98+
if label.Name != want.Name {
99+
return fmt.Errorf("got name %q; want %q", label.Name, want.Name)
100+
}
101+
102+
if label.Description != want.Description {
103+
return fmt.Errorf("got description %q; want %q", label.Description, want.Description)
104+
}
105+
106+
if label.Color != want.Color {
107+
return fmt.Errorf("got color %q; want %q", label.Color, want.Color)
108+
}
109+
110+
return nil
111+
}
112+
}
113+
114+
func testAccCheckGitlabLabelDestroy(s *terraform.State) error {
115+
conn := testAccProvider.Meta().(*gitlab.Client)
116+
117+
for _, rs := range s.RootModule().Resources {
118+
if rs.Type != "gitlab_project" {
119+
continue
120+
}
121+
122+
gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID)
123+
if err == nil {
124+
if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
125+
return fmt.Errorf("Repository still exists")
126+
}
127+
}
128+
if resp.StatusCode != 404 {
129+
return err
130+
}
131+
return nil
132+
}
133+
return nil
134+
}
135+
136+
func testAccGitlabLabelConfig(rInt int) string {
137+
return fmt.Sprintf(`
138+
resource "gitlab_project" "foo" {
139+
name = "foo-%d"
140+
description = "Terraform acceptance tests"
141+
142+
# So that acceptance tests can be run in a gitlab organization
143+
# with no billing
144+
visibility_level = "public"
145+
}
146+
147+
resource "gitlab_label" "fixme" {
148+
project = "${gitlab_project.foo.id}"
149+
name = "FIXME-%d"
150+
color = "#ffcc00"
151+
description = "fix this test"
152+
}
153+
`, rInt, rInt)
154+
}
155+
156+
func testAccGitlabLabelUpdateConfig(rInt int) string {
157+
return fmt.Sprintf(`
158+
resource "gitlab_project" "foo" {
159+
name = "foo-%d"
160+
description = "Terraform acceptance tests"
161+
162+
# So that acceptance tests can be run in a gitlab organization
163+
# with no billing
164+
visibility_level = "public"
165+
}
166+
167+
resource "gitlab_label" "fixme" {
168+
project = "${gitlab_project.foo.id}"
169+
name = "FIXME-%d"
170+
color = "#ff0000"
171+
description = "red label"
172+
}
173+
`, rInt, rInt)
174+
}

website/docs/r/label.html.markdown

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
layout: "gitlab"
3+
page_title: "GitLab: gitlab_label"
4+
sidebar_current: "docs-gitlab-resource-label"
5+
description: |-
6+
Creates and manages labels for GitLab projects
7+
---
8+
9+
# gitlab\_label
10+
11+
This resource allows you to create and manage labels for your GitLab projects.
12+
For further information on labels, consult the [gitlab
13+
documentation](https://docs.gitlab.com/ee/user/project/labels.htm).
14+
15+
16+
## Example Usage
17+
18+
```hcl
19+
resource "gitlab_label" "fixme" {
20+
project = "example"
21+
name = "fixme"
22+
description = "issue with failing tests"
23+
color = "#ffcc00"
24+
}
25+
```
26+
27+
## Argument Reference
28+
29+
The following arguments are supported:
30+
31+
* `project` - (Required) The name or id of the project to add the label to.
32+
33+
* `name` - (Required) The name of the label.
34+
35+
* `color` - (Required) The color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB) or one of the [CSS color names](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#Color_keywords).
36+
37+
* `description` - (Optional) The description of the label.
38+
39+
## Attributes Reference
40+
41+
The resource exports the following attributes:
42+
43+
* `id` - The unique id assigned to the label by the GitLab server (the name of the label).

website/gitlab.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<li<%= sidebar_current("docs-gitlab-resource-group") %>>
2020
<a href="/docs/providers/gitlab/r/group.html">gitlab_group</a>
2121
</li>
22+
<li<%= sidebar_current("docs-gitlab-resource-label") %>>
23+
<a href="/docs/providers/gitlab/r/label.html">gitlab_label</a>
24+
</li>
2225
<li<%= sidebar_current("docs-gitlab-resource-project-hook") %>>
2326
<a href="/docs/providers/gitlab/r/project_hook.html">gitlab_project_hook</a>
2427
</li>

0 commit comments

Comments
 (0)