Skip to content

Commit

Permalink
Create lifecycle rules using the NewLifecycleRule method
Browse files Browse the repository at this point in the history
  • Loading branch information
hangzws committed Apr 12, 2019
1 parent 8c6b6db commit 7efdad0
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 157 deletions.
75 changes: 57 additions & 18 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,27 +655,58 @@ func (s *OssClientSuite) TestSetBucketLifecycle(c *C) {
c.Assert(err, IsNil)
}

// TestSetBucketLifecycle
// TestSetBucketLifecycleNew
func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
var bucketNameTest = bucketNamePrefix + randLowStr(6)
rule1, err := NewLifecycleRuleByCreateBeforeDate("rule1", "one", true, 2015, 11, 11, LRTExpriration)
c.Assert(err, IsNil)
rule2, err := NewLifecycleRuleByDays("rule2", "two", true, 3, LRTAbortMultiPartUpload)
c.Assert(err, IsNil)
rule3, err := NewLifecycleRuleByDays("rule3", "three", true, 3, LRTTransition, StorageIA)
c.Assert(err, IsNil)

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

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

// Set single rule
var rules = []LifecycleRule{*rule1}
expiration := LifecycleExpiration{
CreatedBeforeDate: randStr(10),
}
rule, err := NewLifecycleRule("rule1", "one", true, &expiration, nil)
c.Assert(err, IsNil)
rules := []LifecycleRule{*rule}
err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, NotNil)

abortMPU := LifecycleAbortMultipartUpload{
Days: -30,
}
rule, err = NewLifecycleRule("rule2", "two", true, nil, &abortMPU)
c.Assert(err, IsNil)
rules = []LifecycleRule{*rule}
err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, NotNil)

expiration = LifecycleExpiration{
CreatedBeforeDate: "2015-11-11T00:00:00.000Z",
}
rule1, err := NewLifecycleRule("rule1", "one", true, &expiration, nil)
c.Assert(err, IsNil)
// Double set rule
abortMPU = LifecycleAbortMultipartUpload{
Days: 30,
}
rule2, err := NewLifecycleRule("rule2", "two", true, &expiration, &abortMPU)
c.Assert(err, IsNil)

transition1 := LifecycleTransition{
Days: 3,
StorageClass: StorageIA,
}
transition2 := LifecycleTransition{
Days: 30,
StorageClass: StorageArchive,
}
rule3, err := NewLifecycleRule("rule3", "three", true, nil, &abortMPU, &transition1, &transition2)
c.Assert(err, IsNil)

// Set single rule
rules = []LifecycleRule{*rule1}
err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, IsNil)

Expand All @@ -684,27 +715,35 @@ func (s *OssClientSuite) TestSetBucketLifecycleNew(c *C) {
c.Assert(len(res.Rules), Equals, 1)
c.Assert(res.Rules[0].ID, Equals, "rule1")
c.Assert(res.Rules[0].Expiration, NotNil)
c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")

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

// Set two rules
// Set three rules
rules = []LifecycleRule{*rule1, *rule2, *rule3}
err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, IsNil)

// Eliminate effect of cache
time.Sleep(timeoutInOperation)

