Skip to content

Commit

Permalink
Import group labels (gitlabhq#339)
Browse files Browse the repository at this point in the history
* add support for importing gitlab group labels

* fix resource name in the acceptance test

* convert group ID to string

Co-authored-by: David Townshend <david.townshend@allangray.co.za>
  • Loading branch information
2 people authored and sfang97 committed Sep 8, 2020
1 parent 2153214 commit 54e7e61
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions gitlab/resource_gitlab_group_label.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gitlab

import (
"fmt"
"log"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
gitlab "github.com/xanzy/go-gitlab"
Expand All @@ -13,6 +16,9 @@ func resourceGitlabGroupLabel() *schema.Resource {
Read: resourceGitlabGroupLabelRead,
Update: resourceGitlabGroupLabelUpdate,
Delete: resourceGitlabGroupLabelDelete,
Importer: &schema.ResourceImporter{
State: resourceGitlabGroupLabelImporter,
},

Schema: map[string]*schema.Schema{
"group": {
Expand Down Expand Up @@ -123,3 +129,25 @@ func resourceGitlabGroupLabelDelete(d *schema.ResourceData, meta interface{}) er
_, err := client.GroupLabels.DeleteGroupLabel(group, options)
return err
}

func resourceGitlabGroupLabelImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*gitlab.Client)
parts := strings.SplitN(d.Id(), ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid label id (should be <group ID>.<label name>): %s", d.Id())
}

d.SetId(parts[1])
group, _, err := client.Groups.GetGroup(parts[0])
if err != nil {
return nil, err
}

if err := d.Set("group", strconv.Itoa(group.ID)); err != nil {
return nil, err
}

err = resourceGitlabGroupLabelRead(d, meta)

return []*schema.ResourceData{d}, err
}
42 changes: 42 additions & 0 deletions gitlab/resource_gitlab_group_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,48 @@ func TestAccGitlabGroupLabel_basic(t *testing.T) {
})
}

func TestAccGitlabGroupLabel_import(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "gitlab_group_label.fixme"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGitlabGroupLabelDestroy,
Steps: []resource.TestStep{
{
Config: testAccGitlabGroupLabelConfig(rInt),
},
{
ResourceName: resourceName,
ImportStateIdFunc: getGroupLabelImportID(resourceName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func getGroupLabelImportID(n string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[n]
if !ok {
return "", fmt.Errorf("Not Found: %s", n)
}

labelID := rs.Primary.ID
if labelID == "" {
return "", fmt.Errorf("No deploy key ID is set")
}
groupID := rs.Primary.Attributes["group"]
if groupID == "" {
return "", fmt.Errorf("No group ID is set")
}

return fmt.Sprintf("%s:%s", groupID, labelID), nil
}
}

func testAccCheckGitlabGroupLabelExists(n string, label *gitlab.GroupLabel) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/group_label.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ The following arguments are supported:
The resource exports the following attributes:

* `id` - The unique id assigned to the label by the GitLab server (the name of the label).

## Import

Gitlab group labels can be imported using an id made up of `{group_id}:{group_label_id}`, e.g.

```
$ terraform import gitlab_deploy_key_enable.example 12345:fixme
```

0 comments on commit 54e7e61

Please sign in to comment.