@@ -252,11 +252,11 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
252252user ,err := client .User (ctx ,data .ID .ValueString ())
253253if err != nil {
254254if 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. Markingresource as deleted." ,data .ID .ValueString ()))
256256resp .State .RemoveResource (ctx )
257257return
258258}
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 ))
260260return
261261}
262262if len (user .OrganizationIDs )< 1 {
@@ -275,19 +275,26 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
275275data .LoginType = types .StringValue (string (user .LoginType ))
276276data .Suspended = types .BoolValue (user .Status == codersdk .UserStatusSuspended )
277277
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.
279282userByName ,err := client .User (ctx ,data .Username .ValueString ())
280283if err != nil {
281284if 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 ()))
283288resp .State .RemoveResource (ctx )
284289return
285290}
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 ))
287292return
288293}
289294if 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 ))
291298resp .State .RemoveResource (ctx )
292299return
293300}