Skip to content

Commit

Permalink
add filter node to bucket lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 authored and huiguangjun committed Nov 16, 2022
1 parent 490c38d commit 97c9cb1
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 11 deletions.
61 changes: 61 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,67 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
c.Assert(err, IsNil)
}

// TestSetBucketLifecycleWithFilter
func (s *OssClientSuite) TestSetBucketLifecycleFilter(c *C) {
var bucketNameTest = bucketNamePrefix + RandLowStr(6)

client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

err = client.CreateBucket(bucketNameTest)
c.Assert(err, IsNil)

expiration := LifecycleExpiration{
Days: 30,
}
tag := Tag{
Key: "key1",
Value: "value1",
}
filter := LifecycleFilter{
Not: []LifecycleFilterNot{
{
Prefix: "logs1",
Tag: &tag,
},
},
}
rule := LifecycleRule{
ID: "filter one",
Prefix: "logs",
Status: "Enabled",
Expiration: &expiration,
Transitions: []LifecycleTransition{
{
Days: 10,
StorageClass: StorageIA,
},
},
Filter: &filter,
}
rules := []LifecycleRule{rule}
err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, IsNil)
res, err := client.GetBucketLifecycle(bucketNameTest)
c.Assert(err, IsNil)
c.Assert(len(res.Rules), Equals, 1)
c.Assert(res.Rules[0].ID, Equals, "filter one")
c.Assert(res.Rules[0].Expiration, NotNil)
c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "")
c.Assert(res.Rules[0].Expiration.Days, Equals, 30)
c.Assert(res.Rules[0].Transitions[0].Days, Equals, 10)
c.Assert(res.Rules[0].Transitions[0].StorageClass, Equals, StorageIA)
c.Assert(res.Rules[0].Filter.Not[0].Prefix, Equals, "logs1")
c.Assert(res.Rules[0].Filter.Not[0].Tag.Key, Equals, "key1")
c.Assert(res.Rules[0].Filter.Not[0].Tag.Value, Equals, "value1")

err = client.DeleteBucketLifecycle(bucketNameTest)
c.Assert(err, IsNil)

err = client.DeleteBucket(bucketNameTest)
c.Assert(err, IsNil)
}

