Skip to content

Commit 7ee6243

Browse files
authored
fix(cos): [124583875] tencentcloud_cos_bucket_domain_certificate_attachment optmize code logic (#3402)
* add * add
1 parent 4f15a0d commit 7ee6243

File tree

5 files changed

+148
-61
lines changed

5 files changed

+148
-61
lines changed

.changelog/3402.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_domain_certificate_attachment: optmize code logic
3+
```

tencentcloud/services/cos/resource_tc_cos_bucket_domain_certificate_attachment.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,40 @@ func ResourceTencentCloudCosBucketDomainCertificateAttachment() *schema.Resource
4646
Type: schema.TypeList,
4747
MaxItems: 1,
4848
Required: true,
49+
ForceNew: true,
4950
Description: "Certificate info.",
5051
Elem: &schema.Resource{
5152
Schema: map[string]*schema.Schema{
5253
"cert_type": {
5354
Type: schema.TypeString,
5455
Required: true,
56+
ForceNew: true,
5557
Description: "Certificate type.",
5658
},
5759
"custom_cert": {
5860
Type: schema.TypeList,
5961
MaxItems: 1,
6062
Required: true,
63+
ForceNew: true,
6164
Description: "Custom certificate.",
6265
Elem: &schema.Resource{
6366
Schema: map[string]*schema.Schema{
6467
"cert_id": {
6568
Type: schema.TypeString,
6669
Optional: true,
70+
ForceNew: true,
6771
Description: "ID of certificate.",
6872
},
6973
"cert": {
7074
Type: schema.TypeString,
7175
Required: true,
76+
ForceNew: true,
7277
Description: "Public key of certificate.",
7378
},
7479
"private_key": {
7580
Type: schema.TypeString,
7681
Required: true,
82+
ForceNew: true,
7783
Description: "Private key of certificate.",
7884
},
7985
},
@@ -85,6 +91,7 @@ func ResourceTencentCloudCosBucketDomainCertificateAttachment() *schema.Resource
8591
"domain": {
8692
Type: schema.TypeString,
8793
Required: true,
94+
ForceNew: true,
8895
Description: "The name of domain.",
8996
},
9097
},
@@ -98,9 +105,11 @@ func resourceTencentCloudCosBucketDomainCertificateAttachmentCreate(d *schema.Re
98105
defer tccommon.LogElapsed("resource.tencentcloud_cos_bucket_domain_certificate_attachment.create")()
99106
defer tccommon.InconsistentCheck(d, meta)()
100107

101-
logId := tccommon.GetLogId(tccommon.ContextNil)
102-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
103-
var bucket string
108+
var (
109+
logId = tccommon.GetLogId(tccommon.ContextNil)
110+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
111+
bucket string
112+
)
104113

105114
if v, ok := d.GetOk("bucket"); ok {
106115
bucket = v.(string)
@@ -115,19 +124,24 @@ func resourceTencentCloudCosBucketDomainCertificateAttachmentCreate(d *schema.Re
115124
if v, ok := certMap["cert_type"]; ok {
116125
certificateInfo.CertType = v.(string)
117126
}
127+
118128
if CustomCertMap, ok := helper.InterfaceToMap(certMap, "custom_cert"); ok {
119129
customCert := cos.BucketDomainCustomCert{}
120130
if v, ok := CustomCertMap["cert_id"]; ok {
121131
customCert.CertId = v.(string)
122132
}
133+
123134
if v, ok := CustomCertMap["cert"]; ok {
124135
customCert.Cert = v.(string)
125136
}
137+
126138
if v, ok := CustomCertMap["private_key"]; ok {
127139
customCert.PrivateKey = v.(string)
128140
}
141+
129142
certificateInfo.CustomCert = &customCert
130143
}
144+
131145
option.CertificateInfo = &certificateInfo
132146
}
133147

@@ -141,10 +155,14 @@ func resourceTencentCloudCosBucketDomainCertificateAttachmentCreate(d *schema.Re
141155
if e != nil {
142156
return tccommon.RetryError(e)
143157
} else {
158+
if result == nil || result.Response == nil {
159+
return resource.NonRetryableError(fmt.Errorf("Create cos domain certificate failed, Response is nil."))
160+
}
161+
144162
request, _ := xml.Marshal(option)
145-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
146-
logId, "PutDomainCertificate", request, result.Response.Body)
163+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, "PutDomainCertificate", request, result.Response.Body)
147164
}
165+
148166
return nil
149167
})
150168

@@ -153,25 +171,23 @@ func resourceTencentCloudCosBucketDomainCertificateAttachmentCreate(d *schema.Re
153171
return err
154172
}
155173

156-
ids := strings.Join([]string{bucket, option.DomainList[0]}, tccommon.FILED_SP)
157-
d.SetId(ids)
158-
174+
d.SetId(strings.Join([]string{bucket, option.DomainList[0]}, tccommon.FILED_SP))
159175
return nil
160176
}
161177

162178
func resourceTencentCloudCosBucketDomainCertificateAttachmentRead(d *schema.ResourceData, meta interface{}) error {
163179
defer tccommon.LogElapsed("resource.tencentcloud_cos_bucket_domain_certificate_attachment.read")()
164180
defer tccommon.InconsistentCheck(d, meta)()
165181

166-
logId := tccommon.GetLogId(tccommon.ContextNil)
167-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
168-
169-
service := CosService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
170-
171-
id := d.Id()
182+
var (
183+
logId = tccommon.GetLogId(tccommon.ContextNil)
184+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
185+
service = CosService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
186+
id = d.Id()
187+
)
172188

173189
certResult, bucket, err := service.DescribeCosBucketDomainCertificate(ctx, id)
174-
log.Printf("[DEBUG] resource `bucketDomainCertificate certResult:%s`\n", certResult)
190+
log.Printf("[DEBUG] resource `bucketDomainCertificate certResult: %s`\n", certResult)
175191
if err != nil {
176192
return err
177193
}
@@ -187,14 +203,15 @@ func resourceTencentCloudCosBucketDomainCertificateAttachmentRead(d *schema.Reso
187203
}
188204

189205
func resourceTencentCloudCosBucketDomainCertificateAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
190-
id := d.Id()
191-
defer tccommon.LogElapsed("resource.tencentcloud_cos_bucket_domain_certificate_attachment.delete id:", id)()
206+
defer tccommon.LogElapsed("resource.tencentcloud_cos_bucket_domain_certificate_attachment.delete")()
192207
defer tccommon.InconsistentCheck(d, meta)()
193208

194-
logId := tccommon.GetLogId(tccommon.ContextNil)
195-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
196-
197-
service := CosService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
209+
var (
210+
logId = tccommon.GetLogId(tccommon.ContextNil)
211+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
212+
service = CosService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
213+
id = d.Id()
214+
)
198215

199216
if err := service.DeleteCosBucketDomainCertificate(ctx, id); err != nil {
200217
return err

tencentcloud/services/cos/resource_tc_cos_bucket_domain_certificate_attachment.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Provides a resource to attach/detach the corresponding certificate for the domai
55
Example Usage
66

77
```hcl
8+
variable "custom_origin_domain" {
9+
default = "tf.example.com"
10+
}
11+
812
data "tencentcloud_user_info" "info" {}
913
1014
locals {
@@ -15,18 +19,41 @@ resource "tencentcloud_cos_bucket" "example" {
1519
bucket = "private-bucket-${local.app_id}"
1620
acl = "private"
1721
force_clean = true
22+
23+
origin_domain_rules {
24+
domain = var.custom_origin_domain
25+
status = "ENABLED"
26+
type = "REST"
27+
}
1828
}
1929
2030
resource "tencentcloud_cos_bucket_domain_certificate_attachment" "example" {
2131
bucket = tencentcloud_cos_bucket.example.id
2232
domain_certificate {
23-
domain = "www.example.com"
33+
domain = var.custom_origin_domain
2434
certificate {
2535
cert_type = "CustomCert"
2636
custom_cert {
27-
cert_id = "Mbx45wts"
28-
cert = "-----BEGIN CERTIFICATE-----"
29-
private_key = "-----BEGIN RSA PRIVATE_KEY-----"
37+
cert_id = "JG65alUy"
38+
cert = <<-EOF
39+
-----BEGIN CERTIFICATE-----
40+
MIIGQjCCBSqgAwIBAgIQfTllN2vZr7vcoGF3ZTHwxjANBgkqhkiG9w0BAQsFADBA
41+
...
42+
...
43+
...
44+
9YSJrdvskqI3v/3SkVezzNiWQMuMTg==
45+
-----END CERTIFICATE-----
46+
EOF
47+
48+
private_key = <<-EOF
49+
-----BEGIN RSA PRIVATE KEY-----
50+
MIIEpQIBAAKCAQEAsmwAXXVh6N4fd281K0671jYBrSV2v/5+TCeewsNx6ys3kC8o
51+
...
52+
...
53+
...
54+
MgbOv6byAafSQWU+5+KFfK3Nj7eezx6yfQQM0Kxl4ZPm1w3Fb6gIFBc=
55+
-----END RSA PRIVATE KEY-----
56+
EOF
3057
}
3158
}
3259
}

tencentcloud/services/cos/service_tencentcloud_cos.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ func (me *CosService) DeleteBucketReplication(ctx context.Context, bucket string
16181618
return
16191619
}
16201620

1621-
func (me *CosService) DescribeCosBucketDomainCertificate(ctx context.Context, certId string) (result *cos.BucketGetDomainCertificateResult, bucket string, errRet error) {
1621+
func (me *CosService) DescribeCosBucketDomainCertificate(ctx context.Context, certId string) (res *cos.BucketGetDomainCertificateResult, bucket string, errRet error) {
16221622
logId := tccommon.GetLogId(ctx)
16231623

16241624
ids, err := me.parseCertId(certId)
@@ -1636,26 +1636,32 @@ func (me *CosService) DescribeCosBucketDomainCertificate(ctx context.Context, ce
16361636

16371637
defer func() {
16381638
if errRet != nil {
1639-
log.Printf("[CRITAL]%s api[%s] fail, request[%s], reason[%s]\n",
1640-
logId, "GetDomainCertificate", request, errRet.Error())
1639+
log.Printf("[CRITAL]%s api[%s] fail, request[%s], reason[%s]\n", logId, "GetDomainCertificate", request, errRet.Error())
16411640
}
16421641
}()
16431642

1644-
result, response, err := me.client.UseTencentCosClient(bucket).Bucket.GetDomainCertificate(ctx, option)
1645-
resp, _ := json.Marshal(response.Response.Body)
1646-
if response.StatusCode == 404 {
1647-
log.Printf("[WARN]%s, api[%s] returns %d", logId, "GetDomainCertificate", response.StatusCode)
1648-
return
1649-
}
1643+
errRet = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
1644+
result, response, e := me.client.UseTencentCosClient(bucket).Bucket.GetDomainCertificate(ctx, option)
1645+
if e != nil {
1646+
return tccommon.RetryError(e)
1647+
} else {
1648+
if response.StatusCode == 404 {
1649+
log.Printf("[WARN]%s, api[%s] returns %d", logId, "GetDomainCertificate", response.StatusCode)
1650+
return resource.NonRetryableError(fmt.Errorf("Get domain certificate failed, Status code is 404."))
1651+
}
16501652

1651-
if err != nil {
1652-
errRet = err
1653+
resp, _ := json.Marshal(response.Response.Body)
1654+
log.Printf("[DEBUG]%s api[%s] success, request [%s], response body [%s], result [%s]\n", logId, "GetDomainCertificate", request, resp, result)
1655+
res = result
1656+
}
1657+
1658+
return nil
1659+
})
1660+
1661+
if errRet != nil {
16531662
return
16541663
}
16551664

1656-
log.Printf("[DEBUG]%s api[%s] success, request [%s], response body [%s], result [%s]\n",
1657-
logId, "GetDomainCertificate", request, resp, result)
1658-
16591665
return
16601666
}
16611667

@@ -1676,23 +1682,30 @@ func (me *CosService) DeleteCosBucketDomainCertificate(ctx context.Context, cert
16761682

16771683
defer func() {
16781684
if errRet != nil {
1679-
log.Printf("[CRITAL]%s api[%s] fail, option [%s], reason[%s]\n",
1680-
logId, "DeleteDomainCertificate", option, errRet.Error())
1685+
log.Printf("[CRITAL]%s api[%s] fail, option [%s], reason[%s]\n", logId, "DeleteDomainCertificate", option, errRet.Error())
16811686
}
16821687
}()
16831688

1684-
ratelimit.Check("DeleteDomainCertificate")
1685-
response, err := me.client.UseTencentCosClient(bucket).Bucket.DeleteDomainCertificate(ctx, option)
1689+
errRet = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1690+
ratelimit.Check("DeleteDomainCertificate")
1691+
result, e := me.client.UseTencentCosClient(bucket).Bucket.DeleteDomainCertificate(ctx, option)
1692+
if e != nil {
1693+
return tccommon.RetryError(e)
1694+
} else {
1695+
if result == nil || result.Response == nil {
1696+
return resource.NonRetryableError(fmt.Errorf("Delete cos domain certificate failed, Response is nil."))
1697+
}
16861698

1687-
if err != nil {
1688-
errRet = err
1689-
return err
1690-
}
1699+
resp, _ := json.Marshal(result.Response.Body)
1700+
log.Printf("[DEBUG]%s api[%s] success, option [%s], response body [%s]\n", logId, "DeleteDomainCertificate", option, resp)
1701+
}
16911702

1692-
resp, _ := json.Marshal(response.Response.Body)
1703+
return nil
1704+
})
16931705

1694-
log.Printf("[DEBUG]%s api[%s] success, option [%s], response body [%s]\n",
1695-
logId, "DeleteDomainCertificate", option, resp)
1706+
if errRet != nil {
1707+
return
1708+
}
16961709

16971710
return
16981711
}

0 commit comments

Comments
 (0)