Skip to content

Commit ae04d0a

Browse files
authored
fix(cos): [119668858] Fix null pointer issue of tencentcloud_cos_bucket_inventory (#2819)
* add * add * add
1 parent 0049df8 commit ae04d0a

File tree

4 files changed

+227
-146
lines changed

4 files changed

+227
-146
lines changed

.changelog/2819.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cos_bucket_inventory: Fix null pointer issue
3+
```

tencentcloud/services/cos/resource_tc_cos_bucket_inventory.go

Lines changed: 154 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -175,73 +175,96 @@ func resourceTencentCloudCosBucketInventoryCreate(d *schema.ResourceData, meta i
175175
isEnabled := d.Get("is_enabled").(string)
176176
includedObjectVersions := d.Get("included_object_versions").(string)
177177

178-
var filter cos.BucketInventoryFilter
179-
if v, ok := d.GetOk("filter"); ok && len(v.([]interface{})) != 0 {
180-
var period cos.BucketInventoryFilterPeriod
181-
filterMap := v.([]interface{})[0].(map[string]interface{})
182-
if v, ok := filterMap["period"]; ok && len(v.([]interface{})) > 0 {
183-
periodMap := v.([]interface{})[0].(map[string]interface{})
184-
if v, ok := periodMap["start_time"]; ok && v.(string) != "" {
185-
vStr, err := strconv.ParseInt(v.(string), 10, 64)
186-
if err != nil {
187-
return err
188-
}
189-
period.StartTime = vStr
178+
filter := cos.BucketInventoryFilter{}
179+
if v, ok := d.GetOk("filter"); ok {
180+
for _, item := range v.([]interface{}) {
181+
dMap := item.(map[string]interface{})
182+
if v, ok := dMap["prefix"]; ok {
183+
filter.Prefix = v.(string)
190184
}
191-
if v, ok := periodMap["end_time"]; ok && v.(string) != "" {
192-
vStr, err := strconv.ParseInt(v.(string), 10, 64)
193-
if err != nil {
194-
return err
185+
186+
if v, ok := dMap["period"]; ok {
187+
for _, item := range v.([]interface{}) {
188+
periodMap := item.(map[string]interface{})
189+
period := cos.BucketInventoryFilterPeriod{}
190+
if v, ok := periodMap["start_time"]; ok && v.(string) != "" {
191+
vStr, err := strconv.ParseInt(v.(string), 10, 64)
192+
if err != nil {
193+
return err
194+
}
195+
196+
period.StartTime = vStr
197+
}
198+
199+
if v, ok := periodMap["end_time"]; ok && v.(string) != "" {
200+
vStr, err := strconv.ParseInt(v.(string), 10, 64)
201+
if err != nil {
202+
return err
203+
}
204+
205+
period.EndTime = vStr
206+
}
207+
208+
filter.Period = &period
195209
}
196-
period.EndTime = vStr
197210
}
198-
filter.Period = &period
199-
}
200-
if v, ok := filterMap["prefix"]; ok {
201-
filter.Prefix = v.(string)
202211
}
203212
}
204-
var optionalFields cos.BucketInventoryOptionalFields
205-
if v, ok := d.GetOk("optional_fields"); ok && len(v.([]interface{})) != 0 {
206-
optionalFieldsMap := v.([]interface{})[0].(map[string]interface{})
207-
if v, ok := optionalFieldsMap["fields"]; ok {
208-
optionalFields.BucketInventoryFields = make([]string, 0)
209-
for _, field := range v.(*schema.Set).List() {
210-
optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string))
213+
214+
optionalFields := cos.BucketInventoryOptionalFields{}
215+
if v, ok := d.GetOk("optional_fields"); ok {
216+
for _, item := range v.([]interface{}) {
217+
dMap := item.(map[string]interface{})
218+
if v, ok := dMap["fields"]; ok {
219+
fields := v.(*schema.Set).List()
220+
for _, field := range fields {
221+
optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string))
222+
}
211223
}
212224
}
213225
}
214226

215-
var schedule cos.BucketInventorySchedule
216-
if v, ok := d.GetOk("schedule"); ok && len(v.([]interface{})) != 0 {
217-
scheduleMap := v.([]interface{})[0].(map[string]interface{})
218-
if v, ok := scheduleMap["frequency"]; ok {
219-
schedule.Frequency = v.(string)
227+
schedule := cos.BucketInventorySchedule{}
228+
if v, ok := d.GetOk("schedule"); ok {
229+
for _, item := range v.([]interface{}) {
230+
dMap := item.(map[string]interface{})
231+
if v, ok := dMap["frequency"]; ok {
232+
schedule.Frequency = v.(string)
233+
}
220234
}
221235
}
222236

223-
var destination cos.BucketInventoryDestination
224-
if v, ok := d.GetOk("destination"); ok && len(v.([]interface{})) != 0 {
225-
destinationMap := v.([]interface{})[0].(map[string]interface{})
226-
if v, ok := destinationMap["bucket"]; ok {
227-
destination.Bucket = v.(string)
228-
}
229-
if v, ok := destinationMap["account_id"]; ok {
230-
destination.AccountId = v.(string)
231-
}
232-
if v, ok := destinationMap["prefix"]; ok {
233-
destination.Prefix = v.(string)
234-
}
235-
if v, ok := destinationMap["format"]; ok {
236-
destination.Format = v.(string)
237-
}
238-
if v, ok := destinationMap["encryption"]; ok && len(v.([]interface{})) > 0 {
239-
encryptionMap := v.([]interface{})[0].(map[string]interface{})
240-
if v, ok := encryptionMap["sse_cos"]; ok {
241-
destination.Encryption = &cos.BucketInventoryEncryption{
242-
SSECOS: v.(string),
243-
}
237+
destination := cos.BucketInventoryDestination{}
238+
if v, ok := d.GetOk("destination"); ok {
239+
for _, item := range v.([]interface{}) {
240+
dMap := item.(map[string]interface{})
241+
if v, ok := dMap["bucket"]; ok {
242+
destination.Bucket = v.(string)
243+
}
244244

245+
if v, ok := dMap["account_id"]; ok {
246+
destination.AccountId = v.(string)
247+
}
248+
249+
if v, ok := dMap["prefix"]; ok {
250+
destination.Prefix = v.(string)
251+
}
252+
253+
if v, ok := dMap["format"]; ok {
254+
destination.Format = v.(string)
255+
}
256+
257+
if v, ok := dMap["encryption"]; ok {
258+
for _, item := range v.([]interface{}) {
259+
if item != nil {
260+
dMap := item.(map[string]interface{})
261+
if v, ok := dMap["sse_cos"]; ok {
262+
destination.Encryption = &cos.BucketInventoryEncryption{
263+
SSECOS: v.(string),
264+
}
265+
}
266+
}
267+
}
245268
}
246269
}
247270
}
@@ -364,73 +387,96 @@ func resourceTencentCloudCosBucketInventoryUpdate(d *schema.ResourceData, meta i
364387
isEnabled := d.Get("is_enabled").(string)
365388
includedObjectVersions := d.Get("included_object_versions").(string)
366389

367-
var filter cos.BucketInventoryFilter
368-
if v, ok := d.GetOk("filter"); ok && len(v.([]interface{})) != 0 {
369-
var period cos.BucketInventoryFilterPeriod
370-
filterMap := v.([]interface{})[0].(map[string]interface{})
371-
if v, ok := filterMap["period"]; ok && len(v.([]interface{})) > 0 {
372-
periodMap := v.([]interface{})[0].(map[string]interface{})
373-
if v, ok := periodMap["start_time"]; ok && v.(string) != "" {
374-
vStr, err := strconv.ParseInt(v.(string), 10, 64)
375-
if err != nil {
376-
return err
377-
}
378-
period.StartTime = vStr
390+
filter := cos.BucketInventoryFilter{}
391+
if v, ok := d.GetOk("filter"); ok {
392+
for _, item := range v.([]interface{}) {
393+
dMap := item.(map[string]interface{})
394+
if v, ok := dMap["prefix"]; ok {
395+
filter.Prefix = v.(string)
379396
}
380-
if v, ok := periodMap["end_time"]; ok && v.(string) != "" {
381-
vStr, err := strconv.ParseInt(v.(string), 10, 64)
382-
if err != nil {
383-
return err
397+
398+
if v, ok := dMap["period"]; ok {
399+
for _, item := range v.([]interface{}) {
400+
periodMap := item.(map[string]interface{})
401+
period := cos.BucketInventoryFilterPeriod{}
402+
if v, ok := periodMap["start_time"]; ok && v.(string) != "" {
403+
vStr, err := strconv.ParseInt(v.(string), 10, 64)
404+
if err != nil {
405+
return err
406+
}
407+
408+
period.StartTime = vStr
409+
}
410+
411+
if v, ok := periodMap["end_time"]; ok && v.(string) != "" {
412+
vStr, err := strconv.ParseInt(v.(string), 10, 64)
413+
if err != nil {
414+
return err
415+
}
416+
417+
period.EndTime = vStr
418+
}
419+
420+
filter.Period = &period
384421
}
385-
period.EndTime = vStr
386422
}
387-
filter.Period = &period
388-
}
389-
if v, ok := filterMap["prefix"]; ok {
390-
filter.Prefix = v.(string)
391423
}
392424
}
393-
var optionalFields cos.BucketInventoryOptionalFields
394-
if v, ok := d.GetOk("optional_fields"); ok && len(v.([]interface{})) != 0 {
395-
optionalFieldsMap := v.([]interface{})[0].(map[string]interface{})
396-
if v, ok := optionalFieldsMap["fields"]; ok {
397-
optionalFields.BucketInventoryFields = make([]string, 0)
398-
for _, field := range v.(*schema.Set).List() {
399-
optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string))
425+
426+
optionalFields := cos.BucketInventoryOptionalFields{}
427+
if v, ok := d.GetOk("optional_fields"); ok {
428+
for _, item := range v.([]interface{}) {
429+
dMap := item.(map[string]interface{})
430+
if v, ok := dMap["fields"]; ok {
431+
fields := v.(*schema.Set).List()
432+
for _, field := range fields {
433+
optionalFields.BucketInventoryFields = append(optionalFields.BucketInventoryFields, field.(string))
434+
}
400435
}
401436
}
402437
}
403438

404-
var schedule cos.BucketInventorySchedule
405-
if v, ok := d.GetOk("schedule"); ok && len(v.([]interface{})) != 0 {
406-
scheduleMap := v.([]interface{})[0].(map[string]interface{})
407-
if v, ok := scheduleMap["frequency"]; ok {
408-
schedule.Frequency = v.(string)
439+
schedule := cos.BucketInventorySchedule{}
440+
if v, ok := d.GetOk("schedule"); ok {
441+
for _, item := range v.([]interface{}) {
442+
dMap := item.(map[string]interface{})
443+
if v, ok := dMap["frequency"]; ok {
444+
schedule.Frequency = v.(string)
445+
}
409446
}
410447
}
411448

412-
var destination cos.BucketInventoryDestination
413-
if v, ok := d.GetOk("destination"); ok && len(v.([]interface{})) != 0 {
414-
destinationMap := v.([]interface{})[0].(map[string]interface{})
415-
if v, ok := destinationMap["bucket"]; ok {
416-
destination.Bucket = v.(string)
417-
}
418-
if v, ok := destinationMap["account_id"]; ok {
419-
destination.AccountId = v.(string)
420-
}
421-
if v, ok := destinationMap["prefix"]; ok {
422-
destination.Prefix = v.(string)
423-
}
424-
if v, ok := destinationMap["format"]; ok {
425-
destination.Format = v.(string)
426-
}
427-
if v, ok := destinationMap["encryption"]; ok && len(v.([]interface{})) > 0 {
428-
encryptionMap := v.([]interface{})[0].(map[string]interface{})
429-
if v, ok := encryptionMap["sse_cos"]; ok {
430-
destination.Encryption = &cos.BucketInventoryEncryption{
431-
SSECOS: v.(string),
432-
}
449+
destination := cos.BucketInventoryDestination{}
450+
if v, ok := d.GetOk("destination"); ok {
451+
for _, item := range v.([]interface{}) {
452+
dMap := item.(map[string]interface{})
453+
if v, ok := dMap["bucket"]; ok {
454+
destination.Bucket = v.(string)
455+
}
433456

457+
if v, ok := dMap["account_id"]; ok {
458+
destination.AccountId = v.(string)
459+
}
460+
461+
if v, ok := dMap["prefix"]; ok {
462+
destination.Prefix = v.(string)
463+
}
464+
465+
if v, ok := dMap["format"]; ok {
466+
destination.Format = v.(string)
467+
}
468+
469+
if v, ok := dMap["encryption"]; ok {
470+
for _, item := range v.([]interface{}) {
471+
if item != nil {
472+
dMap := item.(map[string]interface{})
473+
if v, ok := dMap["sse_cos"]; ok {
474+
destination.Encryption = &cos.BucketInventoryEncryption{
475+
SSECOS: v.(string),
476+
}
477+
}
478+
}
479+
}
434480
}
435481
}
436482
}
Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1-
Provides a resource to create a cos bucket_inventory
1+
Provides a resource to create a cos bucket inventory
22

33
Example Usage
44

55
```hcl
6-
resource "tencentcloud_cos_bucket_inventory" "bucket_inventory" {
7-
name = "test123"
8-
bucket = "keep-test-xxxxxx"
9-
is_enabled = "true"
10-
included_object_versions = "Current"
11-
optional_fields {
12-
fields = ["Size", "ETag"]
13-
}
14-
filter {
15-
period {
16-
start_time = "1687276800"
17-
}
18-
}
19-
schedule {
20-
frequency = "Weekly"
21-
}
22-
destination {
23-
bucket = "qcs::cos:ap-guangzhou::keep-test-xxxxxx"
24-
account_id = ""
25-
format = "CSV"
26-
prefix = "cos_bucket_inventory"
6+
# get user info
7+
data "tencentcloud_user_info" "info" {}
8+
9+
locals {
10+
app_id = data.tencentcloud_user_info.info.app_id
11+
}
12+
13+
# create cos
14+
resource "tencentcloud_cos_bucket" "example" {
15+
bucket = "private-bucket-${local.app_id}"
16+
acl = "private"
17+
}
18+
19+
# create cos bucket inventory
20+
resource "tencentcloud_cos_bucket_inventory" "example" {
21+
name = "tf-example"
22+
bucket = tencentcloud_cos_bucket.example.id
23+
is_enabled = "true"
24+
included_object_versions = "Current"
25+
26+
optional_fields {
27+
fields = ["Size", "ETag"]
28+
}
2729
30+
filter {
31+
period {
32+
start_time = "1687276800"
2833
}
34+
}
35+
36+
schedule {
37+
frequency = "Daily"
38+
}
39+
40+
destination {
41+
bucket = "qcs::cos:ap-guangzhou::private-bucket-1309118522"
42+
format = "CSV"
43+
prefix = "frontends"
44+
}
2945
}
3046
```
3147

3248
Import
3349

34-
cos bucket_inventory can be imported using the id, e.g.
50+
cos bucket inventory can be imported using the id, e.g.
3551

3652
```
37-
terraform import tencentcloud_cos_bucket_inventory.bucket_inventory bucket_inventory_id
38-
```
53+
terraform import tencentcloud_cos_bucket_inventory.example private-bucket-1309118522#tf-example
54+
```

0 commit comments

Comments
 (0)