Skip to content

Commit 24990cb

Browse files
committed
review
1 parent f22d9f9 commit 24990cb

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

internal/provider/user_resource.go

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

0 commit comments

Comments
 (0)