1+ package codefresh
2+
3+ import (
4+ "errors"
5+ "fmt"
6+
7+ cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
8+ "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+ )
10+
11+ func dataSourceUsers () * schema.Resource {
12+ return & schema.Resource {
13+ Read : dataSourceUsersRead ,
14+
15+ 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" : {
25+ Type : schema .TypeList ,
26+ Computed : true ,
27+ Elem : & schema.Resource {
28+ 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" : {
38+ Type : schema .TypeString ,
39+ Optional : true ,
40+ },
41+ "phone_number" : {
42+ Type : schema .TypeString ,
43+ Optional : true ,
44+ },
45+ "country" : {
46+ Type : schema .TypeString ,
47+ Optional : true ,
48+ },
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" : {
81+ Type : schema .TypeList ,
82+ Optional : true ,
83+ Elem : & schema.Resource {
84+ Schema : map [string ]* schema.Schema {
85+ "permissions" : {
86+ Type : schema .TypeSet ,
87+ Optional : true ,
88+ Elem : & schema.Schema {
89+ Type : schema .TypeString ,
90+ },
91+ },
92+ },
93+ },
94+ },
95+ "idp" : {
96+ Type : schema .TypeList ,
97+ Optional : true ,
98+ Elem : & schema.Resource {
99+ Schema : map [string ]* schema.Schema {
100+ "id" : {
101+ Type : schema .TypeString ,
102+ Optional : true ,
103+ },
104+ "client_type" : {
105+ Type : schema .TypeString ,
106+ Optional : true ,
107+ },
108+ },
109+ },
110+ },
111+ },
112+ },
113+ },
114+ },
115+ }
116+ }
117+
118+ func dataSourceUsersRead (d * schema.ResourceData , meta interface {}) error {
119+
120+ client := meta .(* cfClient.Client )
121+
122+ users , err := client .ListUsers ()
123+ if err != nil {
124+ return err
125+ }
126+
127+ email := d .Get ("email" ).(string )
128+
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+ }
141+
142+ return nil
143+ }
144+
145+ func mapDataUsersToResource (user cfClient.User , d * schema.ResourceData ) error {
146+
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 ))
153+ }
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 ))
160+
161+ return nil
162+ }
163+
164+ func flattenPersonal (personal * cfClient.Personal ) []map [string ]interface {} {
165+ return []map [string ]interface {}{
166+ {
167+ "first_name" : personal .FirstName ,
168+ "last_name" : personal .LastName ,
169+ "company_name" : personal .CompanyName ,
170+ "phone_number" : personal .PhoneNumber ,
171+ "country" : personal .Country ,
172+ },
173+ }
174+ }
175+
176+ func flattenLogins (logins * []cfClient.Login ) []map [string ]interface {} {
177+
178+ var res = make ([]map [string ]interface {}, len (* logins ))
179+ for i , login := range * logins {
180+ m := make (map [string ]interface {})
181+ m ["credentials" ] = []map [string ]interface {}{
182+ {"permissions" : login .Credentials .Permissions }}
183+
184+ m ["idp" ] = []map [string ]interface {}{
185+ {
186+ "id" : login .IDP .ID ,
187+ "client_type" : login .IDP .ClientType ,
188+ },
189+ }
190+
191+ res [i ] = m
192+ }
193+
194+ return res
195+ }
0 commit comments