Skip to content

Commit

Permalink
ilm: Support Object{LessThan,GreaterThan} (#1903)
Browse files Browse the repository at this point in the history
  • Loading branch information
krisis authored Nov 22, 2023
1 parent a238b5e commit 1f7dd6b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
44 changes: 32 additions & 12 deletions pkg/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,35 +211,43 @@ func (t Transition) MarshalXML(en *xml.Encoder, startElement xml.StartElement) e

// And And Rule for LifecycleTag, to be used in LifecycleRuleFilter
type And struct {
XMLName xml.Name `xml:"And" json:"-"`
Prefix string `xml:"Prefix" json:"Prefix,omitempty"`
Tags []Tag `xml:"Tag" json:"Tags,omitempty"`
XMLName xml.Name `xml:"And" json:"-"`
Prefix string `xml:"Prefix" json:"Prefix,omitempty"`
Tags []Tag `xml:"Tag" json:"Tags,omitempty"`
ObjectSizeLessThan int64 `xml:"ObjectSizeLessThan,omitempty" json:"ObjectSizeLessThan,omitempty"`
ObjectSizeGreaterThan int64 `xml:"ObjectSizeGreaterThan,omitempty" json:"ObjectSizeGreaterThan,omitempty"`
}

// IsEmpty returns true if Tags field is null
func (a And) IsEmpty() bool {
return len(a.Tags) == 0 && a.Prefix == ""
return len(a.Tags) == 0 && a.Prefix == "" &&
a.ObjectSizeLessThan == 0 && a.ObjectSizeGreaterThan == 0
}

// Filter will be used in selecting rule(s) for lifecycle configuration
type Filter struct {
XMLName xml.Name `xml:"Filter" json:"-"`
And And `xml:"And,omitempty" json:"And,omitempty"`
Prefix string `xml:"Prefix,omitempty" json:"Prefix,omitempty"`
Tag Tag `xml:"Tag,omitempty" json:"Tag,omitempty"`
XMLName xml.Name `xml:"Filter" json:"-"`
And And `xml:"And,omitempty" json:"And,omitempty"`
Prefix string `xml:"Prefix,omitempty" json:"Prefix,omitempty"`
Tag Tag `xml:"Tag,omitempty" json:"Tag,omitempty"`
ObjectSizeLessThan int64 `xml:"ObjectSizeLessThan,omitempty" json:"ObjectSizeLessThan,omitempty"`
ObjectSizeGreaterThan int64 `xml:"ObjectSizeGreaterThan,omitempty" json:"ObjectSizeGreaterThan,omitempty"`
}

// IsNull returns true if all Filter fields are empty.
func (f Filter) IsNull() bool {
return f.Tag.IsEmpty() && f.And.IsEmpty() && f.Prefix == ""
return f.Tag.IsEmpty() && f.And.IsEmpty() && f.Prefix == "" &&
f.ObjectSizeLessThan == 0 && f.ObjectSizeGreaterThan == 0
}

// MarshalJSON customizes json encoding by removing empty values.
func (f Filter) MarshalJSON() ([]byte, error) {
type filter struct {
And *And `json:"And,omitempty"`
Prefix string `json:"Prefix,omitempty"`
Tag *Tag `json:"Tag,omitempty"`
And *And `json:"And,omitempty"`
Prefix string `json:"Prefix,omitempty"`
Tag *Tag `json:"Tag,omitempty"`
ObjectSizeLessThan int64 `json:"ObjectSizeLessThan,omitempty"`
ObjectSizeGreaterThan int64 `json:"ObjectSizeGreaterThan,omitempty"`
}

newf := filter{
Expand All @@ -251,6 +259,8 @@ func (f Filter) MarshalJSON() ([]byte, error) {
if !f.And.IsEmpty() {
newf.And = &f.And
}
newf.ObjectSizeLessThan = f.ObjectSizeLessThan
newf.ObjectSizeGreaterThan = f.ObjectSizeGreaterThan
return json.Marshal(newf)
}

Expand All @@ -275,6 +285,16 @@ func (f Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeElement(f.Prefix, xml.StartElement{Name: xml.Name{Local: "Prefix"}}); err != nil {
return err
}
if f.ObjectSizeLessThan > 0 {
if err := e.EncodeElement(f.ObjectSizeLessThan, xml.StartElement{Name: xml.Name{Local: "ObjectSizeLessThan"}}); err != nil {
return err
}
}
if f.ObjectSizeGreaterThan > 0 {
if err := e.EncodeElement(f.ObjectSizeGreaterThan, xml.StartElement{Name: xml.Name{Local: "ObjectSizeGreaterThan"}}); err != nil {
return err
}
}
}

return e.EncodeToken(xml.EndElement{Name: start.Name})
Expand Down
57 changes: 57 additions & 0 deletions pkg/lifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,63 @@ func TestLifecycleUnmarshalJSON(t *testing.T) {
}`,
err: nil,
},
{
input: `{
"Rules": [
{
"ID": "transition-lt",
"Status": "Enabled",
"Filter": {
"ObjectSizeLessThan": 1048576
},
"Transition": {
"StorageClass": "S3TIER-1",
"Days": 1
}
}
]
}`,
err: nil,
},
{
input: `{
"Rules": [
{
"ID": "noncurrent-transition-gt",
"Status": "Enabled",
"Filter": {
"ObjectSizeGreaterThan": 10485760
},
"NoncurrentVersionTransition": {
"StorageClass": "S3TIER-1",
"NoncurrentDays": 1
}
}
]
}`,
err: nil,
},
{
input: `{
"Rules": [
{
"ID": "noncurrent-transition-lt-and-gt",
"Status": "Enabled",
"Filter": {
"And": {
"ObjectSizeGreaterThan": 10485760,
"ObjectSizeLessThan": 1048576
}
},
"NoncurrentVersionTransition": {
"StorageClass": "S3TIER-1",
"NoncurrentDays": 1
}
}
]
}`,
err: nil,
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 1f7dd6b

Please sign in to comment.