|
| 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 | +} |
0 commit comments