Skip to content

Commit 5ed40b2

Browse files
committed
Split data users to user and users
1 parent 76379ac commit 5ed40b2

File tree

3 files changed

+207
-142
lines changed

3 files changed

+207
-142
lines changed

codefresh/data_user.go

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
package codefresh
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
)
9+
10+
func dataSourceUser() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceUserRead,
13+
Schema: *UserSchema(),
14+
}
15+
}
16+
17+
func dataSourceUserRead(d *schema.ResourceData, meta interface{}) error {
18+
19+
client := meta.(*cfClient.Client)
20+
21+
users, err := client.ListUsers()
22+
if err != nil {
23+
return err
24+
}
25+
26+
email := d.Get("email").(string)
27+
28+
for _, user := range *users {
29+
if user.Email == email {
30+
err = mapDataUserToResource(user, d)
31+
if err != nil {
32+
return err
33+
}
34+
}
35+
}
36+
37+
if d.Id() == "" {
38+
return errors.New(fmt.Sprintf("[EROOR] User %s wasn't found", email))
39+
}
40+
41+
return nil
42+
}
43+
44+
func mapDataUserToResource(user cfClient.User, d *schema.ResourceData) error {
45+
46+
d.SetId(user.ID)
47+
d.Set("user_id", user.ID)
48+
d.Set("user_name", user.UserName)
49+
d.Set("email", user.Email)
50+
d.Set("status", user.Status)
51+
if user.Personal != nil {
52+
d.Set("personal", flattenPersonal(user.Personal))
53+
}
54+
d.Set("short_profile",
55+
[]map[string]interface{}{
56+
{"user_name": user.ShortProfile.UserName},
57+
})
58+
d.Set("roles", user.Roles)
59+
d.Set("logins", flattenLogins(&user.Logins))
60+
61+
return nil
62+
}
63+
64+
func flattenPersonal(personal *cfClient.Personal) []map[string]interface{} {
65+
return []map[string]interface{}{
66+
{
67+
"first_name": personal.FirstName,
68+
"last_name": personal.LastName,
69+
"company_name": personal.CompanyName,
70+
"phone_number": personal.PhoneNumber,
71+
"country": personal.Country,
72+
},
73+
}
74+
}
75+
76+
func flattenLogins(logins *[]cfClient.Login) []map[string]interface{} {
77+
78+
var res = make([]map[string]interface{}, len(*logins))
79+
for i, login := range *logins {
80+
m := make(map[string]interface{})
81+
m["credentials"] = []map[string]interface{}{
82+
{"permissions": login.Credentials.Permissions}}
83+
84+
m["idp"] = []map[string]interface{}{
85+
{
86+
"id": login.IDP.ID,
87+
"client_type": login.IDP.ClientType,
88+
},
89+
}
90+
91+
res[i] = m
92+
}
93+
94+
return res
95+
}
96+
97+
func UserSchema() *map[string]*schema.Schema {
98+
return &map[string]*schema.Schema{
99+
"user_name": {
100+
Type: schema.TypeString,
101+
Computed: true,
102+
},
103+
"email": {
104+
Type: schema.TypeString,
105+
Required: true,
106+
},
107+
"user_id": {
108+
Type: schema.TypeString,
109+
Computed: true,
110+
},
111+
"personal": {
112+
Type: schema.TypeList,
113+
Computed: true,
114+
Elem: &schema.Resource{
115+
Schema: map[string]*schema.Schema{
116+
"first_name": {
117+
Type: schema.TypeString,
118+
Optional: true,
119+
},
120+
"last_name": {
121+
Type: schema.TypeString,
122+
Optional: true,
123+
},
124+
"company_name": {
125+
Type: schema.TypeString,
126+
Optional: true,
127+
},
128+
"phone_number": {
129+
Type: schema.TypeString,
130+
Optional: true,
131+
},
132+
"country": {
133+
Type: schema.TypeString,
134+
Optional: true,
135+
},
136+
},
137+
},
138+
},
139+
"short_profile": {
140+
Type: schema.TypeList,
141+
Computed: true,
142+
Elem: &schema.Resource{
143+
Schema: map[string]*schema.Schema{
144+
"user_name": {
145+
Type: schema.TypeString,
146+
Optional: true,
147+
},
148+
},
149+
},
150+
},
151+
"roles": {
152+
Type: schema.TypeSet,
153+
Computed: true,
154+
Elem: &schema.Schema{
155+
Type: schema.TypeString,
156+
},
157+
},
158+
"status": {
159+
Type: schema.TypeString,
160+
Computed: true,
161+
},
162+
"logins": {
163+
Type: schema.TypeList,
164+
Computed: true,
165+
Elem: &schema.Resource{
166+
Schema: map[string]*schema.Schema{
167+
"credentials": {
168+
Type: schema.TypeList,
169+
Optional: true,
170+
Elem: &schema.Resource{
171+
Schema: map[string]*schema.Schema{
172+
"permissions": {
173+
Type: schema.TypeSet,
174+
Optional: true,
175+
Elem: &schema.Schema{
176+
Type: schema.TypeString,
177+
},
178+
},
179+
},
180+
},
181+
},
182+
"idp": {
183+
Type: schema.TypeList,
184+
Optional: true,
185+
Elem: &schema.Resource{
186+
Schema: map[string]*schema.Schema{
187+
"id": {
188+
Type: schema.TypeString,
189+
Optional: true,
190+
},
191+
"client_type": {
192+
Type: schema.TypeString,
193+
Optional: true,
194+
},
195+
},
196+
},
197+
},
198+
},
199+
},
200+
},
201+
}
202+
}