// TestSetBucketLifecycleOverLap
func (s *OssClientSuite) TestSetBucketLifecycleOverLap(c *C) {
var bucketNameTest = bucketNamePrefix + RandLowStr(6)
Expand Down
14 changes: 14 additions & 0 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type LifecycleRule struct {
// Deprecated: Use NonVersionTransitions instead.
NonVersionTransition *LifecycleVersionTransition `xml:"-"` // NonVersionTransition is not suggested to use
NonVersionTransitions []LifecycleVersionTransition `xml:"NoncurrentVersionTransition,omitempty"`
Filter *LifecycleFilter `xml:Filter,omitempty` //condition parameter container of this exclusion rule
}

// LifecycleExpiration defines the rule's expiration property
Expand Down Expand Up @@ -125,6 +126,19 @@ type LifecycleVersionTransition struct {
AllowSmallFile *bool `xml:AllowSmallFile,omitempty`
}

// LifecycleFilter defines the rule's Filter propery
type LifecycleFilter struct {
XMLName xml.Name `xml:"Filter"`
Not []LifecycleFilterNot `xml:"Not,omitempty"`
}

// LifecycleFilterNot defines the rule's Filter Not propery
type LifecycleFilterNot struct {
XMLName xml.Name `xml:"Not"`
Prefix string `xml:"Prefix,omitempty"` //Object prefix applicable to this exclusion rule
Tag *Tag `xml:"Tag,omitempty"` //the tags applicable to this exclusion rule
}

const iso8601DateFormat = "2006-01-02T15:04:05.000Z"
const iso8601DateFormatSecond = "2006-01-02T15:04:05Z"

Expand Down
78 changes: 78 additions & 0 deletions oss/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,81 @@ func (s *OssTypeSuite) TestLifeCycleRules(c *C) {
err = verifyLifecycleRules(rules)
c.Assert(err, IsNil)
}

func (s *OssTypeSuite) TestLifeCycleRulesWithFilter(c *C) {
xmlData := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<LifecycleConfiguration>
<Rule>
<ID>RuleID</ID>
<Prefix>logs</Prefix>
<Status>Enabled</Status>
<Filter>
<Not>
<Prefix>logs1</Prefix>
<Tag><Key>key1</Key><Value>value1</Value></Tag>
</Not>
</Filter>
<Expiration>
<Days>100</Days>
</Expiration>
<Transition>
<Days>Days</Days>
<StorageClass>Archive</StorageClass>
</Transition>
</Rule>
</LifecycleConfiguration>
`)
var res GetBucketLifecycleResult
err := xml.Unmarshal(xmlData, &res)
c.Assert(err, IsNil)
c.Assert(res.Rules[0].ID, Equals, "RuleID")
c.Assert(res.Rules[0].Filter.Not[0].Prefix, Equals, "logs1")
c.Assert(res.Rules[0].Filter.Not[0].Tag.Key, Equals, "key1")
c.Assert(res.Rules[0].Filter.Not[0].Tag.Value, Equals, "value1")

xmlData = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<LifecycleConfiguration>
<Rule>
<ID>test2</ID>
<Prefix>logs</Prefix>
<Status>Enabled</Status>
<Filter>
<Not>
<Prefix>logs-demo</Prefix>
</Not>
<Not>
<Prefix>abc/not1/</Prefix>
<Tag>
<Key>notkey1</Key>
<Value>notvalue1</Value>
</Tag>
</Not>
<Not>
<Prefix>abc/not2/</Prefix>
<Tag>
<Key>notkey2</Key>
<Value>notvalue2</Value>
</Tag>
</Not>
</Filter>
<Expiration>
<Days>100</Days>
</Expiration>
<Transition>
<Days>30</Days>
<StorageClass>Archive</StorageClass>
</Transition>
</Rule>
</LifecycleConfiguration>
`)
err = xml.Unmarshal(xmlData, &res)
c.Assert(err, IsNil)
c.Assert(res.Rules[0].ID, Equals, "test2")
c.Assert(res.Rules[0].Filter.Not[0].Prefix, Equals, "logs-demo")
c.Assert(res.Rules[0].Filter.Not[1].Prefix, Equals, "abc/not1/")
c.Assert(res.Rules[0].Filter.Not[1].Tag.Key, Equals, "notkey1")
c.Assert(res.Rules[0].Filter.Not[1].Tag.Value, Equals, "notvalue1")
c.Assert(res.Rules[0].Filter.Not[2].Prefix, Equals, "abc/not2/")
c.Assert(res.Rules[0].Filter.Not[2].Tag.Key, Equals, "notkey2")
c.Assert(res.Rules[0].Filter.Not[2].Tag.Value, Equals, "notvalue2")
}
69 changes: 58 additions & 11 deletions sample/bucket_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func BucketLifecycleSample() {
}

