Skip to content

Commit 4384da3

Browse files
feat: dnspod - support record list datasource (#1398)
* feat: dnspod - support record list datasource * changelog 1398 * Update 1398.txt * fix: go.sum Co-authored-by: hellertang <hellertang@tencent.com>
1 parent 63336a7 commit 4384da3

File tree

14 files changed

+4942
-512
lines changed

14 files changed

+4942
-512
lines changed

.changelog/1398.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_dnspod_records: dnspod support record list datasource
3+
```

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ require (
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.445
3636
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199
3737
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.412
38-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.538
38+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.539
3939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445
4040
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488
4141
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335
4242
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc v1.0.199
4343
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb v1.0.527
44-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.294
44+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.539
4545
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.414
4646
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.287
4747
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.383

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,9 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.532 h1:DjnG
510510
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.532/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
511511
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.533 h1:UBHA9iYBQfkNYAWb6BixA8JCOQPxyMNMIVFdGjdM+1w=
512512
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.533/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
513-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.538 h1:z3LR9xqthQT11X0APmAdLpVjH95npnmHcu7FfrQp+PI=
514513
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.538/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
514+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.539 h1:5lhWDRRpLF1hErX0H/N4CA2VKEZdx4lxf8tGKj0sQAo=
515+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.539/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
515516
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445 h1:Bh7XD0ypNMHYyBOM8hhKsSu+y0VVKUnJVS+YKKhfpGg=
516517
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445/go.mod h1:jMDD351efCFpT1+KVFbcpu6SbmP4TYmp4qkoCfr63nQ=
517518
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488 h1:A1seXWtMf2atBjSNYvcwxyDoFzCMgqyVnsxnWzhqJEA=
@@ -524,6 +525,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb v1.0.527 h1:YBswG1
524525
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb v1.0.527/go.mod h1:VEKM4CcrzOVtNd8AYJvvNw/z3Sew7mUeekhArWyCo6c=
525526
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.294 h1:VKP8TRlMSDUIJ32GuFKr4a0Fv/oi4LksRVOfJoK9CVc=
526527
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.294/go.mod h1:CuOaLxOQr477GhMWAQPYQFUJrsZbW+ZqkAgP2uHDZXg=
528+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.539 h1:V+oo20w9aLxAoSSEGVplke2kq72MdUA4OuagCv4gsiY=
529+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.539/go.mod h1:gtPd3Hy8DA/4EErHNJliWeJB1sxZ9DhqenWiYZ3PF6g=
527530
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.414 h1:egwjvOEUKBaxsoRVn/YSEhp2E8qdh77Ous9A/wftDo0=
528531
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.414/go.mod h1:LRWnBUNRxs/Ee8MzJJd3eHzydlqyB5qmia975Ca/Ox0=
529532
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.287 h1:+9COBXAbQmL7aJ39Q/mF50Ykxq4m5kq/y6vbO+u3zgI=
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
/*
2+
Use this data source to query dnspod record list.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dnspod_records" "record" {
8+
domain = "example.com"
9+
subdomain = "www.example.com"
10+
}
11+
12+
output "result" {
13+
value = data.tencentcloud_dnspod_records.record.result
14+
}
15+
```
16+
17+
Use verbose filter
18+
19+
```hcl
20+
data "tencentcloud_dnspod_records" "record" {
21+
domain = "example.com"
22+
subdomain = "www.example.com"
23+
limit = 100
24+
record_type = "TXT"
25+
sort_field = "updated_on"
26+
sort_type = "DESC"
27+
}
28+
29+
output "result" {
30+
value = data.tencentcloud_dnspod_records.record.result
31+
}
32+
```
33+
34+
*/
35+
package tencentcloud
36+
37+
import (
38+
"context"
39+
40+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
41+
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
42+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
43+
)
44+
45+
func dataSourceTencentCloudDnspodRecords() *schema.Resource {
46+
return &schema.Resource{
47+
Read: dataSourceTencentCloudDnspodRead,
48+
Schema: map[string]*schema.Schema{
49+
"domain": {
50+
Description: "The domain for which DNS records are to be obtained.",
51+
Optional: true,
52+
Type: schema.TypeString,
53+
AtLeastOneOf: []string{"domain", "domain_id"},
54+
},
55+
"domain_id": {
56+
Description: "The ID of the domain for which DNS records are to be obtained. If DomainId is passed in, the system will omit the parameter domain.",
57+
Optional: true,
58+
Type: schema.TypeString,
59+
AtLeastOneOf: []string{"domain", "domain_id"},
60+
},
61+
"group_id": {
62+
Description: "The group ID.",
63+
Optional: true,
64+
Type: schema.TypeString,
65+
},
66+
"keyword": {
67+
Description: "The keyword for searching for DNS records. Host headers and record values are supported.",
68+
Optional: true,
69+
Type: schema.TypeString,
70+
},
71+
"limit": {
72+
Description: "The limit. It defaults to 100 and can be up to 3,000.",
73+
Optional: true,
74+
Type: schema.TypeInt,
75+
},
76+
"offset": {
77+
Description: "The offset. Default value: 0.",
78+
Optional: true,
79+
Type: schema.TypeInt,
80+
},
81+
"record_count_info": {
82+
Computed: true,
83+
Description: "Count info of the queried record list.",
84+
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
85+
"list_count": {
86+
Computed: true,
87+
Description: "The count of records returned in the list.",
88+
Type: schema.TypeInt,
89+
},
90+
"subdomain_count": {
91+
Computed: true,
92+
Description: "The subdomain count.",
93+
Type: schema.TypeInt,
94+
},
95+
"total_count": {
96+
Computed: true,
97+
Description: "The total record count.",
98+
Type: schema.TypeInt,
99+
},
100+
}},
101+
Type: schema.TypeList,
102+
},
103+
"record_line": {
104+
Description: "The split zone name.",
105+
Optional: true,
106+
Type: schema.TypeString,
107+
},
108+
"record_line_id": {
109+
Description: "The split zone ID. If `record_line_id` is passed in, the system will omit the parameter `record_line`.",
110+
Optional: true,
111+
Type: schema.TypeString,
112+
},
113+
"record_type": {
114+
Description: "The type of DNS record, such as A, CNAME, NS, AAAA, explicit URL, implicit URL, CAA, or SPF record.",
115+
Optional: true,
116+
Type: schema.TypeString,
117+
},
118+
"result": {
119+
Computed: true,
120+
Description: "The record list result.",
121+
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
122+
"line": {
123+
Computed: true,
124+
Description: "The record split zone.",
125+
Type: schema.TypeString,
126+
},
127+
"line_id": {
128+
Computed: true,
129+
Description: "The split zone ID.",
130+
Type: schema.TypeString,
131+
},
132+
"monitor_status": {
133+
Computed: true,
134+
Description: "The monitoring status of the record. Valid values: OK (normal), WARN (warning), and DOWN (downtime). It is empty if no monitoring is set or the monitoring is suspended.",
135+
Type: schema.TypeString,
136+
},
137+
"mx": {
138+
Computed: true,
139+
Description: "The MX value, applicable to the MX record only.\nNote: This field may return null, indicating that no valid values can be obtained.",
140+
Type: schema.TypeInt,
141+
},
142+
"name": {
143+
Computed: true,
144+
Description: "The host name.",
145+
Type: schema.TypeString,
146+
},
147+
"record_id": {
148+
Computed: true,
149+
Description: "Record ID.",
150+
Type: schema.TypeInt,
151+
},
152+
"remark": {
153+
Computed: true,
154+
Description: "The record remarks.",
155+
Type: schema.TypeString,
156+
},
157+
"status": {
158+
Computed: true,
159+
Description: "The record status. Valid values: ENABLE (enabled), DISABLE (disabled).",
160+
Type: schema.TypeString,
161+
},
162+
"ttl": {
163+
Computed: true,
164+
Description: "The record cache time.",
165+
Type: schema.TypeInt,
166+
},
167+
"type": {
168+
Computed: true,
169+
Description: "The record type.",
170+
Type: schema.TypeString,
171+
},
172+
"updated_on": {
173+
Computed: true,
174+
Description: "The update time.",
175+
Type: schema.TypeString,
176+
},
177+
"value": {
178+
Computed: true,
179+
Description: "The record value.",
180+
Type: schema.TypeString,
181+
},
182+
"weight": {
183+
Computed: true,
184+
Description: "The record weight, which is required for round-robin DNS records.",
185+
Type: schema.TypeInt,
186+
},
187+
}},
188+
Type: schema.TypeList,
189+
},
190+
"result_output_file": {
191+
Description: "Used for store query result as JSON.",
192+
Optional: true,
193+
Type: schema.TypeString,
194+
},
195+
"sort_field": {
196+
Description: "The sorting field. Available values: name, line, type, value, weight, mx, and ttl,updated_on.",
197+
Optional: true,
198+
Type: schema.TypeString,
199+
},
200+
"sort_type": {
201+
Description: "The sorting type. Valid values: ASC (ascending, default), DESC (descending).",
202+
Optional: true,
203+
Type: schema.TypeString,
204+
},
205+
"subdomain": {
206+
Description: "The host header of a DNS record. If this parameter is passed in, only the DNS record corresponding to this host header will be returned.",
207+
Optional: true,
208+
Type: schema.TypeString,
209+
},
210+
},
211+
}
212+
}
213+
func dataSourceTencentCloudDnspodRead(d *schema.ResourceData, meta interface{}) error {
214+
defer logElapsed("tencentcloud_dnspod_records.read")()
215+
logId := getLogId(contextNil)
216+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
217+
client := meta.(*TencentCloudClient).apiV3Conn
218+
service := DnspodService{client}
219+
220+
request := dnspod.NewDescribeRecordListRequest()
221+
if v, ok := d.GetOk("domain"); ok {
222+
request.Domain = helper.String(v.(string))
223+
}
224+
if v, ok := d.GetOk("domain_id"); ok {
225+
request.DomainId = helper.IntUint64(v.(int))
226+
}
227+
if v, ok := d.GetOk("subdomain"); ok {
228+
request.Subdomain = helper.String(v.(string))
229+
}
230+
if v, ok := d.GetOk("record_type"); ok {
231+
request.RecordType = helper.String(v.(string))
232+
}
233+
if v, ok := d.GetOk("record_line"); ok {
234+
request.RecordLine = helper.String(v.(string))
235+
}
236+
if v, ok := d.GetOk("record_line"); ok {
237+
request.RecordLineId = helper.String(v.(string))
238+
}
239+
if v, ok := d.GetOk("group_id"); ok {
240+
request.GroupId = helper.IntUint64(v.(int))
241+
}
242+
if v, ok := d.GetOk("keyword"); ok {
243+
request.Keyword = helper.String(v.(string))
244+
}
245+
if v, ok := d.GetOk("sort_field"); ok {
246+
request.SortField = helper.String(v.(string))
247+
}
248+
if v, ok := d.GetOk("sort_type"); ok {
249+
request.SortType = helper.String(v.(string))
250+
}
251+
if v, ok := d.GetOk("offset"); ok {
252+
request.Offset = helper.IntUint64(v.(int))
253+
}
254+
if v, ok := d.GetOk("limit"); ok {
255+
request.Limit = helper.IntUint64(v.(int))
256+
}
257+
258+
list, info, err := service.DescribeRecordList(ctx, request)
259+
260+
if err != nil {
261+
return err
262+
}
263+
264+
d.SetId("dnspod_records" + helper.DataResourceIdHash(request.ToJsonString()))
265+
266+
result := make([]map[string]interface{}, 0, len(list))
267+
for i := range list {
268+
record := list[i]
269+
result = append(result, map[string]interface{}{
270+
"line": record.Line,
271+
"line_id": record.LineId,
272+
"monitor_status": record.MonitorStatus,
273+
"mx": record.MX,
274+
"name": record.Name,
275+
"record_id": record.RecordId,
276+
"remark": record.Remark,
277+
"status": record.Status,
278+
"ttl": record.TTL,
279+
"type": record.Type,
280+
"updated_on": record.UpdatedOn,
281+
"value": record.Value,
282+
"weight": record.Weight,
283+
})
284+
}
285+
286+
err = helper.SetMapInterfaces(d, "record_count_info", map[string]interface{}{
287+
"list_count": info.ListCount,
288+
"subdomain_count": info.SubdomainCount,
289+
"total_count": info.TotalCount,
290+
})
291+
if err != nil {
292+
return err
293+
}
294+
295+
err = d.Set("result", result)
296+
if err != nil {
297+
return err
298+
}
299+
300+
output, ok := d.GetOk("result_output_file")
301+
if ok && output.(string) != "" {
302+
err = writeToFile(output.(string), map[string]interface{}{
303+
"record_count_info": info,
304+
"result": result,
305+
})
306+
if err != nil {
307+
return err
308+
}
309+
}
310+
311+
return nil
312+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudDataSourceDnsPodRecords(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
13+
Providers: testAccProviders,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccDnsPodRecordsBasic,
17+
Check: resource.ComposeAggregateTestCheckFunc(
18+
resource.TestCheckResourceAttrSet("data.tencentcloud_dnspod_records.record", "result.#"),
19+
resource.TestCheckResourceAttrSet("data.tencentcloud_dnspod_records.record", "result.0.record_id"),
20+
resource.TestCheckResourceAttrSet("data.tencentcloud_dnspod_records.record", "result.0.value"),
21+
),
22+
},
23+
},
24+
})
25+
}
26+
27+
const testAccDnsPodRecordsBasic = `
28+
data "tencentcloud_domains" "domains" {}
29+
30+
locals {
31+
domains = data.tencentcloud_domains.domains.list.*.domain_name
32+
target = [for i in local.domains: i if substr(i, 0, 7) == "tencent"]
33+
}
34+
35+
data "tencentcloud_dnspod_records" "record" {
36+
domain = local.target[0]
37+
sort_field = "updated_on"
38+
sort_type = "DESC"
39+
}
40+
`

0 commit comments

Comments
 (0)