Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: handle user deletion config drift#209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanndickson merged 2 commits intomainfromethan/handle-user-deletion-drift
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletionsinternal/provider/user_resource.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -248,14 +248,15 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp

client := r.data.Client

// Lookup by ID to handle imports
user, err := client.User(ctx, data.ID.ValueString())
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("User with ID %q not found. Marking as deleted.", data.ID.ValueString()))
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("User with ID %q not found. Markingresourceas deleted.", data.ID.ValueString()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user, got error: %s", err))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user by ID, got error: %s", err))
return
}
if len(user.OrganizationIDs) < 1 {
Expand All@@ -274,6 +275,30 @@ func (r *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
data.LoginType = types.StringValue(string(user.LoginType))
data.Suspended = types.BoolValue(user.Status == codersdk.UserStatusSuspended)

// The user-by-ID API returns deleted users if the authorized user has
// permission. It does not indicate whether the user is deleted or not.
// The user-by-username API will never return deleted users.
// So, we do another lookup by username.
userByName, err := client.User(ctx, data.Username.ValueString())
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf(
"User with username %q not found. Marking resource as deleted.",
data.Username.ValueString()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get current user by username, got error: %s", err))
return
}
if userByName.ID != data.ID.ValueUUID() {
resp.Diagnostics.AddWarning("Client Error", fmt.Sprintf(
"The username %q has been reassigned to a new user not managed by this Terraform resource. Marking resource as deleted.",
user.Username))
resp.State.RemoveResource(ctx)
return
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Expand Down
14 changes: 14 additions & 0 deletionsinternal/provider/user_resource_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@ import (
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/terraform-provider-coderd/integration"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stretchr/testify/require"
)

Expand DownExpand Up@@ -100,6 +101,19 @@ func TestAccUserResource(t *testing.T) {
resource.TestCheckResourceAttr("coderd_user.test", "login_type", "github"),
),
},
// Verify config drift via deletion is handled
{
Config: cfg4.String(t),
Check: func(*terraform.State) error {
user, err := client.User(ctx, "exampleNew")
if err != nil {
return err
}
return client.DeleteUser(ctx, user.ID)
},
// The Plan should be to create the entire resource
ExpectNonEmptyPlan: true,
},
},
})
}
Expand Down
13 changes: 12 additions & 1 deletioninternal/provider/util.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/coder/coder/v2/codersdk"
"github.com/google/uuid"
Expand DownExpand Up@@ -110,5 +111,15 @@ func memberDiff(currentMembers []uuid.UUID, plannedMembers []UUID) (add, remove

func isNotFound(err error) bool {
var sdkErr *codersdk.Error
return errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound
if !errors.As(err, &sdkErr) {
return false
}
if sdkErr.StatusCode() == http.StatusNotFound {
return true
}
// `httpmw/ExtractUserContext` returns a 400 w/ this message if the user is not found
if sdkErr.StatusCode() == http.StatusBadRequest && strings.Contains(sdkErr.Message, "must be an existing uuid or username") {
return true
}
return false
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp