Skip to content

Commit

Permalink
fix: lifecycle config can not support multiple LifecycleVersionTransi…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
taowei.wtw authored and kkuai committed Jul 24, 2020
1 parent 95f72e6 commit 1892a71
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 21 deletions.
8 changes: 8 additions & 0 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,14 @@ func (client Client) GetBucketLifecycle(bucketName string) (GetBucketLifecycleRe
defer resp.Body.Close()

err = xmlUnmarshal(resp.Body, &out)

// NonVersionTransition is not suggested to use
// to keep compatible
for k, rule := range out.Rules {
if len(rule.NonVersionTransitions) > 0 {
out.Rules[k].NonVersionTransition = &(out.Rules[k].NonVersionTransitions[0])
}
}
return out, err
}

Expand Down
98 changes: 98 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,104 @@ func (s *OssClientSuite) TestSetBucketLifecycleAboutVersionObject(c *C) {
c.Assert(err, IsNil)
}

// TestSetBucketLifecycleAboutVersionObject
func (s *OssClientSuite) TestSetBucketLifecycleAboutVersionObjectError(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)

deleteMark := true
expiration := LifecycleExpiration{
ExpiredObjectDeleteMarker: &deleteMark,
}

versionExpiration := LifecycleVersionExpiration{
NoncurrentDays: 20,
}

versionTransition := LifecycleVersionTransition{
NoncurrentDays: 10,
StorageClass: "IA",
}

// NonVersionTransition and NonVersionTransitions can not both have value
rule := LifecycleRule{
Status: "Enabled",
Expiration: &expiration,
NonVersionExpiration: &versionExpiration,
NonVersionTransition: &versionTransition,
NonVersionTransitions: []LifecycleVersionTransition{versionTransition},
}
rules := []LifecycleRule{rule}

err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, NotNil)

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

// TestSetBucketLifecycleAboutVersionObject
func (s *OssClientSuite) TestSetBucketLifecycleAboutVersionObjectNew(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)

deleteMark := true
expiration := LifecycleExpiration{
ExpiredObjectDeleteMarker: &deleteMark,
}

versionExpiration := LifecycleVersionExpiration{
NoncurrentDays: 20,
}

versionTransition1 := LifecycleVersionTransition{
NoncurrentDays: 10,
StorageClass: "IA",
}

versionTransition2 := LifecycleVersionTransition{
NoncurrentDays: 20,
StorageClass: "ColdArchive",
}

rule := LifecycleRule{
Status: "Enabled",
Expiration: &expiration,
NonVersionExpiration: &versionExpiration,
NonVersionTransitions: []LifecycleVersionTransition{versionTransition1, versionTransition2},
}
rules := []LifecycleRule{rule}

err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, IsNil)

res, err := client.GetBucketLifecycle(bucketNameTest)
c.Assert(err, IsNil)

c.Assert(res.Rules[0].Expiration, NotNil)
c.Assert(res.Rules[0].Expiration.Days, Equals, 0)
c.Assert(res.Rules[0].Expiration.Date, Equals, "")
c.Assert(*(res.Rules[0].Expiration.ExpiredObjectDeleteMarker), Equals, true)

c.Assert(res.Rules[0].NonVersionExpiration.NoncurrentDays, Equals, 20)
c.Assert(res.Rules[0].NonVersionTransition.NoncurrentDays, Equals, 10)
c.Assert(res.Rules[0].NonVersionTransition.StorageClass, Equals, StorageClassType("IA"))
c.Assert(len(res.Rules[0].NonVersionTransitions),Equals, 2)

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

// TestDeleteBucketLifecycle
func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
var bucketNameTest = bucketNamePrefix + RandLowStr(6)
Expand Down
2 changes: 1 addition & 1 deletion oss/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const (

NullVersion = "null"

Version = "v2.1.3" // Go SDK version
Version = "v2.1.4" // Go SDK version
)

// FrameType
Expand Down
37 changes: 20 additions & 17 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ type LifecycleConfiguration struct {

// LifecycleRule defines Lifecycle rules
type LifecycleRule struct {
XMLName xml.Name `xml:"Rule"`
ID string `xml:"ID,omitempty"` // The rule ID
Prefix string `xml:"Prefix"` // The object key prefix
Status string `xml:"Status"` // The rule status (enabled or not)
Tags []Tag `xml:"Tag,omitempty"` // the tags property
Expiration *LifecycleExpiration `xml:"Expiration,omitempty"` // The expiration property
Transitions []LifecycleTransition `xml:"Transition,omitempty"` // The transition property
AbortMultipartUpload *LifecycleAbortMultipartUpload `xml:"AbortMultipartUpload,omitempty"` // The AbortMultipartUpload property
NonVersionExpiration *LifecycleVersionExpiration `xml:"NoncurrentVersionExpiration,omitempty"`
NonVersionTransition *LifecycleVersionTransition `xml:"NoncurrentVersionTransition,omitempty"`
XMLName xml.Name `xml:"Rule"`
ID string `xml:"ID,omitempty"` // The rule ID
Prefix string `xml:"Prefix"` // The object key prefix
Status string `xml:"Status"` // The rule status (enabled or not)
Tags []Tag `xml:"Tag,omitempty"` // the tags property
Expiration *LifecycleExpiration `xml:"Expiration,omitempty"` // The expiration property
Transitions []LifecycleTransition `xml:"Transition,omitempty"` // The transition property
AbortMultipartUpload *LifecycleAbortMultipartUpload `xml:"AbortMultipartUpload,omitempty"` // The AbortMultipartUpload property
NonVersionExpiration *LifecycleVersionExpiration `xml:"NoncurrentVersionExpiration,omitempty"`
NonVersionTransition *LifecycleVersionTransition `xml:"-"` // NonVersionTransition is not suggested to use
NonVersionTransitions []LifecycleVersionTransition `xml:"NoncurrentVersionTransition,omitempty"`
}

// LifecycleExpiration defines the rule's expiration property
Expand Down Expand Up @@ -121,7 +122,7 @@ func verifyLifecycleRules(rules []LifecycleRule) error {
if len(rules) == 0 {
return fmt.Errorf("invalid rules, the length of rules is zero")
}
for _, rule := range rules {
for k, rule := range rules {
if rule.Status != "Enabled" && rule.Status != "Disabled" {
return fmt.Errorf("invalid rule, the value of status must be Enabled or Disabled")
}
Expand All @@ -135,17 +136,19 @@ func verifyLifecycleRules(rules []LifecycleRule) error {

transitions := rule.Transitions
if len(transitions) > 0 {
if len(transitions) > 2 {
return fmt.Errorf("invalid count of transition lifecycles, the count must than less than 3")
}

for _, transition := range transitions {
if (transition.Days != 0 && transition.CreatedBeforeDate != "") || (transition.Days == 0 && transition.CreatedBeforeDate == "") {
return fmt.Errorf("invalid transition lifecycle, must be set one of CreatedBeforeDate and Days")
}
}
} else if rule.Expiration == nil && abortMPU == nil && rule.NonVersionExpiration == nil && rule.NonVersionTransition == nil {
return fmt.Errorf("invalid rule, must set one of Expiration, AbortMultipartUplaod, NonVersionExpiration, NonVersionTransition and Transitions")
}

// NonVersionTransition is not suggested to use
// to keep compatible
if rule.NonVersionTransition != nil && len(rule.NonVersionTransitions) > 0 {
return fmt.Errorf("NonVersionTransition and NonVersionTransitions cannot both have values")
} else if rule.NonVersionTransition != nil {
rules[k].NonVersionTransitions = append(rules[k].NonVersionTransitions, *rule.NonVersionTransition)
}
}

Expand Down
6 changes: 3 additions & 3 deletions oss/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (s *OssTypeSuite) TestValidateLifecleRules(c *C) {
}
rules = []LifecycleRule{rule}
err = verifyLifecycleRules(rules)
c.Assert(err, NotNil)
c.Assert(err, IsNil)

transition1 := LifecycleTransition{
Days: 30,
Expand All @@ -257,7 +257,7 @@ func (s *OssTypeSuite) TestValidateLifecleRules(c *C) {
}
rules = []LifecycleRule{rule}
err = verifyLifecycleRules(rules)
c.Assert(err, NotNil)
c.Assert(err, IsNil)

rule = LifecycleRule{
ID: "ruleID",
Expand All @@ -266,7 +266,7 @@ func (s *OssTypeSuite) TestValidateLifecleRules(c *C) {
}
rules = []LifecycleRule{rule}
err = verifyLifecycleRules(rules)
c.Assert(err, NotNil)
c.Assert(err, IsNil)

rules = []LifecycleRule{}
err1 := verifyLifecycleRules(rules)
Expand Down

0 comments on commit 1892a71

Please sign in to comment.