|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cat metric_data |
| 3 | +Example Usage |
| 4 | +```hcl |
| 5 | +data "tencentcloud_cat_metric_data" "metric_data" { |
| 6 | + analyze_task_type = "AnalyzeTaskType_Network" |
| 7 | + metric_type = "gauge" |
| 8 | + field = "avg(\"ping_time\")" |
| 9 | + filters = [ |
| 10 | + "\"host\" = 'www.qq.com'", |
| 11 | + "time >= now()-1h", |
| 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 | + cat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat/v20180409" |
| 24 | + sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 25 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceTencentCloudCatMetricData() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceTencentCloudCatMetricDataRead, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "analyze_task_type": { |
| 33 | + Required: true, |
| 34 | + Type: schema.TypeString, |
| 35 | + Description: "Analysis of task type, supported types: `AnalyzeTaskType_Network`: network quality, `AnalyzeTaskType_Browse`: page performance, `AnalyzeTaskType_Transport`: port performance, `AnalyzeTaskType_UploadDownload`: file transport, `AnalyzeTaskType_MediaStream`: audiovisual experience.", |
| 36 | + }, |
| 37 | + |
| 38 | + "metric_type": { |
| 39 | + Required: true, |
| 40 | + Type: schema.TypeString, |
| 41 | + Description: "Metric type, metrics queries are passed with gauge by default.", |
| 42 | + }, |
| 43 | + |
| 44 | + "field": { |
| 45 | + Required: true, |
| 46 | + Type: schema.TypeString, |
| 47 | + Description: "Detailed fields of metrics, specified metrics can be passed or aggregate metrics, such as avg(ping_time) means entire delay.", |
| 48 | + }, |
| 49 | + |
| 50 | + "filter": { |
| 51 | + Optional: true, |
| 52 | + Type: schema.TypeString, |
| 53 | + Description: "Filter conditions can be passed as a single filter or multiple parameters concatenated together.", |
| 54 | + }, |
| 55 | + |
| 56 | + "group_by": { |
| 57 | + Optional: true, |
| 58 | + Type: schema.TypeString, |
| 59 | + Description: "Aggregation time, such as 1m, 1d, 30d, and so on.", |
| 60 | + }, |
| 61 | + |
| 62 | + "filters": { |
| 63 | + Required: true, |
| 64 | + Type: schema.TypeSet, |
| 65 | + Elem: &schema.Schema{ |
| 66 | + Type: schema.TypeString, |
| 67 | + }, |
| 68 | + Description: "Multiple condition filtering, supports combining multiple filtering conditions for query.", |
| 69 | + }, |
| 70 | + |
| 71 | + "metric_set": { |
| 72 | + Computed: true, |
| 73 | + Type: schema.TypeString, |
| 74 | + Description: "Return JSON string.", |
| 75 | + }, |
| 76 | + |
| 77 | + "result_output_file": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Optional: true, |
| 80 | + Description: "Used to save results.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func dataSourceTencentCloudCatMetricDataRead(d *schema.ResourceData, meta interface{}) error { |
| 87 | + defer logElapsed("data_source.tencentcloud_cat_metric_data.read")() |
| 88 | + defer inconsistentCheck(d, meta)() |
| 89 | + |
| 90 | + logId := getLogId(contextNil) |
| 91 | + |
| 92 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 93 | + |
| 94 | + paramMap := make(map[string]interface{}) |
| 95 | + if v, ok := d.GetOk("analyze_task_type"); ok { |
| 96 | + paramMap["AnalyzeTaskType"] = helper.String(v.(string)) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("metric_type"); ok { |
| 100 | + paramMap["MetricType"] = helper.String(v.(string)) |
| 101 | + } |
| 102 | + |
| 103 | + if v, ok := d.GetOk("field"); ok { |
| 104 | + paramMap["Field"] = helper.String(v.(string)) |
| 105 | + } |
| 106 | + |
| 107 | + if v, ok := d.GetOk("filter"); ok { |
| 108 | + paramMap["Filter"] = helper.String(v.(string)) |
| 109 | + } |
| 110 | + |
| 111 | + if v, ok := d.GetOk("group_by"); ok { |
| 112 | + paramMap["GroupBy"] = helper.String(v.(string)) |
| 113 | + } |
| 114 | + |
| 115 | + if v, ok := d.GetOk("filters"); ok { |
| 116 | + filtersSet := v.(*schema.Set).List() |
| 117 | + paramMap["Filters"] = helper.InterfacesStringsPoint(filtersSet) |
| 118 | + } |
| 119 | + |
| 120 | + service := CatService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 121 | + |
| 122 | + var metric *cat.DescribeProbeMetricDataResponseParams |
| 123 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 124 | + result, e := service.DescribeCatMetricDataByFilter(ctx, paramMap) |
| 125 | + if e != nil { |
| 126 | + if sdkError, ok := e.(*sdkErrors.TencentCloudSDKError); ok { |
| 127 | + if sdkError.Code == "FailedOperation.DbQueryFailed" { |
| 128 | + return resource.NonRetryableError(e) |
| 129 | + } |
| 130 | + } |
| 131 | + return retryError(e) |
| 132 | + } |
| 133 | + metric = result |
| 134 | + return nil |
| 135 | + }) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + var metricSet string |
| 141 | + if metric != nil && metric.MetricSet != nil { |
| 142 | + metricSet = *metric.MetricSet |
| 143 | + _ = d.Set("metric_set", metric.MetricSet) |
| 144 | + } |
| 145 | + |
| 146 | + d.SetId(helper.DataResourceIdsHash([]string{metricSet})) |
| 147 | + output, ok := d.GetOk("result_output_file") |
| 148 | + if ok && output.(string) != "" { |
| 149 | + if e := writeToFile(output.(string), metricSet); e != nil { |
| 150 | + return e |
| 151 | + } |
| 152 | + } |
| 153 | + return nil |
| 154 | +} |
0 commit comments