mirror of
https://github.com/minio/minio.git
synced 2025-11-09 21:49:46 -05:00
Integrate existing remove bucket functionality from newux to current UI (#5289)
This commit takes the existing remove bucket functionality written by brendanashworth, integrates it to the current UI with a dropdown for each bucket, and fixes small issues that were present, like the dropdown not disappearing after the user clicks on 'Delete' for certain buckets. This feature only deletes a bucket that is empty (that has no objects). Fixes #4166
This commit is contained in:
committed by
Nitish Tiwari
parent
baaf67d82e
commit
659f724f4c
@@ -145,6 +145,36 @@ func (web *webAPIHandlers) MakeBucket(r *http.Request, args *MakeBucketArgs, rep
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveBucketArgs - remove bucket args.
|
||||
type RemoveBucketArgs struct {
|
||||
BucketName string `json:"bucketName"`
|
||||
}
|
||||
|
||||
// DeleteBucket - removes a bucket, must be empty.
|
||||
func (web *webAPIHandlers) DeleteBucket(r *http.Request, args *RemoveBucketArgs, reply *WebGenericRep) error {
|
||||
objectAPI := web.ObjectAPI()
|
||||
if objectAPI == nil {
|
||||
return toJSONError(errServerNotInitialized)
|
||||
}
|
||||
if !isHTTPRequestValid(r) {
|
||||
return toJSONError(errAuthentication)
|
||||
}
|
||||
|
||||
bucketLock := globalNSMutex.NewNSLock(args.BucketName, "")
|
||||
if err := bucketLock.GetLock(globalObjectTimeout); err != nil {
|
||||
return toJSONError(errOperationTimedOut)
|
||||
}
|
||||
defer bucketLock.Unlock()
|
||||
|
||||
err := objectAPI.DeleteBucket(args.BucketName)
|
||||
if err != nil {
|
||||
return toJSONError(err, args.BucketName)
|
||||
}
|
||||
|
||||
reply.UIVersion = browser.UIVersion
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListBucketsRep - list buckets response
|
||||
type ListBucketsRep struct {
|
||||
Buckets []WebBucketInfo `json:"buckets"`
|
||||
|
||||
@@ -305,6 +305,107 @@ func testMakeBucketWebHandler(obj ObjectLayer, instanceType string, t TestErrHan
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper for calling DeleteBucket handler
|
||||
func TestWebHandlerDeleteBucket(t *testing.T) {
|
||||
ExecObjectLayerTest(t, testDeleteBucketWebHandler)
|
||||
}
|
||||
|
||||
// testDeleteBucketWebHandler - Test DeleteBucket web handler
|
||||
func testDeleteBucketWebHandler(obj ObjectLayer, instanceType string, t TestErrHandler) {
|
||||
apiRouter := initTestWebRPCEndPoint(obj)
|
||||
|
||||
credentials := globalServerConfig.GetCredential()
|
||||
token, err := getWebRPCToken(apiRouter, credentials.AccessKey, credentials.SecretKey)
|
||||
if err != nil {
|
||||
t.Fatalf("could not get RPC token, %s", err.Error())
|
||||
}
|
||||
|
||||
bucketName := getRandomBucketName()
|
||||
err = obj.MakeBucketWithLocation(bucketName, "")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create bucket: %s (%s)", err.Error(), instanceType)
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
bucketName string
|
||||
// Whether or not to put an object into the bucket.
|
||||
initWithObject bool
|
||||
token string
|
||||
// Expected error (error must only contain this string to pass test)
|
||||
// Empty string = no error
|
||||
expect string
|
||||
}{
|
||||
{"", false, token, "Bucket Name is invalid"},
|
||||
{".", false, "auth", "Authentication failed"},
|
||||
{".", false, token, "Bucket Name . is invalid"},
|
||||
{"ab", false, token, "Bucket Name ab is invalid"},
|
||||
{"minio", false, "false token", "Authentication failed"},
|
||||
{"minio", false, token, "specified bucket minio does not exist"},
|
||||
{bucketName, false, token, ""},
|
||||
{bucketName, true, token, "Bucket not empty"},
|
||||
{bucketName, false, "", "Authentication failed"},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
if test.initWithObject {
|
||||
data := bytes.NewBufferString("hello")
|
||||
_, err = obj.PutObject(test.bucketName, "object", mustGetHashReader(t, data, int64(data.Len()), "", ""), nil)
|
||||
// _, err = obj.PutObject(test.bucketName, "object", int64(data.Len()), data, nil, "")
|
||||
if err != nil {
|
||||
t.Fatalf("could not put object to %s, %s", test.bucketName, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
makeBucketRequest := MakeBucketArgs{BucketName: test.bucketName}
|
||||
makeBucketReply := &WebGenericRep{}
|
||||
|
||||
req, err := newTestWebRPCRequest("Web.DeleteBucket", test.token, makeBucketRequest)
|
||||
if err != nil {
|
||||
t.Errorf("failed to create HTTP request: <ERROR> %v", err)
|
||||
}
|
||||
|
||||
apiRouter.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("expected the response status to be `%d`, but instead found `%d`", http.StatusOK, rec.Code)
|
||||
}
|
||||
err = getTestWebRPCResponse(rec, &makeBucketReply)
|
||||
|
||||
if test.expect != "" {
|
||||
if err == nil {
|
||||
// If we expected an error, but didn't get one.
|
||||
t.Errorf("expected `..%s..` but got nil error", test.expect)
|
||||
} else if !strings.Contains(err.Error(), test.expect) {
|
||||
// If we got an error that wasn't what we expected.
|
||||
t.Errorf("expected `..%s..` but got `%s`", test.expect, err.Error())
|
||||
}
|
||||
} else if test.expect == "" && err != nil {
|
||||
t.Errorf("expected test success, but got `%s`", err.Error())
|
||||
}
|
||||
|
||||
// If we created the bucket with an object, now delete the object to cleanup.
|
||||
if test.initWithObject {
|
||||
err = obj.DeleteObject(test.bucketName, "object")
|
||||
if err != nil {
|
||||
t.Fatalf("could not delete object, %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// If it did not succeed in deleting the bucket, don't try and recreate it.
|
||||
// Or, it'll fail if there was an object.
|
||||
if err != nil || test.initWithObject {
|
||||
continue
|
||||
}
|
||||
|
||||
err = obj.MakeBucketWithLocation(bucketName, "")
|
||||
if err != nil {
|
||||
// failed to create new bucket, abort.
|
||||
t.Fatalf("failed to create new bucket (%s): %s", instanceType, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper for calling ListBuckets Web Handler
|
||||
func TestWebHandlerListBuckets(t *testing.T) {
|
||||
ExecObjectLayerTest(t, testListBucketsWebHandler)
|
||||
|
||||
Reference in New Issue
Block a user