Skip to content

Commit eafe112

Browse files
committed
resource/gitlab_label: Support import
Refs: #36
1 parent 2eac8c3 commit eafe112

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

docs/resources/label.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ resource "gitlab_label" "devops_create" {
4646
- **description** (String) The description of the label.
4747
- **id** (String) The ID of this resource.
4848

49+
## Import
4950

51+
Import is supported using the following syntax:
52+
53+
```shell
54+
# Gitlab labels can be imported using an id made up of `{project_id}:{group_label_id}`, e.g.
55+
terraform import gitlab_label.example 12345:fixme
56+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Gitlab labels can be imported using an id made up of `{project_id}:{group_label_id}`, e.g.
2+
terraform import gitlab_label.example 12345:fixme

internal/provider/resource_gitlab_label.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package provider
22

33
import (
44
"context"
5+
"fmt"
56
"log"
7+
"strconv"
8+
"strings"
69

710
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
811
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
912
gitlab "github.com/xanzy/go-gitlab"
1013
)
1114

1215
var _ = registerResource("gitlab_label", func() *schema.Resource {
13-
// lintignore: XR002 // TODO: Resolve this tfproviderlint issue
1416
return &schema.Resource{
1517
Description: `The ` + "`" + `gitlab_label` + "`" + ` resource allows to manage the lifecycle of a project label.
1618
@@ -20,6 +22,12 @@ var _ = registerResource("gitlab_label", func() *schema.Resource {
2022
ReadContext: resourceGitlabLabelRead,
2123
UpdateContext: resourceGitlabLabelUpdate,
2224
DeleteContext: resourceGitlabLabelDelete,
25+
// FIXME: this importer sucks a little, but we can't use a passthrough importer, because
26+
// the resource id is flawed and we don't want to break backwards-compatibility.
27+
// We cannot have the same label in two different projects as of now, ...
28+
Importer: &schema.ResourceImporter{
29+
StateContext: resourceGitlabLabelImporter,
30+
},
2331

2432
Schema: map[string]*schema.Schema{
2533
"project": {
@@ -130,3 +138,28 @@ func resourceGitlabLabelDelete(ctx context.Context, d *schema.ResourceData, meta
130138

131139
return nil
132140
}
141+
142+
func resourceGitlabLabelImporter(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
143+
client := meta.(*gitlab.Client)
144+
parts := strings.SplitN(d.Id(), ":", 2)
145+
if len(parts) != 2 {
146+
return nil, fmt.Errorf("invalid label id (should be <project ID>.<label name>): %s", d.Id())
147+
}
148+
149+
d.SetId(parts[1])
150+
project, _, err := client.Projects.GetProject(parts[0], nil)
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
if err := d.Set("project", strconv.Itoa(project.ID)); err != nil {
156+
return nil, err
157+
}
158+
159+
diagnostic := resourceGitlabLabelRead(ctx, d, meta)
160+
if diagnostic.HasError() {
161+
return nil, fmt.Errorf("failed to read project label %s: %s", d.Id(), diagnostic[0].Summary)
162+
}
163+
164+
return []*schema.ResourceData{d}, nil
165+
}

internal/provider/resource_gitlab_label_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,37 @@ func TestAccGitlabLabel_basic(t *testing.T) {
5555
}),
5656
),
5757
},
58+
// Verify Import
59+
{
60+
ResourceName: "gitlab_label.fixme",
61+
ImportStateIdFunc: getLabelImportID("gitlab_label.fixme"),
62+
ImportState: true,
63+
ImportStateVerify: true,
64+
},
5865
},
5966
})
6067
}
6168

69+
func getLabelImportID(n string) resource.ImportStateIdFunc {
70+
return func(s *terraform.State) (string, error) {
71+
rs, ok := s.RootModule().Resources[n]
72+
if !ok {
73+
return "", fmt.Errorf("Not Found: %s", n)
74+
}
75+
76+
labelID := rs.Primary.ID
77+
if labelID == "" {
78+
return "", fmt.Errorf("No label key ID is set")
79+
}
80+
projectID := rs.Primary.Attributes["project"]
81+
if projectID == "" {
82+
return "", fmt.Errorf("No project ID is set")
83+
}
84+
85+
return fmt.Sprintf("%s:%s", projectID, labelID), nil
86+
}
87+
}
88+
6289
func testAccCheckGitlabLabelExists(n string, label *gitlab.Label) resource.TestCheckFunc {
6390
return func(s *terraform.State) error {
6491
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)