Skip to content

Commit 6fd297d

Browse files
support searching for e-mail or username
1 parent 535bbc2 commit 6fd297d

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

gitlab/data_source_gitlab_user.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ func dataSourceGitlabUser() *schema.Resource {
1515
Schema: map[string]*schema.Schema{
1616
"email": {
1717
Type: schema.TypeString,
18-
Required: true,
18+
Optional: true,
19+
},
20+
"username": {
21+
Type: schema.TypeString,
22+
Optional: true,
1923
},
2024
},
2125
}
@@ -27,8 +31,15 @@ func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
2731
log.Printf("[INFO] Reading Gitlab user")
2832

2933
searchEmail := strings.ToLower(d.Get("email").(string))
34+
userName := strings.ToLower(d.Get("username").(string))
35+
var q *string
36+
if searchEmail != "" {
37+
q = &searchEmail
38+
} else {
39+
q = &userName
40+
}
3041
query := &gitlab.ListUsersOptions{
31-
Search: &searchEmail,
42+
Search: q,
3243
}
3344
users, _, err := client.Users.ListUsers(query)
3445
if err != nil {
@@ -37,18 +48,28 @@ func dataSourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
3748

3849
var found *gitlab.User
3950

40-
for _, user := range users {
41-
if strings.ToLower(user.Email) == searchEmail {
42-
found = user
43-
break
51+
if searchEmail != "" {
52+
for _, user := range users {
53+
if strings.ToLower(user.Email) == searchEmail {
54+
found = user
55+
break
56+
}
57+
}
58+
} else {
59+
for _, user := range users {
60+
if strings.ToLower(user.Username) == userName {
61+
found = user
62+
break
63+
}
4464
}
4565
}
66+
4667
if found == nil {
4768
return fmt.Errorf("The email '%s' does not match any user email", searchEmail)
4869
}
4970
d.SetId(fmt.Sprintf("%d", found.ID))
5071
d.Set("name", found.Name)
51-
d.Set("username", found.Username)
72+
d.Set("userName", found.Username)
5273
d.Set("email", found.Email)
5374
return nil
5475
}

website/docs/d/user.html.markdown

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: |-
88

99
# gitlab\_user
1010

11-
Provides details about a specific user in the gitlab provider. The results include username, id, name, etc.
11+
Provides details about a specific user in the gitlab provider. Especially the ability to lookup the id for linking to other resources.
1212

1313
## Example Usage
1414

@@ -22,7 +22,11 @@ data "gitlab_user" "example" {
2222

2323
The following arguments are supported:
2424

25-
* `email` - (Required) The e-mail address of the user.
25+
* `email` - (Optional) The e-mail address of the user. (Requires administrator privileges)
26+
27+
* `username` - (Optional) The username of the user.
28+
29+
If both are given only e-mail is used.
2630

2731
## Attributes Reference
2832

0 commit comments

Comments
 (0)