Added lifecycle rule edit capability (#1539)
Signed-off-by: Benjamin Perez <benjamin@bexsoft.net> Co-authored-by: Benjamin Perez <benjamin@bexsoft.net>
This commit is contained in:
@@ -4425,6 +4425,10 @@ func init() {
|
||||
},
|
||||
"delete_marker": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"noncurrent_expiration_days": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -5947,6 +5951,13 @@ func init() {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"noncurrent_storage_class": {
|
||||
"type": "string"
|
||||
},
|
||||
"noncurrent_transition_days": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"storage_class": {
|
||||
"type": "string"
|
||||
}
|
||||
@@ -5954,6 +5965,9 @@ func init() {
|
||||
},
|
||||
"updateBucketLifecycle": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"type"
|
||||
],
|
||||
"properties": {
|
||||
"disable": {
|
||||
"description": "Non required, toggle to disable or enable rule",
|
||||
@@ -6002,6 +6016,14 @@ func init() {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"default": 0
|
||||
},
|
||||
"type": {
|
||||
"description": "ILM Rule type (Expiry or transition)",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"expiry",
|
||||
"transition"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -10694,6 +10716,10 @@ func init() {
|
||||
},
|
||||
"delete_marker": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"noncurrent_expiration_days": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -12216,6 +12242,13 @@ func init() {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"noncurrent_storage_class": {
|
||||
"type": "string"
|
||||
},
|
||||
"noncurrent_transition_days": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"storage_class": {
|
||||
"type": "string"
|
||||
}
|
||||
@@ -12223,6 +12256,9 @@ func init() {
|
||||
},
|
||||
"updateBucketLifecycle": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"type"
|
||||
],
|
||||
"properties": {
|
||||
"disable": {
|
||||
"description": "Non required, toggle to disable or enable rule",
|
||||
@@ -12271,6 +12307,14 @@ func init() {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"default": 0
|
||||
},
|
||||
"type": {
|
||||
"description": "ILM Rule type (Expiry or transition)",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"expiry",
|
||||
"transition"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -56,7 +56,7 @@ func registerBucketsLifecycleHandlers(api *operations.ConsoleAPI) {
|
||||
api.UserAPIUpdateBucketLifecycleHandler = user_api.UpdateBucketLifecycleHandlerFunc(func(params user_api.UpdateBucketLifecycleParams, session *models.Principal) middleware.Responder {
|
||||
err := getEditBucketLifecycleRule(session, params)
|
||||
if err != nil {
|
||||
user_api.NewUpdateBucketLifecycleDefault(int(err.Code)).WithPayload(err)
|
||||
return user_api.NewUpdateBucketLifecycleDefault(int(err.Code)).WithPayload(err)
|
||||
}
|
||||
|
||||
return user_api.NewUpdateBucketLifecycleOK()
|
||||
@@ -83,13 +83,30 @@ func getBucketLifecycle(ctx context.Context, client MinioClient, bucketName stri
|
||||
})
|
||||
}
|
||||
|
||||
rulePrefix := rule.RuleFilter.And.Prefix
|
||||
|
||||
if rulePrefix == "" {
|
||||
rulePrefix = rule.RuleFilter.Prefix
|
||||
}
|
||||
|
||||
rules = append(rules, &models.ObjectBucketLifecycle{
|
||||
ID: rule.ID,
|
||||
Status: rule.Status,
|
||||
Prefix: rule.RuleFilter.And.Prefix,
|
||||
Expiration: &models.ExpirationResponse{Date: rule.Expiration.Date.Format(time.RFC3339), Days: int64(rule.Expiration.Days), DeleteMarker: rule.Expiration.DeleteMarker.IsEnabled()},
|
||||
Transition: &models.TransitionResponse{Date: rule.Transition.Date.Format(time.RFC3339), Days: int64(rule.Transition.Days), StorageClass: rule.Transition.StorageClass},
|
||||
Tags: tags,
|
||||
ID: rule.ID,
|
||||
Status: rule.Status,
|
||||
Prefix: rulePrefix,
|
||||
Expiration: &models.ExpirationResponse{
|
||||
Date: rule.Expiration.Date.Format(time.RFC3339),
|
||||
Days: int64(rule.Expiration.Days),
|
||||
DeleteMarker: rule.Expiration.DeleteMarker.IsEnabled(),
|
||||
NoncurrentExpirationDays: int64(rule.NoncurrentVersionExpiration.NoncurrentDays),
|
||||
},
|
||||
Transition: &models.TransitionResponse{
|
||||
Date: rule.Transition.Date.Format(time.RFC3339),
|
||||
Days: int64(rule.Transition.Days),
|
||||
StorageClass: rule.Transition.StorageClass,
|
||||
NoncurrentStorageClass: rule.NoncurrentVersionTransition.StorageClass,
|
||||
NoncurrentTransitionDays: int64(rule.NoncurrentVersionTransition.NoncurrentDays),
|
||||
},
|
||||
Tags: tags,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -251,7 +268,7 @@ func getAddBucketLifecycleResponse(session *models.Principal, params user_api.Ad
|
||||
return nil
|
||||
}
|
||||
|
||||
// addBucketLifecycle gets lifecycle lists for a bucket from MinIO API and returns their implementations
|
||||
// editBucketLifecycle gets lifecycle lists for a bucket from MinIO API and updates the selected lifecycle rule
|
||||
func editBucketLifecycle(ctx context.Context, client MinioClient, params user_api.UpdateBucketLifecycleParams) error {
|
||||
// Configuration that is already set.
|
||||
lfcCfg, err := client.getLifecycleRules(ctx, params.BucketName)
|
||||
@@ -268,10 +285,9 @@ func editBucketLifecycle(ctx context.Context, client MinioClient, params user_ap
|
||||
opts := ilm.LifecycleOptions{}
|
||||
|
||||
// Verify if transition items are set
|
||||
if params.Body.ExpiryDays == 0 && params.Body.TransitionDays != 0 {
|
||||
|
||||
if params.Body.NoncurrentversionExpirationDays != 0 {
|
||||
return errors.New("non current version expiration days cannot be set when transition is being configured")
|
||||
if *params.Body.Type == models.UpdateBucketLifecycleTypeTransition {
|
||||
if params.Body.TransitionDays == 0 && params.Body.NoncurrentversionTransitionDays == 0 {
|
||||
return errors.New("you must select transition days or non-current transition days configuration")
|
||||
}
|
||||
|
||||
opts = ilm.LifecycleOptions{
|
||||
@@ -285,8 +301,10 @@ func editBucketLifecycle(ctx context.Context, client MinioClient, params user_ap
|
||||
ExpiredObjectDeleteMarker: params.Body.ExpiredObjectDeleteMarker,
|
||||
NoncurrentVersionTransitionDays: int(params.Body.NoncurrentversionTransitionDays),
|
||||
NoncurrentVersionTransitionStorageClass: strings.ToUpper(params.Body.NoncurrentversionTransitionStorageClass),
|
||||
IsTransitionDaysSet: params.Body.TransitionDays != 0,
|
||||
IsNoncurrentVersionTransitionDaysSet: params.Body.NoncurrentversionTransitionDays != 0,
|
||||
}
|
||||
} else if params.Body.TransitionDays == 0 && params.Body.ExpiryDays != 0 { // Verify if expiry configuration is set
|
||||
} else if *params.Body.Type == models.UpdateBucketLifecycleTypeExpiry { // Verify if expiry configuration is set
|
||||
if params.Body.NoncurrentversionTransitionDays != 0 {
|
||||
return errors.New("non current version Transition Days cannot be set when expiry is being configured")
|
||||
}
|
||||
|
||||
@@ -226,9 +226,12 @@ func TestUpdateLifecycleRule(t *testing.T) {
|
||||
|
||||
// Test-2 : editBucketLifecycle() Update lifecycle rule
|
||||
|
||||
expiryRule := "expiry"
|
||||
|
||||
editMock := user_api.UpdateBucketLifecycleParams{
|
||||
BucketName: "testBucket",
|
||||
Body: &models.UpdateBucketLifecycle{
|
||||
Type: &expiryRule,
|
||||
Disable: false,
|
||||
ExpiredObjectDeleteMarker: false,
|
||||
ExpiryDays: int32(16),
|
||||
@@ -250,6 +253,33 @@ func TestUpdateLifecycleRule(t *testing.T) {
|
||||
|
||||
assert.Equal(nil, err, fmt.Sprintf("Failed on %s: Error returned", function))
|
||||
|
||||
// Test-2a : editBucketLifecycle() Update lifecycle rule
|
||||
|
||||
transitionRule := "transition"
|
||||
|
||||
editMock = user_api.UpdateBucketLifecycleParams{
|
||||
BucketName: "testBucket",
|
||||
Body: &models.UpdateBucketLifecycle{
|
||||
Type: &transitionRule,
|
||||
Disable: false,
|
||||
ExpiredObjectDeleteMarker: false,
|
||||
NoncurrentversionTransitionDays: 5,
|
||||
Prefix: "pref1",
|
||||
StorageClass: "TEST",
|
||||
NoncurrentversionTransitionStorageClass: "TESTNC",
|
||||
Tags: "",
|
||||
TransitionDays: int32(16),
|
||||
},
|
||||
}
|
||||
|
||||
minioSetBucketLifecycleMock = func(ctx context.Context, bucketName string, config *lifecycle.Configuration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = editBucketLifecycle(ctx, minClient, editMock)
|
||||
|
||||
assert.Equal(nil, err, fmt.Sprintf("Failed on %s: Error returned", function))
|
||||
|
||||
// Test-3 : editBucketLifecycle() returns error
|
||||
|
||||
minioSetBucketLifecycleMock = func(ctx context.Context, bucketName string, config *lifecycle.Configuration) error {
|
||||
|
||||
Reference in New Issue
Block a user