Skip to content

Commit e110d81

Browse files
committed
Add data users
1 parent 5bd5cae commit e110d81

File tree

8 files changed

+216
-21
lines changed

8 files changed

+216
-21
lines changed

client/idp.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (client *Client) GetIdpByName(idpName string) (*IDP, error) {
7272
}
7373
}
7474

75-
return nil, errors.New(fmt.Sprintf("[ERROR] IDP with name %s isn't found.", idpName ))
75+
return nil, errors.New(fmt.Sprintf("[ERROR] IDP with name %s isn't found.", idpName))
7676
}
7777

7878
func (client *Client) GetIdpByID(idpID string) (*IDP, error) {
@@ -83,15 +83,14 @@ func (client *Client) GetIdpByID(idpID string) (*IDP, error) {
8383
}
8484

8585
for _, idp := range *idpList {
86-
if idp.ID == idpID{
86+
if idp.ID == idpID {
8787
return &idp, nil
8888
}
8989
}
9090

9191
return nil, errors.New(fmt.Sprintf("[ERROR] IDP with ID %s isn't found.", idpID))
9292
}
9393

94-
9594
// get account idps
9695
func (client *Client) GetAccountIDPs() (*[]IDP, error) {
9796
fullPath := "/idp/account"
@@ -124,7 +123,7 @@ func (client *Client) AddAccountToIDP(accountId, idpId string) error {
124123
opts := RequestOptions{
125124
Path: "/admin/idp/addAccount",
126125
Method: "POST",
127-
Body: []byte(body),
126+
Body: []byte(body),
128127
}
129128

130129
_, err := client.RequestAPI(&opts)

client/team.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,4 @@ func GetUsersDiff(desiredUsers []string, existingUsers []TeamUser) (usersToAdd [
256256
}
257257

258258
return usersToAdd, usersToDelete
259-
}
259+
}

client/user.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ type ShortProfile struct {
1818
}
1919

2020
type Personal struct {
21-
FirstName string `json:"firstName,omitempty"`
22-
LastName string `json:"lastName,omitempty"`
21+
FirstName string `json:"firstName,omitempty"`
22+
LastName string `json:"lastName,omitempty"`
2323
CompanyName string `json:"companyName,omitempty"`
2424
PhoneNumber string `json:"phoneNumber,omitempty"`
25-
Country string `json:"country,omitempty"`
25+
Country string `json:"country,omitempty"`
2626
}
2727

2828
type User struct {
2929
ID string `json:"_id,omitempty"`
3030
UserName string `json:"userName"`
3131
Email string `json:"email"`
32-
Personal Personal `json:"personal,omitempty"`
32+
Personal *Personal `json:"personal,omitempty"`
3333
Roles []string `json:"roles,omitempty"`
3434
DefaultAccount int `json:"defaultAccount,omitempty"`
3535
Account []Account `json:"account,omitempty"`
@@ -193,4 +193,3 @@ func (client *Client) DeleteUser(userName string) error {
193193

194194
return nil
195195
}
196-

codefresh/data_users.go

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
}

codefresh/provider.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ func Provider() terraform.ResourceProvider {
2626
DefaultFunc: schema.EnvDefaultFunc("CODEFRESH_API_KEY", ""),
2727
},
2828
},
29+
DataSourcesMap: map[string]*schema.Resource{
30+
"codefresh_users": dataSourceUsers(),
31+
},
2932
ResourcesMap: map[string]*schema.Resource{
30-
"codefresh_project": resourceProject(),
31-
"codefresh_pipeline": resourcePipeline(),
32-
"codefresh_team": resourceTeam(),
33-
"codefresh_account": resourceAccount(),
34-
"codefresh_api_key": resourceApiKey(),
35-
"codefresh_idp_accounts": resourceIDPAccounts(),
33+
"codefresh_project": resourceProject(),
34+
"codefresh_pipeline": resourcePipeline(),
35+
"codefresh_team": resourceTeam(),
36+
"codefresh_account": resourceAccount(),
37+
"codefresh_api_key": resourceApiKey(),
38+
"codefresh_idp_accounts": resourceIDPAccounts(),
3639
},
3740
ConfigureFunc: configureProvider,
3841
}

codefresh/resource_api_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,4 @@ func mapResourceToApiKey(d *schema.ResourceData) *cfClient.ApiKey {
169169
Scopes: convertStringArr(scopes),
170170
}
171171
return apiKey
172-
}
172+
}

codefresh/resource_idp_accounts.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func resourceAccountIDPCreate(d *schema.ResourceData, meta interface{}) error {
3434

3535
client := meta.(*cfClient.Client)
3636

37-
accounts:= convertStringArr(d.Get("accounts").(*schema.Set).List())
37+
accounts := convertStringArr(d.Get("accounts").(*schema.Set).List())
3838

3939
idpName := d.Get("idp").(string)
4040

@@ -80,7 +80,6 @@ func resourceAccountIDPRead(d *schema.ResourceData, meta interface{}) error {
8080
return nil
8181
}
8282

83-
8483
func resourceAccountIDPDelete(_ *schema.ResourceData, _ interface{}) error {
8584
return nil
8685
}
@@ -107,4 +106,4 @@ func resourceAccountIDPUpdate(d *schema.ResourceData, meta interface{}) error {
107106
}
108107

109108
return nil
110-
}
109+
}

codefresh/resource_team.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) error {
5757
}
5858

5959
d.SetId(resp.ID)
60-
d.Set("account_id",resp.Account)
60+
d.Set("account_id", resp.Account)
6161

6262
return nil
6363
}

0 commit comments

Comments
 (0)