Skip to content

Commit

Permalink
Added IDP resource
Browse files Browse the repository at this point in the history
  • Loading branch information
palson-cf committed Jul 14, 2020
1 parent 4c5fc24 commit 5bd5cae
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 36 deletions.
4 changes: 2 additions & 2 deletions client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ func (client *Client) UpdateAccount(account *Account) (*Account, error) {
}

existingAccount, err := client.GetAccountByID(id)
if err != nil {
if err != nil {
return nil, err
}

err = mergo.Merge(account, existingAccount)
if err != nil {
if err != nil {
return nil, err
}

Expand Down
139 changes: 139 additions & 0 deletions client/idp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package client

import (
"errors"
"fmt"
)

type IDP struct {
Access_token string `json:"access_token,omitempty"`
Accounts []string `json:"accounts,omitempty"`
ApiHost string `json:"apiHost,omitempty"`
ApiPathPrefix string `json:"apiPathPrefix,omitempty"`
ApiURL string `json:"apiURL,omitempty"`
AppId string `json:"appId,omitempty"`
AuthURL string `json:"authURL,omitempty"`
ClientHost string `json:"clientHost,omitempty"`
ClientId string `json:"clientId,omitempty"`
ClientName string `json:"clientName,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
ClientType string `json:"clientType,omitempty"`
CookieIv string `json:"cookieIv,omitempty"`
CookieKey string `json:"cookieKey,omitempty"`
DisplayName string `json:"displayName,omitempty"`
ID string `json:"id,omitempty"`
IDPLoginUrl string `json:"IDPLoginUrl,omitempty"`
LoginUrl string `json:"loginUrl,omitempty"`
RedirectUiUrl string `json:"redirectUiUrl,omitempty"`
RedirectUrl string `json:"redirectUrl,omitempty"`
RefreshTokenURL string `json:"refreshTokenURL,omitempty"`
Scopes []string `json:"scopes,omitempty"`
Tenant string `json:"tenant,omitempty"`
TokenSecret string `json:"tokenSecret,omitempty"`
TokenURL string `json:"tokenURL,omitempty"`
UserProfileURL string `json:"userProfileURL,omitempty"`
}

// get all idps
func (client *Client) GetIDPs() (*[]IDP, error) {
fullPath := "/admin/idp"
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var idps []IDP

err = DecodeResponseInto(resp, &idps)
if err != nil {
return nil, err
}

return &idps, nil
}

// get idp id by idp name
func (client *Client) GetIdpByName(idpName string) (*IDP, error) {

idpList, err := client.GetIDPs()
if err != nil {
return nil, err
}

for _, idp := range *idpList {
if idp.ClientName == idpName {
return &idp, nil
}
}

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

func (client *Client) GetIdpByID(idpID string) (*IDP, error) {

idpList, err := client.GetIDPs()
if err != nil {
return nil, err
}

for _, idp := range *idpList {
if idp.ID == idpID{
return &idp, nil
}
}

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


// get account idps
func (client *Client) GetAccountIDPs() (*[]IDP, error) {
fullPath := "/idp/account"
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return nil, err
}

var idps []IDP

err = DecodeResponseInto(resp, &idps)
if err != nil {
return nil, err
}

return &idps, nil
}

// add account to idp
func (client *Client) AddAccountToIDP(accountId, idpId string) error {

body := fmt.Sprintf(`{"accountId":"%s","IDPConfigId":"%s"}`, accountId, idpId)

opts := RequestOptions{
Path: "/admin/idp/addAccount",
Method: "POST",
Body: []byte(body),
}

_, err := client.RequestAPI(&opts)
if err != nil {
return err
}

return nil
}

// remove account form idp
// doesn't implemente
15 changes: 3 additions & 12 deletions client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,27 +242,18 @@ func GetUsersDiff(desiredUsers []string, existingUsers []TeamUser) (usersToAdd [
}

for _, id := range existingUsersIDs {
ok := find(desiredUsers, id)
ok := FindInSlice(desiredUsers, id)
if !ok {
usersToDelete = append(usersToDelete, id)
}
}

for _, id := range desiredUsers {
ok := find(existingUsersIDs, id)
ok := FindInSlice(existingUsersIDs, id)
if !ok {
usersToAdd = append(usersToAdd, id)
}
}

return usersToAdd, usersToDelete
}

func find(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
}
141 changes: 124 additions & 17 deletions client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@ package client

import "fmt"

type Credentials struct {
Permissions []string `json:"permissions,omitempty"`
}

type Login struct {
Credentials Credentials `json:"credentials,omitempty"`
PersonalGit bool `json:"personalGit,omitempty"`
Permissions []string `json:"permissions,omitempty"`
IDP IDP `json:"idp,omitempty"`
}

type ShortProfile struct {
UserName string `json:"userName,omitempty"`
}

type Personal struct {
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
CompanyName string `json:"companyName,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
Country string `json:"country,omitempty"`
}

type User struct {
ID string `json:"_id"`
ID string `json:"_id,omitempty"`
UserName string `json:"userName"`
Email string `json:"email"`
Roles []interface{} `json:"roles"`
DefaultAccount int `json:"defaultAccount"`
Account []Account `json:"account"`
Status string `json:"status"`
RegisterDate string `json:"register_date"`
HasPassword bool `json:"hasPassword"`
Notifications []NotificationEvent `json:"notifications"`
ShortProfile struct {
UserName string `json:"userName"`
} `json:"shortProfile"`
Settings struct {
SendWeeklyReport bool `json:"sendWeeklyReport"`
} `json:"settings"`
Logins []interface{} `json:"logins"`
InviteURL string `json:"inviteUrl"`
Personal Personal `json:"personal,omitempty"`
Roles []string `json:"roles,omitempty"`
DefaultAccount int `json:"defaultAccount,omitempty"`
Account []Account `json:"account,omitempty"`
Status string `json:"status,omitempty"`
RegisterDate string `json:"register_date,omitempty"`
HasPassword bool `json:"hasPassword,omitempty"`
Notifications []NotificationEvent `json:"notifications,omitempty"`
ShortProfile ShortProfile `json:"shortProfile,omitempty"`
Logins []Login `json:"logins,omitempty"`
InviteURL string `json:"inviteUrl,omitempty"`
}

type NewUser struct {
UserName string `json:"userName"`
Email string `json:"email"`
Logins []Login `json:"logins,omitempty"`
Roles []string `json:"roles,omitempty"`
Account []string `json:"account,omitempty"`
}

func (client *Client) AddNewUserToAccount(accountId, userName, userEmail string) (*User, error) {
Expand Down Expand Up @@ -50,6 +77,33 @@ func (client *Client) AddNewUserToAccount(accountId, userName, userEmail string)
return &user, nil
}

func (client *Client) AddPendingUser(user *NewUser) (*User, error) {

body, err := EncodeToJSON(user)
if err != nil {
return nil, err
}
opts := RequestOptions{
Path: "/admin/accounts/addpendinguser",
Method: "POST",
Body: body,
}

resp, err := client.RequestAPI(&opts)
if err != nil {
return nil, err
}

var respUser User

err = DecodeResponseInto(resp, &respUser)
if err != nil {
return nil, err
}

return &respUser, nil
}

func (client *Client) ActivateUser(userId string) (*User, error) {

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

return nil
}
}

func (client *Client) DeleteUserAsAccountAdmin(accountId, userId string) error {

opts := RequestOptions{
Path: fmt.Sprintf("/accounts/%s/%s/admin", accountId, userId),
Method: "DELETE",
}

_, err := client.RequestAPI(&opts)
if err != nil {
return err
}

return nil
}

func (client *Client) ListUsers() (*[]User, error) {

opts := RequestOptions{
Path: "/admin/user",
Method: "GET",
}

resp, err := client.RequestAPI(&opts)
if err != nil {
return nil, err
}

var users []User

err = DecodeResponseInto(resp, &users)
if err != nil {
return nil, err
}

return &users, nil
}

func (client *Client) DeleteUser(userName string) error {

opts := RequestOptions{
Path: fmt.Sprintf("/admi/user/%s", userName),
Method: "DELETE",
}

_, err := client.RequestAPI(&opts)
if err != nil {
return err
}

return nil
}

9 changes: 9 additions & 0 deletions client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ type Variable struct {
type CodefreshObject interface {
GetID() string
}

func FindInSlice(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Provider() terraform.ResourceProvider {
"codefresh_team": resourceTeam(),
"codefresh_account": resourceAccount(),
"codefresh_api_key": resourceApiKey(),
"codefresh_idp_accounts": resourceIDPAccounts(),
},
ConfigureFunc: configureProvider,
}
Expand Down
4 changes: 0 additions & 4 deletions codefresh/resource_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ func resourceAccountUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}

// TODO
// - rename account
// - add/remove admins

return nil
}

Expand Down

0 comments on commit 5bd5cae

Please sign in to comment.