1
1
package codefresh
2
2
3
3
import (
4
- "errors"
5
- "fmt"
6
-
4
+ "time"
7
5
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
8
6
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9
7
)
@@ -13,97 +11,110 @@ func dataSourceUsers() *schema.Resource {
13
11
Read : dataSourceUsersRead ,
14
12
15
13
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" : {
25
16
Type : schema .TypeList ,
26
17
Computed : true ,
27
18
Elem : & schema.Resource {
28
19
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" : {
38
21
Type : schema .TypeString ,
39
- Optional : true ,
22
+ Computed : true ,
40
23
},
41
- "phone_number " : {
24
+ "email " : {
42
25
Type : schema .TypeString ,
43
- Optional : true ,
26
+ Computed : true ,
44
27
},
45
- "country " : {
28
+ "id " : {
46
29
Type : schema .TypeString ,
47
- Optional : true ,
30
+ Computed : true ,
48
31
},
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" : {
81
33
Type : schema .TypeList ,
82
- Optional : true ,
34
+ Computed : true ,
83
35
Elem : & schema.Resource {
84
36
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 ,
87
55
Optional : true ,
88
- Elem : & schema.Schema {
89
- Type : schema .TypeString ,
90
- },
91
56
},
92
57
},
93
58
},
94
59
},
95
- "idp " : {
60
+ "short_profile " : {
96
61
Type : schema .TypeList ,
97
- Optional : true ,
62
+ Computed : true ,
98
63
Elem : & schema.Resource {
99
64
Schema : map [string ]* schema.Schema {
100
- "id " : {
65
+ "user_name " : {
101
66
Type : schema .TypeString ,
102
67
Optional : true ,
103
68
},
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 ,
106
90
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
+ },
107
118
},
108
119
},
109
120
},
@@ -124,39 +135,34 @@ func dataSourceUsersRead(d *schema.ResourceData, meta interface{}) error {
124
135
return err
125
136
}
126
137
127
- email := d . Get ( "email" ).( string )
138
+ err = mapDataUsersToResource ( * users , d )
128
139
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 ())
141
141
142
142
return nil
143
143
}
144
144
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
146
161
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
153
163
}
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 )
160
166
161
167
return nil
162
168
}
0 commit comments