Skip to content

Commit 3b95340

Browse files
authored
feat: cos - non current version lifecycle (#864)
* feat: cos - non current version lifecycle * fix: cos life testcase
1 parent ca24198 commit 3b95340

File tree

7 files changed

+227
-0
lines changed

7 files changed

+227
-0
lines changed

tencentcloud/data_source_tc_cos_buckets.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,39 @@ func dataSourceTencentCloudCosBuckets() *schema.Resource {
148148
},
149149
},
150150
},
151+
"non_current_transition": {
152+
Type: schema.TypeList,
153+
Computed: true,
154+
Description: "Specifies when to transition objects of non current versions and the target storage class.",
155+
Elem: &schema.Resource{
156+
Schema: map[string]*schema.Schema{
157+
"non_current_days": {
158+
Type: schema.TypeInt,
159+
Computed: true,
160+
Description: "Number of days after non current object creation when the specific rule action takes effect.",
161+
},
162+
"storage_class": {
163+
Type: schema.TypeString,
164+
Computed: true,
165+
Description: "Specifies the storage class to which you want the non current object to transition. Available values include STANDARD, STANDARD_IA and ARCHIVE.",
166+
},
167+
},
168+
},
169+
},
170+
"non_current_expiration": {
171+
Type: schema.TypeList,
172+
Computed: true,
173+
Description: "Specifies when non current object versions shall expire.",
174+
Elem: &schema.Resource{
175+
Schema: map[string]*schema.Schema{
176+
"non_current_days": {
177+
Type: schema.TypeInt,
178+
Computed: true,
179+
Description: "Number of days after non current object creation when the specific rule action takes effect. The maximum value is 3650.",
180+
},
181+
},
182+
},
183+
},
151184
},
152185
},
153186
},

tencentcloud/data_source_tc_cos_buckets_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func TestAccTencentCloudCosBucketDataSource_full(t *testing.T) {
8080
"bucket_list.0.lifecycle_rules.0.expiration.#", "1"),
8181
resource.TestCheckResourceAttr("data.tencentcloud_cos_buckets.bucket_list",
8282
"bucket_list.0.lifecycle_rules.0.transition.#", "2"),
83+
resource.TestCheckResourceAttr("data.tencentcloud_cos_buckets.bucket_list",
84+
"bucket_list.0.lifecycle_rules.0.non_current_expiration.#", "1"),
85+
resource.TestCheckResourceAttr("data.tencentcloud_cos_buckets.bucket_list",
86+
"bucket_list.0.lifecycle_rules.0.non_current_transition.#", "2"),
8387
resource.TestCheckResourceAttr("data.tencentcloud_cos_buckets.bucket_list", "bucket_list.0.website.#", "1"),
8488
resource.TestCheckResourceAttr("data.tencentcloud_cos_buckets.bucket_list", "bucket_list.0.website.0.index_document", "index.html"),
8589
resource.TestCheckResourceAttr("data.tencentcloud_cos_buckets.bucket_list", "bucket_list.0.website.0.error_document", "error.html"),
@@ -145,6 +149,20 @@ resource "tencentcloud_cos_bucket" "bucket_full" {
145149
transition {
146150
days = 60
147151
storage_class = "ARCHIVE"
152+
}
153+
154+
non_current_expiration {
155+
non_current_days = 600
156+
}
157+
158+
non_current_transition {
159+
non_current_days = 90
160+
storage_class = "STANDARD_IA"
161+
}
162+
163+
non_current_transition {
164+
non_current_days = 180
165+
storage_class = "ARCHIVE"
148166
}
149167
}
150168

