Skip to content
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

fix(cos): [121778264] tencentcloud_cos_bucket support routing_rules #3108

Merged
merged 4 commits into from
Jan 24, 2025
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/3108.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_cos_bucket: support routing_rules
```
83 changes: 83 additions & 0 deletions tencentcloud/services/cos/resource_tc_cos_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,50 @@ func ResourceTencentCloudCosBucket() *schema.Resource {
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{"http", "https"}),
Description: "Redirects all request configurations. Valid values: http, https. Default is `http`.",
},
"routing_rules": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Routing rule configuration. A RoutingRules container can contain up to 100 RoutingRule elements.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rules": {
Type: schema.TypeList,
Required: true,
Description: "Routing rule list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"condition_error_code": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the error code as the match condition for the routing rule. Valid values: only 4xx return codes, such as 403 or 404.",
},
"condition_prefix": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the object key prefix as the match condition for the routing rule.",
},
"redirect_protocol": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the target protocol for the routing rule. Only HTTPS is supported.",
},
"redirect_replace_key": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the target object key to replace the original object key in the request.",
},
"redirect_replace_key_prefix": {
Type: schema.TypeString,
Optional: true,
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.",
},
},
},
},
},
},
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -1442,6 +1486,45 @@ func resourceTencentCloudCosBucketWebsiteUpdate(ctx context.Context, meta interf
}
}

if v, ok := w["routing_rules"]; ok {
websiteRoutingRules := cos.WebsiteRoutingRules{}
for _, item := range v.([]interface{}) {
rules := item.(map[string]interface{})
if v, ok := rules["rules"]; ok {
wbRules := []cos.WebsiteRoutingRule{}
for _, rule := range v.([]interface{}) {
dMap := rule.(map[string]interface{})
wbRule := cos.WebsiteRoutingRule{}
if v, ok := dMap["condition_error_code"].(string); ok && v != "" {
wbRule.ConditionErrorCode = v
}

if v, ok := dMap["condition_prefix"].(string); ok && v != "" {
wbRule.ConditionPrefix = v
}

if v, ok := dMap["redirect_protocol"].(string); ok && v != "" {
wbRule.RedirectProtocol = v
}

if v, ok := dMap["redirect_replace_key"].(string); ok && v != "" {
wbRule.RedirectReplaceKey = v
}

if v, ok := dMap["redirect_replace_key_prefix"].(string); ok && v != "" {
wbRule.RedirectReplaceKeyPrefix = v
}

wbRules = append(wbRules, wbRule)
}

websiteRoutingRules.Rules = wbRules
}
}

websiteConfiguration.RoutingRules = &websiteRoutingRules
}

request := websiteConfiguration
response, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClientNew(bucket, cdcId).Bucket.PutWebsite(ctx, &request)
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion tencentcloud/services/cos/resource_tc_cos_bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,20 @@ resource "tencentcloud_cos_bucket" "bucket_with_static_website" {
website {
index_document = "index.html"
error_document = "error.html"
redirect_all_requests_to = "http"
redirect_all_requests_to = "https"
routing_rules {
rules {
condition_error_code = "404"
redirect_protocol = "https"
redirect_replace_key_prefix = "/test"
}

rules {
condition_prefix = "/test"
redirect_protocol = "https"
redirect_replace_key = "key"
}
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions tencentcloud/services/cos/service_tencentcloud_cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,44 @@ func (me *CosService) GetBucketWebsite(ctx context.Context, bucket string, cdcId
if response.RedirectAllRequestsTo != nil {
website["redirect_all_requests_to"] = *response.RedirectAllRequestsTo.Protocol
}
if response.RoutingRules != nil {
tmpList := make([]map[string]interface{}, 0)
routingRules := make(map[string]interface{}, 0)
rulesList := make([]map[string]interface{}, 0, len(response.RoutingRules))
for _, item := range response.RoutingRules {
tmpMap := make(map[string]interface{}, 0)
if item.Condition != nil {
if item.Condition.HttpErrorCodeReturnedEquals != nil {
tmpMap["condition_error_code"] = item.Condition.HttpErrorCodeReturnedEquals
}

if item.Condition.KeyPrefixEquals != nil {
tmpMap["condition_prefix"] = item.Condition.KeyPrefixEquals
}
}

if item.Redirect != nil {
if item.Redirect.Protocol != nil {
tmpMap["redirect_protocol"] = item.Redirect.Protocol
}

if item.Redirect.ReplaceKeyWith != nil {
tmpMap["redirect_replace_key"] = item.Redirect.ReplaceKeyWith
}

if item.Redirect.ReplaceKeyPrefixWith != nil {
tmpMap["redirect_replace_key_prefix"] = item.Redirect.ReplaceKeyPrefixWith
}
}

rulesList = append(rulesList, tmpMap)
}

routingRules["rules"] = rulesList
tmpList = append(tmpList, routingRules)
website["routing_rules"] = tmpList
}

if len(website) > 0 {
websites = append(websites, website)
}
Expand Down
28 changes: 27 additions & 1 deletion website/docs/r/cos_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,20 @@ resource "tencentcloud_cos_bucket" "bucket_with_static_website" {
website {
index_document = "index.html"
error_document = "error.html"
redirect_all_requests_to = "http"
redirect_all_requests_to = "https"
routing_rules {
rules {
condition_error_code = "404"
redirect_protocol = "https"
redirect_replace_key_prefix = "/test"
}

rules {
condition_prefix = "/test"
redirect_protocol = "https"
redirect_replace_key = "key"
}
}
}
}

Expand Down Expand Up @@ -496,6 +509,18 @@ The `replica_rules` object supports the following:
* `id` - (Optional, String) Name of a specific rule.
* `prefix` - (Optional, String) Prefix matching policy. Policies cannot overlap; otherwise, an error will be returned. To match the root directory, leave this parameter empty.

The `routing_rules` object of `website` supports the following:

* `rules` - (Required, List) Routing rule list.

The `rules` object of `routing_rules` supports the following:

* `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.
* `condition_prefix` - (Optional, String) Specifies the object key prefix as the match condition for the routing rule.
* `redirect_protocol` - (Optional, String) Specifies the target protocol for the routing rule. Only HTTPS is supported.
* `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.
* `redirect_replace_key` - (Optional, String) Specifies the target object key to replace the original object key in the request.

The `transition` object of `lifecycle_rules` supports the following:

* `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.
Expand All @@ -507,6 +532,7 @@ The `website` object supports the following:
* `error_document` - (Optional, String) An absolute path to the document to return in case of a 4XX error.
* `index_document` - (Optional, String) COS returns this index document when requests are made to the root domain or any of the subfolders.
* `redirect_all_requests_to` - (Optional, String) Redirects all request configurations. Valid values: http, https. Default is `http`.
* `routing_rules` - (Optional, List) Routing rule configuration. A RoutingRules container can contain up to 100 RoutingRule elements.

## Attributes Reference

Expand Down
Loading