Skip to content

Commit 2c19441

Browse files
author
Jordan Caussat
committed
Add options and expose more parameters
1 parent 6561844 commit 2c19441

File tree

1 file changed

+137
-22
lines changed

1 file changed

+137
-22
lines changed

gitlab/data_source_gitlab_users.go

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

33
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
"time"
8+
49
"github.com/hashicorp/terraform/helper/schema"
510
"github.com/hashicorp/terraform/helper/validation"
611
gitlab "github.com/xanzy/go-gitlab"
@@ -11,25 +16,62 @@ func dataSourceGitlabUsers() *schema.Resource {
1116
Read: dataSourceGitlabUsersRead,
1217

1318
Schema: map[string]*schema.Schema{
14-
"order_by": {
15-
Type: schema.TypeString,
19+
"options": {
20+
Type: schema.TypeList,
1621
Optional: true,
17-
Default: "id",
18-
ValidateFunc: validation.StringInSlice([]string{"id", "name",
19-
"username", "created_at"}, true),
20-
},
21-
"sort": {
22-
Type: schema.TypeString,
23-
Optional: true,
24-
Default: "desc",
25-
ValidateFunc: validation.StringInSlice([]string{"desc", "asc"}, true),
22+
MaxItems: 1,
23+
Elem: &schema.Resource{
24+
Schema: map[string]*schema.Schema{
25+
"order_by": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Default: "id",
29+
ValidateFunc: validation.StringInSlice([]string{"id", "name",
30+
"username", "created_at"}, true),
31+
},
32+
"sort": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
Default: "desc",
36+
ValidateFunc: validation.StringInSlice([]string{"desc", "asc"}, true),
37+
},
38+
"search": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
},
42+
"active": {
43+
Type: schema.TypeBool,
44+
Optional: true,
45+
},
46+
"blocked": {
47+
Type: schema.TypeBool,
48+
Optional: true,
49+
},
50+
"extern_uid": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
},
54+
"provider": {
55+
Type: schema.TypeString,
56+
Optional: true,
57+
},
58+
"created_before": {
59+
Type: schema.TypeString,
60+
Optional: true,
61+
},
62+
"created_after": {
63+
Type: schema.TypeString,
64+
Optional: true,
65+
},
66+
},
67+
},
2668
},
2769
"users": {
2870
Type: schema.TypeList,
2971
Computed: true,
3072
Elem: &schema.Resource{
3173
Schema: map[string]*schema.Schema{
32-
"id": {
74+
"user_id": {
3375
Type: schema.TypeInt,
3476
Computed: true,
3577
},
@@ -73,6 +115,18 @@ func dataSourceGitlabUsers() *schema.Resource {
73115
Type: schema.TypeBool,
74116
Computed: true,
75117
},
118+
"extern_uid": {
119+
Type: schema.TypeString,
120+
Computed: true,
121+
},
122+
"organization": {
123+
Type: schema.TypeString,
124+
Computed: true,
125+
},
126+
"two_factor_enabled": {
127+
Type: schema.TypeBool,
128+
Computed: true,
129+
},
76130
},
77131
},
78132
},
@@ -83,21 +137,20 @@ func dataSourceGitlabUsers() *schema.Resource {
83137
func dataSourceGitlabUsersRead(d *schema.ResourceData, meta interface{}) error {
84138
client := meta.(*gitlab.Client)
85139

86-
orderBy := d.Get("order_by").(string)
87-
sort := d.Get("sort").(string)
88-
listUsersOptions := &gitlab.ListUsersOptions{
89-
OrderBy: &orderBy,
90-
Sort: &sort,
140+
listUsersOptions, id, err := expandGitlabUsersOptions(d.Get("options").([]interface{}))
141+
if err != nil {
142+
return err
91143
}
92-
144+
log.Printf("\n\n\nListOptions\n%v\n\n\n", listUsersOptions)
93145
users, _, err := client.Users.ListUsers(listUsersOptions)
146+
log.Printf("\n\n\nListUsers\n%v\n\n\n", users)
147+
94148
if err != nil {
95149
return err
96150
}
97151

98152
d.Set("users", flattenGitlabUsers(users))
99-
id := "all_users_" + orderBy + "_" + sort
100-
d.SetId(id)
153+
d.SetId(fmt.Sprintf("%d", id))
101154

102155
return nil
103156
}
@@ -107,7 +160,7 @@ func flattenGitlabUsers(users []*gitlab.User) []interface{} {
107160

108161
for _, user := range users {
109162
values := map[string]interface{}{
110-
"id": user.ID,
163+
"user_id": user.ID,
111164
"username": user.Username,
112165
"email": user.Email,
113166
"name": user.Name,
@@ -117,14 +170,76 @@ func flattenGitlabUsers(users []*gitlab.User) []interface{} {
117170
"projects_limit": user.ProjectsLimit,
118171
"state": user.State,
119172
"external": user.External,
173+
"extern_uid": user.ExternUID,
174+
"organization": user.Organization,
175+
"two_factor_enabled": user.TwoFactorEnabled,
120176
}
121177

122178
if user.CreatedAt != nil {
123-
values["created_at"] = user.CreatedAt.String()
179+
values["created_at"] = user.CreatedAt
124180
}
125181

126182
usersList = append(usersList, values)
127183
}
128184

129185
return usersList
130186
}
187+
188+
func expandGitlabUsersOptions(d []interface{}) (*gitlab.ListUsersOptions, int, error) {
189+
if len(d) == 0 {
190+
return nil, 0, nil
191+
}
192+
193+
data := d[0].(map[string]interface{})
194+
listUsersOptions := &gitlab.ListUsersOptions{}
195+
options := ""
196+
197+
if orderBy := data["order_by"].(string); orderBy != "" {
198+
listUsersOptions.OrderBy = &orderBy
199+
options += orderBy
200+
}
201+
if sort := data["sort"].(string); sort != "" {
202+
listUsersOptions.Sort = &sort
203+
options += sort
204+
}
205+
if search := data["search"].(string); search != "" {
206+
listUsersOptions.Search = &search
207+
options += search
208+
}
209+
if active := data["active"].(bool); active != false {
210+
listUsersOptions.Active = &active
211+
options += strconv.FormatBool(active)
212+
}
213+
if blocked := data["blocked"].(bool); blocked != false {
214+
listUsersOptions.Blocked = &blocked
215+
options += strconv.FormatBool(blocked)
216+
}
217+
if externalUID := data["extern_uid"].(string); externalUID != "" {
218+
listUsersOptions.ExternalUID = &externalUID
219+
options += externalUID
220+
}
221+
if provider := data["provider"].(string); provider != "" {
222+
// listUsersOptions.Provider = &provider
223+
options += provider
224+
}
225+
if createdBefore := data["created_before"].(string); createdBefore != "" {
226+
date, err := time.Parse("2006-01-02", createdBefore)
227+
if err != nil {
228+
return nil, 0, fmt.Errorf("created_before must be in yyyy-mm-dd format")
229+
}
230+
listUsersOptions.CreatedBefore = &date
231+
options += createdBefore
232+
}
233+
if createdAfter := data["created_after"].(string); createdAfter != "" {
234+
// date, err := time.Parse("2006-01-02", createdAfter)
235+
// if err != nil {
236+
// return nil, 0, fmt.Errorf("created_after must be in yyyy-mm-dd format")
237+
// }
238+
// listUsersOptions.CreatedAfter = &date
239+
options += createdAfter
240+
}
241+
242+
id := schema.HashString(options)
243+
244+
return listUsersOptions, id, nil
245+
}

0 commit comments

Comments
 (0)