Skip to content

Commit b44c0ab

Browse files
tongyimingmikatong
andauthored
feat: support dnspod record (#891)
Co-authored-by: mikatong <mikatong@tencent.com>
1 parent e62be8d commit b44c0ab

File tree

7 files changed

+474
-0
lines changed

7 files changed

+474
-0
lines changed

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.351/go.mod
494494
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.353 h1:rFkcKuLFxt2xolSWbyeznetTBw9Cyhe1ycILmEkE1Pk=
495495
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.353/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
496496
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.357/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
497+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.358/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
497498
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.359 h1:m7Ga+AAWcngpWVIU6TjtyJ2MZupZvyY4soTNKDYQVVs=
498499
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.359/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
499500
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199 h1:ajgJogYSIQ5u1PIbiV5nsvr5K0fYpm1/T7Dy+mxEM6U=

tencentcloud/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ EMR
586586
DNSPOD
587587
Resource
588588
tencentcloud_dnspod_domain_instance
589+
tencentcloud_dnspod_record
589590
590591
PrivateDNS
591592
Resource
@@ -621,10 +622,13 @@ const (
621622
ACCOUNT_TYPE_INTERNATION = "INTERNATION"
622623
ACCOUNT_TYPE_PREPAY = "PREPAY"
623624
ACCOUNT_TYPE_COMMON = "COMMON"
625+
ACCOUNT_TYPE_PRIVATE = "PRIVATE"
624626
INTERNATION_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_INTERNATION"
625627
INTERNATION_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_INTERNATION"
626628
PREPAY_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_PREPAY"
627629
PREPAY_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_PREPAY"
630+
PRIVATE_PROVIDER_SECRET_ID = "TENCENTCLOUD_SECRET_ID_PRIVATE"
631+
PRIVATE_PROVIDER_SECRET_KEY = "TENCENTCLOUD_SECRET_KEY_PRIVATE"
628632
)
629633

630634
type TencentCloudClient struct {
@@ -1063,6 +1067,7 @@ func Provider() terraform.ResourceProvider {
10631067
"tencentcloud_ssm_secret_version": resourceTencentCloudSsmSecretVersion(),
10641068
"tencentcloud_cdh_instance": resourceTencentCloudCdhInstance(),
10651069
"tencentcloud_dnspod_domain_instance": resourceTencentCloudDnspodDomainInstance(),
1070+
"tencentcloud_dnspod_record": resourceTencentCloudDnspodRecord(),
10661071
"tencentcloud_private_dns_zone": resourceTencentCloudPrivateDnsZone(),
10671072
"tencentcloud_private_dns_record": resourceTencentCloudPrivateDnsRecord(),
10681073
},

tencentcloud/provider_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ func testAccPreCheckCommon(t *testing.T, accountType string) {
6666
}
6767
os.Setenv(PROVIDER_SECRET_ID, secretId)
6868
os.Setenv(PROVIDER_SECRET_KEY, secretKey)
69+
case accountType == ACCOUNT_TYPE_PRIVATE:
70+
secretId := os.Getenv(PRIVATE_PROVIDER_SECRET_ID)
71+
secretKey := os.Getenv(PRIVATE_PROVIDER_SECRET_KEY)
72+
if secretId == "" || secretKey == "" {
73+
t.Fatalf("%v and %v must be set for acceptance tests\n", PRIVATE_PROVIDER_SECRET_ID, PRIVATE_PROVIDER_SECRET_KEY)
74+
}
75+
os.Setenv(PROVIDER_SECRET_ID, secretId)
76+
os.Setenv(PROVIDER_SECRET_KEY, secretKey)
6977
default:
7078
if v := os.Getenv(PROVIDER_SECRET_ID); v == "" {
7179
t.Fatalf("%v must be set for acceptance tests\n", PROVIDER_SECRET_ID)
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
/*
2+
Provide a resource to create a DnsPod record.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_dnspod_record" "demo" {
8+
domain="mikatong.com"
9+
record_type="A"
10+
record_line="默认"
11+
value="1.2.3.9"
12+
sub_domain="demo"
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"fmt"
20+
"log"
21+
"strconv"
22+
"strings"
23+
24+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
25+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
26+
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
27+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
28+
)
29+
30+
func resourceTencentCloudDnspodRecord() *schema.Resource {
31+
return &schema.Resource{
32+
Create: resourceTencentCloudDnspodRecordCreate,
33+
Read: resourceTencentCloudDnspodRecordRead,
34+
Update: resourceTencentCloudDnspodRecordUpdate,
35+
Delete: resourceTencentCloudDnspodRecordDelete,
36+
37+
Schema: map[string]*schema.Schema{
38+
"domain": {
39+
Type: schema.TypeString,
40+
Required: true,
41+
Description: "The Domain.",
42+
},
43+
"record_type": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "The record type.",
47+
},
48+
"record_line": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
Description: "The record line.",
52+
},
53+
"value": {
54+
Type: schema.TypeString,
55+
Required: true,
56+
Description: "The record value.",
57+
},
58+
"sub_domain": {
59+
Type: schema.TypeString,
60+
Optional: true,
61+
Default: "@",
62+
Description: "The host records, default value is `@`.",
63+
},
64+
"mx": {
65+
Type: schema.TypeInt,
66+
Optional: true,
67+
Description: "MX priority, valid when the record type is MX, range 1-20. Note: must set when record type equal MX.",
68+
},
69+
"ttl": {
70+
Type: schema.TypeInt,
71+
Optional: true,
72+
Default: 600,
73+
Description: "TTL, the range is 1-604800, and the minimum value of different levels of domain names is different. Default is 600.",
74+
},
75+
"weight": {
76+
Type: schema.TypeInt,
77+
Optional: true,
78+
Default: 0,
79+
Description: "Weight information. An integer from 0 to 100. Only enterprise VIP domain names are available, 0 means off, does not pass this parameter, means that the weight information is not set. Default is 0.",
80+
},
81+
"status": {
82+
Type: schema.TypeString,
83+
Optional: true,
84+
Default: "ENABLE",
85+
Description: "Records the initial state, with values ranging from ENABLE and DISABLE. The default is ENABLE, and if DISABLE is passed in, resolution will not take effect and the limits of load balancing will not be verified.",
86+
},
87+
"monitor_status": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
Description: "The D monitoring status of the record.",
91+
},
92+
},
93+
}
94+
}
95+
96+
func resourceTencentCloudDnspodRecordCreate(d *schema.ResourceData, meta interface{}) error {
97+
defer logElapsed("resource.tencentcloud_dnspod_record.create")()
98+
logId := getLogId(contextNil)
99+
request := dnspod.NewCreateRecordRequest()
100+
101+
domain := d.Get("domain").(string)
102+
recordType := d.Get("record_type").(string)
103+
recordLine := d.Get("record_line").(string)
104+
value := d.Get("value").(string)
105+
subDomain := d.Get("sub_domain").(string)
106+
ttl := d.Get("ttl").(int)
107+
status := d.Get("status").(string)
108+
request.Domain = &domain
109+
request.RecordType = &recordType
110+
request.RecordLine = &recordLine
111+
request.Value = &value
112+
request.SubDomain = &subDomain
113+
if v, ok := d.GetOk("mx"); ok {
114+
request.MX = helper.IntUint64(v.(int))
115+
}
116+
request.TTL = helper.IntUint64(ttl)
117+
if v, ok := d.GetOk("weight"); ok {
118+
request.Weight = helper.IntUint64(v.(int))
119+
}
120+
request.Status = &status
121+
122+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
123+
response, e := meta.(*TencentCloudClient).apiV3Conn.UseDnsPodClient().CreateRecord(request)
124+
if e != nil {
125+
return retryError(e)
126+
}
127+
recordId := *response.Response.RecordId
128+
129+
d.SetId(domain + FILED_SP + fmt.Sprint(recordId))
130+
131+
return nil
132+
})
133+
if err != nil {
134+
log.Printf("[CRITAL]%s create DnsPod record failed, reason:%s\n", logId, err.Error())
135+
return err
136+
}
137+
138+
return resourceTencentCloudDnspodRecordRead(d, meta)
139+
}
140+
141+
func resourceTencentCloudDnspodRecordRead(d *schema.ResourceData, meta interface{}) error {
142+
defer logElapsed("resource.tencentcloud_dnspod_record.read")()
143+
defer inconsistentCheck(d, meta)()
144+
145+
logId := getLogId(contextNil)
146+
147+
id := d.Id()
148+
items := strings.Split(id, FILED_SP)
149+
if len(items) < 2 {
150+
return nil
151+
}
152+
request := dnspod.NewDescribeRecordRequest()
153+
request.Domain = helper.String(items[0])
154+
recordId, err := strconv.Atoi(items[1])
155+
if err != nil {
156+
return err
157+
}
158+
request.RecordId = helper.IntUint64(recordId)
159+
160+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
161+
response, e := meta.(*TencentCloudClient).apiV3Conn.UseDnsPodClient().DescribeRecord(request)
162+
if e != nil {
163+
return retryError(e)
164+
}
165+
166+
recordInfo := response.Response.RecordInfo
167+
168+
_ = d.Set("sub_domain", recordInfo.SubDomain)
169+
_ = d.Set("mx", recordInfo.MX)
170+
_ = d.Set("ttl", recordInfo.TTL)
171+
_ = d.Set("monitor_status", recordInfo.MonitorStatus)
172+
_ = d.Set("weight", recordInfo.Weight)
173+
if *recordInfo.Enabled == uint64(0) {
174+
_ = d.Set("status", "DISABLE")
175+
} else {
176+
_ = d.Set("status", "ENABLE")
177+
}
178+
179+
return nil
180+
})
181+
if err != nil {
182+
log.Printf("[CRITAL]%s read DnsPod record failed, reason:%s\n", logId, err.Error())
183+
return err
184+
}
185+
return nil
186+
}
187+
188+
func resourceTencentCloudDnspodRecordUpdate(d *schema.ResourceData, meta interface{}) error {
189+
defer logElapsed("resource.tencentcloud_dnspod_record.update")()
190+
191+
id := d.Id()
192+
items := strings.Split(id, FILED_SP)
193+
if len(items) < 2 {
194+
return nil
195+
}
196+
domain := items[0]
197+
recordId, err := strconv.Atoi(items[1])
198+
if err != nil {
199+
return err
200+
}
201+
request := dnspod.NewModifyRecordRequest()
202+
request.Domain = &domain
203+
request.RecordId = helper.IntUint64(recordId)
204+
recordType := d.Get("record_type").(string)
205+
recordLine := d.Get("record_line").(string)
206+
value := d.Get("value").(string)
207+
subDomain := d.Get("sub_domain").(string)
208+
request.RecordType = &recordType
209+
request.RecordLine = &recordLine
210+
request.Value = &value
211+
request.SubDomain = &subDomain
212+
213+
d.SetPartial("record_type")
214+
d.SetPartial("record_line")
215+
d.SetPartial("value")
216+
d.SetPartial("sub_domain")
217+
218+
if d.HasChange("status") {
219+
status := d.Get("status").(string)
220+
request.Status = &status
221+
d.SetPartial("status")
222+
}
223+
if d.HasChange("mx") {
224+
if v, ok := d.GetOk("mx"); ok {
225+
request.MX = helper.IntUint64(v.(int))
226+
d.SetPartial("mx")
227+
}
228+
}
229+
if d.HasChange("ttl") {
230+
ttl := d.Get("ttl").(int)
231+
request.TTL = helper.IntUint64(ttl)
232+
d.SetPartial("ttl")
233+
}
234+
if d.HasChange("weight") {
235+
weight := d.Get("weight").(int)
236+
request.TTL = helper.IntUint64(weight)
237+
d.SetPartial("weight")
238+
239+
}
240+
d.Partial(true)
241+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
242+
_, e := meta.(*TencentCloudClient).apiV3Conn.UseDnsPodClient().ModifyRecord(request)
243+
if e != nil {
244+
return retryError(e)
245+
}
246+
return nil
247+
})
248+
249+
if err != nil {
250+
return err
251+
}
252+
d.Partial(false)
253+
return resourceTencentCloudDnspodRecordRead(d, meta)
254+
}
255+
256+
func resourceTencentCloudDnspodRecordDelete(d *schema.ResourceData, meta interface{}) error {
257+
defer logElapsed("resource.tencentcloud_dnspod_record.delete")()
258+
259+
logId := getLogId(contextNil)
260+
id := d.Id()
261+
items := strings.Split(id, FILED_SP)
262+
if len(items) < 2 {
263+
return nil
264+
}
265+
domain := items[0]
266+
recordId, err := strconv.Atoi(items[1])
267+
if err != nil {
268+
return err
269+
}
270+
request := dnspod.NewDeleteRecordRequest()
271+
request.Domain = helper.String(domain)
272+
request.RecordId = helper.IntUint64(recordId)
273+
274+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
275+
_, e := meta.(*TencentCloudClient).apiV3Conn.UseDnsPodClient().DeleteRecord(request)
276+
if e != nil {
277+
return retryError(e)
278+
}
279+
return nil
280+
})
281+
if err != nil {
282+
log.Printf("[CRITAL]%s delete DnsPod record failed, reason:%s\n", logId, err.Error())
283+
return err
284+
}
285+
return nil
286+
}

0 commit comments

Comments
 (0)