Skip to content

Import group labels #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to read here? Doesn't Terraform perform a read for you before the next operation anyway? I don't think it changes the provider behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly... I was modelling this on how all the other Importers were implemented?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think it makes sense to read and make sure the resource you try to import exists.


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
```