Skip to content

Commit d4c3f86

Browse files
authored
Merge pull request #2095 from tencentcloudstack/feat/private-dns-datasource
private-dns-datasource
2 parents e304803 + 8fa9520 commit d4c3f86

File tree

9 files changed

+374
-11
lines changed

9 files changed

+374
-11
lines changed

.changelog/2095.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_private_dns_records
3+
```
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
/*
2+
Use this data source to query detailed information of private dns records
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_private_dns_records" "private_dns_record" {
8+
zone_id = "zone-xxxxxx"
9+
filters {
10+
name = "Value"
11+
values = ["8.8.8.8"]
12+
}
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23+
privatedns "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns/v20201028"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
)
26+
27+
func dataSourceTencentCloudPrivateDnsRecords() *schema.Resource {
28+
return &schema.Resource{
29+
Read: dataSourceTencentCloudPrivateDnsRecordsRead,
30+
Schema: map[string]*schema.Schema{
31+
"zone_id": {
32+
Required: true,
33+
Type: schema.TypeString,
34+
Description: "Private zone id: zone-xxxxxx.",
35+
},
36+
37+
"filters": {
38+
Optional: true,
39+
Type: schema.TypeList,
40+
Description: "Filter parameters (Value and RecordType filtering are supported).",
41+
Elem: &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"name": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "Parameter name.",
47+
},
48+
"values": {
49+
Type: schema.TypeSet,
50+
Elem: &schema.Schema{
51+
Type: schema.TypeString,
52+
},
53+
Required: true,
54+
Description: "Parameter values.",
55+
},
56+
},
57+
},
58+
},
59+
60+
"record_set": {
61+
Computed: true,
62+
Type: schema.TypeList,
63+
Description: "Parse record list.",
64+
Elem: &schema.Resource{
65+
Schema: map[string]*schema.Schema{
66+
"record_id": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Record sid.",
70+
},
71+
"zone_id": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "Private zone id: zone-xxxxxx.",
75+
},
76+
"sub_domain": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "Subdomain name.",
80+
},
81+
"record_type": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "Record type, optional record type are: A, AAAA, CNAME, MX, TXT, PTR.",
85+
},
86+
"record_value": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "Record value.",
90+
},
91+
"ttl": {
92+
Type: schema.TypeInt,
93+
Computed: true,
94+
Description: "Record cache time, the smaller the value, the faster it takes effect. The value is 1-86400s. The default is 600.",
95+
},
96+
"mx": {
97+
Type: schema.TypeInt,
98+
Computed: true,
99+
Description: "MX priority: required if the record type is MX. Value range: 5,10,15,20,30,40,50.",
100+
},
101+
"status": {
102+
Type: schema.TypeString,
103+
Computed: true,
104+
Description: "Record status.",
105+
},
106+
"weight": {
107+
Type: schema.TypeInt,
108+
Computed: true,
109+
Description: "Record weight, value is 1-100.",
110+
},
111+
"created_on": {
112+
Type: schema.TypeString,
113+
Computed: true,
114+
Description: "Record creation time.",
115+
},
116+
"updated_on": {
117+
Type: schema.TypeString,
118+
Computed: true,
119+
Description: "Record update time.",
120+
},
121+
"extra": {
122+
Type: schema.TypeString,
123+
Computed: true,
124+
Description: "Additional information.",
125+
},
126+
"enabled": {
127+
Type: schema.TypeInt,
128+
Computed: true,
129+
Description: "Enabled. 0 meaning paused, 1 meaning senabled.",
130+
},
131+
},
132+
},
133+
},
134+
135+
"result_output_file": {
136+
Type: schema.TypeString,
137+
Optional: true,
138+
Description: "Used to save results.",
139+
},
140+
},
141+
}
142+
}
143+
144+
func dataSourceTencentCloudPrivateDnsRecordsRead(d *schema.ResourceData, meta interface{}) error {
145+
defer logElapsed("data_source.tencentcloud_private_dns_records.read")()
146+
defer inconsistentCheck(d, meta)()
147+
148+
logId := getLogId(contextNil)
149+
150+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
151+
152+
zoneId := d.Get("zone_id").(string)
153+
filterList := make([]*privatedns.Filter, 0)
154+
155+
if v, ok := d.GetOk("filters"); ok {
156+
filters := v.([]interface{})
157+
158+
for _, item := range filters {
159+
filter := privatedns.Filter{}
160+
filterMap := item.(map[string]interface{})
161+
162+
if v, ok := filterMap["name"]; ok {
163+
filter.Name = helper.String(v.(string))
164+
}
165+
if v, ok := filterMap["values"]; ok {
166+
valuesSet := v.(*schema.Set).List()
167+
filter.Values = helper.InterfacesStringsPoint(valuesSet)
168+
}
169+
filterList = append(filterList, &filter)
170+
}
171+
}
172+
173+
service := PrivateDnsService{client: meta.(*TencentCloudClient).apiV3Conn}
174+
175+
var recordSet []*privatedns.PrivateZoneRecord
176+
177+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
178+
result, e := service.DescribePrivateDnsRecordByFilter(ctx, zoneId, filterList)
179+
if e != nil {
180+
return retryError(e)
181+
}
182+
recordSet = result
183+
return nil
184+
})
185+
if err != nil {
186+
return err
187+
}
188+
189+
ids := make([]string, 0, len(recordSet))
190+
tmpList := make([]map[string]interface{}, 0, len(recordSet))
191+
192+
if recordSet != nil {
193+
for _, privateZoneRecord := range recordSet {
194+
privateZoneRecordMap := map[string]interface{}{}
195+
196+
if privateZoneRecord.RecordId != nil {
197+
privateZoneRecordMap["record_id"] = privateZoneRecord.RecordId
198+
}
199+
200+
if privateZoneRecord.ZoneId != nil {
201+
privateZoneRecordMap["zone_id"] = privateZoneRecord.ZoneId
202+
}
203+
204+
if privateZoneRecord.SubDomain != nil {
205+
privateZoneRecordMap["sub_domain"] = privateZoneRecord.SubDomain
206+
}
207+
208+
if privateZoneRecord.RecordType != nil {
209+
privateZoneRecordMap["record_type"] = privateZoneRecord.RecordType
210+
}
211+
212+
if privateZoneRecord.RecordValue != nil {
213+
privateZoneRecordMap["record_value"] = privateZoneRecord.RecordValue
214+
}
215+
216+
if privateZoneRecord.TTL != nil {
217+
privateZoneRecordMap["ttl"] = privateZoneRecord.TTL
218+
}
219+
220+
if privateZoneRecord.MX != nil {
221+
privateZoneRecordMap["mx"] = privateZoneRecord.MX
222+
}
223+
224+
if privateZoneRecord.Status != nil {
225+
privateZoneRecordMap["status"] = privateZoneRecord.Status
226+
}
227+
228+
if privateZoneRecord.Weight != nil {
229+
privateZoneRecordMap["weight"] = privateZoneRecord.Weight
230+
}
231+
232+
if privateZoneRecord.CreatedOn != nil {
233+
privateZoneRecordMap["created_on"] = privateZoneRecord.CreatedOn
234+
}
235+
236+
if privateZoneRecord.UpdatedOn != nil {
237+
privateZoneRecordMap["updated_on"] = privateZoneRecord.UpdatedOn
238+
}
239+
240+
if privateZoneRecord.Extra != nil {
241+
privateZoneRecordMap["extra"] = privateZoneRecord.Extra
242+
}
243+
244+
if privateZoneRecord.Enabled != nil {
245+
privateZoneRecordMap["enabled"] = privateZoneRecord.Enabled
246+
}
247+
248+
ids = append(ids, *privateZoneRecord.RecordId)
249+
tmpList = append(tmpList, privateZoneRecordMap)
250+
}
251+
252+
_ = d.Set("record_set", tmpList)
253+
}
254+
255+
d.SetId(helper.DataResourceIdsHash(ids))
256+
output, ok := d.GetOk("result_output_file")
257+
if ok && output.(string) != "" {
258+
if e := writeToFile(output.(string), tmpList); e != nil {
259+
return e
260+
}
261+
}
262+
return nil
263+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudPrivateDnsRecordsDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccPrivateDnsRecordsDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_private_dns_records.private_dns_record")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccPrivateDnsRecordsDataSource = `
26+
27+
data "tencentcloud_private_dns_records" "private_dns_record" {
28+
zone_id = "zone-6t11lof0"
29+
filters {
30+
name = "Value"
31+
values = ["8.8.8.8"]
32+
}
33+
}
34+
`

tencentcloud/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,8 @@ PrivateDNS
10941094
Resource
10951095
tencentcloud_private_dns_zone
10961096
tencentcloud_private_dns_record
1097+
Data Source
1098+
tencentcloud_private_dns_records
10971099
10981100
Cloud Log Service(CLS)
10991101
Resource
@@ -2255,6 +2257,7 @@ func Provider() *schema.Provider {
22552257
"tencentcloud_eb_bus": dataSourceTencentCloudEbBus(),
22562258
"tencentcloud_eb_event_rules": dataSourceTencentCloudEbEventRules(),
22572259
"tencentcloud_wedata_rule_templates": dataSourceTencentCloudWedataRuleTemplates(),
2260+
"tencentcloud_private_dns_records": dataSourceTencentCloudPrivateDnsRecords(),
22582261
},
22592262

22602263
ResourcesMap: map[string]*schema.Resource{

tencentcloud/resource_tc_private_dns_record.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func resourceTencentCloudDPrivateDnsRecordRead(d *schema.ResourceData, meta inte
154154
zoneId := idSplit[0]
155155
recordId := idSplit[1]
156156

157-
records, err := service.DescribePrivateDnsRecordByFilter(ctx, zoneId, "")
157+
records, err := service.DescribePrivateDnsRecordByFilter(ctx, zoneId, nil)
158158
if err != nil {
159159
return err
160160
}

tencentcloud/service_tencentcloud_private_dns.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8-
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
98

109
privatedns "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns/v20201028"
1110
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity"
@@ -20,7 +19,7 @@ type PrivateDnsService struct {
2019

2120
// ////////api
2221
func (me *PrivateDnsService) DescribePrivateDnsRecordByFilter(ctx context.Context, zoneId string,
23-
recordId string) (recordInfos []*privatedns.PrivateZoneRecord, errRet error) {
22+
filterList []*privatedns.Filter) (recordInfos []*privatedns.PrivateZoneRecord, errRet error) {
2423
logId := getLogId(ctx)
2524
request := privatedns.NewDescribePrivateZoneRecordListRequest()
2625
defer func() {
@@ -35,14 +34,9 @@ func (me *PrivateDnsService) DescribePrivateDnsRecordByFilter(ctx context.Contex
3534
total int64 = -1
3635
)
3736
request.ZoneId = &zoneId
38-
request.Filters = make([]*privatedns.Filter, 0)
3937

40-
if recordId != "" {
41-
filter := privatedns.Filter{
42-
Name: helper.String("RecordId"),
43-
Values: []*string{&recordId},
44-
}
45-
request.Filters = append(request.Filters, &filter)
38+
if filterList != nil {
39+
request.Filters = filterList
4640
}
4741

4842
getMoreData:

vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)