codefresh/data_users.go

+4-142
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,20 @@
11
package codefresh
22

33
import (
4-
"time"
54
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
65
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
6+
"time"
77
)
88

99
func dataSourceUsers() *schema.Resource {
1010
return &schema.Resource{
1111
Read: dataSourceUsersRead,
12-
1312
Schema: map[string]*schema.Schema{
14-
1513
"users": {
1614
Type: schema.TypeList,
1715
Computed: true,
1816
Elem: &schema.Resource{
19-
Schema: map[string]*schema.Schema{
20-
"user_name": {
21-
Type: schema.TypeString,
22-
Computed: true,
23-
},
24-
"email": {
25-
Type: schema.TypeString,
26-
Computed: true,
27-
},
28-
"id": {
29-
Type: schema.TypeString,
30-
Computed: true,
31-
},
32-
"personal": {
33-
Type: schema.TypeList,
34-
Computed: true,
35-
Elem: &schema.Resource{
36-
Schema: map[string]*schema.Schema{
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,
55-
Optional: true,
56-
},
57-
},
58-
},
59-
},
60-
"short_profile": {
61-
Type: schema.TypeList,
62-
Computed: true,
63-
Elem: &schema.Resource{
64-
Schema: map[string]*schema.Schema{
65-
"user_name": {
66-
Type: schema.TypeString,
67-
Optional: true,
68-
},
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,
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-
},
118-
},
119-
},
120-
},
121-
},
122-
},
17+
Schema: *UserSchema(),
12318
},
12419
},
12520
},
@@ -154,10 +49,10 @@ func mapDataUsersToResource(users []cfClient.User, d *schema.ResourceData) error
15449
m["personal"] = flattenPersonal(user.Personal)
15550
}
15651
m["short_profile"] = []map[string]interface{}{
157-
{"user_name": user.ShortProfile.UserName},}
52+
{"user_name": user.ShortProfile.UserName}}
15853
m["roles"] = user.Roles
15954
m["logins"] = flattenLogins(&user.Logins)
160-
m["id"] = user.ID
55+
m["user_id"] = user.ID
16156

16257
res[i] = m
16358
}
@@ -166,36 +61,3 @@ func mapDataUsersToResource(users []cfClient.User, d *schema.ResourceData) error
16661

16762
return nil
16863
}
169-
170-
func flattenPersonal(personal *cfClient.Personal) []map[string]interface{} {
171-
return []map[string]interface{}{
172-
{
173-
"first_name": personal.FirstName,
174-
"last_name": personal.LastName,
175-
"company_name": personal.CompanyName,
176-
"phone_number": personal.PhoneNumber,
177-
"country": personal.Country,
178-
},
179-
}
180-
}
181-
182-
func flattenLogins(logins *[]cfClient.Login) []map[string]interface{} {
183-
184-
var res = make([]map[string]interface{}, len(*logins))
185-
for i, login := range *logins {
186-
m := make(map[string]interface{})
187-
m["credentials"] = []map[string]interface{}{
188-
{"permissions": login.Credentials.Permissions}}
189-
190-
m["idp"] = []map[string]interface{}{
191-
{
192-
"id": login.IDP.ID,
193-
"client_type": login.IDP.ClientType,
194-
},
195-
}
196-
197-
res[i] = m
198-
}
199-
200-
return res
201-
}

codefresh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func Provider() terraform.ResourceProvider {
2828
},
2929
DataSourcesMap: map[string]*schema.Resource{
3030
"codefresh_users": dataSourceUsers(),
31+
"codefresh_user": dataSourceUser(),
3132
},
3233
ResourcesMap: map[string]*schema.Resource{
3334
"codefresh_project": resourceProject(),

0 commit comments

Comments
 (0)