@@ -252,11 +252,11 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
252
252
user , err := client .User (ctx , data .ID .ValueString ())
253
253
if err != nil {
254
254
if isNotFound (err ) {
255
- resp .Diagnostics .AddWarning ("Client Warning" , fmt .Sprintf ("User with ID %q not found. Marking as deleted." , data .ID .ValueString ()))
255
+ resp .Diagnostics .AddWarning ("Client Warning" , fmt .Sprintf ("User with ID %q not found. Marking resource as deleted." , data .ID .ValueString ()))
256
256
resp .State .RemoveResource (ctx )
257
257
return
258
258
}
259
- resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get current user, got error: %s" , err ))
259
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get current user by ID , got error: %s" , err ))
260
260
return
261
261
}
262
262
if len (user .OrganizationIDs ) < 1 {
@@ -275,19 +275,26 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
275
275
data .LoginType = types .StringValue (string (user .LoginType ))
276
276
data .Suspended = types .BoolValue (user .Status == codersdk .UserStatusSuspended )
277
277
278
- // Also query by username to check for deletion or username reassignment
278
+ // The user-by-ID API returns deleted users if the authorized user has
279
+ // permission. It does not indicate whether the user is deleted or not.
280
+ // The user-by-username API will never return deleted users.
281
+ // So, we do another lookup by username.
279
282
userByName , err := client .User (ctx , data .Username .ValueString ())
280
283
if err != nil {
281
284
if isNotFound (err ) {
282
- resp .Diagnostics .AddWarning ("Client Warning" , fmt .Sprintf ("User with ID %q not found. Marking as deleted." , data .ID .ValueString ()))
285
+ resp .Diagnostics .AddWarning ("Client Warning" , fmt .Sprintf (
286
+ "User with username %q not found. Marking resource as deleted." ,
287
+ data .Username .ValueString ()))
283
288
resp .State .RemoveResource (ctx )
284
289
return
285
290
}
286
- resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get current user, got error: %s" , err ))
291
+ resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Unable to get current user by username , got error: %s" , err ))
287
292
return
288
293
}
289
294
if userByName .ID != data .ID .ValueUUID () {
290
- resp .Diagnostics .AddWarning ("Client Error" , fmt .Sprintf ("The username %q has been reassigned to a new user. Marking as deleted." , user .Username ))
295
+ resp .Diagnostics .AddWarning ("Client Error" , fmt .Sprintf (
296
+ "The username %q has been reassigned to a new user not managed by this Terraform resource. Marking resource as deleted." ,
297
+ user .Username ))
291
298
resp .State .RemoveResource (ctx )
292
299
return
293
300
}
0 commit comments