Skip to content

Commit 5bd5cae

Browse files
committed
Added IDP resource
1 parent 4c5fc24 commit 5bd5cae

File tree

10 files changed

+422
-36
lines changed

10 files changed

+422
-36
lines changed

client/account.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ func (client *Client) UpdateAccount(account *Account) (*Account, error) {
217217
}
218218

219219
existingAccount, err := client.GetAccountByID(id)
220-
if err != nil {
220+
if err != nil {
221221
return nil, err
222222
}
223223

224224
err = mergo.Merge(account, existingAccount)
225-
if err != nil {
225+
if err != nil {
226226
return nil, err
227227
}
228228

client/idp.go

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package client
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
type IDP struct {
9+
Access_token string `json:"access_token,omitempty"`
10+
Accounts []string `json:"accounts,omitempty"`
11+
ApiHost string `json:"apiHost,omitempty"`
12+
ApiPathPrefix string `json:"apiPathPrefix,omitempty"`
13+
ApiURL string `json:"apiURL,omitempty"`
14+
AppId string `json:"appId,omitempty"`
15+
AuthURL string `json:"authURL,omitempty"`
16+
ClientHost string `json:"clientHost,omitempty"`
17+
ClientId string `json:"clientId,omitempty"`
18+
ClientName string `json:"clientName,omitempty"`
19+
ClientSecret string `json:"clientSecret,omitempty"`
20+
ClientType string `json:"clientType,omitempty"`
21+
CookieIv string `json:"cookieIv,omitempty"`
22+
CookieKey string `json:"cookieKey,omitempty"`
23+
DisplayName string `json:"displayName,omitempty"`
24+
ID string `json:"id,omitempty"`
25+
IDPLoginUrl string `json:"IDPLoginUrl,omitempty"`
26+
LoginUrl string `json:"loginUrl,omitempty"`
27+
RedirectUiUrl string `json:"redirectUiUrl,omitempty"`
28+
RedirectUrl string `json:"redirectUrl,omitempty"`
29+
RefreshTokenURL string `json:"refreshTokenURL,omitempty"`
30+
Scopes []string `json:"scopes,omitempty"`
31+
Tenant string `json:"tenant,omitempty"`
32+
TokenSecret string `json:"tokenSecret,omitempty"`
33+
TokenURL string `json:"tokenURL,omitempty"`
34+
UserProfileURL string `json:"userProfileURL,omitempty"`
35+
}
36+
37+
// get all idps
38+
func (client *Client) GetIDPs() (*[]IDP, error) {
39+
fullPath := "/admin/idp"
40+
opts := RequestOptions{
41+
Path: fullPath,
42+
Method: "GET",
43+
}
44+
45+
resp, err := client.RequestAPI(&opts)
46+
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
var idps []IDP
52+
53+
err = DecodeResponseInto(resp, &idps)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
return &idps, nil
59+
}
60+
61+
// get idp id by idp name
62+
func (client *Client) GetIdpByName(idpName string) (*IDP, error) {
63+
64+
idpList, err := client.GetIDPs()
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
for _, idp := range *idpList {
70+
if idp.ClientName == idpName {
71+
return &idp, nil
72+
}
73+
}
74+
75+
return nil, errors.New(fmt.Sprintf("[ERROR] IDP with name %s isn't found.", idpName ))
76+
}
77+
78+
func (client *Client) GetIdpByID(idpID string) (*IDP, error) {
79+
80+
idpList, err := client.GetIDPs()
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
for _, idp := range *idpList {
86+
if idp.ID == idpID{
87+
return &idp, nil
88+
}
89+
}
90+
91+
return nil, errors.New(fmt.Sprintf("[ERROR] IDP with ID %s isn't found.", idpID))
92+
}
93+
94+
95+
// get account idps
96+
func (client *Client) GetAccountIDPs() (*[]IDP, error) {
97+
fullPath := "/idp/account"
98+
opts := RequestOptions{
99+
Path: fullPath,
100+
Method: "GET",
101+
}
102+
103+
resp, err := client.RequestAPI(&opts)
104+
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
var idps []IDP
110+
111+
err = DecodeResponseInto(resp, &idps)
112+
if err != nil {
113+
return nil, err
114+
}
115+
116+
return &idps, nil
117+
}
118+
119+
// add account to idp
120+
func (client *Client) AddAccountToIDP(accountId, idpId string) error {
121+
122+
body := fmt.Sprintf(`{"accountId":"%s","IDPConfigId":"%s"}`, accountId, idpId)
123+
124+
opts := RequestOptions{
125+
Path: "/admin/idp/addAccount",
126+
Method: "POST",
127+
Body: []byte(body),
128+
}
129+
130+
_, err := client.RequestAPI(&opts)
131+
if err != nil {
132+
return err
133+
}
134+
135+
return nil
136+
}
137+
138+
// remove account form idp
139+
// doesn't implemente

client/team.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -242,27 +242,18 @@ func GetUsersDiff(desiredUsers []string, existingUsers []TeamUser) (usersToAdd [
242242
}
243243

244244
for _, id := range existingUsersIDs {
245-
ok := find(desiredUsers, id)
245+
ok := FindInSlice(desiredUsers, id)
246246
if !ok {
247247
usersToDelete = append(usersToDelete, id)
248248
}
249249
}
250250

251251
for _, id := range desiredUsers {
252-
ok := find(existingUsersIDs, id)
252+
ok := FindInSlice(existingUsersIDs, id)
253253
if !ok {
254254
usersToAdd = append(usersToAdd, id)
255255
}
256256
}
257257

258258
return usersToAdd, usersToDelete
259-
}
260-
261-
func find(slice []string, val string) bool {
262-
for _, item := range slice {
263-
if item == val {
264-
return true
265-
}
266-
}
267-
return false
268-
}
259+
}

client/user.go

+124-17
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,52 @@ package client
22

33
import "fmt"
44

5+
type Credentials struct {
6+
Permissions []string `json:"permissions,omitempty"`
7+
}
8+
9+
type Login struct {
10+
Credentials Credentials `json:"credentials,omitempty"`
11+
PersonalGit bool `json:"personalGit,omitempty"`
12+
Permissions []string `json:"permissions,omitempty"`
13+
IDP IDP `json:"idp,omitempty"`
14+
}
15+
16+
type ShortProfile struct {
17+
UserName string `json:"userName,omitempty"`
18+
}
19+
20+
type Personal struct {
21+
FirstName string `json:"firstName,omitempty"`
22+
LastName string `json:"lastName,omitempty"`
23+
CompanyName string `json:"companyName,omitempty"`
24+
PhoneNumber string `json:"phoneNumber,omitempty"`
25+
Country string `json:"country,omitempty"`
26+
}
27+
528
type User struct {
6-
ID string `json:"_id"`
29+
ID string `json:"_id,omitempty"`
730
UserName string `json:"userName"`
831
Email string `json:"email"`
9-
Roles []interface{} `json:"roles"`
10-
DefaultAccount int `json:"defaultAccount"`
11-
Account []Account `json:"account"`
12-
Status string `json:"status"`
13-
RegisterDate string `json:"register_date"`
14-
HasPassword bool `json:"hasPassword"`
15-
Notifications []NotificationEvent `json:"notifications"`
16-
ShortProfile struct {
17-
UserName string `json:"userName"`
18-
} `json:"shortProfile"`
19-
Settings struct {
20-
SendWeeklyReport bool `json:"sendWeeklyReport"`
21-
} `json:"settings"`
22-
Logins []interface{} `json:"logins"`
23-
InviteURL string `json:"inviteUrl"`
32+
Personal Personal `json:"personal,omitempty"`
33+
Roles []string `json:"roles,omitempty"`
34+
DefaultAccount int `json:"defaultAccount,omitempty"`
35+
Account []Account `json:"account,omitempty"`
36+
Status string `json:"status,omitempty"`
37+
RegisterDate string `json:"register_date,omitempty"`
38+
HasPassword bool `json:"hasPassword,omitempty"`
39+
Notifications []NotificationEvent `json:"notifications,omitempty"`
40+
ShortProfile ShortProfile `json:"shortProfile,omitempty"`
41+
Logins []Login `json:"logins,omitempty"`
42+
InviteURL string `json:"inviteUrl,omitempty"`
43+
}
44+
45+
type NewUser struct {
46+
UserName string `json:"userName"`
47+
Email string `json:"email"`
48+
Logins []Login `json:"logins,omitempty"`
49+
Roles []string `json:"roles,omitempty"`
50+
Account []string `json:"account,omitempty"`
2451
}
2552

2653
func (client *Client) AddNewUserToAccount(accountId, userName, userEmail string) (*User, error) {
@@ -50,6 +77,33 @@ func (client *Client) AddNewUserToAccount(accountId, userName, userEmail string)
5077
return &user, nil
5178
}
5279

80+
func (client *Client) AddPendingUser(user *NewUser) (*User, error) {
81+
82+
body, err := EncodeToJSON(user)
83+
if err != nil {
84+
return nil, err
85+
}
86+
opts := RequestOptions{
87+
Path: "/admin/accounts/addpendinguser",
88+
Method: "POST",
89+
Body: body,
90+
}
91+
92+
resp, err := client.RequestAPI(&opts)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
var respUser User
98+
99+
err = DecodeResponseInto(resp, &respUser)
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
return &respUser, nil
105+
}
106+
53107
func (client *Client) ActivateUser(userId string) (*User, error) {
54108

55109
opts := RequestOptions{
@@ -86,4 +140,57 @@ func (client *Client) SetUserAsAccountAdmin(accountId, userId string) error {
86140
}
87141

88142
return nil
89-
}
143+
}
144+
145+
func (client *Client) DeleteUserAsAccountAdmin(accountId, userId string) error {
146+
147+
opts := RequestOptions{
148+
Path: fmt.Sprintf("/accounts/%s/%s/admin", accountId, userId),
149+
Method: "DELETE",
150+
}
151+
152+
_, err := client.RequestAPI(&opts)
153+
if err != nil {
154+
return err
155+
}
156+
157+
return nil
158+
}
159+
160+
func (client *Client) ListUsers() (*[]User, error) {
161+
162+
opts := RequestOptions{
163+
Path: "/admin/user",
164+
Method: "GET",
165+
}
166+
167+
resp, err := client.RequestAPI(&opts)
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
var users []User
173+
174+
err = DecodeResponseInto(resp, &users)
175+
if err != nil {
176+
return nil, err
177+
}
178+
179+
return &users, nil
180+
}
181+
182+
func (client *Client) DeleteUser(userName string) error {
183+
184+
opts := RequestOptions{
185+
Path: fmt.Sprintf("/admi/user/%s", userName),
186+
Method: "DELETE",
187+
}
188+
189+
_, err := client.RequestAPI(&opts)
190+
if err != nil {
191+
return err
192+
}
193+
194+
return nil
195+
}
196+

client/utils.go

+9
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ type Variable struct {
1010
type CodefreshObject interface {
1111
GetID() string
1212
}
13+
14+
func FindInSlice(slice []string, val string) bool {
15+
for _, item := range slice {
16+
if item == val {
17+
return true
18+
}
19+
}
20+
return false
21+
}

codefresh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func Provider() terraform.ResourceProvider {
3232
"codefresh_team": resourceTeam(),
3333
"codefresh_account": resourceAccount(),
3434
"codefresh_api_key": resourceApiKey(),
35+
"codefresh_idp_accounts": resourceIDPAccounts(),
3536
},
3637
ConfigureFunc: configureProvider,
3738
}

codefresh/resource_account.go

-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ func resourceAccountUpdate(d *schema.ResourceData, meta interface{}) error {
112112
return err
113113
}
114114

115-
// TODO
116-
// - rename account
117-
// - add/remove admins
118-
119115
return nil
120116
}
121117

0 commit comments

Comments
 (0)