1
1
package gitlab
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"log"
6
7
"strings"
7
8
9
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
11
gitlab "github.com/xanzy/go-gitlab"
10
12
)
11
13
12
14
func dataSourceGitlabUser () * schema.Resource {
13
15
return & schema.Resource {
14
- Read : dataSourceGitlabUserRead ,
16
+ ReadContext : dataSourceGitlabUserRead ,
15
17
Schema : map [string ]* schema.Schema {
16
18
"user_id" : {
17
19
Type : schema .TypeInt ,
@@ -140,7 +142,7 @@ func dataSourceGitlabUser() *schema.Resource {
140
142
}
141
143
}
142
144
143
- func dataSourceGitlabUserRead (d * schema.ResourceData , meta interface {}) error {
145
+ func dataSourceGitlabUserRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
144
146
client := meta .(* gitlab.Client )
145
147
146
148
var user * gitlab.User
@@ -154,9 +156,9 @@ func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
154
156
155
157
if userIDOk {
156
158
// Get user by id
157
- user , _ , err = client .Users .GetUser (userIDData .(int ), gitlab.GetUsersOptions {})
159
+ user , _ , err = client .Users .GetUser (userIDData .(int ), gitlab.GetUsersOptions {}, gitlab . WithContext ( ctx ) )
158
160
if err != nil {
159
- return err
161
+ return diag . FromErr ( err )
160
162
}
161
163
} else if usernameOk || emailOk {
162
164
username := strings .ToLower (usernameData .(string ))
@@ -172,20 +174,20 @@ func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
172
174
}
173
175
174
176
var users []* gitlab.User
175
- users , _ , err = client .Users .ListUsers (listUsersOptions )
177
+ users , _ , err = client .Users .ListUsers (listUsersOptions , gitlab . WithContext ( ctx ) )
176
178
if err != nil {
177
- return err
179
+ return diag . FromErr ( err )
178
180
}
179
181
180
182
if len (users ) == 0 {
181
- return fmt .Errorf ("couldn't find a user matching: %s%s" , username , email )
183
+ return diag .Errorf ("couldn't find a user matching: %s%s" , username , email )
182
184
} else if len (users ) != 1 {
183
- return fmt .Errorf ("more than one user found matching: %s%s" , username , email )
185
+ return diag .Errorf ("more than one user found matching: %s%s" , username , email )
184
186
}
185
187
186
188
user = users [0 ]
187
189
} else {
188
- return fmt .Errorf ("one and only one of user_id, username or email must be set" )
190
+ return diag .Errorf ("one and only one of user_id, username or email must be set" )
189
191
}
190
192
191
193
d .Set ("user_id" , user .ID )
0 commit comments