Skip to content

fix(cos): [119668858] Fix null pointer issue of tencentcloud_cos_bucket_inventory #2819

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

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2819.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_cos_bucket_inventory: Fix null pointer issue
```
262 changes: 154 additions & 108 deletions tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
}
}
}
}
Expand Down Expand Up @@ -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),
}
}
}
}
}
}
}
Expand Down
66 changes: 41 additions & 25 deletions tencentcloud/services/cos/resource_tc_cos_bucket_inventory.md
Original file line number Diff line number Diff line change
@@ -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
```
terraform import tencentcloud_cos_bucket_inventory.example private-bucket-1309118522#tf-example
```
Loading
Loading