1
1
package codefresh
2
2
3
3
import (
4
+ "errors"
4
5
"log"
5
6
6
7
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
@@ -24,6 +25,17 @@ func resourceUser() *schema.Resource {
24
25
Type : schema .TypeString ,
25
26
Required : true ,
26
27
},
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
+ },
27
39
"email" : {
28
40
Description : "The email of the user." ,
29
41
Type : schema .TypeString ,
@@ -148,7 +160,11 @@ func resourceUsersCreate(d *schema.ResourceData, meta interface{}) error {
148
160
client .ActivateUser (d .Id ())
149
161
}
150
162
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 )
152
168
}
153
169
154
170
func resourceUsersRead (d * schema.ResourceData , meta interface {}) error {
@@ -198,7 +214,15 @@ func resourceUsersUpdate(d *schema.ResourceData, meta interface{}) error {
198
214
for _ , account := range * accounts {
199
215
_ = client .AddUserToTeamByAdmin (userId , account .ID , "users" )
200
216
}
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 )
202
226
}
203
227
204
228
func resourceUsersDelete (d * schema.ResourceData , meta interface {}) error {
@@ -231,6 +255,7 @@ func mapUserToResource(user cfclient.User, d *schema.ResourceData) error {
231
255
[]map [string ]interface {}{
232
256
{"user_name" : user .ShortProfile .UserName },
233
257
})
258
+ d .Set ("has_password" , user .PublicProfile .HasPassword )
234
259
d .Set ("roles" , user .Roles )
235
260
d .Set ("login" , flattenUserLogins (& user .Logins ))
236
261
@@ -325,3 +350,33 @@ func mapResourceToNewUser(d *schema.ResourceData) *cfclient.NewUser {
325
350
326
351
return user
327
352
}
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