Skip to content

Commit 840a3fb

Browse files
aquavitaeDavid Townshend
and
David Townshend
authored
Import group labels (#339)
* add support for importing gitlab group labels * fix resource name in the acceptance test * convert group ID to string Co-authored-by: David Townshend <[email protected]>
1 parent 0f4e513 commit 840a3fb

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

gitlab/resource_gitlab_group_label.go

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

33
import (
4+
"fmt"
45
"log"
6+
"strconv"
7+
"strings"
58

69
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
710
gitlab "github.com/xanzy/go-gitlab"
@@ -13,6 +16,9 @@ func resourceGitlabGroupLabel() *schema.Resource {
1316
Read: resourceGitlabGroupLabelRead,
1417
Update: resourceGitlabGroupLabelUpdate,
1518
Delete: resourceGitlabGroupLabelDelete,
19+
Importer: &schema.ResourceImporter{
20+
State: resourceGitlabGroupLabelImporter,
21+
},
1622

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

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.fixme"
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)