Skip to content

Commit

Permalink
Merge pull request #17 from codefresh-io/CR-702-users-team
Browse files Browse the repository at this point in the history
new users to users team
  • Loading branch information
kosta709 authored Oct 20, 2020
2 parents a61a609 + deda752 commit af5e3b9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
13 changes: 11 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Client token, host, htpp.Client
type Client struct {
Token string
TokenHeader string
Host string
Client *http.Client
}
Expand All @@ -28,10 +29,14 @@ type RequestOptions struct {
// NewClient returns a new client configured to communicate on a server with the
// given hostname and to send an Authorization Header with the value of
// token
func NewClient(hostname string, token string) *Client {
func NewClient(hostname string, token string, tokenHeader string) *Client {
if tokenHeader == "" {
tokenHeader = "Authorization"
}
return &Client{
Host: hostname,
Token: token,
TokenHeader: tokenHeader,
Client: &http.Client{},
}

Expand All @@ -48,7 +53,11 @@ func (client *Client) RequestAPI(opt *RequestOptions) ([]byte, error) {
return nil, err
}

request.Header.Set("Authorization", client.Token)
tokenHeader := client.TokenHeader
if tokenHeader == "" {
tokenHeader = "Authorization"
}
request.Header.Set(tokenHeader, client.Token)
request.Header.Set("Content-Type", "application/json; charset=utf-8")

resp, err := client.Client.Do(request)
Expand Down
39 changes: 39 additions & 0 deletions client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,45 @@ func (client *Client) AddPendingUser(user *NewUser) (*User, error) {
return &respUser, nil
}

// AddUserToTeamByAdmin - adds user to team with swich account
func (client *Client) AddUserToTeamByAdmin(userID string, accountID string, team string) error {
// get first accountAdmin and its token
account, err := client.GetAccountByID(accountID)
if err != nil {
return err
}
if len(account.Admins) == 0 {
return fmt.Errorf("Error adding userID %s to Users team of account %s - account does not have any admin", userID, account.Name)
}

accountAdminUserID := account.Admins[0]
accountAdminToken, err := client.GetXAccessToken(accountAdminUserID, accountID)
if err != nil {
return err
}
// new Client for accountAdmin
accountAdminClient := NewClient(client.Host, accountAdminToken, "x-access-token")
usersTeam, err := accountAdminClient.GetTeamByName(team)
if err != nil {
return err
}
if usersTeam == nil {
fmt.Printf("cannot find users team for account %s", account.Name)
return nil
}

// return is user already assigned to the team
for _, teamUser := range usersTeam.Users {
if teamUser.ID == userID {
return nil
}
}

err = accountAdminClient.AddUserToTeam(usersTeam.ID, userID)

return err
}

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

opts := RequestOptions{
Expand Down
2 changes: 1 addition & 1 deletion codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
if token == "" {
token = os.Getenv("CODEFRESH_API_KEY")
}
return cfClient.NewClient(apiURL, token), nil
return cfClient.NewClient(apiURL, token, ""), nil
}
9 changes: 9 additions & 0 deletions codefresh/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func resourceUsersCreate(d *schema.ResourceData, meta interface{}) error {

d.SetId(resp.ID)

// Adding user to users teams
for _, accountID := range user.Account {
_ = client.AddUserToTeamByAdmin(resp.ID, accountID, "users")
}

if d.Get("activate").(bool) {
client.ActivateUser(d.Id())
}
Expand Down Expand Up @@ -186,6 +191,10 @@ func resourceUsersUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}

// Adding user to users teams
for _, account := range *accounts {
_ = client.AddUserToTeamByAdmin(userId, account.ID, "users")
}
return nil
}

Expand Down

0 comments on commit af5e3b9

Please sign in to comment.