Skip to content

Commit 3e199cc

Browse files
authored
feat: cdn - support PostMaxSizeConfig (#1329)
* feat: cdn - support PostMaxSizeConfig * changelog #1329 * fix: cdn - add post_max_size to testcase
1 parent 8c7a2e2 commit 3e199cc

File tree

6 files changed

+172
-14
lines changed

6 files changed

+172
-14
lines changed

.changelog/1329.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cdn_domain: support PostMaxSize Params.
3+
```
4+
5+
```release-note:bug
6+
resource/tencentcloud_cdn_domain: Fix testing domain verification failed.
7+
```
8+

tencentcloud/extension_cdn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131

3232
CDN_HOST_NOT_FOUND = "ResourceNotFound.CdnHostNotExists"
3333
CDN_HOST_EXISTS = "ResourceInUse.CdnHostExists"
34-
CDN_DOMAIN_CONFIG_ERROE = "FailedOperation.CdnConfigError"
34+
CDN_DOMAIN_CONFIG_ERROR = "FailedOperation.CdnConfigError"
3535

3636
CDN_RULE_TYPE_ALL = "all"
3737
CDN_RULE_TYPE_FILE = "file"

tencentcloud/resource_tc_cdn_domain.go

Lines changed: 119 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,25 @@ func resourceTencentCloudCdnDomain() *schema.Resource {
13461346
Optional: true,
13471347
Description: "Offline cache switch, available values: `on`, `off` (default).",
13481348
},
1349+
"post_max_size": {
1350+
Type: schema.TypeList,
1351+
Optional: true,
1352+
Description: "Maximum post size configuration.",
1353+
Elem: &schema.Resource{
1354+
Schema: map[string]*schema.Schema{
1355+
"switch": {
1356+
Type: schema.TypeString,
1357+
Required: true,
1358+
Description: "Configuration switch, available values: `on`, `off` (default).",
1359+
},
1360+
"max_size": {
1361+
Type: schema.TypeInt,
1362+
Optional: true,
1363+
Description: "Maximum size in MB, value range is `[1, 200]`.",
1364+
},
1365+
},
1366+
},
1367+
},
13491368
"quic_switch": {
13501369
Type: schema.TypeString,
13511370
Optional: true,
@@ -2284,7 +2303,7 @@ func resourceTencentCloudCdnDomainCreate(d *schema.ResourceData, meta interface{
22842303
_, err := meta.(*TencentCloudClient).apiV3Conn.UseCdnClient().AddCdnDomain(request)
22852304
if err != nil {
22862305
if sdkErr, ok := err.(*sdkErrors.TencentCloudSDKError); ok {
2287-
if sdkErr.Code == CDN_DOMAIN_CONFIG_ERROE || sdkErr.Code == CDN_HOST_EXISTS {
2306+
if sdkErr.Code == CDN_DOMAIN_CONFIG_ERROR || sdkErr.Code == CDN_HOST_EXISTS {
22882307
return resource.NonRetryableError(err)
22892308
}
22902309
}
@@ -2312,6 +2331,10 @@ func resourceTencentCloudCdnDomainCreate(d *schema.ResourceData, meta interface{
23122331
return err
23132332
}
23142333

2334+
if err := updateCdnModifyOnlyParams(d, meta, ctx); err != nil {
2335+
return err
2336+
}
2337+
23152338
// tags
23162339
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
23172340
client := meta.(*TencentCloudClient).apiV3Conn
@@ -2802,6 +2825,13 @@ func resourceTencentCloudCdnDomainRead(d *schema.ResourceData, meta interface{})
28022825
"receive_timeout": dc.OriginPullTimeout.ReceiveTimeout,
28032826
})
28042827
}
2828+
if ok := checkCdnInfoWritable(d, "post_max_size", dc.PostMaxSize); ok {
2829+
dMap := map[string]interface{}{
2830+
"switch": dc.PostMaxSize.Switch,
2831+
"max_size": *dc.PostMaxSize.MaxSize / 1024 / 1024,
2832+
}
2833+
_ = helper.SetMapInterfaces(d, "post_max_size", dMap)
2834+
}
28052835
if _, ok := d.GetOk("offline_cache_switch"); ok && dc.OfflineCache != nil {
28062836
_ = d.Set("offline_cache_switch", dc.OfflineCache.Switch)
28072837
}
@@ -3189,6 +3219,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
31893219

31903220
// more added
31913221
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "ip_filter"); ok && hasChanged {
3222+
updateAttrs = append(updateAttrs, "ip_filter")
31923223
vSwitch := v["switch"].(string)
31933224
request.IpFilter = &cdn.IpFilter{
31943225
Switch: &vSwitch,
@@ -3228,6 +3259,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
32283259
}
32293260
}
32303261
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "ip_freq_limit"); ok && hasChanged {
3262+
updateAttrs = append(updateAttrs, "ip_freq_limit")
32313263
vSwitch := v["switch"].(string)
32323264
qps := v["qps"].(int)
32333265
request.IpFreqLimit = &cdn.IpFreqLimit{
@@ -3236,6 +3268,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
32363268
}
32373269
}
32383270
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "status_code_cache"); ok && hasChanged {
3271+
updateAttrs = append(updateAttrs, "status_code_cache")
32393272
vSwitch := v["switch"].(string)
32403273
request.StatusCodeCache = &cdn.StatusCodeCache{
32413274
Switch: &vSwitch,
@@ -3255,6 +3288,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
32553288
request.StatusCodeCache.CacheRules = requestRules
32563289
}
32573290
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "compression"); ok && hasChanged {
3291+
updateAttrs = append(updateAttrs, "compression")
32583292
vSwitch := v["switch"].(string)
32593293
request.Compression = &cdn.Compression{
32603294
Switch: &vSwitch,
@@ -3294,6 +3328,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
32943328

32953329
}
32963330
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "band_width_alert"); ok && hasChanged {
3331+
updateAttrs = append(updateAttrs, "band_width_alert")
32973332
vSwitch := v["switch"].(string)
32983333
request.BandwidthAlert = &cdn.BandwidthAlert{
32993334
Switch: &vSwitch,
@@ -3321,6 +3356,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
33213356
}
33223357
}
33233358
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "error_page"); ok && hasChanged {
3359+
updateAttrs = append(updateAttrs, "error_page")
33243360
vSwitch := v["switch"].(string)
33253361
request.ErrorPage = &cdn.ErrorPage{
33263362
Switch: &vSwitch,
@@ -3344,6 +3380,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
33443380
request.ErrorPage.PageRules = requestRules
33453381
}
33463382
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "response_header"); ok && hasChanged {
3383+
updateAttrs = append(updateAttrs, "response_header")
33473384
vSwitch := v["switch"].(string)
33483385
request.ResponseHeader = &cdn.ResponseHeader{
33493386
Switch: &vSwitch,
@@ -3373,6 +3410,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
33733410
request.ResponseHeader.HeaderRules = responseRules
33743411
}
33753412
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "downstream_capping"); ok && hasChanged {
3413+
updateAttrs = append(updateAttrs, "downstream_capping")
33763414
vSwitch := v["switch"].(string)
33773415
request.DownstreamCapping = &cdn.DownstreamCapping{
33783416
Switch: &vSwitch,
@@ -3395,12 +3433,18 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
33953433
}
33963434
request.DownstreamCapping.CappingRules = requestRules
33973435
}
3398-
if v, ok := d.GetOk("response_header_cache_switch"); ok {
3436+
if d.HasChange("response_header_cache_switch") {
3437+
updateAttrs = append(updateAttrs, "response_header_cache_switch")
3438+
v, ok := d.Get("response_header_cache_switch").(string)
3439+
if !ok || v == "" {
3440+
v = "off"
3441+
}
33993442
request.ResponseHeaderCache = &cdn.ResponseHeaderCache{
3400-
Switch: helper.String(v.(string)),
3443+
Switch: &v,
34013444
}
34023445
}
34033446
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "origin_pull_optimization"); ok && hasChanged {
3447+
updateAttrs = append(updateAttrs, "origin_pull_optimization")
34043448
vSwitch := v["switch"].(string)
34053449
request.OriginPullOptimization = &cdn.OriginPullOptimization{
34063450
Switch: &vSwitch,
@@ -3409,12 +3453,18 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
34093453
request.OriginPullOptimization.OptimizationType = &v
34103454
}
34113455
}
3412-
if v, ok := d.GetOk("seo_switch"); ok {
3456+
if d.HasChange("seo_switch") {
3457+
updateAttrs = append(updateAttrs, "seo_switch")
3458+
v, ok := d.Get("seo_switch").(string)
3459+
if !ok || v == "" {
3460+
v = "off"
3461+
}
34133462
request.Seo = &cdn.Seo{
3414-
Switch: helper.String(v.(string)),
3463+
Switch: &v,
34153464
}
34163465
}
34173466
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "referer"); ok && hasChanged {
3467+
updateAttrs = append(updateAttrs, "referer")
34183468
vSwitch := v["switch"].(string)
34193469
request.Referer = &cdn.Referer{
34203470
Switch: &vSwitch,
@@ -3443,12 +3493,18 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
34433493
}
34443494
request.Referer.RefererRules = requestRules
34453495
}
3446-
if v, ok := d.GetOk("video_seek_switch"); ok {
3496+
if d.HasChange("video_seek_switch") {
3497+
updateAttrs = append(updateAttrs, "video_seek_switch")
3498+
v, ok := d.Get("video_seek_switch").(string)
3499+
if !ok || v == "" {
3500+
v = "off"
3501+
}
34473502
request.VideoSeek = &cdn.VideoSeek{
3448-
Switch: helper.String(v.(string)),
3503+
Switch: &v,
34493504
}
34503505
}
34513506
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "max_age"); ok && hasChanged {
3507+
updateAttrs = append(updateAttrs, "max_age")
34523508
vSwitch := v["switch"].(string)
34533509
request.MaxAge = &cdn.MaxAge{
34543510
Switch: &vSwitch,
@@ -3477,6 +3533,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
34773533
request.MaxAge.MaxAgeRules = requestRules
34783534
}
34793535
if v, ok := d.GetOk("specific_config_mainland"); ok && v.(string) != "" {
3536+
updateAttrs = append(updateAttrs, "specific_config_mainland")
34803537
request.SpecificConfig = &cdn.SpecificConfig{}
34813538
configStruct := cdn.MainlandConfig{}
34823539
err := json.Unmarshal([]byte(v.(string)), &configStruct)
@@ -3486,6 +3543,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
34863543
request.SpecificConfig.Mainland = &configStruct
34873544
}
34883545
if v, ok := d.GetOk("specific_config_overseas"); ok && v.(string) != "" {
3546+
updateAttrs = append(updateAttrs, "specific_config_overseas")
34893547
if request.SpecificConfig == nil {
34903548
request.SpecificConfig = &cdn.SpecificConfig{}
34913549
}
@@ -3497,6 +3555,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
34973555
request.SpecificConfig.Overseas = &configStruct
34983556
}
34993557
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "origin_pull_timeout"); ok && hasChanged {
3558+
updateAttrs = append(updateAttrs, "origin_pull_timeout")
35003559
request.OriginPullTimeout = &cdn.OriginPullTimeout{}
35013560
if vv, ok := v["connect_timeout"].(int); ok && vv > 0 {
35023561
request.OriginPullTimeout.ConnectTimeout = helper.IntUint64(vv)
@@ -3505,6 +3564,17 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
35053564
request.OriginPullTimeout.ReceiveTimeout = helper.IntUint64(vv)
35063565
}
35073566
}
3567+
if v, ok, hasChanged := checkCdnHeadMapOkAndChanged(d, "post_max_size"); ok && hasChanged {
3568+
updateAttrs = append(updateAttrs, "post_max_size")
3569+
vSwitch := v["switch"].(string)
3570+
maxSize := v["max_size"].(int)
3571+
request.PostMaxSize = &cdn.PostSize{
3572+
Switch: &vSwitch,
3573+
}
3574+
if maxSize > 0 {
3575+
request.PostMaxSize.MaxSize = helper.IntInt64(maxSize * 1024 * 1024)
3576+
}
3577+
}
35083578
if v, ok := d.GetOk("offline_cache_switch"); ok {
35093579
request.OfflineCache = &cdn.OfflineCache{
35103580
Switch: helper.String(v.(string)),
@@ -3590,7 +3660,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
35903660
_, err := meta.(*TencentCloudClient).apiV3Conn.UseCdnClient().UpdateDomainConfig(request)
35913661
if err != nil {
35923662
if sdkErr, ok := err.(*sdkErrors.TencentCloudSDKError); ok {
3593-
if sdkErr.Code == CDN_DOMAIN_CONFIG_ERROE {
3663+
if sdkErr.Code == CDN_DOMAIN_CONFIG_ERROR {
35943664
return resource.NonRetryableError(err)
35953665
}
35963666
}
@@ -3602,10 +3672,6 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
36023672
return err
36033673
}
36043674

3605-
for _, attr := range updateAttrs {
3606-
d.SetPartial(attr)
3607-
}
3608-
36093675
err = resource.Retry(5*readRetryTimeout, func() *resource.RetryError {
36103676
domainConfig, err := cdnService.DescribeDomainsConfigByDomain(ctx, domain)
36113677
if err != nil {
@@ -3725,6 +3791,47 @@ func resourceTencentCloudCdnDomainDelete(d *schema.ResourceData, meta interface{
37253791
return nil
37263792
}
37273793

3794+
func updateCdnModifyOnlyParams(d *schema.ResourceData, meta interface{}, ctx context.Context) error {
3795+
if !d.HasChanges("post_max_size") {
3796+
return nil
3797+
}
3798+
3799+
domain := d.Id()
3800+
client := meta.(*TencentCloudClient).apiV3Conn
3801+
service := CdnService{client}
3802+
request := cdn.NewUpdateDomainConfigRequest()
3803+
request.Domain = &domain
3804+
3805+
if v, ok := helper.InterfacesHeadMap(d, "post_max_size"); ok {
3806+
vSwitch := v["switch"].(string)
3807+
maxSize := v["max_size"].(int)
3808+
request.PostMaxSize = &cdn.PostSize{
3809+
Switch: &vSwitch,
3810+
}
3811+
if maxSize > 0 {
3812+
request.PostMaxSize.MaxSize = helper.IntInt64(maxSize * 1024 * 1024)
3813+
}
3814+
}
3815+
3816+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
3817+
err := service.UpdateDomainConfig(ctx, request)
3818+
if err != nil {
3819+
if sdkErr, ok := err.(*sdkErrors.TencentCloudSDKError); ok {
3820+
if sdkErr.Code == CDN_DOMAIN_CONFIG_ERROR {
3821+
return resource.NonRetryableError(err)
3822+
}
3823+
}
3824+
return retryError(err)
3825+
}
3826+
return nil
3827+
})
3828+
3829+
if err != nil {
3830+
return err
3831+
}
3832+
return nil
3833+
}
3834+
37283835
func checkCdnHeadMapOkAndChanged(d *schema.ResourceData, key string) (v map[string]interface{}, ok bool, changed bool) {
37293836
changed = d.HasChange(key)
37303837
v, ok = helper.InterfacesHeadMap(d, key)

tencentcloud/resource_tc_cdn_domain_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strings"
78
"testing"
89

910
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
@@ -112,6 +113,8 @@ func TestAccTencentCloudCdnDomainResource(t *testing.T) {
112113
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "status_code_cache.0.cache_rules.0.cache_time", "10"),
113114
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "status_code_cache.0.cache_rules.1.status_code", "404"),
114115
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "status_code_cache.0.cache_rules.1.cache_time", "10"),
116+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "post_max_size.0.switch", "on"),
117+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "post_max_size.0.max_size", "63"),
115118
),
116119
},
117120
{
@@ -129,6 +132,7 @@ func TestAccTencentCloudCdnDomainResource(t *testing.T) {
129132
"response_header",
130133
"downstream_capping",
131134
"origin_pull_optimization",
135+
"post_max_size",
132136
"referer",
133137
"max_age",
134138
"aws_private_access",
@@ -322,7 +326,13 @@ func testAccGetTestingDomain() (string, error) {
322326
if len(domains) == 0 {
323327
return "", nil
324328
}
325-
return *domains[0].DomainName, nil
329+
for i := range domains {
330+
item := domains[i]
331+
if v := *item.DomainName; strings.HasPrefix(v, "tencent") {
332+
return v, nil
333+
}
334+
}
335+
return "", fmt.Errorf("no available domain")
326336
}
327337

328338
func testAccCdnDomainVerify(domainPrefix string) error {
@@ -581,6 +591,10 @@ resource "tencentcloud_cdn_domain" "foo" {
581591
cache_time = 10
582592
}
583593
}
594+
post_max_size {
595+
switch = "on"
596+
max_size = 63
597+
}
584598
}
585599
`
586600

tencentcloud/service_tencentcloud_cdn.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ func (me *CdnService) DescribeDomainsConfigByDomain(ctx context.Context, domain
5454
return
5555
}
5656

57+
func (me *CdnService) UpdateDomainConfig(ctx context.Context, request *cdn.UpdateDomainConfigRequest) (errRet error) {
58+
logId := getLogId(ctx)
59+
defer func() {
60+
if errRet != nil {
61+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
62+
logId, request.GetAction(), request.ToJsonString(), errRet.Error())
63+
}
64+
}()
65+
66+
ratelimit.Check(request.GetAction())
67+
response, err := me.client.UseCdnClient().UpdateDomainConfig(request)
68+
69+
if err != nil {
70+
errRet = err
71+
return
72+
}
73+
74+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
75+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
76+
77+
return
78+
}
79+
5780
func (me *CdnService) DeleteDomain(ctx context.Context, domain string) error {
5881
logId := getLogId(ctx)
5982
request := cdn.NewDeleteCdnDomainRequest()

0 commit comments

Comments
 (0)