From eedd87fb03737b122b43ec3c81e1482276f67f6c Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Wed, 11 Sep 2024 16:27:39 +0800 Subject: [PATCH 1/3] add --- .../cos/resource_tc_cos_bucket_inventory.go | 268 ++++++++++-------- .../cos/resource_tc_cos_bucket_inventory.md | 66 +++-- .../docs/r/cos_bucket_inventory.html.markdown | 42 ++- 3 files changed, 227 insertions(+), 149 deletions(-) diff --git a/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go b/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go index c294aeb528..63cd1cc410 100644 --- a/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go +++ b/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go @@ -83,9 +83,9 @@ func ResourceTencentCloudCosBucketInventory() *schema.Resource { }, }, "optional_fields": { - Type: schema.TypeList, - MaxItems: 1, - Optional: true, + Type: schema.TypeList, + MaxItems: 1, + Optional: true, Description: "Analysis items to include in the inventory result .", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -175,73 +175,96 @@ func resourceTencentCloudCosBucketInventoryCreate(d *schema.ResourceData, meta i isEnabled := d.Get("is_enabled").(string) includedObjectVersions := d.Get("included_object_versions").(string) - var filter cos.BucketInventoryFilter - if v, ok := d.GetOk("filter"); ok && len(v.([]interface{})) != 0 { - var period cos.BucketInventoryFilterPeriod - filterMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := filterMap["period"]; ok && len(v.([]interface{})) > 0 { - periodMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := periodMap["start_time"]; ok && v.(string) != "" { - vStr, err := strconv.ParseInt(v.(string), 10, 64) - if err != nil { - return err - } - period.StartTime = vStr + filter := cos.BucketInventoryFilter{} + if v, ok := d.GetOk("filter"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["prefix"]; ok { + filter.Prefix = v.(string) } - if v, ok := periodMap["end_time"]; ok && v.(string) != "" { - vStr, err := strconv.ParseInt(v.(string), 10, 64) - if err != nil { - return err + + if v, ok := dMap["period"]; ok { + for _, item := range v.([]interface{}) { + periodMap := item.(map[string]interface{}) + period := cos.BucketInventoryFilterPeriod{} + if v, ok := periodMap["start_time"]; ok && v.(string) != "" { + vStr, err := strconv.ParseInt(v.(string), 10, 64) + if err != nil { + return err + } + + period.StartTime = vStr + } + + if v, ok := periodMap["end_time"]; ok && v.(string) != "" { + vStr, err := strconv.ParseInt(v.(string), 10, 64) + if err != nil { + return err + } + + period.EndTime = vStr + } + + filter.Period = &period } - period.EndTime = vStr } - filter.Period = &period - } - if v, ok := filterMap["prefix"]; ok { - filter.Prefix = v.(string) } } - var optionalFields cos.BucketInventoryOptionalFields - if v, ok := d.GetOk("optional_fields"); ok && len(v.([]interface{})) != 0 { - optionalFieldsMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := optionalFieldsMap["fields"]; ok { - optionalFields.BucketInventoryFields = make([]string, 0) - for _, field := range v.(*schema.Set).List() { - optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string)) + + optionalFields := cos.BucketInventoryOptionalFields{} + if v, ok := d.GetOk("optional_fields"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["fields"]; ok { + fields := v.(*schema.Set).List() + for _, field := range fields { + optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string)) + } } } } - var schedule cos.BucketInventorySchedule - if v, ok := d.GetOk("schedule"); ok && len(v.([]interface{})) != 0 { - scheduleMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := scheduleMap["frequency"]; ok { - schedule.Frequency = v.(string) + schedule := cos.BucketInventorySchedule{} + if v, ok := d.GetOk("schedule"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["frequency"]; ok { + schedule.Frequency = v.(string) + } } } - var destination cos.BucketInventoryDestination - if v, ok := d.GetOk("destination"); ok && len(v.([]interface{})) != 0 { - destinationMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := destinationMap["bucket"]; ok { - destination.Bucket = v.(string) - } - if v, ok := destinationMap["account_id"]; ok { - destination.AccountId = v.(string) - } - if v, ok := destinationMap["prefix"]; ok { - destination.Prefix = v.(string) - } - if v, ok := destinationMap["format"]; ok { - destination.Format = v.(string) - } - if v, ok := destinationMap["encryption"]; ok && len(v.([]interface{})) > 0 { - encryptionMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := encryptionMap["sse_cos"]; ok { - destination.Encryption = &cos.BucketInventoryEncryption{ - SSECOS: v.(string), - } + destination := cos.BucketInventoryDestination{} + if v, ok := d.GetOk("destination"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["bucket"]; ok { + destination.Bucket = v.(string) + } + + if v, ok := dMap["account_id"]; ok { + destination.AccountId = v.(string) + } + + if v, ok := dMap["prefix"]; ok { + destination.Prefix = v.(string) + } + if v, ok := dMap["format"]; ok { + destination.Format = v.(string) + } + + if v, ok := dMap["encryption"]; ok { + for _, item := range v.([]interface{}) { + if item != nil { + dMap := item.(map[string]interface{}) + if v, ok := dMap["sse_cos"]; ok { + destination.Encryption = &cos.BucketInventoryEncryption{ + SSECOS: v.(string), + } + } + } + } } } } @@ -364,73 +387,96 @@ func resourceTencentCloudCosBucketInventoryUpdate(d *schema.ResourceData, meta i isEnabled := d.Get("is_enabled").(string) includedObjectVersions := d.Get("included_object_versions").(string) - var filter cos.BucketInventoryFilter - if v, ok := d.GetOk("filter"); ok && len(v.([]interface{})) != 0 { - var period cos.BucketInventoryFilterPeriod - filterMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := filterMap["period"]; ok && len(v.([]interface{})) > 0 { - periodMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := periodMap["start_time"]; ok && v.(string) != "" { - vStr, err := strconv.ParseInt(v.(string), 10, 64) - if err != nil { - return err - } - period.StartTime = vStr + filter := cos.BucketInventoryFilter{} + if v, ok := d.GetOk("filter"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["prefix"]; ok { + filter.Prefix = v.(string) } - if v, ok := periodMap["end_time"]; ok && v.(string) != "" { - vStr, err := strconv.ParseInt(v.(string), 10, 64) - if err != nil { - return err + + if v, ok := dMap["period"]; ok { + for _, item := range v.([]interface{}) { + periodMap := item.(map[string]interface{}) + period := cos.BucketInventoryFilterPeriod{} + if v, ok := periodMap["start_time"]; ok && v.(string) != "" { + vStr, err := strconv.ParseInt(v.(string), 10, 64) + if err != nil { + return err + } + + period.StartTime = vStr + } + + if v, ok := periodMap["end_time"]; ok && v.(string) != "" { + vStr, err := strconv.ParseInt(v.(string), 10, 64) + if err != nil { + return err + } + + period.EndTime = vStr + } + + filter.Period = &period } - period.EndTime = vStr } - filter.Period = &period - } - if v, ok := filterMap["prefix"]; ok { - filter.Prefix = v.(string) } } - var optionalFields cos.BucketInventoryOptionalFields - if v, ok := d.GetOk("optional_fields"); ok && len(v.([]interface{})) != 0 { - optionalFieldsMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := optionalFieldsMap["fields"]; ok { - optionalFields.BucketInventoryFields = make([]string, 0) - for _, field := range v.(*schema.Set).List() { - optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string)) + + optionalFields := cos.BucketInventoryOptionalFields{} + if v, ok := d.GetOk("optional_fields"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["fields"]; ok { + fields := v.(*schema.Set).List() + for _, field := range fields { + optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string)) + } } } } - var schedule cos.BucketInventorySchedule - if v, ok := d.GetOk("schedule"); ok && len(v.([]interface{})) != 0 { - scheduleMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := scheduleMap["frequency"]; ok { - schedule.Frequency = v.(string) + schedule := cos.BucketInventorySchedule{} + if v, ok := d.GetOk("schedule"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["frequency"]; ok { + schedule.Frequency = v.(string) + } } } - var destination cos.BucketInventoryDestination - if v, ok := d.GetOk("destination"); ok && len(v.([]interface{})) != 0 { - destinationMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := destinationMap["bucket"]; ok { - destination.Bucket = v.(string) - } - if v, ok := destinationMap["account_id"]; ok { - destination.AccountId = v.(string) - } - if v, ok := destinationMap["prefix"]; ok { - destination.Prefix = v.(string) - } - if v, ok := destinationMap["format"]; ok { - destination.Format = v.(string) - } - if v, ok := destinationMap["encryption"]; ok && len(v.([]interface{})) > 0 { - encryptionMap := v.([]interface{})[0].(map[string]interface{}) - if v, ok := encryptionMap["sse_cos"]; ok { - destination.Encryption = &cos.BucketInventoryEncryption{ - SSECOS: v.(string), - } + destination := cos.BucketInventoryDestination{} + if v, ok := d.GetOk("destination"); ok { + for _, item := range v.([]interface{}) { + dMap := item.(map[string]interface{}) + if v, ok := dMap["bucket"]; ok { + destination.Bucket = v.(string) + } + + if v, ok := dMap["account_id"]; ok { + destination.AccountId = v.(string) + } + if v, ok := dMap["prefix"]; ok { + destination.Prefix = v.(string) + } + + if v, ok := dMap["format"]; ok { + destination.Format = v.(string) + } + + if v, ok := dMap["encryption"]; ok { + for _, item := range v.([]interface{}) { + if item != nil { + dMap := item.(map[string]interface{}) + if v, ok := dMap["sse_cos"]; ok { + destination.Encryption = &cos.BucketInventoryEncryption{ + SSECOS: v.(string), + } + } + } + } } } } diff --git a/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.md b/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.md index 4b46ad1118..2a59846633 100644 --- a/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.md +++ b/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.md @@ -1,38 +1,54 @@ -Provides a resource to create a cos bucket_inventory +Provides a resource to create a cos bucket inventory Example Usage ```hcl -resource "tencentcloud_cos_bucket_inventory" "bucket_inventory" { - name = "test123" - bucket = "keep-test-xxxxxx" - is_enabled = "true" - included_object_versions = "Current" - optional_fields { - fields = ["Size", "ETag"] - } - filter { - period { - start_time = "1687276800" - } - } - schedule { - frequency = "Weekly" - } - destination { - bucket = "qcs::cos:ap-guangzhou::keep-test-xxxxxx" - account_id = "" - format = "CSV" - prefix = "cos_bucket_inventory" +# get user info +data "tencentcloud_user_info" "info" {} + +locals { + app_id = data.tencentcloud_user_info.info.app_id +} + +# create cos +resource "tencentcloud_cos_bucket" "example" { + bucket = "private-bucket-${local.app_id}" + acl = "private" +} + +# create cos bucket inventory +resource "tencentcloud_cos_bucket_inventory" "example" { + name = "tf-example" + bucket = tencentcloud_cos_bucket.example.id + is_enabled = "true" + included_object_versions = "Current" + + optional_fields { + fields = ["Size", "ETag"] + } + filter { + period { + start_time = "1687276800" } + } + + schedule { + frequency = "Daily" + } + + destination { + bucket = "qcs::cos:ap-guangzhou::private-bucket-1309118522" + format = "CSV" + prefix = "frontends" + } } ``` Import -cos bucket_inventory can be imported using the id, e.g. +cos bucket inventory can be imported using the id, e.g. ``` -terraform import tencentcloud_cos_bucket_inventory.bucket_inventory bucket_inventory_id -``` \ No newline at end of file +terraform import tencentcloud_cos_bucket_inventory.example private-bucket-1309118522#tf-example +``` diff --git a/website/docs/r/cos_bucket_inventory.html.markdown b/website/docs/r/cos_bucket_inventory.html.markdown index edc5a47da3..fe42498d67 100644 --- a/website/docs/r/cos_bucket_inventory.html.markdown +++ b/website/docs/r/cos_bucket_inventory.html.markdown @@ -4,38 +4,54 @@ layout: "tencentcloud" page_title: "TencentCloud: tencentcloud_cos_bucket_inventory" sidebar_current: "docs-tencentcloud-resource-cos_bucket_inventory" description: |- - Provides a resource to create a cos bucket_inventory + Provides a resource to create a cos bucket inventory --- # tencentcloud_cos_bucket_inventory -Provides a resource to create a cos bucket_inventory +Provides a resource to create a cos bucket inventory ## Example Usage ```hcl -resource "tencentcloud_cos_bucket_inventory" "bucket_inventory" { - name = "test123" - bucket = "keep-test-xxxxxx" +# get user info +data "tencentcloud_user_info" "info" {} + +locals { + app_id = data.tencentcloud_user_info.info.app_id +} + +# create cos +resource "tencentcloud_cos_bucket" "example" { + bucket = "private-bucket-${local.app_id}" + acl = "private" +} + +# create cos bucket inventory +resource "tencentcloud_cos_bucket_inventory" "example" { + name = "tf-example" + bucket = tencentcloud_cos_bucket.example.id is_enabled = "true" included_object_versions = "Current" + optional_fields { fields = ["Size", "ETag"] } + filter { period { start_time = "1687276800" } } + schedule { - frequency = "Weekly" + frequency = "Daily" } - destination { - bucket = "qcs::cos:ap-guangzhou::keep-test-xxxxxx" - account_id = "" - format = "CSV" - prefix = "cos_bucket_inventory" + destination { + bucket = "qcs::cos:ap-guangzhou::private-bucket-1309118522" + format = "CSV" + prefix = "frontends" } } ``` @@ -93,9 +109,9 @@ In addition to all arguments above, the following attributes are exported: ## Import -cos bucket_inventory can be imported using the id, e.g. +cos bucket inventory can be imported using the id, e.g. ``` -terraform import tencentcloud_cos_bucket_inventory.bucket_inventory bucket_inventory_id +terraform import tencentcloud_cos_bucket_inventory.example private-bucket-1309118522#tf-example ``` From 8ee67250086d6843a82dacdb137fa81bf3691a99 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Wed, 11 Sep 2024 16:54:43 +0800 Subject: [PATCH 2/3] add --- .changelog/2819.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2819.txt diff --git a/.changelog/2819.txt b/.changelog/2819.txt new file mode 100644 index 0000000000..47e83080b3 --- /dev/null +++ b/.changelog/2819.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_cos_bucket_inventory: Fix null pointer issue +``` From 47b3e2ab23713fd23088d52fff9aef86cf21c708 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Sat, 14 Sep 2024 11:48:39 +0800 Subject: [PATCH 3/3] add --- .../services/cos/resource_tc_cos_bucket_inventory.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go b/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go index 63cd1cc410..d8513885dc 100644 --- a/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go +++ b/tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go @@ -83,9 +83,9 @@ func ResourceTencentCloudCosBucketInventory() *schema.Resource { }, }, "optional_fields": { - Type: schema.TypeList, - MaxItems: 1, - Optional: true, + Type: schema.TypeList, + MaxItems: 1, + Optional: true, Description: "Analysis items to include in the inventory result .", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{