tencentcloud/resource_tc_cos_bucket.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,11 @@ func resourceTencentCloudCosBucket() *schema.Resource {
562562
Description: "A configuration of object lifecycle management (documented below).",
563563
Elem: &schema.Resource{
564564
Schema: map[string]*schema.Schema{
565+
"id": {
566+
Type: schema.TypeString,
567+
Optional: true,
568+
Description: "A unique identifier for the rule. It can be up to 255 characters.",
569+
},
565570
"filter_prefix": {
566571
Type: schema.TypeString,
567572
Required: true,
@@ -618,6 +623,45 @@ func resourceTencentCloudCosBucket() *schema.Resource {
618623
},
619624
},
620625
},
626+
"non_current_transition": {
627+
Type: schema.TypeSet,
628+
Optional: true,
629+
Set: nonCurrentTransitionHash,
630+
Description: "Specifies a period in the non current object's transitions.",
631+
Elem: &schema.Resource{
632+
Schema: map[string]*schema.Schema{
633+
"non_current_days": {
634+
Type: schema.TypeInt,
635+
Optional: true,
636+
ValidateFunc: validateIntegerMin(0),
637+
Description: "Number of days after non current object creation when the specific rule action takes effect.",
638+
},
639+
"storage_class": {
640+
Type: schema.TypeString,
641+
Required: true,
642+
ValidateFunc: validateAllowedStringValue(availableCosStorageClass),
643+
Description: "Specifies the storage class to which you want the non current object to transition. Available values include `STANDARD`, `STANDARD_IA` and `ARCHIVE`.",
644+
},
645+
},
646+
},
647+
},
648+
"non_current_expiration": {
649+
Type: schema.TypeSet,
650+
Optional: true,
651+
Set: nonCurrentExpirationHash,
652+
MaxItems: 1,
653+
Description: "Specifies when non current object versions shall expire.",
654+
Elem: &schema.Resource{
655+
Schema: map[string]*schema.Schema{
656+
"non_current_days": {
657+
Type: schema.TypeInt,
658+
Optional: true,
659+
ValidateFunc: validateIntegerMin(0),
660+
Description: "Number of days after non current object creation when the specific rule action takes effect. The maximum value is 3650.",
661+
},
662+
},
663+
},
664+
},
621665
},
622666
},
623667
},
@@ -1285,6 +1329,37 @@ func resourceTencentCloudCosBucketLifecycleUpdate(ctx context.Context, client *s
12851329

12861330
rule.Expiration = e
12871331
}
1332+
1333+
// Non Current Transitions
1334+
nonCurrentTransitions := d.Get(fmt.Sprintf("lifecycle_rules.%d.non_current_transition", i)).(*schema.Set).List()
1335+
if len(nonCurrentTransitions) > 0 {
1336+
rule.NoncurrentVersionTransitions = make([]*s3.NoncurrentVersionTransition, 0, len(transitions))
1337+
for _, transition := range nonCurrentTransitions {
1338+
transitionValue := transition.(map[string]interface{})
1339+
t := &s3.NoncurrentVersionTransition{}
1340+
if val, ok := transitionValue["non_current_days"].(int); ok && val >= 0 {
1341+
t.NoncurrentDays = aws.Int64(int64(val))
1342+
}
1343+
if val, ok := transitionValue["storage_class"].(string); ok && val != "" {
1344+
t.StorageClass = aws.String(val)
1345+
}
1346+
1347+
rule.NoncurrentVersionTransitions = append(rule.NoncurrentVersionTransitions, t)
1348+
}
1349+
}
1350+
1351+
// Non Current Expiration
1352+
nonCurrentExpirations := d.Get(fmt.Sprintf("lifecycle_rules.%d.non_current_expiration", i)).(*schema.Set).List()
1353+
if len(nonCurrentExpirations) > 0 {
1354+
nonCurrentExpiration := nonCurrentExpirations[0].(map[string]interface{})
1355+
e := &s3.NoncurrentVersionExpiration{}
1356+
1357+
if val, ok := nonCurrentExpiration["non_current_days"].(int); ok && val > 0 {
1358+
e.NoncurrentDays = aws.Int64(int64(val))
1359+
}
1360+
1361+
rule.NoncurrentVersionExpiration = e
1362+
}
12881363
rules = append(rules, rule)
12891364
}
12901365

@@ -1637,6 +1712,15 @@ func expirationHash(v interface{}) int {
16371712
return hashcode.String(buf.String())
16381713
}
16391714

