11package codefresh
22
33import (
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
154170func 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
204228func 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