ilm: ObjectSizeLessThan and ObjectSizeGreaterThan (#18500)

This commit is contained in:
Krishnan Parthasarathi
2023-11-22 13:42:39 -08:00
committed by GitHub
parent e6b0fc465b
commit a93214ea63
7 changed files with 472 additions and 47 deletions
+41 -10
View File
@@ -25,26 +25,37 @@ var errDuplicateTagKey = Errorf("Duplicate Tag Keys are not allowed")
// And - a tag to combine a prefix and multiple tags for lifecycle configuration rule.
type And struct {
XMLName xml.Name `xml:"And"`
Prefix Prefix `xml:"Prefix,omitempty"`
Tags []Tag `xml:"Tag,omitempty"`
XMLName xml.Name `xml:"And"`
ObjectSizeGreaterThan int64 `xml:"ObjectSizeGreaterThan,omitempty"`
ObjectSizeLessThan int64 `xml:"ObjectSizeLessThan,omitempty"`
Prefix Prefix `xml:"Prefix,omitempty"`
Tags []Tag `xml:"Tag,omitempty"`
}
// isEmpty returns true if Tags field is null
func (a And) isEmpty() bool {
return len(a.Tags) == 0 && !a.Prefix.set
return len(a.Tags) == 0 && !a.Prefix.set &&
a.ObjectSizeGreaterThan == 0 && a.ObjectSizeLessThan == 0
}
// Validate - validates the And field
func (a And) Validate() error {
emptyPrefix := !a.Prefix.set
emptyTags := len(a.Tags) == 0
if emptyPrefix && emptyTags {
return nil
// > This is used in a Lifecycle Rule Filter to apply a logical AND to two or more predicates.
// ref: https://docs.aws.amazon.com/AmazonS3/latest/API/API_LifecycleRuleAndOperator.html
// i.e, predCount >= 2
var predCount int
if a.Prefix.set {
predCount++
}
predCount += len(a.Tags)
if a.ObjectSizeGreaterThan > 0 {
predCount++
}
if a.ObjectSizeLessThan > 0 {
predCount++
}
if emptyPrefix && !emptyTags || !emptyPrefix && emptyTags {
if predCount < 2 {
return errXMLNotWellFormed
}
@@ -56,6 +67,10 @@ func (a And) Validate() error {
return err
}
}
if a.ObjectSizeGreaterThan < 0 || a.ObjectSizeLessThan < 0 {
return errXMLNotWellFormed
}
return nil
}
@@ -72,3 +87,19 @@ func (a And) ContainsDuplicateTag() bool {
return false
}
// BySize returns true when sz satisfies a
// ObjectSizeLessThan/ObjectSizeGreaterthan or a logial AND of these predicates
// Note: And combines size and other predicates like Tags, Prefix, etc. This
// method applies exclusively to size predicates only.
func (a And) BySize(sz int64) bool {
if a.ObjectSizeGreaterThan > 0 &&
sz <= a.ObjectSizeGreaterThan {
return false
}
if a.ObjectSizeLessThan > 0 &&
sz >= a.ObjectSizeLessThan {
return false
}
return true
}