Skip to content

Commit 80839cb

Browse files
authored
fix(private_dns): [116599618] fix PrivateDns Record (#2567)
* add * add * add * Update resource_tc_private_dns_record.go
1 parent 3baa584 commit 80839cb

7 files changed

+230
-103
lines changed

.changelog/2567.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_private_dns_record: params `zone_id` Add ForceNew
3+
```

tencentcloud/services/privatedns/resource_tc_private_dns_record.go

Lines changed: 95 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func ResourceTencentCloudPrivateDnsRecord() *schema.Resource {
2929
"zone_id": {
3030
Type: schema.TypeString,
3131
Required: true,
32+
ForceNew: true,
3233
Description: "Private domain ID.",
3334
},
3435
"record_type": {
@@ -42,10 +43,9 @@ func ResourceTencentCloudPrivateDnsRecord() *schema.Resource {
4243
Description: "Subdomain, such as \"www\", \"m\", and \"@\".",
4344
},
4445
"record_value": {
45-
Type: schema.TypeString,
46-
Required: true,
47-
Description: "Record value, such as IP: 192.168.10.2," +
48-
" CNAME: cname.qcloud.com, and MX: mail.qcloud.com..",
46+
Type: schema.TypeString,
47+
Required: true,
48+
Description: "Record value, such as IP: 192.168.10.2, CNAME: cname.qcloud.com, and MX: mail.qcloud.com.",
4949
},
5050
"weight": {
5151
Type: schema.TypeInt,
@@ -59,10 +59,10 @@ func ResourceTencentCloudPrivateDnsRecord() *schema.Resource {
5959
" Valid values: 5, 10, 15, 20, 30, 40, 50.",
6060
},
6161
"ttl": {
62-
Type: schema.TypeInt,
63-
Optional: true,
64-
Description: "Record cache time. The smaller the value, the faster the record will take effect." +
65-
" Value range: 1~86400s.",
62+
Type: schema.TypeInt,
63+
Optional: true,
64+
Computed: true,
65+
Description: "Record cache time. The smaller the value, the faster the record will take effect. Value range: 1~86400s.",
6666
},
6767
},
6868
}
@@ -71,42 +71,64 @@ func ResourceTencentCloudPrivateDnsRecord() *schema.Resource {
7171
func resourceTencentCloudDPrivateDnsRecordCreate(d *schema.ResourceData, meta interface{}) error {
7272
defer tccommon.LogElapsed("resource.tencentcloud_private_dns_record.create")()
7373

74-
logId := tccommon.GetLogId(tccommon.ContextNil)
74+
var (
75+
logId = tccommon.GetLogId(tccommon.ContextNil)
76+
request = privatedns.NewCreatePrivateZoneRecordRequest()
77+
response = privatedns.NewCreatePrivateZoneRecordResponse()
78+
zoneId string
79+
)
7580

76-
request := privatedns.NewCreatePrivateZoneRecordRequest()
77-
78-
zoneId := d.Get("zone_id").(string)
79-
request.ZoneId = &zoneId
81+
if v, ok := d.GetOk("zone_id"); ok {
82+
request.ZoneId = helper.String(v.(string))
83+
zoneId = v.(string)
84+
}
8085

81-
recordType := d.Get("record_type").(string)
82-
request.RecordType = &recordType
86+
if v, ok := d.GetOk("record_type"); ok {
87+
request.RecordType = helper.String(v.(string))
88+
}
8389

84-
subDomain := d.Get("sub_domain").(string)
85-
request.SubDomain = &subDomain
90+
if v, ok := d.GetOk("sub_domain"); ok {
91+
request.SubDomain = helper.String(v.(string))
92+
}
8693

87-
recordValue := d.Get("record_value").(string)
88-
request.RecordValue = &recordValue
94+
if v, ok := d.GetOk("record_value"); ok {
95+
request.RecordValue = helper.String(v.(string))
96+
}
8997

90-
if v, ok := d.GetOk("weight"); ok {
98+
if v, ok := d.GetOkExists("weight"); ok {
9199
request.Weight = helper.Int64(int64(v.(int)))
92100
}
93101

94-
if v, ok := d.GetOk("mx"); ok {
102+
if v, ok := d.GetOkExists("mx"); ok {
95103
request.MX = helper.Int64(int64(v.(int)))
96104
}
97-
if v, ok := d.GetOk("ttl"); ok {
105+
106+
if v, ok := d.GetOkExists("ttl"); ok {
98107
request.TTL = helper.Int64(int64(v.(int)))
99108
}
100109

101-
result, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivateDnsClient().CreatePrivateZoneRecord(request)
110+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
111+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivateDnsClient().CreatePrivateZoneRecord(request)
112+
if e != nil {
113+
return tccommon.RetryError(e)
114+
} else {
115+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
116+
}
117+
118+
if result == nil {
119+
e = fmt.Errorf("create PrivateDns record failed")
120+
return resource.NonRetryableError(e)
121+
}
122+
123+
response = result
124+
return nil
125+
})
102126

103127
if err != nil {
104128
log.Printf("[CRITAL]%s create PrivateDns record failed, reason:%s\n", logId, err.Error())
105129
return err
106130
}
107131

108-
response := result
109-
110132
recordId := *response.Response.RecordId
111133
d.SetId(strings.Join([]string{zoneId, recordId}, tccommon.FILED_SP))
112134

@@ -117,17 +139,17 @@ func resourceTencentCloudDPrivateDnsRecordRead(d *schema.ResourceData, meta inte
117139
defer tccommon.LogElapsed("resource.tencentcloud_private_dns_zone.read")()
118140
defer tccommon.InconsistentCheck(d, meta)()
119141

120-
logId := tccommon.GetLogId(tccommon.ContextNil)
121-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
122-
123-
service := PrivateDnsService{
124-
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
125-
}
142+
var (
143+
logId = tccommon.GetLogId(tccommon.ContextNil)
144+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
145+
service = PrivateDnsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
146+
)
126147

127148
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
128149
if len(idSplit) != 2 {
129150
return fmt.Errorf("record id strategy is can't read, id is borken, id is %s", d.Id())
130151
}
152+
131153
zoneId := idSplit[0]
132154
recordId := idSplit[1]
133155

@@ -169,73 +191,57 @@ func resourceTencentCloudDPrivateDnsRecordRead(d *schema.ResourceData, meta inte
169191
func resourceTencentCloudDPrivateDnsRecordUpdate(d *schema.ResourceData, meta interface{}) error {
170192
defer tccommon.LogElapsed("resource.tencentcloud_private_dns_record.update")()
171193

194+
var (
195+
logId = tccommon.GetLogId(tccommon.ContextNil)
196+
request = privatedns.NewModifyPrivateZoneRecordRequest()
197+
)
198+
172199
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
173200
if len(idSplit) != 2 {
174201
return fmt.Errorf("record id strategy is can't read, id is borken, id is %s", d.Id())
175202
}
176-
logId := tccommon.GetLogId(tccommon.ContextNil)
203+
177204
zoneId := idSplit[0]
178205
recordId := idSplit[1]
179206

180-
request := privatedns.NewModifyPrivateZoneRecordRequest()
181207
request.ZoneId = helper.String(zoneId)
182208
request.RecordId = helper.String(recordId)
183-
184-
needModify := false
185-
if d.HasChange("record_type") {
186-
needModify = true
209+
if v, ok := d.GetOk("record_type"); ok {
210+
request.RecordType = helper.String(v.(string))
187211
}
188212

189-
if d.HasChange("sub_domain") {
190-
needModify = true
213+
if v, ok := d.GetOk("sub_domain"); ok {
214+
request.SubDomain = helper.String(v.(string))
191215
}
192216

193-
if d.HasChange("record_value") {
194-
needModify = true
217+
if v, ok := d.GetOk("record_value"); ok {
218+
request.RecordValue = helper.String(v.(string))
195219
}
196220

197-
if d.HasChange("weight") {
198-
needModify = true
199-
if v, ok := d.GetOk("weight"); ok {
200-
request.Weight = helper.Int64(int64(v.(int)))
201-
}
221+
if v, ok := d.GetOk("weight"); ok {
222+
request.Weight = helper.Int64(int64(v.(int)))
202223
}
203224

204-
if d.HasChange("mx") {
205-
needModify = true
206-
if v, ok := d.GetOk("mx"); ok {
207-
request.MX = helper.Int64(int64(v.(int)))
208-
}
225+
if v, ok := d.GetOk("mx"); ok {
226+
request.MX = helper.Int64(int64(v.(int)))
209227
}
210228

211-
if d.HasChange("ttl") {
212-
needModify = true
213-
if v, ok := d.GetOk("ttl"); ok {
214-
request.TTL = helper.Int64(int64(v.(int)))
215-
}
229+
if v, ok := d.GetOk("ttl"); ok {
230+
request.TTL = helper.Int64(int64(v.(int)))
216231
}
217232

218-
if needModify {
219-
if v, ok := d.GetOk("record_type"); ok {
220-
request.RecordType = helper.String(v.(string))
221-
}
222-
if v, ok := d.GetOk("sub_domain"); ok {
223-
request.SubDomain = helper.String(v.(string))
224-
}
225-
if v, ok := d.GetOk("record_value"); ok {
226-
request.RecordValue = helper.String(v.(string))
227-
}
228-
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
229-
_, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivateDnsClient().ModifyPrivateZoneRecord(request)
230-
if e != nil {
231-
return tccommon.RetryError(e)
232-
}
233-
return nil
234-
})
235-
if err != nil {
236-
log.Printf("[CRITAL]%s modify privateDns record info failed, reason:%s\n", logId, err.Error())
237-
return err
233+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
234+
_, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivateDnsClient().ModifyPrivateZoneRecord(request)
235+
if e != nil {
236+
return tccommon.RetryError(e)
238237
}
238+
239+
return nil
240+
})
241+
242+
if err != nil {
243+
log.Printf("[CRITAL]%s modify privateDns record info failed, reason:%s\n", logId, err.Error())
244+
return err
239245
}
240246

241247
return resourceTencentCloudDPrivateDnsRecordRead(d, meta)
@@ -244,21 +250,22 @@ func resourceTencentCloudDPrivateDnsRecordUpdate(d *schema.ResourceData, meta in
244250
func resourceTencentCloudDPrivateDnsRecordDelete(d *schema.ResourceData, meta interface{}) error {
245251
defer tccommon.LogElapsed("resource.tencentcloud_private_dns_record.delete")()
246252

247-
logId := tccommon.GetLogId(tccommon.ContextNil)
253+
var (
254+
logId = tccommon.GetLogId(tccommon.ContextNil)
255+
request = privatedns.NewDescribePrivateZoneRequest()
256+
)
248257

249258
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
250259
if len(idSplit) != 2 {
251260
return fmt.Errorf("record id strategy is can't read, id is borken, id is %s", d.Id())
252261
}
262+
253263
zoneId := idSplit[0]
254264
recordId := idSplit[1]
255265

256266
// unbind
257-
request := privatedns.NewDescribePrivateZoneRequest()
258-
request.ZoneId = helper.String(zoneId)
259-
260267
var response *privatedns.DescribePrivateZoneResponse
261-
268+
request.ZoneId = helper.String(zoneId)
262269
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
263270
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePrivateDnsClient().DescribePrivateZone(request)
264271
if e != nil {
@@ -268,6 +275,7 @@ func resourceTencentCloudDPrivateDnsRecordDelete(d *schema.ResourceData, meta in
268275
response = result
269276
return nil
270277
})
278+
271279
if err != nil {
272280
log.Printf("[CRITAL]%s read private dns failed, reason:%s\n", logId, err.Error())
273281
return err
@@ -287,8 +295,10 @@ func resourceTencentCloudDPrivateDnsRecordDelete(d *schema.ResourceData, meta in
287295
if e != nil {
288296
return tccommon.RetryError(e)
289297
}
298+
290299
return nil
291300
})
301+
292302
if err != nil {
293303
log.Printf("[CRITAL]%s unbind privateDns zone vpc failed, reason:%s\n", logId, err.Error())
294304
return err
@@ -304,8 +314,10 @@ func resourceTencentCloudDPrivateDnsRecordDelete(d *schema.ResourceData, meta in
304314
if e != nil {
305315
return tccommon.RetryError(e)
306316
}
317+
307318
return nil
308319
})
320+
309321
if err != nil {
310322
log.Printf("[CRITAL]%s delete privateDns record failed, reason:%s\n", logId, err.Error())
311323
return err
@@ -333,8 +345,10 @@ func resourceTencentCloudDPrivateDnsRecordDelete(d *schema.ResourceData, meta in
333345
if e != nil {
334346
return tccommon.RetryError(e)
335347
}
348+
336349
return nil
337350
})
351+
338352
if err != nil {
339353
log.Printf("[CRITAL]%s rebind privateDns zone vpc failed, reason:%s\n", logId, err.Error())
340354
return err

tencentcloud/services/privatedns/resource_tc_private_dns_record.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,30 @@ Provide a resource to create a Private Dns Record.
33
Example Usage
44

55
```hcl
6-
resource "tencentcloud_private_dns_record" "foo" {
7-
zone_id = "zone-rqndjnki"
6+
resource "tencentcloud_vpc" "vpc" {
7+
name = "vpc-example"
8+
cidr_block = "10.0.0.0/16"
9+
}
10+
11+
resource "tencentcloud_private_dns_zone" "example" {
12+
domain = "domain.com"
13+
remark = "remark."
14+
15+
vpc_set {
16+
region = "ap-guangzhou"
17+
uniq_vpc_id = tencentcloud_vpc.vpc.id
18+
}
19+
20+
dns_forward_status = "DISABLED"
21+
cname_speedup_status = "ENABLED"
22+
23+
tags = {
24+
createdBy : "terraform"
25+
}
26+
}
27+
28+
resource "tencentcloud_private_dns_record" "example" {
29+
zone_id = tencentcloud_private_dns_zone.example.id
830
record_type = "A"
931
record_value = "192.168.1.2"
1032
sub_domain = "www"
@@ -19,5 +41,5 @@ Import
1941
Private Dns Record can be imported, e.g.
2042

2143
```
22-
$ terraform import tencentcloud_private_dns_zone.foo zone_id#record_id
44+
$ terraform import tencentcloud_private_dns_record.example zone-iza3a33s#1983030
2345
```

0 commit comments

Comments
 (0)