|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of antiddos overview_cc_trend |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_antiddos_overview_cc_trend" "overview_cc_trend" { |
| 8 | + period = 300 |
| 9 | + start_time = "2023-11-20 00:00:00" |
| 10 | + end_time = "2023-11-21 00:00:00" |
| 11 | + metric_name = "inqps" |
| 12 | + business = "bgpip" |
| 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 | + antiddos "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/antiddos/v20200309" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func dataSourceTencentCloudAntiddosOverviewCcTrend() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Read: dataSourceTencentCloudAntiddosOverviewCcTrendRead, |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "period": { |
| 32 | + Required: true, |
| 33 | + Type: schema.TypeInt, |
| 34 | + Description: "Statistical granularity, values [300 (5 minutes), 3600 (hours), 86400 (days)].", |
| 35 | + }, |
| 36 | + |
| 37 | + "start_time": { |
| 38 | + Required: true, |
| 39 | + Type: schema.TypeString, |
| 40 | + Description: "StartTime.", |
| 41 | + }, |
| 42 | + |
| 43 | + "end_time": { |
| 44 | + Required: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "EndTime.", |
| 47 | + }, |
| 48 | + |
| 49 | + "metric_name": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Indicator, values [inqps (peak total requests, dropqps (peak attack requests)), incount (number of requests), dropcount (number of attacks)].", |
| 53 | + }, |
| 54 | + |
| 55 | + "business": { |
| 56 | + Optional: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Dayu sub product code (bgpip represents advanced defense IP; net represents professional version of advanced defense IP).", |
| 59 | + }, |
| 60 | + |
| 61 | + "ip_list": { |
| 62 | + Optional: true, |
| 63 | + Type: schema.TypeSet, |
| 64 | + Elem: &schema.Schema{ |
| 65 | + Type: schema.TypeString, |
| 66 | + }, |
| 67 | + Description: "resource id list.", |
| 68 | + }, |
| 69 | + |
| 70 | + "data": { |
| 71 | + Computed: true, |
| 72 | + Type: schema.TypeSet, |
| 73 | + Elem: &schema.Schema{ |
| 74 | + Type: schema.TypeInt, |
| 75 | + }, |
| 76 | + Description: "Data.", |
| 77 | + }, |
| 78 | + |
| 79 | + "result_output_file": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Optional: true, |
| 82 | + Description: "Used to save results.", |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func dataSourceTencentCloudAntiddosOverviewCcTrendRead(d *schema.ResourceData, meta interface{}) error { |
| 89 | + defer logElapsed("data_source.tencentcloud_antiddos_overview_cc_trend.read")() |
| 90 | + defer inconsistentCheck(d, meta)() |
| 91 | + |
| 92 | + logId := getLogId(contextNil) |
| 93 | + |
| 94 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 95 | + |
| 96 | + paramMap := make(map[string]interface{}) |
| 97 | + if v, _ := d.GetOk("period"); v != nil { |
| 98 | + paramMap["Period"] = helper.IntInt64(v.(int)) |
| 99 | + } |
| 100 | + |
| 101 | + if v, ok := d.GetOk("start_time"); ok { |
| 102 | + paramMap["StartTime"] = helper.String(v.(string)) |
| 103 | + } |
| 104 | + |
| 105 | + if v, ok := d.GetOk("end_time"); ok { |
| 106 | + paramMap["EndTime"] = helper.String(v.(string)) |
| 107 | + } |
| 108 | + |
| 109 | + if v, ok := d.GetOk("metric_name"); ok { |
| 110 | + paramMap["MetricName"] = helper.String(v.(string)) |
| 111 | + } |
| 112 | + |
| 113 | + if v, ok := d.GetOk("business"); ok { |
| 114 | + paramMap["Business"] = helper.String(v.(string)) |
| 115 | + } |
| 116 | + |
| 117 | + if v, ok := d.GetOk("ip_list"); ok { |
| 118 | + ipListSet := v.(*schema.Set).List() |
| 119 | + paramMap["IpList"] = helper.InterfacesStringsPoint(ipListSet) |
| 120 | + } |
| 121 | + |
| 122 | + service := AntiddosService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 123 | + var overviewCCTrendResponseParams *antiddos.DescribeOverviewCCTrendResponseParams |
| 124 | + |
| 125 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 126 | + result, e := service.DescribeAntiddosOverviewCcTrendByFilter(ctx, paramMap) |
| 127 | + if e != nil { |
| 128 | + return retryError(e) |
| 129 | + } |
| 130 | + overviewCCTrendResponseParams = result |
| 131 | + return nil |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + d.SetId(helper.BuildToken()) |
| 138 | + resultMap := make(map[string]interface{}) |
| 139 | + |
| 140 | + if overviewCCTrendResponseParams != nil { |
| 141 | + _ = d.Set("data", overviewCCTrendResponseParams.Data) |
| 142 | + resultMap["data"] = overviewCCTrendResponseParams.Data |
| 143 | + } |
| 144 | + output, ok := d.GetOk("result_output_file") |
| 145 | + if ok && output.(string) != "" { |
| 146 | + if e := writeToFile(output.(string), resultMap); e != nil { |
| 147 | + return e |
| 148 | + } |
| 149 | + } |
| 150 | + return nil |
| 151 | +} |
0 commit comments