terraform/provider: stop retrying CreateAccessKey when a failed attempt left a key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-05 14:22:22 -08:00
1 parent d65ca4462c
commit 58f480b252
2 files changed
+67 -1

No files matched your search

@@ -86,9 +86,25 @@ func (r *iamAccessKeyResource) Create(ctx context.Context, req resource.CreateRe
}
var key iamAccessKey
var known map[string]bool
err := r.data.withUserLock(plan.UserName.ValueString(), func() error {
return retryIAMEventuallyConsistent(ctx, 20, func() error {
var innerErr error
keys, innerErr := r.client.ListAccessKeys(ctx, plan.UserName.ValueString())
if innerErr != nil {
return innerErr
}
if known == nil {
known = map[string]bool{}
for _, existing := range keys {
known[existing.AccessKeyID] = true
}
} else {
for _, existing := range keys {
if !known[existing.AccessKeyID] {
return fmt.Errorf("a failed CreateAccessKey attempt left access key %s on user %s; delete or import it before retrying", existing.AccessKeyID, plan.UserName.ValueString())
}
}
}
key, innerErr = r.client.CreateAccessKey(ctx, plan.UserName.ValueString())
return innerErr
})
@@ -0,0 +1,50 @@
package seaweedfs
import (
"context"
"net/http"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestIAMAccessKeyCreateDoesNotRetryOverACommittedKey(t *testing.T) {
ctx := context.Background()
keys := []string{"EXISTING"}
creates := 0
client := testIAMClient(t, func(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
switch r.Form.Get("Action") {
case "ListAccessKeys":
var members strings.Builder
for _, id := range keys {
members.WriteString(`<member><UserName>alice</UserName><AccessKeyId>` + id + `</AccessKeyId><Status>Active</Status></member>`)
}
_, _ = w.Write([]byte(`<ListAccessKeysResponse><ListAccessKeysResult><AccessKeyMetadata>` + members.String() + `</AccessKeyMetadata></ListAccessKeysResult></ListAccessKeysResponse>`))
case "CreateAccessKey":
creates++
keys = append(keys, "ORPHAN")
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`<ErrorResponse><Error><Code>ServiceFailure</Code><Message>deadline exceeded</Message></Error></ErrorResponse>`))
default:
t.Errorf("unexpected action %s", r.Form.Get("Action"))
}
})
r := &iamAccessKeyResource{client: client, data: &providerData{}}
plan, state := testResourcePlan(ctx, r, map[string]tftypes.Value{
"user_name": tftypes.NewValue(tftypes.String, "alice"),
})
resp := resource.CreateResponse{State: state}
r.Create(ctx, resource.CreateRequest{Plan: plan}, &resp)
if !resp.Diagnostics.HasError() {
t.Fatal("expected create to fail")
}
if creates != 1 {
t.Fatalf("CreateAccessKey called %d times, want 1", creates)
}
if detail := resp.Diagnostics.Errors()[0].Detail(); !strings.Contains(detail, "ORPHAN") {
t.Fatalf("diagnostic %q does not name the orphaned key", detail)
}
}