@@ -6,17 +6,18 @@ package provider
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "strings"
9
10
10
11
"github.com/google/uuid"
11
- "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator "
12
+ "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator "
12
13
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
13
14
"github.com/hashicorp/terraform-plugin-framework/attr"
14
15
"github.com/hashicorp/terraform-plugin-framework/path"
15
16
"github.com/hashicorp/terraform-plugin-framework/resource"
16
17
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
17
18
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
18
- "github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault"
19
19
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
20
+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
20
21
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
21
22
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
22
23
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -46,7 +47,7 @@ type UserResourceModel struct {
46
47
Username types.String `tfsdk:"username"`
47
48
Name types.String `tfsdk:"name"`
48
49
Email types.String `tfsdk:"email"`
49
- Roles types.List `tfsdk:"roles"` // owner, template-admin, user-admin, auditor (member is implicit)
50
+ Roles types.Set `tfsdk:"roles"` // owner, template-admin, user-admin, auditor (member is implicit)
50
51
LoginType types.String `tfsdk:"login_type"` // none, password, github, oidc
51
52
Password types.String `tfsdk:"password"` // only when login_type is password
52
53
Suspended types.Bool `tfsdk:"suspended"`
@@ -83,19 +84,18 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
83
84
MarkdownDescription : "Email address of the user." ,
84
85
Required : true ,
85
86
},
86
- "roles" : schema.ListAttribute {
87
+ "roles" : schema.SetAttribute {
87
88
MarkdownDescription : "Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'." ,
88
89
Required : false ,
89
90
Optional : true ,
90
91
Computed : true ,
91
92
ElementType : types .StringType ,
92
- Validators : []validator.List {
93
- listvalidator .UniqueValues (),
94
- listvalidator .ValueStringsAre (
93
+ Validators : []validator.Set {
94
+ setvalidator .ValueStringsAre (
95
95
stringvalidator .OneOf ("owner" , "template-admin" , "user-admin" , "auditor" ),
96
96
),
97
97
},
98
- Default : listdefault .StaticValue (types .ListValueMust (types .StringType , []attr.Value {})),
98
+ Default : setdefault .StaticValue (types .SetValueMust (types .StringType , []attr.Value {})),
99
99
},
100
100
"login_type" : schema.StringAttribute {
101
101
MarkdownDescription : "Type of login for the user. Valid types are 'none', 'password', 'github', and 'oidc'." ,
@@ -215,6 +215,13 @@ func (r *UserResource) Create(ctx context.Context, req resource.CreateRequest, r
215
215
}
216
216
tflog .Trace (ctx , "successfully updated user roles" )
217
217
218
+ if data .Suspended .ValueBool () {
219
+ _ , err = r .client .UpdateUserStatus (ctx , data .ID .ValueString (), codersdk .UserStatus ("suspended" ))
220
+ }
221
+ if err != nil {
222
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to update user status, got error: %s" , err ))
223
+ return
224
+ }
218
225
// Save data into Terraform state
219
226
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
220
227
}
@@ -241,11 +248,12 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
241
248
242
249
data .Email = types .StringValue (user .Email )
243
250
data .Name = types .StringValue (user .Name )
251
+ data .Username = types .StringValue (user .Username )
244
252
roles := make ([]attr.Value , 0 , len (user .Roles ))
245
253
for _ , role := range user .Roles {
246
254
roles = append (roles , types .StringValue (role .Name ))
247
255
}
248
- data .Roles = types .ListValueMust (types .StringType , roles )
256
+ data .Roles = types .SetValueMust (types .StringType , roles )
249
257
data .LoginType = types .StringValue (string (user .LoginType ))
250
258
data .Suspended = types .BoolValue (user .Status == codersdk .UserStatusSuspended )
251
259
@@ -307,12 +315,24 @@ func (r *UserResource) Update(ctx context.Context, req resource.UpdateRequest, r
307
315
err = r .client .UpdateUserPassword (ctx , user .ID .String (), codersdk.UpdateUserPasswordRequest {
308
316
Password : data .Password .ValueString (),
309
317
})
310
- if err != nil {
318
+ if err != nil && ! strings . Contains ( err . Error (), "New password cannot match old password." ) {
311
319
resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to update password, got error: %s" , err ))
312
320
return
313
321
}
314
322
tflog .Trace (ctx , "successfully updated password" )
315
323
324
+ var statusErr error
325
+ if data .Suspended .ValueBool () {
326
+ _ , statusErr = r .client .UpdateUserStatus (ctx , data .ID .ValueString (), codersdk .UserStatus ("suspended" ))
327
+ }
328
+ if ! data .Suspended .ValueBool () && user .Status == codersdk .UserStatusSuspended {
329
+ _ , statusErr = r .client .UpdateUserStatus (ctx , data .ID .ValueString (), codersdk .UserStatus ("active" ))
330
+ }
331
+ if statusErr != nil {
332
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to update user status, got error: %s" , err ))
333
+ return
334
+ }
335
+
316
336
// Save updated data into Terraform state
317
337
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
318
338
}
0 commit comments