Skip to content

Commit af5e3b9

Browse files
authored
Merge pull request #17 from codefresh-io/CR-702-users-team
new users to users team
2 parents a61a609 + deda752 commit af5e3b9

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

client/client.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
// Client token, host, htpp.Client
1313
type Client struct {
1414
Token string
15+
TokenHeader string
1516
Host string
1617
Client *http.Client
1718
}
@@ -28,10 +29,14 @@ type RequestOptions struct {
2829
// NewClient returns a new client configured to communicate on a server with the
2930
// given hostname and to send an Authorization Header with the value of
3031
// token
31-
func NewClient(hostname string, token string) *Client {
32+
func NewClient(hostname string, token string, tokenHeader string) *Client {
33+
if tokenHeader == "" {
34+
tokenHeader = "Authorization"
35+
}
3236
return &Client{
3337
Host: hostname,
3438
Token: token,
39+
TokenHeader: tokenHeader,
3540
Client: &http.Client{},
3641
}
3742

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

51-
request.Header.Set("Authorization", client.Token)
56+
tokenHeader := client.TokenHeader
57+
if tokenHeader == "" {
58+
tokenHeader = "Authorization"
59+
}
60+
request.Header.Set(tokenHeader, client.Token)
5261
request.Header.Set("Content-Type", "application/json; charset=utf-8")
5362

5463
resp, err := client.Client.Do(request)

client/user.go

+39
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,45 @@ func (client *Client) AddPendingUser(user *NewUser) (*User, error) {
117117
return &respUser, nil
118118
}
119119

120+
// AddUserToTeamByAdmin - adds user to team with swich account
121+
func (client *Client) AddUserToTeamByAdmin(userID string, accountID string, team string) error {
122+
// get first accountAdmin and its token
123+
account, err := client.GetAccountByID(accountID)
124+
if err != nil {
125+
return err
126+
}
127+
if len(account.Admins) == 0 {
128+
return fmt.Errorf("Error adding userID %s to Users team of account %s - account does not have any admin", userID, account.Name)
129+
}
130+
131+
accountAdminUserID := account.Admins[0]
132+
accountAdminToken, err := client.GetXAccessToken(accountAdminUserID, accountID)
133+
if err != nil {
134+
return err
135+
}
136+
// new Client for accountAdmin
137+
accountAdminClient := NewClient(client.Host, accountAdminToken, "x-access-token")
138+
usersTeam, err := accountAdminClient.GetTeamByName(team)
139+
if err != nil {
140+
return err
141+
}
142+
if usersTeam == nil {
143+
fmt.Printf("cannot find users team for account %s", account.Name)
144+
return nil
145+
}
146+
147+
// return is user already assigned to the team
148+
for _, teamUser := range usersTeam.Users {
149+
if teamUser.ID == userID {
150+
return nil
151+
}
152+
}
153+
154+
err = accountAdminClient.AddUserToTeam(usersTeam.ID, userID)
155+
156+
return err
157+
}
158+
120159
func (client *Client) ActivateUser(userId string) (*User, error) {
121160

122161
opts := RequestOptions{

codefresh/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
5555
if token == "" {
5656
token = os.Getenv("CODEFRESH_API_KEY")
5757
}
58-
return cfClient.NewClient(apiURL, token), nil
58+
return cfClient.NewClient(apiURL, token, ""), nil
5959
}

codefresh/resource_user.go

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ func resourceUsersCreate(d *schema.ResourceData, meta interface{}) error {
136136

137137
d.SetId(resp.ID)
138138

139+
// Adding user to users teams
140+
for _, accountID := range user.Account {
141+
_ = client.AddUserToTeamByAdmin(resp.ID, accountID, "users")
142+
}
143+
139144
if d.Get("activate").(bool) {
140145
client.ActivateUser(d.Id())
141146
}
@@ -186,6 +191,10 @@ func resourceUsersUpdate(d *schema.ResourceData, meta interface{}) error {
186191
return err
187192
}
188193

194+
// Adding user to users teams
195+
for _, account := range *accounts {
196+
_ = client.AddUserToTeamByAdmin(userId, account.ID, "users")
197+
}
189198
return nil
190199
}
191200

0 commit comments

Comments
 (0)