1715+
func nonCurrentExpirationHash(v interface{}) int {
1716+
var buf bytes.Buffer
1717+
m := v.(map[string]interface{})
1718+
if v, ok := m["non_current_days"]; ok {
1719+
buf.WriteString(fmt.Sprintf("%d-", v.(int)))
1720+
}
1721+
return hashcode.String(buf.String())
1722+
}
1723+
16401724
func transitionHash(v interface{}) int {
16411725
var buf bytes.Buffer
16421726
m := v.(map[string]interface{})
@@ -1652,6 +1736,18 @@ func transitionHash(v interface{}) int {
16521736
return hashcode.String(buf.String())
16531737
}
16541738

1739+
func nonCurrentTransitionHash(v interface{}) int {
1740+
var buf bytes.Buffer
1741+
m := v.(map[string]interface{})
1742+
if v, ok := m["non_current_days"]; ok {
1743+
buf.WriteString(fmt.Sprintf("%d-", v.(int)))
1744+
}
1745+
if v, ok := m["storage_class"]; ok {
1746+
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
1747+
}
1748+
return hashcode.String(buf.String())
1749+
}
1750+
16551751
func getBucketReplications(d *schema.ResourceData) (role string, rules []cos.BucketReplicationRule, err error) {
16561752
role = d.Get("replica_role").(string)
16571753
replicaRules := d.Get("replica_rules").([]interface{})

tencentcloud/resource_tc_cos_bucket_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ func TestAccTencentCloudCosBucket_lifecycle(t *testing.T) {
249249
resource.TestCheckResourceAttr("tencentcloud_cos_bucket.bucket_lifecycle", "lifecycle_rules.0.transition.2000431762.storage_class", "STANDARD_IA"),
250250
resource.TestCheckResourceAttr("tencentcloud_cos_bucket.bucket_lifecycle", "lifecycle_rules.0.transition.1139768587.days", "90"),
251251
resource.TestCheckResourceAttr("tencentcloud_cos_bucket.bucket_lifecycle", "lifecycle_rules.0.transition.1139768587.storage_class", "ARCHIVE"),
252+
resource.TestCheckResourceAttr("tencentcloud_cos_bucket.bucket_lifecycle", "lifecycle_rules.0.non_current_expiration.#", "1"),
253+
resource.TestCheckResourceAttr("tencentcloud_cos_bucket.bucket_lifecycle", "lifecycle_rules.0.non_current_transition.#", "2"),
252254
),
253255
},
254256
{
@@ -688,6 +690,20 @@ resource "tencentcloud_cos_bucket" "bucket_lifecycle" {
688690
days = 90
689691
storage_class = "ARCHIVE"
690692
}
693+
694+
non_current_expiration {
695+
non_current_days = 600
696+
}
697+
698+
non_current_transition {
699+
non_current_days = 90
700+
storage_class = "STANDARD_IA"
701+
}
702+
703+
non_current_transition {
704+
non_current_days = 180
705+
storage_class = "ARCHIVE"
706+
}
691707
}
692708
}
693709
`, appid)

tencentcloud/service_tencentcloud_cos.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,30 @@ func (me *CosService) GetBucketLifecycle(ctx context.Context, bucket string) (li
355355
rule["expiration"] = schema.NewSet(expirationHash, []interface{}{e})
356356
}
357357

358+
// transition
359+
if len(value.NoncurrentVersionTransitions) > 0 {
360+
transitions := make([]interface{}, 0, len(value.NoncurrentVersionTransitions))
361+
for _, v := range value.NoncurrentVersionTransitions {
362+
t := make(map[string]interface{})
363+
if v.NoncurrentDays != nil {
364+
t["non_current_days"] = int(*v.NoncurrentDays)
365+
}
366+
if v.StorageClass != nil {
367+
t["storage_class"] = *v.StorageClass
368+
}
369+
transitions = append(transitions, t)
370+
}
371+
rule["non_current_transition"] = schema.NewSet(transitionHash, transitions)
372+
}
373+
// non current expiration
374+
if value.NoncurrentVersionExpiration != nil {
375+
e := make(map[string]interface{})
376+
if value.NoncurrentVersionExpiration.NoncurrentDays != nil {
377+
e["non_current_days"] = int(*value.NoncurrentVersionExpiration.NoncurrentDays)
378+
}
379+
rule["non_current_expiration"] = schema.NewSet(nonCurrentExpirationHash, []interface{}{e})
380+
}
381+
358382
lifecycleRules = append(lifecycleRules, rule)
359383
}
360384
}
@@ -427,6 +451,29 @@ func (me *CosService) GetDataSourceBucketLifecycle(ctx context.Context, bucket s
427451
}
428452
rule["expiration"] = []interface{}{e}
429453
}
454+
// non current transition
455+
if len(value.NoncurrentVersionTransitions) > 0 {
456+
transitions := make([]interface{}, 0, len(value.NoncurrentVersionTransitions))
457+
for _, v := range value.NoncurrentVersionTransitions {
458+
t := make(map[string]interface{})
459+
if v.NoncurrentDays != nil {
460+
t["non_current_days"] = int(*v.NoncurrentDays)
461+
}
462+
if v.StorageClass != nil {
463+
t["storage_class"] = *v.StorageClass
464+
}
465+
transitions = append(transitions, t)
466+
}
467+
rule["non_current_transition"] = transitions
468+
}
469+
// non current expiration
470+
if value.NoncurrentVersionExpiration != nil {
471+
e := make(map[string]interface{})
472+
if value.NoncurrentVersionExpiration.NoncurrentDays != nil {
473+
e["non_current_days"] = int(*value.NoncurrentVersionExpiration.NoncurrentDays)
474+
}
475+
rule["non_current_expiration"] = []interface{}{e}
476+
}
430477

431478
lifecycleRules = append(lifecycleRules, rule)
432479
}

website/docs/d/cos_buckets.html.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ In addition to all arguments above, the following attributes are exported:
4848
* `date` - Specifies the date after which you want the corresponding action to take effect.
4949
* `days` - Specifies the number of days after object creation when the specific rule action takes effect.
5050
* `filter_prefix` - Object key prefix identifying one or more objects to which the rule applies.
51+
* `non_current_expiration` - Specifies when non current object versions shall expire.
52+
* `non_current_days` - Number of days after non current object creation when the specific rule action takes effect. The maximum value is 3650.
53+
* `non_current_transition` - Specifies when to transition objects of non current versions and the target storage class.
54+
* `non_current_days` - Number of days after non current object creation when the specific rule action takes effect.
55+
* `storage_class` - Specifies the storage class to which you want the non current object to transition. Available values include STANDARD, STANDARD_IA and ARCHIVE.
5156
* `transition` - Specifies a period in the object's transitions.
5257
* `date` - Specifies the date after which you want the corresponding action to take effect.
5358
* `days` - Specifies the number of days after object creation when the specific rule action takes effect.

website/docs/r/cos_bucket.html.markdown

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,20 @@ The `lifecycle_rules` object supports the following:
271271

272272
* `filter_prefix` - (Required) Object key prefix identifying one or more objects to which the rule applies.
273273
* `expiration` - (Optional) Specifies a period in the object's expire (documented below).
274+
* `id` - (Optional) A unique identifier for the rule. It can be up to 255 characters.
275+
* `non_current_expiration` - (Optional) Specifies when non current object versions shall expire.
276+
* `non_current_transition` - (Optional) Specifies a period in the non current object's transitions.
274277
* `transition` - (Optional) Specifies a period in the object's transitions (documented below).
275278

279+
The `non_current_expiration` object supports the following:
280+
281+
* `non_current_days` - (Optional) Number of days after non current object creation when the specific rule action takes effect. The maximum value is 3650.
282+
283+
The `non_current_transition` object supports the following:
284+
285+
* `storage_class` - (Required) Specifies the storage class to which you want the non current object to transition. Available values include `STANDARD`, `STANDARD_IA` and `ARCHIVE`.
286+
* `non_current_days` - (Optional) Number of days after non current object creation when the specific rule action takes effect.
287+
276288
The `origin_domain_rules` object supports the following:
277289

278290
* `domain` - (Required) Specify domain host.

0 commit comments

Comments
 (0)