Skip to content

Commit 64d97cc

Browse files
committed
Use context-aware CRUD functions for Project Variable resource
1 parent 80ae472 commit 64d97cc

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

gitlab/resource_gitlab_project_variable.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package gitlab
22

33
import (
4+
"context"
45
"errors"
5-
"fmt"
66
"log"
77
"net/http"
88
"net/url"
99
"strings"
1010

1111
retryablehttp "github.com/hashicorp/go-retryablehttp"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
gitlab "github.com/xanzy/go-gitlab"
1415
)
1516

1617
func resourceGitlabProjectVariable() *schema.Resource {
1718
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,
2223
Importer: &schema.ResourceImporter{
2324
StateContext: schema.ImportStatePassthroughContext,
2425
},
@@ -67,7 +68,7 @@ func resourceGitlabProjectVariable() *schema.Resource {
6768
}
6869
}
6970

70-
func resourceGitlabProjectVariableCreate(d *schema.ResourceData, meta interface{}) error {
71+
func resourceGitlabProjectVariableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
7172
client := meta.(*gitlab.Client)
7273

7374
project := d.Get("project").(string)
@@ -91,17 +92,17 @@ func resourceGitlabProjectVariableCreate(d *schema.ResourceData, meta interface{
9192

9293
log.Printf("[DEBUG] create gitlab project variable %q", id)
9394

94-
_, _, err := client.ProjectVariables.CreateVariable(project, &options)
95+
_, _, err := client.ProjectVariables.CreateVariable(project, &options, gitlab.WithContext(ctx))
9596
if err != nil {
9697
return augmentProjectVariableClientError(d, err)
9798
}
9899

99100
d.SetId(id)
100101

101-
return resourceGitlabProjectVariableRead(d, meta)
102+
return resourceGitlabProjectVariableRead(ctx, d, meta)
102103
}
103104

104-
func resourceGitlabProjectVariableRead(d *schema.ResourceData, meta interface{}) error {
105+
func resourceGitlabProjectVariableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
105106
client := meta.(*gitlab.Client)
106107

107108
var (
@@ -123,12 +124,12 @@ func resourceGitlabProjectVariableRead(d *schema.ResourceData, meta interface{})
123124
key = parts[1]
124125
environmentScope = parts[2]
125126
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())
127128
}
128129

129130
log.Printf("[DEBUG] read gitlab project variable %q", d.Id())
130131

131-
v, err := getProjectVariable(client, project, key, environmentScope)
132+
v, err := getProjectVariable(ctx, client, project, key, environmentScope)
132133
if err != nil {
133134
if errors.Is(err, errProjectVariableNotExist) {
134135
log.Printf("[DEBUG] read gitlab project variable %q was not found", d.Id())
@@ -148,7 +149,7 @@ func resourceGitlabProjectVariableRead(d *schema.ResourceData, meta interface{})
148149
return nil
149150
}
150151

151-
func resourceGitlabProjectVariableUpdate(d *schema.ResourceData, meta interface{}) error {
152+
func resourceGitlabProjectVariableUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
152153
client := meta.(*gitlab.Client)
153154

154155
project := d.Get("project").(string)
@@ -168,15 +169,15 @@ func resourceGitlabProjectVariableUpdate(d *schema.ResourceData, meta interface{
168169
}
169170
log.Printf("[DEBUG] update gitlab project variable %q", d.Id())
170171

171-
_, _, err := client.ProjectVariables.UpdateVariable(project, key, options, withEnvironmentScopeFilter(environmentScope))
172+
_, _, err := client.ProjectVariables.UpdateVariable(project, key, options, withEnvironmentScopeFilter(ctx, environmentScope))
172173
if err != nil {
173174
return augmentProjectVariableClientError(d, err)
174175
}
175176

176-
return resourceGitlabProjectVariableRead(d, meta)
177+
return resourceGitlabProjectVariableRead(ctx, d, meta)
177178
}
178179

179-
func resourceGitlabProjectVariableDelete(d *schema.ResourceData, meta interface{}) error {
180+
func resourceGitlabProjectVariableDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
180181
client := meta.(*gitlab.Client)
181182
project := d.Get("project").(string)
182183
key := d.Get("key").(string)
@@ -187,19 +188,23 @@ func resourceGitlabProjectVariableDelete(d *schema.ResourceData, meta interface{
187188
// but it will be ignored in prior versions, causing nondeterministic destroy behavior when
188189
// destroying or updating scoped variables.
189190
// 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))
191192
return augmentProjectVariableClientError(d, err)
192193
}
193194

194-
func augmentProjectVariableClientError(d *schema.ResourceData, err error) error {
195+
func augmentProjectVariableClientError(d *schema.ResourceData, err error) diag.Diagnostics {
195196
// Masked values will commonly error due to their strict requirements, and the error message from the GitLab API is not very informative,
196197
// so we return a custom error message in this case.
197198
if d.Get("masked").(bool) && isInvalidValueError(err) {
198199
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")
200201
}
201202

202-
return err
203+
if err != nil {
204+
return diag.FromErr(err)
205+
}
206+
207+
return nil
203208
}
204209

205210
func isInvalidValueError(err error) bool {
@@ -210,8 +215,9 @@ func isInvalidValueError(err error) bool {
210215
strings.Contains(httpErr.Message, "invalid")
211216
}
212217

213-
func withEnvironmentScopeFilter(environmentScope string) gitlab.RequestOptionFunc {
218+
func withEnvironmentScopeFilter(ctx context.Context, environmentScope string) gitlab.RequestOptionFunc {
214219
return func(req *retryablehttp.Request) error {
220+
*req = *req.WithContext(ctx)
215221
query, err := url.ParseQuery(req.Request.URL.RawQuery)
216222
if err != nil {
217223
return err
@@ -224,14 +230,14 @@ func withEnvironmentScopeFilter(environmentScope string) gitlab.RequestOptionFun
224230

225231
var errProjectVariableNotExist = errors.New("project variable does not exist")
226232

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) {
228234
// List and filter variables manually to support GitLab versions < v13.4 (2020-08-22)
229235
// ref: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39209
230236

231237
page := 1
232238

233239
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))
235241
if err != nil {
236242
return nil, err
237243
}

gitlab/resource_gitlab_project_variable_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gitlab
22

33
import (
4+
"context"
45
"fmt"
56
"regexp"
67
"strconv"
@@ -66,7 +67,7 @@ func testAccCheckGitlabProjectVariableExists(client *gitlab.Client, name string)
6667
func(state *terraform.State) error {
6768
attributes := state.RootModule().Resources[name].Primary.Attributes
6869

69-
got, err := getProjectVariable(client, attributes["project"], attributes["key"], attributes["environment_scope"])
70+
got, err := getProjectVariable(context.Background(), client, attributes["project"], attributes["key"], attributes["environment_scope"])
7071
if err != nil {
7172
return err
7273
}

0 commit comments

Comments
 (0)