mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
terraform/provider: do not adopt an IAM user found after a failed CreateUser attempt
A retry that hits EntityAlreadyExists cannot tell whether the failed attempt or another client created the user, so ask for an import. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
05ebcbbe03
commit
d44b8be5d7
2 files changed
+23
-32
No files matched your search
@@ -105,17 +105,19 @@ func (r *iamUserResource) Create(ctx context.Context, req resource.CreateRequest
|
||||
}
|
||||
|
||||
var user getUserResponse
|
||||
attempts := 0
|
||||
var firstErr error
|
||||
err := r.data.withUserLock(plan.Name.ValueString(), func() error {
|
||||
return retryIAMEventuallyConsistent(ctx, 8, func() error {
|
||||
attempts++
|
||||
var innerErr error
|
||||
user, innerErr = r.client.CreateUser(ctx, plan.Name.ValueString(), plan.Path.ValueString())
|
||||
if firstErr == nil {
|
||||
firstErr = innerErr
|
||||
}
|
||||
return innerErr
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
if isEntityAlreadyExistsError(err) && attempts == 1 {
|
||||
if isEntityAlreadyExistsError(err) && isEntityAlreadyExistsError(firstErr) {
|
||||
resp.Diagnostics.AddError(
|
||||
"IAM user already exists",
|
||||
fmt.Sprintf("IAM user %q already exists. Import it with `terraform import` to manage it.", plan.Name.ValueString()),
|
||||
@@ -123,25 +125,17 @@ func (r *iamUserResource) Create(ctx context.Context, req resource.CreateRequest
|
||||
return
|
||||
}
|
||||
if isEntityAlreadyExistsError(err) {
|
||||
readErr := retryIAMEventuallyConsistent(ctx, 6, func() error {
|
||||
var innerErr error
|
||||
user, innerErr = r.client.GetUser(ctx, plan.Name.ValueString())
|
||||
return innerErr
|
||||
})
|
||||
if readErr != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Failed to read existing IAM user",
|
||||
readErr.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
resp.Diagnostics.AddError(
|
||||
"Failed to create IAM user",
|
||||
err.Error(),
|
||||
"IAM user creation outcome unknown",
|
||||
fmt.Sprintf("CreateUser first failed with %v, and a retry found IAM user %q already present. The failed attempt may have created it, or another client did. Check the user and import it with `terraform import` to manage it.", firstErr, plan.Name.ValueString()),
|
||||
)
|
||||
return
|
||||
}
|
||||
resp.Diagnostics.AddError(
|
||||
"Failed to create IAM user",
|
||||
err.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
userPath := user.User.Path
|
||||
|
||||
@@ -12,13 +12,12 @@ import (
|
||||
func TestIAMUserCreateRejectsExistingUser(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
failFirst bool
|
||||
wantError bool
|
||||
wantStateName string
|
||||
name string
|
||||
failFirst bool
|
||||
wantSummary string
|
||||
}{
|
||||
{name: "collision on first attempt", wantError: true},
|
||||
{name: "retried create that already committed", failFirst: true, wantStateName: "alice"},
|
||||
{name: "collision on first attempt", wantSummary: "IAM user already exists"},
|
||||
{name: "retried create that may have committed", failFirst: true, wantSummary: "IAM user creation outcome unknown"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
creates := 0
|
||||
@@ -34,8 +33,6 @@ func TestIAMUserCreateRejectsExistingUser(t *testing.T) {
|
||||
}
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_, _ = w.Write([]byte(`<ErrorResponse><Error><Code>EntityAlreadyExists</Code><Message>exists</Message></Error></ErrorResponse>`))
|
||||
case "GetUser":
|
||||
_, _ = w.Write([]byte(`<GetUserResponse><GetUserResult><User><Path>/</Path><UserName>alice</UserName><UserId>uid-1</UserId><Arn>arn:aws:iam:::user/alice</Arn></User></GetUserResult></GetUserResponse>`))
|
||||
default:
|
||||
t.Errorf("unexpected action %s", r.Form.Get("Action"))
|
||||
}
|
||||
@@ -47,15 +44,15 @@ func TestIAMUserCreateRejectsExistingUser(t *testing.T) {
|
||||
})
|
||||
resp := resource.CreateResponse{State: state}
|
||||
r.Create(ctx, resource.CreateRequest{Plan: plan}, &resp)
|
||||
if resp.Diagnostics.HasError() != tc.wantError {
|
||||
t.Fatalf("diagnostics = %v, want error %v", resp.Diagnostics, tc.wantError)
|
||||
if !resp.Diagnostics.HasError() || resp.Diagnostics.Errors()[0].Summary() != tc.wantSummary {
|
||||
t.Fatalf("diagnostics = %v, want error %q", resp.Diagnostics, tc.wantSummary)
|
||||
}
|
||||
var got iamUserResourceModel
|
||||
if !resp.State.Raw.IsNull() {
|
||||
var got iamUserResourceModel
|
||||
resp.Diagnostics.Append(resp.State.Get(ctx, &got)...)
|
||||
}
|
||||
if got.Name.ValueString() != tc.wantStateName {
|
||||
t.Fatalf("state name = %q, want %q", got.Name.ValueString(), tc.wantStateName)
|
||||
if got.Name.ValueString() != "" {
|
||||
t.Fatalf("state adopted user %q", got.Name.ValueString())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user