Skip to content

Commit 878115b

Browse files
author
Jordan Caussat
committed
Move options to root & rename identities parameters
1 parent 2c19441 commit 878115b

File tree

1 file changed

+73
-81
lines changed

1 file changed

+73
-81
lines changed

gitlab/data_source_gitlab_users.go

Lines changed: 73 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitlab
22

33
import (
44
"fmt"
5-
"log"
65
"strconv"
76
"time"
87

@@ -16,62 +15,53 @@ func dataSourceGitlabUsers() *schema.Resource {
1615
Read: dataSourceGitlabUsersRead,
1716

1817
Schema: map[string]*schema.Schema{
19-
"options": {
20-
Type: schema.TypeList,
18+
"order_by": {
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Default: "id",
22+
ValidateFunc: validation.StringInSlice([]string{"id", "name",
23+
"username", "created_at"}, true),
24+
},
25+
"sort": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Default: "desc",
29+
ValidateFunc: validation.StringInSlice([]string{"desc", "asc"}, true),
30+
},
31+
"search": {
32+
Type: schema.TypeString,
33+
Optional: true,
34+
},
35+
"active": {
36+
Type: schema.TypeBool,
37+
Optional: true,
38+
},
39+
"blocked": {
40+
Type: schema.TypeBool,
41+
Optional: true,
42+
},
43+
"identities_extern_uid": {
44+
Type: schema.TypeString,
45+
Optional: true,
46+
},
47+
"identities_provider": {
48+
Type: schema.TypeString,
49+
Optional: true,
50+
},
51+
"created_before": {
52+
Type: schema.TypeString,
53+
Optional: true,
54+
},
55+
"created_after": {
56+
Type: schema.TypeString,
2157
Optional: 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-
},
6858
},
6959
"users": {
7060
Type: schema.TypeList,
7161
Computed: true,
7262
Elem: &schema.Resource{
7363
Schema: map[string]*schema.Schema{
74-
"user_id": {
64+
"id": {
7565
Type: schema.TypeInt,
7666
Computed: true,
7767
},
@@ -137,13 +127,11 @@ func dataSourceGitlabUsers() *schema.Resource {
137127
func dataSourceGitlabUsersRead(d *schema.ResourceData, meta interface{}) error {
138128
client := meta.(*gitlab.Client)
139129

140-
listUsersOptions, id, err := expandGitlabUsersOptions(d.Get("options").([]interface{}))
130+
listUsersOptions, id, err := expandGitlabUsersOptions(d)
141131
if err != nil {
142132
return err
143133
}
144-
log.Printf("\n\n\nListOptions\n%v\n\n\n", listUsersOptions)
145134
users, _, err := client.Users.ListUsers(listUsersOptions)
146-
log.Printf("\n\n\nListUsers\n%v\n\n\n", users)
147135

148136
if err != nil {
149137
return err
@@ -160,7 +148,7 @@ func flattenGitlabUsers(users []*gitlab.User) []interface{} {
160148

161149
for _, user := range users {
162150
values := map[string]interface{}{
163-
"user_id": user.ID,
151+
"id": user.ID,
164152
"username": user.Username,
165153
"email": user.Email,
166154
"name": user.Name,
@@ -176,7 +164,7 @@ func flattenGitlabUsers(users []*gitlab.User) []interface{} {
176164
}
177165

178166
if user.CreatedAt != nil {
179-
values["created_at"] = user.CreatedAt
167+
values["created_at"] = user.CreatedAt.String()
180168
}
181169

182170
usersList = append(usersList, values)
@@ -185,61 +173,65 @@ func flattenGitlabUsers(users []*gitlab.User) []interface{} {
185173
return usersList
186174
}
187175

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{})
176+
func expandGitlabUsersOptions(d *schema.ResourceData) (*gitlab.ListUsersOptions, int, error) {
194177
listUsersOptions := &gitlab.ListUsersOptions{}
195-
options := ""
178+
optionsHash := ""
196179

197-
if orderBy := data["order_by"].(string); orderBy != "" {
180+
if data, ok := d.GetOk("order_by"); ok {
181+
orderBy := data.(string)
198182
listUsersOptions.OrderBy = &orderBy
199-
options += orderBy
183+
optionsHash += orderBy
200184
}
201-
if sort := data["sort"].(string); sort != "" {
185+
if data, ok := d.GetOk("sort"); ok {
186+
sort := data.(string)
202187
listUsersOptions.Sort = &sort
203-
options += sort
188+
optionsHash += sort
204189
}
205-
if search := data["search"].(string); search != "" {
190+
if data, ok := d.GetOk("search"); ok {
191+
search := data.(string)
206192
listUsersOptions.Search = &search
207-
options += search
193+
optionsHash += search
208194
}
209-
if active := data["active"].(bool); active != false {
195+
if data, ok := d.GetOk("active"); ok {
196+
active := data.(bool)
210197
listUsersOptions.Active = &active
211-
options += strconv.FormatBool(active)
198+
optionsHash += strconv.FormatBool(active)
212199
}
213-
if blocked := data["blocked"].(bool); blocked != false {
200+
if data, ok := d.GetOk("blocked"); ok {
201+
blocked := data.(bool)
214202
listUsersOptions.Blocked = &blocked
215-
options += strconv.FormatBool(blocked)
203+
optionsHash += strconv.FormatBool(blocked)
216204
}
217-
if externalUID := data["extern_uid"].(string); externalUID != "" {
205+
if data, ok := d.GetOk("identities_extern_uid"); ok {
206+
externalUID := data.(string)
218207
listUsersOptions.ExternalUID = &externalUID
219-
options += externalUID
208+
optionsHash += externalUID
220209
}
221-
if provider := data["provider"].(string); provider != "" {
210+
if data, ok := d.GetOk("identities_provider"); ok {
211+
provider := data.(string)
222212
// listUsersOptions.Provider = &provider
223-
options += provider
213+
optionsHash += provider
224214
}
225-
if createdBefore := data["created_before"].(string); createdBefore != "" {
215+
if data, ok := d.GetOk("created_before"); ok {
216+
createdBefore := data.(string)
226217
date, err := time.Parse("2006-01-02", createdBefore)
227218
if err != nil {
228219
return nil, 0, fmt.Errorf("created_before must be in yyyy-mm-dd format")
229220
}
230221
listUsersOptions.CreatedBefore = &date
231-
options += createdBefore
222+
optionsHash += createdBefore
232223
}
233-
if createdAfter := data["created_after"].(string); createdAfter != "" {
224+
if data, ok := d.GetOk("created_after"); ok {
225+
createdAfter := data.(string)
234226
// date, err := time.Parse("2006-01-02", createdAfter)
235227
// if err != nil {
236228
// return nil, 0, fmt.Errorf("created_after must be in yyyy-mm-dd format")
237229
// }
238230
// listUsersOptions.CreatedAfter = &date
239-
options += createdAfter
231+
optionsHash += createdAfter
240232
}
241233

242-
id := schema.HashString(options)
234+
id := schema.HashString(optionsHash)
243235

244236
return listUsersOptions, id, nil
245237
}

0 commit comments

Comments
 (0)