From f449a7ae2cd90e638a08afd4b19e999947babd0d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 25 Jan 2024 17:20:54 -0800 Subject: [PATCH] allow bucket import to be idempotent (#18873) do not need to be defensive in our approach, we should simply override anything everything in import process, do not care about what currently exists on the disk - backup is the source of truth. --- cmd/admin-bucket-handlers.go | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/cmd/admin-bucket-handlers.go b/cmd/admin-bucket-handlers.go index 509ae364f..811555055 100644 --- a/cmd/admin-bucket-handlers.go +++ b/cmd/admin-bucket-handlers.go @@ -680,24 +680,17 @@ func (a adminAPIHandlers) ImportBucketMetadataHandler(w http.ResponseWriter, r * if _, ok := bucketMap[bucket]; !ok { opts := MakeBucketOptions{ LockEnabled: config.Enabled(), + ForceCreate: true, // ignore if it already exists } err = objectAPI.MakeBucket(ctx, bucket, opts) if err != nil { - if _, ok := err.(BucketExists); !ok { - rpt.SetStatus(bucket, fileName, err) - continue - } + rpt.SetStatus(bucket, fileName, err) + continue } v := newBucketMetadata(bucket) bucketMap[bucket] = &v } - // Deny object locking configuration settings on existing buckets without object lock enabled. - if _, _, err = globalBucketMetadataSys.GetObjectLockConfig(bucket); err != nil { - rpt.SetStatus(bucket, fileName, err) - continue - } - bucketMap[bucket].ObjectLockConfigXML = configData bucketMap[bucket].ObjectLockConfigUpdatedAt = updatedAt rpt.SetStatus(bucket, fileName, nil) @@ -724,11 +717,11 @@ func (a adminAPIHandlers) ImportBucketMetadataHandler(w http.ResponseWriter, r * continue } if _, ok := bucketMap[bucket]; !ok { - if err = objectAPI.MakeBucket(ctx, bucket, MakeBucketOptions{}); err != nil { - if _, ok := err.(BucketExists); !ok { - rpt.SetStatus(bucket, fileName, err) - continue - } + if err = objectAPI.MakeBucket(ctx, bucket, MakeBucketOptions{ + ForceCreate: true, // ignore if it already exists + }); err != nil { + rpt.SetStatus(bucket, fileName, err) + continue } v := newBucketMetadata(bucket) bucketMap[bucket] = &v @@ -776,12 +769,12 @@ func (a adminAPIHandlers) ImportBucketMetadataHandler(w http.ResponseWriter, r * // create bucket if it does not exist yet. if _, ok := bucketMap[bucket]; !ok { - err = objectAPI.MakeBucket(ctx, bucket, MakeBucketOptions{}) + err = objectAPI.MakeBucket(ctx, bucket, MakeBucketOptions{ + ForceCreate: true, // ignore if it already exists + }) if err != nil { - if _, ok := err.(BucketExists); !ok { - rpt.SetStatus(bucket, "", err) - continue - } + rpt.SetStatus(bucket, "", err) + continue } v := newBucketMetadata(bucket) bucketMap[bucket] = &v