@@ -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" : {
@@ -77,27 +85,19 @@ func resourceGitlabLabelRead(ctx context.Context, d *schema.ResourceData, meta i
77
85
labelName := d .Id ()
78
86
log .Printf ("[DEBUG] read gitlab label %s/%s" , project , labelName )
79
87
80
- page := 1
81
- labelsLen := 0
82
- for page == 1 || labelsLen != 0 {
83
- labels , _ , err := client .Labels .ListLabels (project , & gitlab.ListLabelsOptions {ListOptions : gitlab.ListOptions {Page : page }}, gitlab .WithContext (ctx ))
84
- if err != nil {
85
- return diag .FromErr (err )
86
- }
87
- for _ , label := range labels {
88
- if label .Name == labelName {
89
- d .Set ("description" , label .Description )
90
- d .Set ("color" , label .Color )
91
- d .Set ("name" , label .Name )
92
- return nil
93
- }
88
+ label , _ , err := client .Labels .GetLabel (project , labelName , gitlab .WithContext (ctx ))
89
+ if err != nil {
90
+ if is404 (err ) {
91
+ log .Printf ("[DEBUG] failed to read gitlab label %s/%s" , project , labelName )
92
+ d .SetId ("" )
93
+ return nil
94
94
}
95
- labelsLen = len (labels )
96
- page = page + 1
95
+ return diag .FromErr (err )
97
96
}
98
97
99
- log .Printf ("[DEBUG] failed to read gitlab label %s/%s" , project , labelName )
100
- d .SetId ("" )
98
+ d .Set ("description" , label .Description )
99
+ d .Set ("color" , label .Color )
100
+ d .Set ("name" , label .Name )
101
101
return nil
102
102
}
103
103
@@ -138,3 +138,28 @@ func resourceGitlabLabelDelete(ctx context.Context, d *schema.ResourceData, meta
138
138
139
139
return nil
140
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