Skip to content

Commit 04683f7

Browse files
author
David Townshend
committed
add support for importing gitlab group labels
1 parent 08bba6a commit 04683f7

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

gitlab/resource_gitlab_group_label.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package gitlab
22

33
import (
4+
"fmt"
45
"log"
6+
"strings"
57

68
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
79
gitlab "github.com/xanzy/go-gitlab"
@@ -13,6 +15,9 @@ func resourceGitlabGroupLabel() *schema.Resource {
1315
Read: resourceGitlabGroupLabelRead,
1416
Update: resourceGitlabGroupLabelUpdate,
1517
Delete: resourceGitlabGroupLabelDelete,
18+
Importer: &schema.ResourceImporter{
19+
State: resourceGitlabGroupLabelImporter,
20+
},
1621

1722
Schema: map[string]*schema.Schema{
1823
"group": {
@@ -123,3 +128,25 @@ func resourceGitlabGroupLabelDelete(d *schema.ResourceData, meta interface{}) er
123128
_, err := client.GroupLabels.DeleteGroupLabel(group, options)
124129
return err
125130
}
131+
132+
func resourceGitlabGroupLabelImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
133+
client := meta.(*gitlab.Client)
134+
parts := strings.SplitN(d.Id(), ":", 2)
135+
if len(parts) != 2 {
136+
return nil, fmt.Errorf("invalid label id (should be <group ID>.<label name>): %s", d.Id())
137+
}
138+
139+
d.SetId(parts[1])
140+
group, _, err := client.Groups.GetGroup(parts[0])
141+
if err != nil {
142+
return nil, err
143+
}
144+
145+
if err := d.Set("group", group.ID); err != nil {
146+
return nil, err
147+
}
148+
149+
err = resourceGitlabGroupLabelRead(d, meta)
150+
151+
return []*schema.ResourceData{d}, err
152+
}

gitlab/resource_gitlab_group_label_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,48 @@ func TestAccGitlabGroupLabel_basic(t *testing.T) {
8484
})
8585
}
8686

87+
func TestAccGitlabGroupLabel_import(t *testing.T) {
88+
rInt := acctest.RandInt()
89+
resourceName := "gitlab_group_label.foo"
90+
91+
resource.Test(t, resource.TestCase{
92+
PreCheck: func() { testAccPreCheck(t) },
93+
Providers: testAccProviders,
94+
CheckDestroy: testAccCheckGitlabGroupLabelDestroy,
95+
Steps: []resource.TestStep{
96+
{
97+
Config: testAccGitlabGroupLabelConfig(rInt),
98+
},
99+
{
100+
ResourceName: resourceName,
101+
ImportStateIdFunc: getGroupLabelImportID(resourceName),
102+
ImportState: true,
103+
ImportStateVerify: true,
104+
},
105+
},
106+
})
107+
}
108+
109+
func getGroupLabelImportID(n string) resource.ImportStateIdFunc {
110+
return func(s *terraform.State) (string, error) {
111+
rs, ok := s.RootModule().Resources[n]
112+
if !ok {
113+
return "", fmt.Errorf("Not Found: %s", n)
114+
}
115+
116+
labelID := rs.Primary.ID
117+
if labelID == "" {
118+
return "", fmt.Errorf("No deploy key ID is set")
119+
}
120+
groupID := rs.Primary.Attributes["group"]
121+
if groupID == "" {
122+
return "", fmt.Errorf("No group ID is set")
123+
}
124+
125+
return fmt.Sprintf("%s:%s", groupID, labelID), nil
126+
}
127+
}
128+
87129
func testAccCheckGitlabGroupLabelExists(n string, label *gitlab.GroupLabel) resource.TestCheckFunc {
88130
return func(s *terraform.State) error {
89131
rs, ok := s.RootModule().Resources[n]

website/docs/r/group_label.html.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ The following arguments are supported:
4141
The resource exports the following attributes:
4242

4343
* `id` - The unique id assigned to the label by the GitLab server (the name of the label).
44+
45+
## Import
46+
47+
Gitlab group labels can be imported using an id made up of `{group_id}:{group_label_id}`, e.g.
48+
49+
```
50+
$ terraform import gitlab_deploy_key_enable.example 12345:fixme
51+
```

0 commit comments

Comments
 (0)