Add MaxNoncurrentVersions to NoncurrentExpiration action (#13580)

This unit allows users to limit the maximum number of noncurrent 
versions of an object.

To enable this rule you need the following *ilm.json*
```
cat >> ilm.json <<EOF
{
    "Rules": [
        {
            "ID": "test-max-noncurrent",
            "Status": "Enabled",
            "Filter": {
                "Prefix": "user-uploads/"
            },
            "NoncurrentVersionExpiration": {
                "MaxNoncurrentVersions": 5
            }
        }
    ]
}
EOF
mc ilm import myminio/mybucket < ilm.json
```
This commit is contained in:
Krishnan Parthasarathi
2021-11-19 17:54:10 -08:00
committed by GitHub
parent 1e2fac054c
commit 3da9ee15d3
16 changed files with 707 additions and 308 deletions

View File

@@ -24,14 +24,15 @@ import (
// NoncurrentVersionExpiration - an action for lifecycle configuration rule.
type NoncurrentVersionExpiration struct {
XMLName xml.Name `xml:"NoncurrentVersionExpiration"`
NoncurrentDays ExpirationDays `xml:"NoncurrentDays,omitempty"`
set bool
XMLName xml.Name `xml:"NoncurrentVersionExpiration"`
NoncurrentDays ExpirationDays `xml:"NoncurrentDays,omitempty"`
MaxNoncurrentVersions int `xml:"MaxNoncurrentVersions,omitempty"`
set bool
}
// MarshalXML if non-current days not set to non zero value
func (n NoncurrentVersionExpiration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if n.IsDaysNull() {
if n.IsNull() {
return nil
}
type noncurrentVersionExpirationWrapper NoncurrentVersionExpiration
@@ -51,6 +52,11 @@ func (n *NoncurrentVersionExpiration) UnmarshalXML(d *xml.Decoder, startElement
return nil
}
// IsNull returns if both NoncurrentDays and NoncurrentVersions are empty
func (n NoncurrentVersionExpiration) IsNull() bool {
return n.IsDaysNull() && n.MaxNoncurrentVersions == 0
}
// IsDaysNull returns true if days field is null
func (n NoncurrentVersionExpiration) IsDaysNull() bool {
return n.NoncurrentDays == ExpirationDays(0)
@@ -62,8 +68,17 @@ func (n NoncurrentVersionExpiration) Validate() error {
return nil
}
val := int(n.NoncurrentDays)
if val <= 0 {
switch {
case val == 0 && n.MaxNoncurrentVersions == 0:
// both fields can't be zero
return errXMLNotWellFormed
case val > 0 && n.MaxNoncurrentVersions > 0:
// both tags can't be non-zero simultaneously
return errLifecycleInvalidNoncurrentExpiration
case val < 0, n.MaxNoncurrentVersions < 0:
// negative values are not supported
}
return nil
}