Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create lifecycle rules to cleanup old versions #2611

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
83 changes: 83 additions & 0 deletions alicloud/resource_alicloud_oss_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ func resourceAlicloudOssBucket() *schema.Resource {
},
},
},
"noncurrent_version_expiration": {
Type: schema.TypeSet,
MaxItems: 1,
Optional: true,
Set: expirationHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"days": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},
},
},
},
"transitions": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -204,6 +219,31 @@ func resourceAlicloudOssBucket() *schema.Resource {
},
},
},
"noncurrent_version_transition": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个接口实现有问题。
一个rule 可以设置多个 noncurrent_version_transition 条件。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @huiguangjun , it seems like the go sdk currently does not support the above said configuration. I have made changes as per the go sdk. Here is the goDoc for Lifecycle Rules struct - https://godoc.org/github.com/aliyun/aliyun-oss-go-sdk/oss#LifecycleRule

The LifecycleRule struct currently takes in 1 object for NonVersionTransition .

Also, it would be great if you continue conversation in english :) ! I have to translate the above comment :P

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huiguangjun any updates on this ? Let me know your thoughts.

Type: schema.TypeSet,
MaxItems: 1,
Optional: true,
Set: transitionsHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"days": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},
"storage_class": {
Type: schema.TypeString,
Default: oss.StorageStandard,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(oss.StorageStandard),
string(oss.StorageIA),
string(oss.StorageArchive),
}, false),
},
},
},
},
},
},
MaxItems: 1000,
Expand Down Expand Up @@ -523,6 +563,12 @@ func resourceAlicloudOssBucketRead(d *schema.ResourceData, meta interface{}) err
e["days"] = int(lifecycleRule.Expiration.Days)
rule["expiration"] = schema.NewSet(expirationHash, []interface{}{e})
}
//noncurrent_version_expiration
if lifecycleRule.NonVersionExpiration != nil {
e := make(map[string]interface{})
e["days"] = int(lifecycleRule.NonVersionExpiration.NoncurrentDays)
rule["noncurrent_version_expiration"] = schema.NewSet(expirationHash, []interface{}{e})
}
// transitions
if len(lifecycleRule.Transitions) != 0 {
var eSli []interface{}
Expand All @@ -541,6 +587,13 @@ func resourceAlicloudOssBucketRead(d *schema.ResourceData, meta interface{}) err
}
rule["transitions"] = schema.NewSet(transitionsHash, eSli)
}
//noncurrent_version_transition
if lifecycleRule.NonVersionTransition != nil {
e := make(map[string]interface{})
e["days"] = int(lifecycleRule.NonVersionTransition.NoncurrentDays)
e["storage_class"] = string(lifecycleRule.NonVersionTransition.StorageClass)
rule["noncurrent_version_transition"] = schema.NewSet(transitionsHash, []interface{}{e})
}

lrules = append(lrules, rule)
}
Expand Down Expand Up @@ -932,6 +985,18 @@ func resourceAlicloudOssBucketLifecycleRuleUpdate(client *connectivity.AliyunCli
rule.Expiration = &i
}

//NoncurrentVersionExpiration
nc_expiration := d.Get(fmt.Sprintf("lifecycle_rule.%d.noncurrent_version_expiration", i)).(*schema.Set).List()
if len(nc_expiration) > 0 && nc_expiration[0] != nil {
e := nc_expiration[0].(map[string]interface{})
i := oss.LifecycleVersionExpiration{}

if val, ok := e["days"].(int); ok && val > 0 {
i.NoncurrentDays = val
rule.NonVersionExpiration = &i
}
}

// Transitions
transitions := d.Get(fmt.Sprintf("lifecycle_rule.%d.transitions", i)).(*schema.Set).List()
if len(transitions) > 0 {
Expand Down Expand Up @@ -960,6 +1025,24 @@ func resourceAlicloudOssBucketLifecycleRuleUpdate(client *connectivity.AliyunCli
}
}

// NoncurrentVersionTransition
nc_transition := d.Get(fmt.Sprintf("lifecycle_rule.%d.noncurrent_version_transition", i)).(*schema.Set).List()
if len(nc_transition) > 0 {
e := nc_transition[0].(map[string]interface{})
i := oss.LifecycleVersionTransition{}
valDays := e["days"].(int)
valStorageClass := e["storage_class"].(string)

if (valDays >= 0) || (valStorageClass == "") {
WrapError(Error("'Days' must be set greater than 0. 'storage_class' must be set."))
}

i.NoncurrentDays = valDays
i.StorageClass = oss.StorageClassType(valStorageClass)

rule.NonVersionTransition = &i
}

rules = append(rules, rule)
}

