@@ -2,15 +2,17 @@ package provider
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"log"
7
+ "strconv"
8
+ "strings"
6
9
7
10
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
12
gitlab "github.com/xanzy/go-gitlab"
10
13
)
11
14
12
15
var _ = registerResource ("gitlab_label" , func () * schema.Resource {
13
- // lintignore: XR002 // TODO: Resolve this tfproviderlint issue
14
16
return & schema.Resource {
15
17
Description : `The ` + "`" + `gitlab_label` + "`" + ` resource allows to manage the lifecycle of a project label.
16
18
@@ -20,6 +22,12 @@ var _ = registerResource("gitlab_label", func() *schema.Resource {
20
22
ReadContext : resourceGitlabLabelRead ,
21
23
UpdateContext : resourceGitlabLabelUpdate ,
22
24
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
+ },
23
31
24
32
Schema : map [string ]* schema.Schema {
25
33
"project" : {
@@ -130,3 +138,28 @@ func resourceGitlabLabelDelete(ctx context.Context, d *schema.ResourceData, meta
130
138
131
139
return nil
132
140
}
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
+ }
0 commit comments