mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
terraform/provider: point BucketAlreadyExists on bucket create to terraform import
SeaweedFS answers BucketAlreadyExists both for another identity's bucket and for the caller's own bucket that has an ACL, so the create cannot adopt it safely. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d44b8be5d7
commit
0add6aa830
3 files changed
+46
No files matched your search
@@ -596,6 +596,14 @@ func isBucketAlreadyExistsError(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func isBucketTakenError(err error) bool {
|
||||
var apiErr iamError
|
||||
if errors.As(err, &apiErr) {
|
||||
return apiErr.Code == "BucketAlreadyExists"
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isNoSuchTagSetError(err error) bool {
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
|
||||
@@ -92,6 +92,13 @@ func (r *bucketResource) Create(ctx context.Context, req resource.CreateRequest,
|
||||
defer lock.Unlock()
|
||||
|
||||
if err := r.client.CreateBucket(ctx, plan.Bucket.ValueString()); err != nil {
|
||||
if isBucketTakenError(err) {
|
||||
resp.Diagnostics.AddError(
|
||||
"Bucket already exists",
|
||||
fmt.Sprintf("Bucket %q already exists. SeaweedFS reports this for a bucket owned by another identity, and also for a bucket of yours that has an ACL. If the bucket is yours, import it with `terraform import` to manage it.", plan.Bucket.ValueString()),
|
||||
)
|
||||
return
|
||||
}
|
||||
if !isBucketAlreadyExistsError(err) {
|
||||
resp.Diagnostics.AddError("Failed to create bucket", err.Error())
|
||||
return
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package seaweedfs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-go/tftypes"
|
||||
)
|
||||
|
||||
func TestBucketCreateDoesNotAdoptTakenBucket(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
client := testIAMClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_, _ = w.Write([]byte(`<Error><Code>BucketAlreadyExists</Code><Message>taken</Message></Error>`))
|
||||
})
|
||||
r := &bucketResource{client: client, data: &providerData{}}
|
||||
plan, state := testResourcePlan(ctx, r, map[string]tftypes.Value{
|
||||
"bucket": tftypes.NewValue(tftypes.String, "photos"),
|
||||
})
|
||||
resp := resource.CreateResponse{State: state}
|
||||
r.Create(ctx, resource.CreateRequest{Plan: plan}, &resp)
|
||||
if !resp.Diagnostics.HasError() || !strings.Contains(resp.Diagnostics.Errors()[0].Detail(), "terraform import") {
|
||||
t.Fatalf("diagnostics = %v, want an import hint", resp.Diagnostics)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user