diff --git a/client/client.go b/client/client.go index 5bb78b0..125f846 100644 --- a/client/client.go +++ b/client/client.go @@ -12,6 +12,7 @@ import ( // Client token, host, htpp.Client type Client struct { Token string + TokenHeader string Host string Client *http.Client } @@ -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{}, } @@ -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) diff --git a/client/user.go b/client/user.go index a0c338d..c8f2d31 100644 --- a/client/user.go +++ b/client/user.go @@ -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{ diff --git a/codefresh/provider.go b/codefresh/provider.go index f8fc80c..beab329 100644 --- a/codefresh/provider.go +++ b/codefresh/provider.go @@ -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 } diff --git a/codefresh/resource_user.go b/codefresh/resource_user.go index 2ff3107..cf4c7d8 100644 --- a/codefresh/resource_user.go +++ b/codefresh/resource_user.go @@ -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()) } @@ -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 }