Skip to content

Commit cb43198

Browse files
committed
fix: cos - fix bugs, add unit tests, update docs
1 parent dbcafb4 commit cb43198

File tree

6 files changed

+340
-132
lines changed

6 files changed

+340
-132
lines changed

tencentcloud/data_source_tc_cos_buckets.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func dataSourceTencentCloudCosBuckets() *schema.Resource {
195195
Type: schema.TypeString,
196196
Optional: true,
197197
Default: "",
198-
Description: "the protocol used for COS to access the specified origin server. The available value include `HTTP`, `HTTPS` and `FOLLOW`",
198+
Description: "the protocol used for COS to access the specified origin server. The available value include `HTTP`, `HTTPS` and `FOLLOW`.",
199199
},
200200
"host": {
201201
Type: schema.TypeString,
@@ -212,7 +212,7 @@ func dataSourceTencentCloudCosBuckets() *schema.Resource {
212212
Type: schema.TypeBool,
213213
Optional: true,
214214
Default: true,
215-
Description: "Specifies whether to follow 3XX redirect to another origin server to pull data from",
215+
Description: "Specifies whether to follow 3XX redirect to another origin server to pull data from.",
216216
},
217217
//"copy_origin_data": {
218218
// Type: schema.TypeBool,
@@ -231,16 +231,16 @@ func dataSourceTencentCloudCosBuckets() *schema.Resource {
231231
Optional: true,
232232
Description: "Specifies the custom headers that you can add for COS to access your origin server.",
233233
},
234-
"redirect_prefix": {
235-
Type: schema.TypeString,
236-
Optional: true,
237-
Description: "Prefix for the file to which a request is redirected when the origin-pull rule is triggered.",
238-
},
239-
"redirect_suffix": {
240-
Type: schema.TypeString,
241-
Optional: true,
242-
Description: "Suffix for the file to which a request is redirected when the origin-pull rule is triggered.",
243-
},
234+
//"redirect_prefix": {
235+
// Type: schema.TypeString,
236+
// Optional: true,
237+
// Description: "Prefix for the file to which a request is redirected when the origin-pull rule is triggered.",
238+
//},
239+
//"redirect_suffix": {
240+
// Type: schema.TypeString,
241+
// Optional: true,
242+
// Description: "Suffix for the file to which a request is redirected when the origin-pull rule is triggered.",
243+
//},
244244
},
245245
},
246246
},
@@ -250,7 +250,24 @@ func dataSourceTencentCloudCosBuckets() *schema.Resource {
250250
Description: "Bucket origin domain rules.",
251251
Elem: &schema.Resource{
252252
Schema: map[string]*schema.Schema{
253-
253+
"domain": {
254+
Type: schema.TypeString,
255+
Required: true,
256+
Description: "Specify domain host.",
257+
},
258+
"type": {
259+
Type: schema.TypeString,
260+
Optional: true,
261+
Default: "REST",
262+
Description: "Specify origin domain type, available values: `REST`, `WEBSITE`, `ACCELERATE`, default: `REST`.",
263+
},
264+
"status": {
265+
Type: schema.TypeString,
266+
Optional: true,
267+
Default: "ENABLED",
268+
Description: "Domain status, default: `ENABLED`.",
269+
ValidateFunc: validateAllowedStringValue([]string{"ENABLED", "DISABLED"}),
270+
},
254271
},
255272
},
256273
},
@@ -322,9 +339,10 @@ LOOP:
322339
bucket["website"] = website
323340