// Case 1: Set the lifecycle. The rule ID is rule1 and the applied objects' prefix is one and the last modified Date is before 2015/11/11
expriation := oss.LifecycleExpiration{
expiration := oss.LifecycleExpiration{
CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
}
rule1 := oss.LifecycleRule{
ID: "rule1",
Prefix: "one",
Status: "Enabled",
Expiration: &expriation,
Expiration: &expiration,
}
var rules = []oss.LifecycleRule{rule1}
err = client.SetBucketLifecycle(bucketName, rules)
Expand Down Expand Up @@ -81,7 +81,7 @@ func BucketLifecycleSample() {
}

// Case 5: Set the lifecycle. The rule ID is rule4 and the applied objects' has the tagging which prefix is four and the last modified Date is before 2015/11/11
expriation = oss.LifecycleExpiration{
expiration = oss.LifecycleExpiration{
CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
}
tag1 := oss.Tag{
Expand All @@ -97,15 +97,50 @@ func BucketLifecycleSample() {
Prefix: "four",
Status: "Enabled",
Tags: []oss.Tag{tag1, tag2},
Expiration: &expriation,
Expiration: &expiration,
}
rules = []oss.LifecycleRule{rule4}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
HandleError(err)
}

// Case 6: Set the lifecycle. The rules with amtime and return to std when visit
// Case 6: Set the lifecycle. The rule ID is filter one and Include Not exclusion conditions
expiration = oss.LifecycleExpiration{
CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
}
tag := oss.Tag{
Key: "key1",
Value: "value1",
}
filter := oss.LifecycleFilter{
Not: []oss.LifecycleFilterNot{
{
Prefix: "logs1",
Tag: &tag,
},
},
}
filterRule := oss.LifecycleRule{
ID: "filter one",
Prefix: "logs",
Status: "Enabled",
Expiration: &expiration,
Transitions: []oss.LifecycleTransition{
{
Days: 10,
StorageClass: oss.StorageIA,
},
},
Filter: &filter,
}
rules = []oss.LifecycleRule{filterRule}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
HandleError(err)
}

// Case 7: Set the lifecycle. The rules with amtime and return to std when visit
isTrue := true
isFalse := false
rule1 = oss.LifecycleRule{
Expand Down Expand Up @@ -178,8 +213,8 @@ func BucketLifecycleSample() {
HandleError(err)
}

// case 7: Set bucket's Lifecycle with xml
xml := `<?xml version="1.0" encoding="UTF-8"?>
// case 8: Set bucket's Lifecycle with xml
xmlData := `<?xml version="1.0" encoding="UTF-8"?>
<LifecycleConfiguration>
<Rule>
<ID>mtime transition1</ID>
Expand Down Expand Up @@ -237,12 +272,12 @@ func BucketLifecycleSample() {
</Rule>
</LifecycleConfiguration>
`
err = client.SetBucketLifecycleXml(bucketName, xml)
err = client.SetBucketLifecycleXml(bucketName, xmlData)
if err != nil {
HandleError(err)
}

// case 8: Get bucket's Lifecycle print info
// case 9: Get bucket's Lifecycle print info
lcRes, err := client.GetBucketLifecycle(bucketName)
if err != nil {
HandleError(err)
Expand All @@ -255,7 +290,9 @@ func BucketLifecycleSample() {
fmt.Println("Lifecycle Rule Expiration Days:", rule.Expiration.Days)
fmt.Println("Lifecycle Rule Expiration Date:", rule.Expiration.Date)
fmt.Println("Lifecycle Rule Expiration Created Before Date:", rule.Expiration.CreatedBeforeDate)
fmt.Println("Lifecycle Rule Expiration Expired Object DeleteMarker:", rule.Expiration.ExpiredObjectDeleteMarker)
if rule.Expiration.ExpiredObjectDeleteMarker != nil {
fmt.Println("Lifecycle Rule Expiration Expired Object DeleteMarker:", *rule.Expiration.ExpiredObjectDeleteMarker)
}
}

for _, tag := range rule.Tags {
Expand Down Expand Up @@ -302,10 +339,20 @@ func BucketLifecycleSample() {
if nonVersionTransition.AllowSmallFile != nil {
fmt.Println("Lifecycle Rule Non Version Allow Small File:", *nonVersionTransition.AllowSmallFile)
}

if rule.Filter != nil {
for _, filterNot := range rule.Filter.Not {
fmt.Println("Lifecycle Rule Filter Not Prefix:", filterNot.Prefix)
if filterNot.Tag != nil {
fmt.Println("Lifecycle Rule Filter Not Tag Key:", filterNot.Tag.Key)
fmt.Println("Lifecycle Rule Filter Not Tag Value:", filterNot.Tag.Value)
}
}
}
}
}

// Case 9: Delete bucket's Lifecycle
// Case 10: Delete bucket's Lifecycle
err = client.DeleteBucketLifecycle(bucketName)
if err != nil {
HandleError(err)
Expand Down

0 comments on commit 97c9cb1

Please sign in to comment.