Skip to content

Commit 1470b21

Browse files
support local password crud
1 parent 1877a9d commit 1470b21

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

codefresh/cfclient/user.go

+44
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ type ShortProfile struct {
2323
UserName string `json:"userName,omitempty"`
2424
}
2525

26+
type PublicProfile struct {
27+
HasPassword bool `json:"hasPassword,omitempty"`
28+
}
29+
2630
type Personal struct {
2731
FirstName string `json:"firstName,omitempty"`
2832
LastName string `json:"lastName,omitempty"`
@@ -44,6 +48,7 @@ type User struct {
4448
HasPassword bool `json:"hasPassword,omitempty"`
4549
Notifications []NotificationEvent `json:"notifications,omitempty"`
4650
ShortProfile ShortProfile `json:"shortProfile,omitempty"`
51+
PublicProfile PublicProfile `json:"publicProfile,omitempty"`
4752
Logins []Login `json:"logins,omitempty"`
4853
InviteURL string `json:"inviteUrl,omitempty"`
4954
}
@@ -368,3 +373,42 @@ func (client *Client) UpdateUserDetails(accountId, userId, userName, userEmail s
368373

369374
return &respUser, nil
370375
}
376+
377+
func (client *Client) UpdateLocalUserPassword(userName, password string) (error) {
378+
379+
fullPath := "/admin/user/localProvider"
380+
381+
requestBody := fmt.Sprintf(`{"userName": "%s","password": "%s"}`, userName, password)
382+
383+
opts := RequestOptions{
384+
Path: fullPath,
385+
Method: "POST",
386+
Body: []byte(requestBody),
387+
}
388+
389+
_, err := client.RequestAPI(&opts)
390+
391+
if err != nil {
392+
return err
393+
}
394+
395+
return nil
396+
}
397+
398+
func (client *Client) DeleteLocalUserPassword(userName string) (error) {
399+
400+
fullPath := fmt.Sprintf("/admin/user/localProvider?userName=%s", userName)
401+
402+
opts := RequestOptions{
403+
Path: fullPath,
404+
Method: "DELETE",
405+
}
406+
407+
_, err := client.RequestAPI(&opts)
408+
409+
if err != nil {
410+
return err
411+
}
412+
413+
return nil
414+
}

codefresh/resource_user.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codefresh
22

33
import (
4+
"errors"
45
"log"
56

67
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
@@ -24,6 +25,17 @@ func resourceUser() *schema.Resource {
2425
Type: schema.TypeString,
2526
Required: true,
2627
},
28+
"password": {
29+
Description: "Password - for users without SSO.",
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Sensitive: true,
33+
},
34+
"has_password" : {
35+
Description: "Whether the user has a local password.",
36+
Type: schema.TypeBool,
37+
Computed: true,
38+
},
2739
"email": {
2840
Description: "The email of the user.",
2941
Type: schema.TypeString,
@@ -148,7 +160,11 @@ func resourceUsersCreate(d *schema.ResourceData, meta interface{}) error {
148160
client.ActivateUser(d.Id())
149161
}
150162

151-
return nil
163+
if d.Get("password") != "" {
164+
client.UpdateLocalUserPassword(d.Get("user_name").(string), d.Get("password").(string))
165+
}
166+
167+
return resourceUsersRead(d, meta)
152168
}
153169

154170
func resourceUsersRead(d *schema.ResourceData, meta interface{}) error {
@@ -198,7 +214,15 @@ func resourceUsersUpdate(d *schema.ResourceData, meta interface{}) error {
198214
for _, account := range *accounts {
199215
_ = client.AddUserToTeamByAdmin(userId, account.ID, "users")
200216
}
201-
return nil
217+
218+
// Update local password
219+
err = updateUserLocalPassword(d, client)
220+
221+
if err != nil {
222+
return err
223+
}
224+
225+
return resourceUsersRead(d, meta)
202226
}
203227

204228
func resourceUsersDelete(d *schema.ResourceData, meta interface{}) error {
@@ -231,6 +255,7 @@ func mapUserToResource(user cfclient.User, d *schema.ResourceData) error {
231255
[]map[string]interface{}{
232256
{"user_name": user.ShortProfile.UserName},
233257
})
258+
d.Set("has_password", user.PublicProfile.HasPassword)
234259
d.Set("roles", user.Roles)
235260
d.Set("login", flattenUserLogins(&user.Logins))
236261

@@ -325,3 +350,33 @@ func mapResourceToNewUser(d *schema.ResourceData) *cfclient.NewUser {
325350

326351
return user
327352
}
353+
354+
func updateUserLocalPassword(d *schema.ResourceData, client *cfclient.Client) error {
355+
356+
if (d.HasChange("password")) {
357+
hasPassword := d.Get("has_password").(bool)
358+
359+
if _, ok := d.GetOk("user_name"); !ok {
360+
return errors.New("cannot update password as username attribute is not set")
361+
}
362+
363+
userName := d.Get("user_name").(string)
364+
365+
if password := d.Get("password"); password != "" {
366+
err := client.UpdateLocalUserPassword(userName, password.(string))
367+
368+
if err != nil {
369+
return err
370+
}
371+
// If password is not set but has_password returns true, it means that it was removed
372+
} else if hasPassword {
373+
err := client.DeleteLocalUserPassword(userName)
374+
375+
if err != nil {
376+
return err
377+
}
378+
}
379+
}
380+
381+
return nil
382+
}

0 commit comments

Comments
 (0)