1
1
package gitlab
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
- "fmt"
6
6
"log"
7
7
"net/http"
8
8
"net/url"
9
9
"strings"
10
10
11
11
retryablehttp "github.com/hashicorp/go-retryablehttp"
12
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
14
gitlab "github.com/xanzy/go-gitlab"
14
15
)
15
16
16
17
func resourceGitlabProjectVariable () * schema.Resource {
17
18
return & schema.Resource {
18
- Create : resourceGitlabProjectVariableCreate ,
19
- Read : resourceGitlabProjectVariableRead ,
20
- Update : resourceGitlabProjectVariableUpdate ,
21
- Delete : resourceGitlabProjectVariableDelete ,
19
+ CreateContext : resourceGitlabProjectVariableCreate ,
20
+ ReadContext : resourceGitlabProjectVariableRead ,
21
+ UpdateContext : resourceGitlabProjectVariableUpdate ,
22
+ DeleteContext : resourceGitlabProjectVariableDelete ,
22
23
Importer : & schema.ResourceImporter {
23
24
StateContext : schema .ImportStatePassthroughContext ,
24
25
},
@@ -67,7 +68,7 @@ func resourceGitlabProjectVariable() *schema.Resource {
67
68
}
68
69
}
69
70
70
- func resourceGitlabProjectVariableCreate (d * schema.ResourceData , meta interface {}) error {
71
+ func resourceGitlabProjectVariableCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
71
72
client := meta .(* gitlab.Client )
72
73
73
74
project := d .Get ("project" ).(string )
@@ -91,17 +92,17 @@ func resourceGitlabProjectVariableCreate(d *schema.ResourceData, meta interface{
91
92
92
93
log .Printf ("[DEBUG] create gitlab project variable %q" , id )
93
94
94
- _ , _ , err := client .ProjectVariables .CreateVariable (project , & options )
95
+ _ , _ , err := client .ProjectVariables .CreateVariable (project , & options , gitlab . WithContext ( ctx ) )
95
96
if err != nil {
96
97
return augmentProjectVariableClientError (d , err )
97
98
}
98
99
99
100
d .SetId (id )
100
101
101
- return resourceGitlabProjectVariableRead (d , meta )
102
+ return resourceGitlabProjectVariableRead (ctx , d , meta )
102
103
}
103
104
104
- func resourceGitlabProjectVariableRead (d * schema.ResourceData , meta interface {}) error {
105
+ func resourceGitlabProjectVariableRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
105
106
client := meta .(* gitlab.Client )
106
107
107
108
var (
@@ -123,12 +124,12 @@ func resourceGitlabProjectVariableRead(d *schema.ResourceData, meta interface{})
123
124
key = parts [1 ]
124
125
environmentScope = parts [2 ]
125
126
default :
126
- return fmt .Errorf (`Failed to parse project variable ID %q: expected format project:key or project:key:environment_scope` , d .Id ())
127
+ return diag .Errorf (`Failed to parse project variable ID %q: expected format project:key or project:key:environment_scope` , d .Id ())
127
128
}
128
129
129
130
log .Printf ("[DEBUG] read gitlab project variable %q" , d .Id ())
130
131
131
- v , err := getProjectVariable (client , project , key , environmentScope )
132
+ v , err := getProjectVariable (ctx , client , project , key , environmentScope )
132
133
if err != nil {
133
134
if errors .Is (err , errProjectVariableNotExist ) {
134
135
log .Printf ("[DEBUG] read gitlab project variable %q was not found" , d .Id ())
@@ -148,7 +149,7 @@ func resourceGitlabProjectVariableRead(d *schema.ResourceData, meta interface{})
148
149
return nil
149
150
}
150
151
151
- func resourceGitlabProjectVariableUpdate (d * schema.ResourceData , meta interface {}) error {
152
+ func resourceGitlabProjectVariableUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
152
153
client := meta .(* gitlab.Client )
153
154
154
155
project := d .Get ("project" ).(string )
@@ -168,15 +169,15 @@ func resourceGitlabProjectVariableUpdate(d *schema.ResourceData, meta interface{
168
169
}
169
170
log .Printf ("[DEBUG] update gitlab project variable %q" , d .Id ())
170
171
171
- _ , _ , err := client .ProjectVariables .UpdateVariable (project , key , options , withEnvironmentScopeFilter (environmentScope ))
172
+ _ , _ , err := client .ProjectVariables .UpdateVariable (project , key , options , withEnvironmentScopeFilter (ctx , environmentScope ))
172
173
if err != nil {
173
174
return augmentProjectVariableClientError (d , err )
174
175
}
175
176
176
- return resourceGitlabProjectVariableRead (d , meta )
177
+ return resourceGitlabProjectVariableRead (ctx , d , meta )
177
178
}
178
179
179
- func resourceGitlabProjectVariableDelete (d * schema.ResourceData , meta interface {}) error {
180
+ func resourceGitlabProjectVariableDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
180
181
client := meta .(* gitlab.Client )
181
182
project := d .Get ("project" ).(string )
182
183
key := d .Get ("key" ).(string )
@@ -187,19 +188,23 @@ func resourceGitlabProjectVariableDelete(d *schema.ResourceData, meta interface{
187
188
// but it will be ignored in prior versions, causing nondeterministic destroy behavior when
188
189
// destroying or updating scoped variables.
189
190
// ref: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39209
190
- _ , err := client .ProjectVariables .RemoveVariable (project , key , withEnvironmentScopeFilter (environmentScope ))
191
+ _ , err := client .ProjectVariables .RemoveVariable (project , key , withEnvironmentScopeFilter (ctx , environmentScope ))
191
192
return augmentProjectVariableClientError (d , err )
192
193
}
193
194
194
- func augmentProjectVariableClientError (d * schema.ResourceData , err error ) error {
195
+ func augmentProjectVariableClientError (d * schema.ResourceData , err error ) diag. Diagnostics {
195
196
// Masked values will commonly error due to their strict requirements, and the error message from the GitLab API is not very informative,
196
197
// so we return a custom error message in this case.
197
198
if d .Get ("masked" ).(bool ) && isInvalidValueError (err ) {
198
199
log .Printf ("[ERROR] %v" , err )
199
- return errors . New ("Invalid value for a masked variable. Check the masked variable requirements: https://docs.gitlab.com/ee/ci/variables/#masked-variable-requirements" )
200
+ return diag . Errorf ("Invalid value for a masked variable. Check the masked variable requirements: https://docs.gitlab.com/ee/ci/variables/#masked-variable-requirements" )
200
201
}
201
202
202
- return err
203
+ if err != nil {
204
+ return diag .FromErr (err )
205
+ }
206
+
207
+ return nil
203
208
}
204
209
205
210
func isInvalidValueError (err error ) bool {
@@ -210,8 +215,9 @@ func isInvalidValueError(err error) bool {
210
215
strings .Contains (httpErr .Message , "invalid" )
211
216
}
212
217
213
- func withEnvironmentScopeFilter (environmentScope string ) gitlab.RequestOptionFunc {
218
+ func withEnvironmentScopeFilter (ctx context. Context , environmentScope string ) gitlab.RequestOptionFunc {
214
219
return func (req * retryablehttp.Request ) error {
220
+ * req = * req .WithContext (ctx )
215
221
query , err := url .ParseQuery (req .Request .URL .RawQuery )
216
222
if err != nil {
217
223
return err
@@ -224,14 +230,14 @@ func withEnvironmentScopeFilter(environmentScope string) gitlab.RequestOptionFun
224
230
225
231
var errProjectVariableNotExist = errors .New ("project variable does not exist" )
226
232
227
- func getProjectVariable (client * gitlab.Client , project interface {}, key , environmentScope string ) (* gitlab.ProjectVariable , error ) {
233
+ func getProjectVariable (ctx context. Context , client * gitlab.Client , project interface {}, key , environmentScope string ) (* gitlab.ProjectVariable , error ) {
228
234
// List and filter variables manually to support GitLab versions < v13.4 (2020-08-22)
229
235
// ref: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39209
230
236
231
237
page := 1
232
238
233
239
for {
234
- projectVariables , resp , err := client .ProjectVariables .ListVariables (project , & gitlab.ListProjectVariablesOptions {Page : page })
240
+ projectVariables , resp , err := client .ProjectVariables .ListVariables (project , & gitlab.ListProjectVariablesOptions {Page : page }, gitlab . WithContext ( ctx ) )
235
241
if err != nil {
236
242
return nil , err
237
243
}
0 commit comments