|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dnspod domain_analytics |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +
|
| 8 | + data "tencentcloud_dnspod_domain_analytics" "domain_analytics" { |
| 9 | + domain = "dnspod.cn" |
| 10 | + start_date = "2023-10-07" |
| 11 | + end_date = "2023-10-12" |
| 12 | + dns_format = "HOUR" |
| 13 | + # domain_id = 123 |
| 14 | + } |
| 15 | +
|
| 16 | +``` |
| 17 | +*/ |
| 18 | +package tencentcloud |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 25 | + dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323" |
| 26 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 27 | +) |
| 28 | + |
| 29 | +func dataSourceTencentCloudDnspodDomainAnalytics() *schema.Resource { |
| 30 | + return &schema.Resource{ |
| 31 | + Read: dataSourceTencentCloudDnspodDomainAnalyticsRead, |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "domain": { |
| 34 | + Required: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "The domain name to query for resolution volume.", |
| 37 | + }, |
| 38 | + |
| 39 | + "start_date": { |
| 40 | + Required: true, |
| 41 | + Type: schema.TypeString, |
| 42 | + Description: "The start date of the query, format: YYYY-MM-DD.", |
| 43 | + }, |
| 44 | + |
| 45 | + "end_date": { |
| 46 | + Required: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "The end date of the query, format: YYYY-MM-DD.", |
| 49 | + }, |
| 50 | + |
| 51 | + "dns_format": { |
| 52 | + Optional: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "DATE: Statistics by day dimension HOUR: Statistics by hour dimension.", |
| 55 | + }, |
| 56 | + |
| 57 | + "domain_id": { |
| 58 | + Optional: true, |
| 59 | + Type: schema.TypeInt, |
| 60 | + Description: "Domain ID. The parameter DomainId has a higher priority than the parameter Domain. If the parameter DomainId is passed, the parameter Domain will be ignored. You can find all Domains and DomainIds through the DescribeDomainList interface.", |
| 61 | + }, |
| 62 | + |
| 63 | + "data": { |
| 64 | + Computed: true, |
| 65 | + Type: schema.TypeList, |
| 66 | + Description: "Subtotal of resolution volume for the current statistical dimension.", |
| 67 | + Elem: &schema.Resource{ |
| 68 | + Schema: map[string]*schema.Schema{ |
| 69 | + "num": { |
| 70 | + Type: schema.TypeInt, |
| 71 | + Computed: true, |
| 72 | + Description: "Subtotal of resolution volume for the current statistical dimension.", |
| 73 | + }, |
| 74 | + "date_key": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "For daily statistics, it is the statistical date.", |
| 78 | + }, |
| 79 | + "hour_key": { |
| 80 | + Type: schema.TypeInt, |
| 81 | + Computed: true, |
| 82 | + Description: "For hourly statistics, it is the hour of the current time (0-23), for example, when HourKey is 23, the statistical period is the resolution volume from 22:00 to 23:00. Note: This field may return null, indicating that no valid value can be obtained.", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + |
| 88 | + "info": { |
| 89 | + Computed: true, |
| 90 | + Type: schema.TypeList, |
| 91 | + Description: "Domain resolution volume statistics query information.", |
| 92 | + Elem: &schema.Resource{ |
| 93 | + Schema: map[string]*schema.Schema{ |
| 94 | + "dns_format": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "DATE: Statistics by day dimension HOUR: Statistics by hour dimension.", |
| 98 | + }, |
| 99 | + "dns_total": { |
| 100 | + Type: schema.TypeInt, |
| 101 | + Computed: true, |
| 102 | + Description: "Total resolution volume for the current statistical period.", |
| 103 | + }, |
| 104 | + "domain": { |
| 105 | + Type: schema.TypeString, |
| 106 | + Computed: true, |
| 107 | + Description: "The domain name currently being queried.", |
| 108 | + }, |
| 109 | + "start_date": { |
| 110 | + Type: schema.TypeString, |
| 111 | + Computed: true, |
| 112 | + Description: "Start time of the current statistical period.", |
| 113 | + }, |
| 114 | + "end_date": { |
| 115 | + Type: schema.TypeString, |
| 116 | + Computed: true, |
| 117 | + Description: "End time of the current statistical period.", |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + |
| 123 | + "alias_data": { |
| 124 | + Computed: true, |
| 125 | + Type: schema.TypeList, |
| 126 | + Description: "Domain alias resolution volume statistics information.", |
| 127 | + Elem: &schema.Resource{ |
| 128 | + Schema: map[string]*schema.Schema{ |
| 129 | + "info": { |
| 130 | + Type: schema.TypeList, |
| 131 | + Computed: true, |
| 132 | + Description: "Domain resolution volume statistics query information.", |
| 133 | + Elem: &schema.Resource{ |
| 134 | + Schema: map[string]*schema.Schema{ |
| 135 | + "dns_format": { |
| 136 | + Type: schema.TypeString, |
| 137 | + Computed: true, |
| 138 | + Description: "DATE: Statistics by day dimension HOUR: Statistics by hour dimension.", |
| 139 | + }, |
| 140 | + "dns_total": { |
| 141 | + Type: schema.TypeInt, |
| 142 | + Computed: true, |
| 143 | + Description: "Total resolution volume for the current statistical period.", |
| 144 | + }, |
| 145 | + "domain": { |
| 146 | + Type: schema.TypeString, |
| 147 | + Computed: true, |
| 148 | + Description: "The domain name currently being queried.", |
| 149 | + }, |
| 150 | + "start_date": { |
| 151 | + Type: schema.TypeString, |
| 152 | + Computed: true, |
| 153 | + Description: "Start time of the current statistical period.", |
| 154 | + }, |
| 155 | + "end_date": { |
| 156 | + Type: schema.TypeString, |
| 157 | + Computed: true, |
| 158 | + Description: "End time of the current statistical period.", |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + "data": { |
| 164 | + Type: schema.TypeList, |
| 165 | + Computed: true, |
| 166 | + Description: "Subtotal of resolution volume for the current statistical dimension.", |
| 167 | + Elem: &schema.Resource{ |
| 168 | + Schema: map[string]*schema.Schema{ |
| 169 | + "num": { |
| 170 | + Type: schema.TypeInt, |
| 171 | + Computed: true, |
| 172 | + Description: "Subtotal of resolution volume for the current statistical dimension.", |
| 173 | + }, |
| 174 | + "date_key": { |
| 175 | + Type: schema.TypeString, |
| 176 | + Computed: true, |
| 177 | + Description: "For daily statistics, it is the statistical date.", |
| 178 | + }, |
| 179 | + "hour_key": { |
| 180 | + Type: schema.TypeInt, |
| 181 | + Computed: true, |
| 182 | + Description: "For hourly statistics, it is the hour of the current time (0-23), for example, when HourKey is 23, the statistical period is the resolution volume from 22:00 to 23:00. Note: This field may return null, indicating that no valid value can be obtained.", |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + |
| 191 | + "result_output_file": { |
| 192 | + Type: schema.TypeString, |
| 193 | + Optional: true, |
| 194 | + Description: "Used to save results.", |
| 195 | + }, |
| 196 | + }, |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +func dataSourceTencentCloudDnspodDomainAnalyticsRead(d *schema.ResourceData, meta interface{}) error { |
| 201 | + defer logElapsed("data_source.tencentcloud_dnspod_domain_analytics.read")() |
| 202 | + defer inconsistentCheck(d, meta)() |
| 203 | + |
| 204 | + logId := getLogId(contextNil) |
| 205 | + |
| 206 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 207 | + var ( |
| 208 | + domain string |
| 209 | + aliasData []*dnspod.DomainAliasAnalyticsItem |
| 210 | + data []*dnspod.DomainAnalyticsDetail |
| 211 | + info *dnspod.DomainAnalyticsInfo |
| 212 | + err error |
| 213 | + ) |
| 214 | + |
| 215 | + paramMap := make(map[string]interface{}) |
| 216 | + if v, ok := d.GetOk("domain"); ok { |
| 217 | + domain = v.(string) |
| 218 | + paramMap["Domain"] = helper.String(v.(string)) |
| 219 | + } |
| 220 | + |
| 221 | + if v, ok := d.GetOk("start_date"); ok { |
| 222 | + paramMap["StartDate"] = helper.String(v.(string)) |
| 223 | + } |
| 224 | + |
| 225 | + if v, ok := d.GetOk("end_date"); ok { |
| 226 | + paramMap["EndDate"] = helper.String(v.(string)) |
| 227 | + } |
| 228 | + |
| 229 | + if v, ok := d.GetOk("dns_format"); ok { |
| 230 | + paramMap["DnsFormat"] = helper.String(v.(string)) |
| 231 | + } |
| 232 | + |
| 233 | + if v, ok := d.GetOkExists("domain_id"); ok { |
| 234 | + paramMap["DomainId"] = helper.IntUint64(v.(int)) |
| 235 | + } |
| 236 | + |
| 237 | + service := DnspodService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 238 | + e := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 239 | + aliasData, data, info, err = service.DescribeDnspodDomainAnalyticsByFilter(ctx, paramMap) |
| 240 | + if err != nil { |
| 241 | + return retryError(err) |
| 242 | + } |
| 243 | + return nil |
| 244 | + }) |
| 245 | + if e != nil { |
| 246 | + return e |
| 247 | + } |
| 248 | + |
| 249 | + // ids := make([]string, 0, len(data)) |
| 250 | + tmpDataList := make([]map[string]interface{}, 0, len(data)) |
| 251 | + tmpAliasDataList := make([]map[string]interface{}, 0, len(aliasData)) |
| 252 | + |
| 253 | + if data != nil { |
| 254 | + for _, domainAnalyticsDetail := range data { |
| 255 | + domainAnalyticsDetailMap := map[string]interface{}{} |
| 256 | + |
| 257 | + if domainAnalyticsDetail.Num != nil { |
| 258 | + domainAnalyticsDetailMap["num"] = domainAnalyticsDetail.Num |
| 259 | + } |
| 260 | + |
| 261 | + if domainAnalyticsDetail.DateKey != nil { |
| 262 | + domainAnalyticsDetailMap["date_key"] = domainAnalyticsDetail.DateKey |
| 263 | + } |
| 264 | + |
| 265 | + if domainAnalyticsDetail.HourKey != nil { |
| 266 | + domainAnalyticsDetailMap["hour_key"] = domainAnalyticsDetail.HourKey |
| 267 | + } |
| 268 | + |
| 269 | + // ids = append(ids, *domainAnalyticsDetail.Domain) |
| 270 | + tmpDataList = append(tmpDataList, domainAnalyticsDetailMap) |
| 271 | + } |
| 272 | + |
| 273 | + _ = d.Set("data", tmpDataList) |
| 274 | + } |
| 275 | + |
| 276 | + if info != nil { |
| 277 | + domainAnalyticsInfoMap := map[string]interface{}{} |
| 278 | + |
| 279 | + if info.DnsFormat != nil { |
| 280 | + domainAnalyticsInfoMap["dns_format"] = info.DnsFormat |
| 281 | + } |
| 282 | + |
| 283 | + if info.DnsTotal != nil { |
| 284 | + domainAnalyticsInfoMap["dns_total"] = info.DnsTotal |
| 285 | + } |
| 286 | + |
| 287 | + if info.Domain != nil { |
| 288 | + domainAnalyticsInfoMap["domain"] = info.Domain |
| 289 | + } |
| 290 | + |
| 291 | + if info.StartDate != nil { |
| 292 | + domainAnalyticsInfoMap["start_date"] = info.StartDate |
| 293 | + } |
| 294 | + |
| 295 | + if info.EndDate != nil { |
| 296 | + domainAnalyticsInfoMap["end_date"] = info.EndDate |
| 297 | + } |
| 298 | + |
| 299 | + // ids = append(ids, *info.Domain) |
| 300 | + // _ = d.Set("info", domainAnalyticsInfoMap) |
| 301 | + e = helper.SetMapInterfaces(d, "info", domainAnalyticsInfoMap) |
| 302 | + if e != nil { |
| 303 | + return e |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + if aliasData != nil { |
| 308 | + for _, domainAliasAnalyticsItem := range aliasData { |
| 309 | + domainAliasAnalyticsItemMap := map[string]interface{}{} |
| 310 | + |
| 311 | + if domainAliasAnalyticsItem.Info != nil { |
| 312 | + infoMap := map[string]interface{}{} |
| 313 | + |
| 314 | + if domainAliasAnalyticsItem.Info.DnsFormat != nil { |
| 315 | + infoMap["dns_format"] = domainAliasAnalyticsItem.Info.DnsFormat |
| 316 | + } |
| 317 | + |
| 318 | + if domainAliasAnalyticsItem.Info.DnsTotal != nil { |
| 319 | + infoMap["dns_total"] = domainAliasAnalyticsItem.Info.DnsTotal |
| 320 | + } |
| 321 | + |
| 322 | + if domainAliasAnalyticsItem.Info.Domain != nil { |
| 323 | + infoMap["domain"] = domainAliasAnalyticsItem.Info.Domain |
| 324 | + } |
| 325 | + |
| 326 | + if domainAliasAnalyticsItem.Info.StartDate != nil { |
| 327 | + infoMap["start_date"] = domainAliasAnalyticsItem.Info.StartDate |
| 328 | + } |
| 329 | + |
| 330 | + if domainAliasAnalyticsItem.Info.EndDate != nil { |
| 331 | + infoMap["end_date"] = domainAliasAnalyticsItem.Info.EndDate |
| 332 | + } |
| 333 | + |
| 334 | + domainAliasAnalyticsItemMap["info"] = []interface{}{infoMap} |
| 335 | + } |
| 336 | + |
| 337 | + if domainAliasAnalyticsItem.Data != nil { |
| 338 | + dataList := []interface{}{} |
| 339 | + for _, data := range domainAliasAnalyticsItem.Data { |
| 340 | + dataMap := map[string]interface{}{} |
| 341 | + |
| 342 | + if data.Num != nil { |
| 343 | + dataMap["num"] = data.Num |
| 344 | + } |
| 345 | + |
| 346 | + if data.DateKey != nil { |
| 347 | + dataMap["date_key"] = data.DateKey |
| 348 | + } |
| 349 | + |
| 350 | + if data.HourKey != nil { |
| 351 | + dataMap["hour_key"] = data.HourKey |
| 352 | + } |
| 353 | + |
| 354 | + dataList = append(dataList, dataMap) |
| 355 | + } |
| 356 | + |
| 357 | + domainAliasAnalyticsItemMap["data"] = []interface{}{dataList} |
| 358 | + } |
| 359 | + |
| 360 | + // ids = append(ids, *domainAliasAnalyticsItem.Domain) |
| 361 | + tmpAliasDataList = append(tmpAliasDataList, domainAliasAnalyticsItemMap) |
| 362 | + } |
| 363 | + |
| 364 | + _ = d.Set("alias_data", tmpAliasDataList) |
| 365 | + } |
| 366 | + |
| 367 | + d.SetId(helper.DataResourceIdHash(domain)) |
| 368 | + output, ok := d.GetOk("result_output_file") |
| 369 | + if ok && output.(string) != "" { |
| 370 | + e = writeToFile(output.(string), map[string]interface{}{ |
| 371 | + "info": info, |
| 372 | + "data": data, |
| 373 | + "alias_data": aliasData, |
| 374 | + }) |
| 375 | + if e != nil { |
| 376 | + return e |
| 377 | + } |
| 378 | + } |
| 379 | + return nil |
| 380 | +} |
0 commit comments