res, err = client.GetBucketLifecycle(bucketNameTest)
c.Assert(err, IsNil)
c.Assert(len(res.Rules), Equals, 3)
c.Assert(res.Rules[0].ID, Equals, "rule1")
c.Assert(res.Rules[0].Expiration, NotNil)
c.Assert(res.Rules[0].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
c.Assert(res.Rules[1].ID, Equals, "rule2")
c.Assert(res.Rules[1].Expiration, NotNil)
c.Assert(res.Rules[1].Expiration.CreatedBeforeDate, Equals, "2015-11-11T00:00:00.000Z")
c.Assert(res.Rules[1].AbortMultipartUpload, NotNil)
c.Assert(res.Rules[1].AbortMultipartUpload.Days, Equals, 30)
c.Assert(res.Rules[2].ID, Equals, "rule3")
c.Assert(res.Rules[2].AbortMultipartUpload, NotNil)
c.Assert(res.Rules[2].AbortMultipartUpload.Days, Equals, 30)
c.Assert(res.Rules[2].Transition, NotNil)
c.Assert(res.Rules[2].Transition[0].StorageClass, Equals, StorageIA)
c.Assert(res.Rules[2].Transition[0].Days, Equals, 3)
c.Assert(res.Rules[2].Transition[1].StorageClass, Equals, StorageArchive)
c.Assert(res.Rules[2].Transition[1].Days, Equals, 30)

err = client.DeleteBucket(bucketNameTest)
c.Assert(err, IsNil)
Expand All @@ -723,14 +762,14 @@ func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {

err = client.CreateBucket(bucketNameTest)
c.Assert(err, IsNil)
time.Sleep(timeoutInOperation)
//time.Sleep(timeoutInOperation)

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

err = client.SetBucketLifecycle(bucketNameTest, rules)
c.Assert(err, IsNil)
time.Sleep(timeoutInOperation)
//time.Sleep(timeoutInOperation)

res, err := client.GetBucketLifecycle(bucketNameTest)
c.Assert(err, IsNil)
Expand All @@ -740,12 +779,12 @@ func (s *OssClientSuite) TestDeleteBucketLifecycle(c *C) {
err = client.DeleteBucketLifecycle(bucketNameTest)
c.Assert(err, IsNil)

time.Sleep(timeoutInOperation)
//time.Sleep(timeoutInOperation)
res, err = client.GetBucketLifecycle(bucketNameTest)
c.Assert(err, NotNil)

// Eliminate effect of cache
time.Sleep(timeoutInOperation)
//time.Sleep(timeoutInOperation)

// Delete when not set
err = client.DeleteBucketLifecycle(bucketNameTest)
Expand Down
12 changes: 0 additions & 12 deletions oss/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,6 @@ const (
HTTPDelete HTTPMethod = "DELETE"
)

// LifecycleRuleType rule type of lifecycle
type LifecycleRuleType string

const (
// LRTExpriration Expriation
LRTExpriration LifecycleRuleType = "Expriation"
// LRTTransition Transition
LRTTransition LifecycleRuleType = "Transition"
// LRTAbortMultiPartUpload AbortMultiPartUpload
LRTAbortMultiPartUpload LifecycleRuleType = "AbortMultiPartUpload"
)

// HTTP headers
const (
HTTPHeaderAcceptEncoding string = "Accept-Encoding"
Expand Down
125 changes: 49 additions & 76 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,31 @@ type LifecycleRule struct {
Prefix string `xml:"Prefix"` // The object key prefix
Status string `xml:"Status"` // The rule status (enabled or not)
Expiration *LifecycleExpiration `xml:"Expiration,omitempty"` // The expiration property
Transition *LifecycleTransition `xml:"Transition,omitempty"` // The transition property
Transition []*LifecycleTransition `xml:"Transition,omitempty"` // The transition property
AbortMultipartUpload *LifecycleAbortMultipartUpload `xml:"AbortMultipartUpload,omitempty"` // The AbortMultipartUpload property
}

// LifecycleExpiration defines the rule's expiration property
type LifecycleExpiration struct {
XMLName xml.Name `xml:"Expiration"`
Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time
//Date *time.Time `xml:"Date,omitempty"` // Absolute expiration time: The expiration time in date.
//CreatedBeforeDate *time.Time `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired
Date string `xml:"Date,omitempty"` // Absolute expiration time: The expiration time in date.
CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired
XMLName xml.Name `xml:"Expiration"`
Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time
Date string `xml:"Date,omitempty"` // Absolute expiration time: The expiration time in date, not recommended
CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired
}

// LifecycleTransition defines the rule's transition propery
type LifecycleTransition struct {
XMLName xml.Name `xml:"Transition"`
Days int `xml:"Days,omitempty"` // Relative transition time: The transition time in days after the last modified time
//CreatedBeforeDate *time.Time `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be restored
XMLName xml.Name `xml:"Transition"`
Days int `xml:"Days,omitempty"` // Relative transition time: The transition time in days after the last modified time
CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired
StorageClass StorageClassType `xml:"StorageClass,omitempty"` // Specifies the target storage type
}

// LifecycleAbortMultipartUpload defines the rule's abort multipart upload propery
type LifecycleAbortMultipartUpload struct {
XMLName xml.Name `xml:"AbortMultipartUpload"`
Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time
//CreatedBeforeDate *time.Time `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired
CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired
XMLName xml.Name `xml:"AbortMultipartUpload"`
Days int `xml:"Days,omitempty"` // Relative expiration time: The expiration time in days after the last modified time
CreatedBeforeDate string `xml:"CreatedBeforeDate,omitempty"` // objects created before the date will be expired
}

const iso8601DateFormat = "2006-01-02T15:04:05.000Z"
Expand All @@ -102,80 +98,57 @@ func BuildLifecycleRuleByDate(id, prefix string, status bool, year, month, day i
Expiration: &LifecycleExpiration{Date: date}}
}

// NewLifecycleRuleByDays builds a lifecycle rule objects will expiration in days after the last modified time
func NewLifecycleRuleByDays(id, prefix string, status bool, days int, lrt LifecycleRuleType, sc ...StorageClassType) (*LifecycleRule, error) {
if len(sc) > 1 {
return nil, fmt.Errorf("invalid count of storage class type, the count should be 0 or 1")
}

var statusStr = "Enabled"
// NewLifecycleRule build a lifecycle rule
func NewLifecycleRule(id, prefix string, status bool, expiration *LifecycleExpiration, abortMPU *LifecycleAbortMultipartUpload, transitions ...*LifecycleTransition) (*LifecycleRule, error) {
statusStr := "Enabled"
if !status {
statusStr = "Disabled"
}
switch lrt {
case LRTExpriration:
if len(sc) == 1 {
return nil, fmt.Errorf("the count of storage class type should be 0")
}
return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
Expiration: &LifecycleExpiration{Days: days}}, nil
case LRTTransition:
if len(sc) == 0 {
return nil, fmt.Errorf("miss storage class of transition lifecycle rule")
}
if sc[0] != StorageIA && sc[0] != StorageArchive {
return nil, fmt.Errorf("invalid storage class of transition lifecycle rule, storage class: %v", sc)
}
return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
Transition: &LifecycleTransition{Days: days, StorageClass: sc[0]}}, nil
case LRTAbortMultiPartUpload:
if len(sc) == 1 {
return nil, fmt.Errorf("the count of storage class type should be 0")
}
return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
AbortMultipartUpload: &LifecycleAbortMultipartUpload{Days: days}}, nil
default:
return nil, fmt.Errorf("invalid type of lifecycle rule: %v", lrt)
rule := LifecycleRule{
ID: id,
Prefix: prefix,
Status: statusStr,
}
}

// NewLifecycleRuleByCreateBeforeDate builds a lifecycle rule objects created before the date will be expired.
func NewLifecycleRuleByCreateBeforeDate(id, prefix string, status bool, year, month, day int, lrt LifecycleRuleType, sc ...StorageClassType) (*LifecycleRule, error) {
if len(sc) > 1 {
return nil, fmt.Errorf("invalid count of storage class type, the cound should be 0 or 1")
if expiration != nil {
if (expiration.Days != 0 && expiration.CreatedBeforeDate != "") || (expiration.Days == 0 && expiration.CreatedBeforeDate == "") {
return nil, fmt.Errorf("invalid expiration lifecycle, must be set one of CreatedBeforeDate and Days")
}
rule.Expiration = expiration
}

var statusStr = "Enabled"
if !status {
statusStr = "Disabled"
if abortMPU != nil {
if (abortMPU.Days != 0 && abortMPU.CreatedBeforeDate != "") || (abortMPU.Days == 0 && abortMPU.CreatedBeforeDate == "") {
return nil, fmt.Errorf("invalid abort multipart upload lifecycle, must be set one of CreatedBeforeDate and Days")
}
rule.AbortMultipartUpload = abortMPU
}

date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC).Format(iso8601DateFormat)
switch lrt {
case LRTExpriration:
if len(sc) == 1 {
return nil, fmt.Errorf("the count of storage class type should be 0")
}
return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
Expiration: &LifecycleExpiration{CreatedBeforeDate: date}}, nil
case LRTTransition:
if len(sc) == 0 {
return nil, fmt.Errorf("miss storage class of transition lifecycle rule")
}
if sc[0] != StorageIA && sc[0] != StorageArchive {
return nil, fmt.Errorf("invalid storage class of transition lifecycle rule, storage class: %v", sc)
if len(transitions) > 0 {
if len(transitions) > 2 {
return nil, fmt.Errorf("invalid count of transition lifecycles, the count must than less than 3")
}
return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
Transition: &LifecycleTransition{CreatedBeforeDate: date, StorageClass: sc[0]}}, nil
case LRTAbortMultiPartUpload:
if len(sc) == 1 {
return nil, fmt.Errorf("the count of storage class type should be 0")

for _, transition := range transitions {
if transition == nil {
return nil, fmt.Errorf("invalid transitions, there is a transition not be initiated")
}
if (transition.Days != 0 && transition.CreatedBeforeDate != "") || (transition.Days == 0 && transition.CreatedBeforeDate == "") {
return nil, fmt.Errorf("invalid transition lifecycle, must be set one of CreatedBeforeDate and Days")
}
if transition.StorageClass != StorageIA && transition.StorageClass != StorageArchive {
return nil, fmt.Errorf("invalid transition lifecylce, the value of storage class must be IA or Archive")
}
}
return &LifecycleRule{ID: id, Prefix: prefix, Status: statusStr,
AbortMultipartUpload: &LifecycleAbortMultipartUpload{CreatedBeforeDate: date}}, nil
default:
return nil, fmt.Errorf("invalid type of lifecycle rule: %v", lrt)

rule.Transition = transitions
}

if rule.Expiration == nil && rule.AbortMultipartUpload == nil && len(rule.Transition) == 0 {
return nil, fmt.Errorf("invalid lifecycle rule, must be set one of Expiration, AbortMultipartUpload and Transition")
}

return &rule, nil
}

// GetBucketLifecycleResult defines GetBucketLifecycle's result object
Expand Down
Loading

0 comments on commit 7efdad0

Please sign in to comment.