Skip to content

Commit d11b46f

Browse files
authored
fix(cos): [121778264] tencentcloud_cos_bucket support routing_rules (#3108)
* add * add * add * add
1 parent 61ad762 commit d11b46f

File tree

5 files changed

+165
-2
lines changed

5 files changed

+165
-2
lines changed

.changelog/3108.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: support routing_rules
3+
```

tencentcloud/services/cos/resource_tc_cos_bucket.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,50 @@ func ResourceTencentCloudCosBucket() *schema.Resource {
484484
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{"http", "https"}),
485485
Description: "Redirects all request configurations. Valid values: http, https. Default is `http`.",
486486
},
487+
"routing_rules": {
488+
Type: schema.TypeList,
489+
Optional: true,
490+
MaxItems: 1,
491+
Description: "Routing rule configuration. A RoutingRules container can contain up to 100 RoutingRule elements.",
492+
Elem: &schema.Resource{
493+
Schema: map[string]*schema.Schema{
494+
"rules": {
495+
Type: schema.TypeList,
496+
Required: true,
497+
Description: "Routing rule list.",
498+
Elem: &schema.Resource{
499+
Schema: map[string]*schema.Schema{
500+
"condition_error_code": {
501+
Type: schema.TypeString,
502+
Optional: true,
503+
Description: "Specifies the error code as the match condition for the routing rule. Valid values: only 4xx return codes, such as 403 or 404.",
504+
},
505+
"condition_prefix": {
506+
Type: schema.TypeString,
507+
Optional: true,
508+
Description: "Specifies the object key prefix as the match condition for the routing rule.",
509+
},
510+
"redirect_protocol": {
511+
Type: schema.TypeString,
512+
Optional: true,
513+
Description: "Specifies the target protocol for the routing rule. Only HTTPS is supported.",
514+
},
515+
"redirect_replace_key": {
516+
Type: schema.TypeString,
517+
Optional: true,
518+
Description: "Specifies the target object key to replace the original object key in the request.",
519+
},
520+
"redirect_replace_key_prefix": {
521+
Type: schema.TypeString,
522+
Optional: true,
523+
Description: "Specifies the object key prefix to replace the original prefix in the request. You can set this parameter only if the condition is KeyPrefixEquals.",
524+
},
525+
},
526+
},
527+
},
528+
},
529+
},
530+
},
487531
"endpoint": {
488532
Type: schema.TypeString,
489533
Computed: true,
@@ -1442,6 +1486,45 @@ func resourceTencentCloudCosBucketWebsiteUpdate(ctx context.Context, meta interf
14421486
}
14431487
}
14441488

1489+
if v, ok := w["routing_rules"]; ok {
1490+
websiteRoutingRules := cos.WebsiteRoutingRules{}
1491+
for _, item := range v.([]interface{}) {
1492+
rules := item.(map[string]interface{})
1493+
if v, ok := rules["rules"]; ok {
1494+
wbRules := []cos.WebsiteRoutingRule{}
1495+
for _, rule := range v.([]interface{}) {
1496+
dMap := rule.(map[string]interface{})
1497+
wbRule := cos.WebsiteRoutingRule{}
1498+
if v, ok := dMap["condition_error_code"].(string); ok && v != "" {
1499+
wbRule.ConditionErrorCode = v
1500+
}
1501+
1502+
if v, ok := dMap["condition_prefix"].(string); ok && v != "" {
1503+
wbRule.ConditionPrefix = v
1504+
}
1505+
1506+
if v, ok := dMap["redirect_protocol"].(string); ok && v != "" {
1507+
wbRule.RedirectProtocol = v
1508+
}
1509+
1510+
if v, ok := dMap["redirect_replace_key"].(string); ok && v != "" {
1511+
wbRule.RedirectReplaceKey = v
1512+
}
1513+
1514+
if v, ok := dMap["redirect_replace_key_prefix"].(string); ok && v != "" {
1515+
wbRule.RedirectReplaceKeyPrefix = v
1516+
}
1517+
1518+
wbRules = append(wbRules, wbRule)
1519+
}
1520+
1521+
websiteRoutingRules.Rules = wbRules
1522+
}
1523+
}
1524+
1525+
websiteConfiguration.RoutingRules = &websiteRoutingRules
1526+
}
1527+
14451528
request := websiteConfiguration
14461529
response, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClientNew(bucket, cdcId).Bucket.PutWebsite(ctx, &request)
14471530
if err != nil {

tencentcloud/services/cos/resource_tc_cos_bucket.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,20 @@ resource "tencentcloud_cos_bucket" "bucket_with_static_website" {
236236
website {
237237
index_document = "index.html"
238238
error_document = "error.html"
239-
redirect_all_requests_to = "http"
239+
redirect_all_requests_to = "https"
240+
routing_rules {
241+
rules {
242+
condition_error_code = "404"
243+
redirect_protocol = "https"
244+
redirect_replace_key_prefix = "/test"
245+
}
246+
247+
rules {
248+
condition_prefix = "/test"
249+
redirect_protocol = "https"
250+
redirect_replace_key = "key"
251+
}
252+
}
240253
}
241254
}
242255

tencentcloud/services/cos/service_tencentcloud_cos.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,44 @@ func (me *CosService) GetBucketWebsite(ctx context.Context, bucket string, cdcId
780780
if response.RedirectAllRequestsTo != nil {
781781
website["redirect_all_requests_to"] = *response.RedirectAllRequestsTo.Protocol
782782
}
783+
if response.RoutingRules != nil {
784+
tmpList := make([]map[string]interface{}, 0)
785+
routingRules := make(map[string]interface{}, 0)
786+
rulesList := make([]map[string]interface{}, 0, len(response.RoutingRules))
787+
for _, item := range response.RoutingRules {
788+
tmpMap := make(map[string]interface{}, 0)
789+
if item.Condition != nil {
790+
if item.Condition.HttpErrorCodeReturnedEquals != nil {
791+
tmpMap["condition_error_code"] = item.Condition.HttpErrorCodeReturnedEquals
792+
}
793+
794+
if item.Condition.KeyPrefixEquals != nil {
795+
tmpMap["condition_prefix"] = item.Condition.KeyPrefixEquals
796+
}
797+
}
798+
799+
if item.Redirect != nil {
800+
if item.Redirect.Protocol != nil {
801+
tmpMap["redirect_protocol"] = item.Redirect.Protocol
802+
}
803+
804+
if item.Redirect.ReplaceKeyWith != nil {
805+
tmpMap["redirect_replace_key"] = item.Redirect.ReplaceKeyWith
806+
}
807+
808+
if item.Redirect.ReplaceKeyPrefixWith != nil {
809+
tmpMap["redirect_replace_key_prefix"] = item.Redirect.ReplaceKeyPrefixWith
810+
}
811+
}
812+
813+
rulesList = append(rulesList, tmpMap)
814+
}
815+
816+
routingRules["rules"] = rulesList
817+
tmpList = append(tmpList, routingRules)
818+
website["routing_rules"] = tmpList
819+
}
820+
783821
if len(website) > 0 {
784822
websites = append(websites, website)
785823
}

website/docs/r/cos_bucket.html.markdown

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,20 @@ resource "tencentcloud_cos_bucket" "bucket_with_static_website" {
247247
website {
248248
index_document = "index.html"
249249
error_document = "error.html"
250-
redirect_all_requests_to = "http"
250+
redirect_all_requests_to = "https"
251+
routing_rules {
252+
rules {
253+
condition_error_code = "404"
254+
redirect_protocol = "https"
255+
redirect_replace_key_prefix = "/test"
256+
}
257+
258+
rules {
259+
condition_prefix = "/test"
260+
redirect_protocol = "https"
261+
redirect_replace_key = "key"
262+
}
263+
}
251264
}
252265
}
253266
@@ -496,6 +509,18 @@ The `replica_rules` object supports the following:
496509
* `id` - (Optional, String) Name of a specific rule.
497510
* `prefix` - (Optional, String) Prefix matching policy. Policies cannot overlap; otherwise, an error will be returned. To match the root directory, leave this parameter empty.
498511

512+
The `routing_rules` object of `website` supports the following:
513+
514+
* `rules` - (Required, List) Routing rule list.
515+
516+
The `rules` object of `routing_rules` supports the following:
517+
518+
* `condition_error_code` - (Optional, String) Specifies the error code as the match condition for the routing rule. Valid values: only 4xx return codes, such as 403 or 404.
519+
* `condition_prefix` - (Optional, String) Specifies the object key prefix as the match condition for the routing rule.
520+
* `redirect_protocol` - (Optional, String) Specifies the target protocol for the routing rule. Only HTTPS is supported.
521+
* `redirect_replace_key_prefix` - (Optional, String) Specifies the object key prefix to replace the original prefix in the request. You can set this parameter only if the condition is KeyPrefixEquals.
522+
* `redirect_replace_key` - (Optional, String) Specifies the target object key to replace the original object key in the request.
523+
499524
The `transition` object of `lifecycle_rules` supports the following:
500525

501526
* `storage_class` - (Required, String) Specifies the storage class to which you want the object to transition. Available values include `STANDARD_IA`, `MAZ_STANDARD_IA`, `INTELLIGENT_TIERING`, `MAZ_INTELLIGENT_TIERING`, `ARCHIVE`, `DEEP_ARCHIVE`. For more information, please refer to: https://cloud.tencent.com/document/product/436/33417.
@@ -507,6 +532,7 @@ The `website` object supports the following:
507532
* `error_document` - (Optional, String) An absolute path to the document to return in case of a 4XX error.
508533
* `index_document` - (Optional, String) COS returns this index document when requests are made to the root domain or any of the subfolders.
509534
* `redirect_all_requests_to` - (Optional, String) Redirects all request configurations. Valid values: http, https. Default is `http`.
535+
* `routing_rules` - (Optional, List) Routing rule configuration. A RoutingRules container can contain up to 100 RoutingRule elements.
510536

511537
## Attributes Reference
512538

0 commit comments

Comments
 (0)