Skip to content

Commit 76379ac

Browse files
committed
Update data users
1 parent e110d81 commit 76379ac

File tree

1 file changed

+101
-95
lines changed

1 file changed

+101
-95
lines changed

codefresh/data_users.go

Lines changed: 101 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package codefresh
22

33
import (
4-
"errors"
5-
"fmt"
6-
4+
"time"
75
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
86
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
97
)
@@ -13,97 +11,110 @@ func dataSourceUsers() *schema.Resource {
1311
Read: dataSourceUsersRead,
1412

1513
Schema: map[string]*schema.Schema{
16-
"user_name": {
17-
Type: schema.TypeString,
18-
Computed: true,
19-
},
20-
"email": {
21-
Type: schema.TypeString,
22-
Required: true,
23-
},
24-
"personal": {
14+
15+
"users": {
2516
Type: schema.TypeList,
2617
Computed: true,
2718
Elem: &schema.Resource{
2819
Schema: map[string]*schema.Schema{
29-
"first_name": {
30-
Type: schema.TypeString,
31-
Optional: true,
32-
},
33-
"last_name": {
34-
Type: schema.TypeString,
35-
Optional: true,
36-
},
37-
"company_name": {
20+
"user_name": {
3821
Type: schema.TypeString,
39-
Optional: true,
22+
Computed: true,
4023
},
41-
"phone_number": {
24+
"email": {
4225
Type: schema.TypeString,
43-
Optional: true,
26+
Computed: true,
4427
},
45-
"country": {
28+
"id": {
4629
Type: schema.TypeString,
47-
Optional: true,
30+
Computed: true,
4831
},
49-
},
50-
},
51-
},
52-
"short_profile": {
53-
Type: schema.TypeList,
54-
Computed: true,
55-
Elem: &schema.Resource{
56-
Schema: map[string]*schema.Schema{
57-
"user_name": {
58-
Type: schema.TypeString,
59-
Optional: true,
60-
},
61-
},
62-
},
63-
},
64-
"roles": {
65-
Type: schema.TypeSet,
66-
Computed: true,
67-
Elem: &schema.Schema{
68-
Type: schema.TypeString,
69-
},
70-
},
71-
"status": {
72-
Type: schema.TypeString,
73-
Computed: true,
74-
},
75-
"logins": {
76-
Type: schema.TypeList,
77-
Computed: true,
78-
Elem: &schema.Resource{
79-
Schema: map[string]*schema.Schema{
80-
"credentials": {
32+
"personal": {
8133
Type: schema.TypeList,
82-
Optional: true,
34+
Computed: true,
8335
Elem: &schema.Resource{
8436
Schema: map[string]*schema.Schema{
85-
"permissions": {
86-
Type: schema.TypeSet,
37+
"first_name": {
38+
Type: schema.TypeString,
39+
Optional: true,
40+
},
41+
"last_name": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
},
45+
"company_name": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
},
49+
"phone_number": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
},
53+
"country": {
54+
Type: schema.TypeString,
8755
Optional: true,
88-
Elem: &schema.Schema{
89-
Type: schema.TypeString,
90-
},
9156
},
9257
},
9358
},
9459
},
95-
"idp": {
60+
"short_profile": {
9661
Type: schema.TypeList,
97-
Optional: true,
62+
Computed: true,
9863
Elem: &schema.Resource{
9964
Schema: map[string]*schema.Schema{
100-
"id": {
65+
"user_name": {
10166
Type: schema.TypeString,
10267
Optional: true,
10368
},
104-
"client_type": {
105-
Type: schema.TypeString,
69+
},
70+
},
71+
},
72+
"roles": {
73+
Type: schema.TypeSet,
74+
Computed: true,
75+
Elem: &schema.Schema{
76+
Type: schema.TypeString,
77+
},
78+
},
79+
"status": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
},
83+
"logins": {
84+
Type: schema.TypeList,
85+
Computed: true,
86+
Elem: &schema.Resource{
87+
Schema: map[string]*schema.Schema{
88+
"credentials": {
89+
Type: schema.TypeList,
10690
Optional: true,
91+
Elem: &schema.Resource{
92+
Schema: map[string]*schema.Schema{
93+
"permissions": {
94+
Type: schema.TypeSet,
95+
Optional: true,
96+
Elem: &schema.Schema{
97+
Type: schema.TypeString,
98+
},
99+
},
100+
},
101+
},
102+
},
103+
"idp": {
104+
Type: schema.TypeList,
105+
Optional: true,
106+
Elem: &schema.Resource{
107+
Schema: map[string]*schema.Schema{
108+
"id": {
109+
Type: schema.TypeString,
110+
Optional: true,
111+
},
112+
"client_type": {
113+
Type: schema.TypeString,
114+
Optional: true,
115+
},
116+
},
117+
},
107118
},
108119
},
109120
},
@@ -124,39 +135,34 @@ func dataSourceUsersRead(d *schema.ResourceData, meta interface{}) error {
124135
return err
125136
}
126137

127-
email := d.Get("email").(string)
138+
err = mapDataUsersToResource(*users, d)
128139

129-
for _, user := range *users {
130-
if user.Email == email {
131-
err = mapDataUsersToResource(user, d)
132-
if err != nil {
133-
return err
134-
}
135-
}
136-
}
137-
138-
if d.Id() == "" {
139-
return errors.New(fmt.Sprintf("[EROOR] User %s wasn't found", email))
140-
}
140+
d.SetId(time.Now().UTC().String())
141141

142142
return nil
143143
}
144144

145-
func mapDataUsersToResource(user cfClient.User, d *schema.ResourceData) error {
145+
func mapDataUsersToResource(users []cfClient.User, d *schema.ResourceData) error {
146+
147+
var res = make([]map[string]interface{}, len(users))
148+
for i, user := range users {
149+
m := make(map[string]interface{})
150+
m["user_name"] = user.UserName
151+
m["email"] = user.Email
152+
m["status"] = user.Status
153+
if user.Personal != nil {
154+
m["personal"] = flattenPersonal(user.Personal)
155+
}
156+
m["short_profile"] = []map[string]interface{}{
157+
{"user_name": user.ShortProfile.UserName},}
158+
m["roles"] = user.Roles
159+
m["logins"] = flattenLogins(&user.Logins)
160+
m["id"] = user.ID
146161

147-
d.SetId(user.ID)
148-
d.Set("user_name", user.UserName)
149-
d.Set("email", user.Email)
150-
d.Set("status", user.Status)
151-
if user.Personal != nil {
152-
d.Set("personal", flattenPersonal(user.Personal))
162+
res[i] = m
153163
}
154-
d.Set("short_profile",
155-
[]map[string]interface{}{
156-
{"user_name": user.ShortProfile.UserName},
157-
})
158-
d.Set("roles", user.Roles)
159-
d.Set("logins", flattenLogins(&user.Logins))
164+
165+
d.Set("users", res)
160166

161167
return nil
162168
}

0 commit comments

Comments
 (0)