Added versioning edit in console (#645)
This commit is contained in:
@@ -191,6 +191,7 @@ type MCClient interface {
|
||||
list(ctx context.Context, opts mc.ListOptions) <-chan *mc.ClientContent
|
||||
get(ctx context.Context, opts mc.GetOptions) (io.ReadCloser, *probe.Error)
|
||||
shareDownload(ctx context.Context, versionID string, expires time.Duration) (string, *probe.Error)
|
||||
setVersioning(ctx context.Context, status string) *probe.Error
|
||||
}
|
||||
|
||||
// Interface implementation
|
||||
@@ -219,6 +220,10 @@ func (c mcClient) setReplication(ctx context.Context, cfg *replication.Config, o
|
||||
return c.client.SetReplication(ctx, cfg, opts)
|
||||
}
|
||||
|
||||
func (c mcClient) setVersioning(ctx context.Context, status string) *probe.Error {
|
||||
return c.client.SetVersion(ctx, status)
|
||||
}
|
||||
|
||||
func (c mcClient) remove(ctx context.Context, isIncomplete, isRemoveBucket, isBypass bool, contentCh <-chan *mc.ClientContent) <-chan *probe.Error {
|
||||
return c.client.Remove(ctx, isIncomplete, isRemoveBucket, isBypass, contentCh)
|
||||
}
|
||||
|
||||
@@ -89,7 +89,14 @@ func registerBucketsHandlers(api *operations.ConsoleAPI) {
|
||||
}
|
||||
return user_api.NewGetBucketVersioningOK().WithPayload(getBucketVersioning)
|
||||
})
|
||||
|
||||
// update bucket versioning
|
||||
api.UserAPISetBucketVersioningHandler = user_api.SetBucketVersioningHandlerFunc(func(params user_api.SetBucketVersioningParams, session *models.Principal) middleware.Responder {
|
||||
err := setBucketVersioningResponse(session, params.BucketName, ¶ms)
|
||||
if err != nil {
|
||||
return user_api.NewSetBucketVersioningDefault(500).WithPayload(err)
|
||||
}
|
||||
return user_api.NewSetBucketVersioningCreated()
|
||||
})
|
||||
// get bucket replication
|
||||
api.UserAPIGetBucketReplicationHandler = user_api.GetBucketReplicationHandlerFunc(func(params user_api.GetBucketReplicationParams, session *models.Principal) middleware.Responder {
|
||||
getBucketReplication, err := getBucketReplicationdResponse(session, params.BucketName)
|
||||
@@ -200,6 +207,47 @@ func getAddBucketReplicationdResponse(session *models.Principal, bucketName stri
|
||||
return nil
|
||||
}
|
||||
|
||||
type VersionState string
|
||||
|
||||
const (
|
||||
VersionEnable VersionState = "enable"
|
||||
VersionSuspend = "suspend"
|
||||
)
|
||||
|
||||
// removeBucket deletes a bucket
|
||||
func doSetVersioning(client MCClient, state VersionState) error {
|
||||
err := client.setVersioning(context.Background(), string(state))
|
||||
if err != nil {
|
||||
log.Println("error setting versioning for bucket:", err.Cause)
|
||||
return err.Cause
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setBucketVersioningResponse(session *models.Principal, bucketName string, params *user_api.SetBucketVersioningParams) *models.Error {
|
||||
s3Client, err := newS3BucketClient(session, bucketName, "")
|
||||
if err != nil {
|
||||
return prepareError(err)
|
||||
}
|
||||
// create a mc S3Client interface implementation
|
||||
// defining the client to be used
|
||||
amcClient := mcClient{client: s3Client}
|
||||
|
||||
var versioningState VersionState = VersionSuspend
|
||||
|
||||
if params.Body.Versioning {
|
||||
versioningState = VersionEnable
|
||||
}
|
||||
|
||||
err2 := doSetVersioning(amcClient, versioningState)
|
||||
|
||||
if err2 != nil {
|
||||
return prepareError(err2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBucketReplicationdResponse(session *models.Principal, bucketName string) (*models.BucketReplicationResponse, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
||||
defer cancel()
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
|
||||
"github.com/go-openapi/swag"
|
||||
"github.com/minio/console/models"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/sse"
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
@@ -44,6 +45,7 @@ var minioRemoveBucketEncryptionMock func(ctx context.Context, bucketName string)
|
||||
var minioGetBucketEncryptionMock func(ctx context.Context, bucketName string) (*sse.Configuration, error)
|
||||
var minioSetObjectLockConfigMock func(ctx context.Context, bucketName string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error
|
||||
var minioGetObjectLockConfigMock func(ctx context.Context, bucketName string) (mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, err error)
|
||||
var minioSetVersioningMock func(ctx context.Context, state string) *probe.Error
|
||||
|
||||
// Define a mock struct of minio Client interface implementation
|
||||
type minioClientMock struct {
|
||||
@@ -94,6 +96,10 @@ func (mc minioClientMock) getBucketObjectLockConfig(ctx context.Context, bucketN
|
||||
return minioGetObjectLockConfigMock(ctx, bucketName)
|
||||
}
|
||||
|
||||
func (c s3ClientMock) setVersioning(ctx context.Context, state string) *probe.Error {
|
||||
return minioSetVersioningMock(ctx, state)
|
||||
}
|
||||
|
||||
var minioAccountInfoMock func(ctx context.Context) (madmin.AccountInfo, error)
|
||||
|
||||
// mock function of dataUsageInfo() needed for list bucket's usage
|
||||
@@ -765,3 +771,65 @@ func Test_GetBucketRetentionConfig(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetBucketVersioning(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
ctx := context.Background()
|
||||
errorMsg := "Error Message"
|
||||
minClient := s3ClientMock{}
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
state VersionState
|
||||
bucketName string
|
||||
client s3ClientMock
|
||||
setVersioningFunc func(ctx context.Context, state string) *probe.Error
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
name: "Set Bucket Version Success",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
state: VersionEnable,
|
||||
bucketName: "test",
|
||||
client: minClient,
|
||||
setVersioningFunc: func(ctx context.Context, state string) *probe.Error {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "Set Bucket Version Error",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
state: VersionEnable,
|
||||
bucketName: "test",
|
||||
client: minClient,
|
||||
setVersioningFunc: func(ctx context.Context, state string) *probe.Error {
|
||||
return probe.NewError(errors.New(errorMsg))
|
||||
},
|
||||
},
|
||||
expectedError: errors.New(errorMsg),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
minioSetVersioningMock = tt.args.setVersioningFunc
|
||||
|
||||
err := doSetVersioning(tt.args.client, tt.args.state)
|
||||
|
||||
fmt.Println(t.Name())
|
||||
fmt.Println("Expected:", tt.expectedError, "Error:", err)
|
||||
|
||||
if tt.expectedError != nil {
|
||||
fmt.Println(t.Name())
|
||||
assert.Equal(tt.expectedError.Error(), err.Error(), fmt.Sprintf("getBucketRetentionConfig() error: `%s`, wantErr: `%s`", err, tt.expectedError))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user