324341
originRules, err := cosService.GetBucketPullOrigin(ctx, *v.Name)
325-
if err == nil {
326-
bucket["origin_pull_rules"] = originRules
342+
if err != nil {
343+
return err
327344
}
345+
bucket["origin_pull_rules"] = originRules
328346

329347
domainRules, err := cosService.GetBucketOriginDomain(ctx, *v.Name)
330348
if err == nil {

tencentcloud/resource_tc_cos_bucket.go

Lines changed: 57 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ resource "tencentcloud_cos_bucket" "with_origin" {
125125
protocol = "FOLLOW" // "HTTP" "HTTPS"
126126
follow_query_string = true
127127
follow_redirection = true
128-
follow_http_headers = ["Origin", "Host"]
128+
follow_http_headers = ["origin", "host"]
129129
custom_http_headers = {
130-
"X-Custom-Header" = "custom_value"
130+
"x-custom-header" = "custom_value"
131131
}
132-
redirect_prefix = "prefix"
133-
redirect_suffix = ".jpg"
134132
}
135133
}
136134
```
@@ -241,7 +239,7 @@ func originPullRules() *schema.Resource {
241239
Type: schema.TypeBool,
242240
Optional: true,
243241
Default: false,
244-
Description: "If `true`, COS will not return 3XX status code when pulling data from an origin server. Currently available zone: ap-beijing, ap-shanghai, ap-singapore, ap-mumbai.",
242+
Description: "If `true`, COS will not return 3XX status code when pulling data from an origin server. Current available zone: ap-beijing, ap-shanghai, ap-singapore, ap-mumbai.",
245243
},
246244
"prefix": {
247245
Type: schema.TypeString,
@@ -253,7 +251,7 @@ func originPullRules() *schema.Resource {
253251
Type: schema.TypeString,
254252
Optional: true,
255253
Default: "",
256-
Description: "the protocol used for COS to access the specified origin server. The available value include `HTTP`, `HTTPS` and `FOLLOW`",
254+
Description: "the protocol used for COS to access the specified origin server. The available value include `HTTP`, `HTTPS` and `FOLLOW`.",
257255
},
258256
"host": {
259257
Type: schema.TypeString,
@@ -270,7 +268,7 @@ func originPullRules() *schema.Resource {
270268
Type: schema.TypeBool,
271269
Optional: true,
272270
Default: true,
273-
Description: "Specifies whether to follow 3XX redirect to another origin server to pull data from",
271+
Description: "Specifies whether to follow 3XX redirect to another origin server to pull data from.",
274272
},
275273
//"copy_origin_data": {
276274
// Type: schema.TypeBool,
@@ -289,16 +287,16 @@ func originPullRules() *schema.Resource {
289287
Optional: true,
290288
Description: "Specifies the custom headers that you can add for COS to access your origin server.",
291289
},
292-
"redirect_prefix": {
293-
Type: schema.TypeString,
294-
Optional: true,
295-
Description: "Prefix for the file to which a request is redirected when the origin-pull rule is triggered.",
296-
},
297-
"redirect_suffix": {
298-
Type: schema.TypeString,
299-
Optional: true,
300-
Description: "Suffix for the file to which a request is redirected when the origin-pull rule is triggered.",
301-
},
290+
//"redirect_prefix": {
291+
// Type: schema.TypeString,
292+
// Optional: true,
293+
// Description: "Prefix for the file to which a request is redirected when the origin-pull rule is triggered.",
294+
//},
295+
//"redirect_suffix": {
296+
// Type: schema.TypeString,
297+
// Optional: true,
298+
// Description: "Suffix for the file to which a request is redirected when the origin-pull rule is triggered.",
299+
//},
302300
},
303301
}
304302
}
@@ -373,7 +371,7 @@ func resourceTencentCloudCosBucket() *schema.Resource {
373371
"acl_body": {
374372
Type: schema.TypeString,
375373
Optional: true,
376-
Description: "ACL XML body for multiple grant info",
374+
Description: "ACL XML body for multiple grant info.",
377375
},
378376
"encryption_algorithm": {
379377
Type: schema.TypeString,
@@ -445,7 +443,7 @@ func resourceTencentCloudCosBucket() *schema.Resource {
445443
Type: schema.TypeString,
446444
Optional: true,
447445
Default: "REST",
448-
Description: "Specify origin domain type. Available values: `REST` , `WEBSITE`, `ACCELERATE`. Default: `REST`",
446+
Description: "Specify origin domain type, available values: `REST`, `WEBSITE`, `ACCELERATE`, default: `REST`.",
449447
},
450448
"status": {
451449
Type: schema.TypeString,
@@ -649,15 +647,6 @@ func resourceTencentCloudCosBucketRead(d *schema.ResourceData, meta interface{})
649647
_ = d.Set("bucket", d.Id())
650648
}
651649

652-
acl, err := cosService.GetBucketACLXML(ctx, bucket)
653-
if err != nil {
654-
return err
655-
}
656-
657-
if err = d.Set("acl_body", acl); err != nil {
658-
return fmt.Errorf("setting acl_body error: %v", err)
659-
}
660-
661650
if err != nil {
662651
return err
663652
}
@@ -679,8 +668,13 @@ func resourceTencentCloudCosBucketRead(d *schema.ResourceData, meta interface{})
679668
return fmt.Errorf("setting origin_pull_rules error: %v", err)
680669
}
681670

682-
originDomainRules, _ := cosService.GetBucketOriginDomain(ctx, bucket)
683-
log.Printf("--+-----*-*-*-originDomainRules, %s", originDomainRules)
671+
originDomainRules, err := cosService.GetBucketOriginDomain(ctx, bucket)
672+
if err != nil {
673+
return err
674+
}
675+
if err = d.Set("origin_domain_rules", originDomainRules); err != nil {
676+
return fmt.Errorf("setting origin_domain_rules error: %v", err)
677+
}
684678

685679
// read the lifecycle
686680
lifecycleRules, err := cosService.GetBucketLifecycle(ctx, bucket)
@@ -975,27 +969,6 @@ func resourceTencentCloudCosBucketAclUpdate(ctx context.Context, client *s3.S3,
975969
return nil
976970
}
977971

978-
func resourceTencentCloudCosBucketAclHeaderUpdate(ctx context.Context, client *s3.S3, d *schema.ResourceData) error {
979-
logId := getLogId(ctx)
980-
981-
bucket := d.Get("bucket").(string)
982-
acl := d.Get("acl").(string)
983-
request := s3.PutBucketAclInput{
984-
Bucket: aws.String(bucket),
985-
ACL: aws.String(acl),
986-
}
987-
response, err := client.PutBucketAcl(&request)
988-
if err != nil {
989-
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
990-
logId, "put bucket acl", request.String(), err.Error())
991-
return fmt.Errorf("cos put bucket error: %s, bucket: %s", err.Error(), bucket)
992-
}
993-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
994-
logId, "put bucket acl", request.String(), response.String())
995-
996-
return nil
997-
}
998-
999972
func resourceTencentCloudCosBucketCorsUpdate(ctx context.Context, client *s3.S3, d *schema.ResourceData) error {
1000973
logId := getLogId(ctx)
1001974

@@ -1271,15 +1244,20 @@ func resourceTencentCloudCosBucketLogStatusUpdate(ctx context.Context, client *s
12711244
}
12721245

12731246
func resourceTencentCloudCosBucketOriginACLBodyUpdate(ctx context.Context, service CosService, d *schema.ResourceData ) error {
1274-
var body string
1275-
v, ok := d.GetOk("acl_body")
1247+
aclHeader := ""
1248+
aclBody := ""
1249+
body, bodyOk := d.GetOk("acl_body")
1250+
header, headerOk := d.GetOk("acl")
12761251
bucket := d.Get("bucket").(string)
1277-
if !ok {
1278-
body = ""
1252+
// If ACLXML update to empty, this will pass default header to delete verbose acl info
1253+
if bodyOk {
1254+
aclBody = body.(string)
1255+
} else if headerOk {
1256+
aclHeader = header.(string)
12791257
} else {
1280-
body = v.(string)
1258+
aclHeader = "private"
12811259
}
1282-
if err := service.TencentCosPutBucketACL(ctx, bucket, body); err != nil {
1260+
if err := service.TencentCosPutBucketACL(ctx, bucket, aclBody, aclHeader); err != nil {
12831261
return err
12841262
}
12851263
return nil
@@ -1345,19 +1323,27 @@ func resourceTencentCloudCosBucketOriginPullUpdate(ctx context.Context, service
13451323
// item.OriginParameter.CopyOriginData = v.(bool)
13461324
//}
13471325
if v, ok := dMap["redirect_prefix"]; ok {
1348-
item.OriginInfo.FileInfo.Prefix = v.(string)
1326+
value := v.(string)
1327+
if value != "" {
1328+
item.OriginInfo.FileInfo.PrefixDirective = true
1329+
}
1330+
item.OriginInfo.FileInfo.Prefix = value
13491331
}
13501332
if v, ok := dMap["redirect_suffix"]; ok {
1351-
item.OriginInfo.FileInfo.Suffix = v.(string)
1333+
value := v.(string)
1334+
if value != "" {
1335+
item.OriginInfo.FileInfo.PrefixDirective = true
1336+
}
1337+
item.OriginInfo.FileInfo.Suffix = value
13521338
}
13531339
if v, ok := dMap["custom_http_headers"]; ok {
13541340
var customHeaders []cos.OriginHttpHeader
1355-
header := cos.OriginHttpHeader{}
13561341
for key, val := range v.(map[string]interface{}) {
1357-
header.Key = key
1358-
header.Value = val.(string)
1342+
customHeaders = append(customHeaders, cos.OriginHttpHeader{
1343+
Key: key,
1344+
Value: val.(string),
1345+
})
13591346
}
1360-
customHeaders = append(customHeaders, header)
13611347
item.OriginParameter.HttpHeader.NewHttpHeaders = customHeaders
13621348
}
13631349
if v, ok := dMap["follow_http_headers"]; ok {
@@ -1385,23 +1371,26 @@ func resourceTencentCloudCosBucketOriginDomainUpdate(ctx context.Context, servic
13851371
v, ok := d.GetOk("origin_domain_rules")
13861372
bucket := d.Get("bucket").(string)
13871373
if !ok {
1374+
if err := service.DeleteBucketOriginDomain(ctx, bucket); err != nil {
1375+
return err
1376+
}
13881377
return nil
13891378
}
1390-
rules := v.([]map[string]interface{})
1379+
rules := v.([]interface{})
13911380
domainRules := make([]cos.BucketDomainRule, 0)
13921381

13931382
for _, rule := range rules {
1383+
dMap := rule.(map[string]interface{})
13941384
item := cos.BucketDomainRule{}
1395-
if name, ok := rule["domain"]; ok {
1385+
if name, ok := dMap["domain"]; ok {
13961386
item.Name = name.(string)
13971387
}
1398-
if status, ok := rule["status"]; ok {
1388+
if status, ok := dMap["status"]; ok {
13991389
item.Status = status.(string)
14001390
}
1401-
if domainType, ok := rule["type"]; ok {
1391+
if domainType, ok := dMap["type"]; ok {
14021392
item.Type = domainType.(string)
14031393
}
1404-
14051394
domainRules = append(domainRules, item)
14061395
}
14071396

0 commit comments

Comments
 (0)