Skip to content

Commit 548ae4d

Browse files
committed
resource/gitlab_project_variable: Use GET API to retrieve single resource instead of listing
1 parent 49db985 commit 548ae4d

File tree

2 files changed

+11
-39
lines changed

2 files changed

+11
-39
lines changed

internal/provider/resource_gitlab_project_variable.go

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package provider
22

33
import (
44
"context"
5-
"errors"
65
"log"
76
"strings"
87

@@ -139,23 +138,23 @@ func resourceGitlabProjectVariableRead(ctx context.Context, d *schema.ResourceDa
139138

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

142-
v, err := getProjectVariable(ctx, client, project, key, environmentScope)
141+
variable, _, err := client.ProjectVariables.GetVariable(project, key, nil, gitlab.WithContext(ctx), withEnvironmentScopeFilter(ctx, environmentScope))
143142
if err != nil {
144-
if errors.Is(err, errProjectVariableNotExist) {
145-
log.Printf("[DEBUG] read gitlab project variable %q was not found", d.Id())
143+
if is404(err) {
144+
log.Printf("[DEBUG] read gitlab project variable %q was not found, removing from state", d.Id())
146145
d.SetId("")
147146
return nil
148147
}
149148
return augmentVariableClientError(d, err)
150149
}
151150

152-
d.Set("key", v.Key)
153-
d.Set("value", v.Value)
154-
d.Set("variable_type", v.VariableType)
151+
d.Set("key", variable.Key)
152+
d.Set("value", variable.Value)
153+
d.Set("variable_type", variable.VariableType)
155154
d.Set("project", project)
156-
d.Set("protected", v.Protected)
157-
d.Set("masked", v.Masked)
158-
d.Set("environment_scope", v.EnvironmentScope)
155+
d.Set("protected", variable.Protected)
156+
d.Set("masked", variable.Masked)
157+
d.Set("environment_scope", variable.EnvironmentScope)
159158
return nil
160159
}
161160

@@ -201,31 +200,3 @@ func resourceGitlabProjectVariableDelete(ctx context.Context, d *schema.Resource
201200
_, err := client.ProjectVariables.RemoveVariable(project, key, nil, withEnvironmentScopeFilter(ctx, environmentScope))
202201
return augmentVariableClientError(d, err)
203202
}
204-
205-
var errProjectVariableNotExist = errors.New("project variable does not exist")
206-
207-
func getProjectVariable(ctx context.Context, client *gitlab.Client, project interface{}, key, environmentScope string) (*gitlab.ProjectVariable, error) {
208-
// List and filter variables manually to support GitLab versions < v13.4 (2020-08-22)
209-
// ref: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39209
210-
211-
page := 1
212-
213-
for {
214-
projectVariables, resp, err := client.ProjectVariables.ListVariables(project, &gitlab.ListProjectVariablesOptions{Page: page}, gitlab.WithContext(ctx))
215-
if err != nil {
216-
return nil, err
217-
}
218-
219-
for _, v := range projectVariables {
220-
if v.Key == key && v.EnvironmentScope == environmentScope {
221-
return v, nil
222-
}
223-
}
224-
225-
if resp.NextPage == 0 {
226-
return nil, errProjectVariableNotExist
227-
}
228-
229-
page++
230-
}
231-
}

internal/provider/resource_gitlab_project_variable_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
12+
"github.com/xanzy/go-gitlab"
1213
)
1314

1415
func testAccCheckGitlabProjectVariableExists(name string) resource.TestCheckFunc {
@@ -26,7 +27,7 @@ func testAccCheckGitlabProjectVariableExists(name string) resource.TestCheckFunc
2627
func(state *terraform.State) error {
2728
attributes := state.RootModule().Resources[name].Primary.Attributes
2829

29-
got, err := getProjectVariable(context.Background(), testGitlabClient, attributes["project"], attributes["key"], attributes["environment_scope"])
30+
got, _, err := testGitlabClient.ProjectVariables.GetVariable(attributes["project"], attributes["key"], nil, gitlab.WithContext(context.Background()), withEnvironmentScopeFilter(context.Background(), attributes["environment_scope"]))
3031
if err != nil {
3132
return err
3233
}

0 commit comments

Comments
 (0)