Skip to content

Commit ae1a075

Browse files
committed
Use context-aware CRUD functions for Project data source
1 parent 2209d29 commit ae1a075

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

gitlab/data_source_gitlab_project.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package gitlab
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"log"
78
"net/http"
89

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
"github.com/xanzy/go-gitlab"
1113
)
1214

1315
func dataSourceGitlabProject() *schema.Resource {
1416
return &schema.Resource{
15-
Read: dataSourceGitlabProjectRead,
17+
ReadContext: dataSourceGitlabProjectRead,
1618

1719
Schema: map[string]*schema.Schema{
1820
"id": {
@@ -156,16 +158,16 @@ func dataSourceGitlabProject() *schema.Resource {
156158
}
157159
}
158160

159-
func dataSourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error {
161+
func dataSourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
160162
client := meta.(*gitlab.Client)
161163

162164
log.Printf("[INFO] Reading Gitlab project")
163165

164166
v, _ := d.GetOk("id")
165167

166-
found, _, err := client.Projects.GetProject(v, nil)
168+
found, _, err := client.Projects.GetProject(v, nil, gitlab.WithContext(ctx))
167169
if err != nil {
168-
return err
170+
return diag.FromErr(err)
169171
}
170172

171173
d.SetId(fmt.Sprintf("%d", found.ID))
@@ -191,12 +193,12 @@ func dataSourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error
191193

192194
log.Printf("[DEBUG] Reading Gitlab project %q push rules", d.Id())
193195

194-
pushRules, _, err := client.Projects.GetProjectPushRules(d.Id())
196+
pushRules, _, err := client.Projects.GetProjectPushRules(d.Id(), gitlab.WithContext(ctx))
195197
var httpError *gitlab.ErrorResponse
196198
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
197199
log.Printf("[DEBUG] Failed to get push rules for project %q: %v", d.Id(), err)
198200
} else if err != nil {
199-
return fmt.Errorf("Failed to get push rules for project %q: %w", d.Id(), err)
201+
return diag.Errorf("Failed to get push rules for project %q: %v", d.Id(), err)
200202
}
201203

202204
d.Set("push_rules", flattenProjectPushRules(pushRules)) // lintignore: XR004 // TODO: Resolve this tfproviderlint issue

0 commit comments

Comments
 (0)