Expand Down
52 changes: 51 additions & 1 deletion alicloud/resource_alicloud_oss_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ func TestAccAlicloudOssBucketBasic(t *testing.T) {
"created_before_date": "2021-11-11",
"storage_class": "Archive",
}))
hashcode7 := strconv.Itoa(transitionsHash(map[string]interface{}{
"days": 3,
}))
hashcode8 := strconv.Itoa(transitionsHash(map[string]interface{}{
"days": 30,
"storage_class": "Archive",
}))
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
Expand Down Expand Up @@ -310,11 +317,32 @@ func TestAccAlicloudOssBucketBasic(t *testing.T) {
},
},
},
{
"id": "rule5",
"prefix": "path5/",
"enabled": "true",
"noncurrent_version_expiration": []map[string]interface{}{
{
"days": "3",
},
},
},
{
"id": "rule6",
"prefix": "path6/",
"enabled": "true",
"noncurrent_version_transition": []map[string]interface{}{
{
"days": "30",
"storage_class": "Archive",
},
},
},
},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"lifecycle_rule.#": "4",
"lifecycle_rule.#": "6",
"lifecycle_rule.0.id": "rule1",
"lifecycle_rule.0.prefix": "path1/",
"lifecycle_rule.0.enabled": "true",
Expand All @@ -339,6 +367,17 @@ func TestAccAlicloudOssBucketBasic(t *testing.T) {
"lifecycle_rule.3.transitions." + hashcode5 + ".storage_class": string(oss.StorageIA),
"lifecycle_rule.3.transitions." + hashcode6 + ".created_before_date": "2021-11-11",
"lifecycle_rule.3.transitions." + hashcode6 + ".storage_class": string(oss.StorageArchive),

"lifecycle_rule.4.id": "rule5",
"lifecycle_rule.4.prefix": "path5/",
"lifecycle_rule.4.enabled": "true",
"lifecycle_rule.4.noncurrent_version_expiration." + hashcode7 + ".days": "3",

"lifecycle_rule.5.id": "rule6",
"lifecycle_rule.5.prefix": "path6/",
"lifecycle_rule.5.enabled": "true",
"lifecycle_rule.5.noncurrent_version_transition." + hashcode8 + ".days": "30",
"lifecycle_rule.5.noncurrent_version_transition." + hashcode8 + ".storage_class": string(oss.StorageArchive),
}),
),
},
Expand Down Expand Up @@ -429,6 +468,17 @@ func TestAccAlicloudOssBucketBasic(t *testing.T) {
"lifecycle_rule.3.transitions." + hashcode6 + ".created_before_date": REMOVEKEY,
"lifecycle_rule.3.transitions." + hashcode6 + ".storage_class": REMOVEKEY,

"lifecycle_rule.4.id": REMOVEKEY,
"lifecycle_rule.4.prefix": REMOVEKEY,
"lifecycle_rule.4.enabled": REMOVEKEY,
"lifecycle_rule.4.noncurrent_version_expiration." + hashcode7 + ".days": REMOVEKEY,

"lifecycle_rule.5.id": REMOVEKEY,
"lifecycle_rule.5.prefix": REMOVEKEY,
"lifecycle_rule.5.enabled": REMOVEKEY,
"lifecycle_rule.5.noncurrent_version_transition." + hashcode8 + ".days": REMOVEKEY,
"lifecycle_rule.5.noncurrent_version_transition." + hashcode8 + ".storage_class": REMOVEKEY,

"tags.%": "0",
"tags.key1-update": REMOVEKEY,
"tags.Key2-update": REMOVEKEY,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/aliyun/aliyun-datahub-sdk-go v0.0.0-20180929121038-c1c85baca7c0
github.com/aliyun/aliyun-log-go-sdk v0.1.6
github.com/aliyun/aliyun-mns-go-sdk v0.0.0-20191115025756-088ba95470f4
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190528142024-f8d6d645dc4b
github.com/aliyun/aliyun-oss-go-sdk v2.1.2+incompatible
github.com/aliyun/aliyun-tablestore-go-sdk v4.1.3+incompatible
github.com/aliyun/fc-go-sdk v0.0.0-20190326033901-db3e654c23d6
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
Expand Down Expand Up @@ -34,6 +34,7 @@ require (
github.com/valyala/fasthttp v0.0.0-20180927122258-761788a34bb6 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/aliyun/aliyun-mns-go-sdk v0.0.0-20191115025756-088ba95470f4/go.mod h1
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190103054945-8205d1f41e70/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190528142024-f8d6d645dc4b h1:z6MCjm8mbUthTytmIVw5l7RiFF4GltKgZpNL5n84O/k=
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190528142024-f8d6d645dc4b/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/aliyun/aliyun-oss-go-sdk v2.1.2+incompatible h1:kB3yKV/bBZLX5Rm8aOaFsM6M7ZmyOiUgAElhfn/R27c=
github.com/aliyun/aliyun-oss-go-sdk v2.1.2+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/aliyun/aliyun-tablestore-go-sdk v4.1.2+incompatible h1:ABQ7FF+IxSFHDMOTtjCfmMDMHiCq6EsAoCV/9sFinaM=
github.com/aliyun/aliyun-tablestore-go-sdk v4.1.2+incompatible/go.mod h1:LDQHRZylxvcg8H7wBIDfvO5g/cy4/sz1iucBlc2l3Jw=
github.com/aliyun/aliyun-tablestore-go-sdk v4.1.3+incompatible h1:UbBDubZ5xDDaB50NvikAEPxz9dNG4+JVgIvV4y3dvFM=
Expand Down Expand Up @@ -547,6 +549,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
14 changes: 14 additions & 0 deletions vendor/github.com/aliyun/aliyun-oss-go-sdk/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 72 additions & 12 deletions vendor/github.com/aliyun/aliyun-oss-go-sdk/oss/auth.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading