|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of antiddos basic_device_status |
| 3 | +
|
| 4 | +# Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +
|
| 8 | +data "tencentcloud_antiddos_basic_device_status" "basic_device_status" { |
| 9 | + ip_list = [ |
| 10 | + "127.0.0.1" |
| 11 | + ] |
| 12 | + filter_region = 1 |
| 13 | +} |
| 14 | +
|
| 15 | +``` |
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | + antiddos "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/antiddos/v20200309" |
| 25 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceTencentCloudAntiddosBasicDeviceStatus() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceTencentCloudAntiddosBasicDeviceStatusRead, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "ip_list": { |
| 33 | + Optional: true, |
| 34 | + Type: schema.TypeSet, |
| 35 | + Elem: &schema.Schema{ |
| 36 | + Type: schema.TypeString, |
| 37 | + }, |
| 38 | + Description: "Ip resource list.", |
| 39 | + }, |
| 40 | + |
| 41 | + "id_list": { |
| 42 | + Optional: true, |
| 43 | + Type: schema.TypeSet, |
| 44 | + Elem: &schema.Schema{ |
| 45 | + Type: schema.TypeString, |
| 46 | + }, |
| 47 | + Description: "Named resource transfer ID.", |
| 48 | + }, |
| 49 | + |
| 50 | + "filter_region": { |
| 51 | + Optional: true, |
| 52 | + Type: schema.TypeInt, |
| 53 | + Description: "Region Id.", |
| 54 | + }, |
| 55 | + |
| 56 | + "data": { |
| 57 | + Computed: true, |
| 58 | + Type: schema.TypeList, |
| 59 | + Description: "Return resources and status, status code: 1- Blocking status 2- Normal status 3- Attack status.", |
| 60 | + Elem: &schema.Resource{ |
| 61 | + Schema: map[string]*schema.Schema{ |
| 62 | + "key": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "Properties name.", |
| 66 | + }, |
| 67 | + "value": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Properties value.", |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + |
| 76 | + "c_l_b_data": { |
| 77 | + Computed: true, |
| 78 | + Type: schema.TypeList, |
| 79 | + Description: "Note: This field may return null, indicating that a valid value cannot be obtained.", |
| 80 | + Elem: &schema.Resource{ |
| 81 | + Schema: map[string]*schema.Schema{ |
| 82 | + "key": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "Properties name.", |
| 86 | + }, |
| 87 | + "value": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "Properties value.", |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + |
| 96 | + "result_output_file": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Optional: true, |
| 99 | + Description: "Used to save results.", |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func dataSourceTencentCloudAntiddosBasicDeviceStatusRead(d *schema.ResourceData, meta interface{}) error { |
| 106 | + defer logElapsed("data_source.tencentcloud_antiddos_basic_device_status.read")() |
| 107 | + defer inconsistentCheck(d, meta)() |
| 108 | + |
| 109 | + logId := getLogId(contextNil) |
| 110 | + |
| 111 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 112 | + |
| 113 | + paramMap := make(map[string]interface{}) |
| 114 | + if v, ok := d.GetOk("ip_list"); ok { |
| 115 | + ipListSet := v.(*schema.Set).List() |
| 116 | + paramMap["IpList"] = helper.InterfacesStringsPoint(ipListSet) |
| 117 | + } |
| 118 | + |
| 119 | + if v, ok := d.GetOk("id_list"); ok { |
| 120 | + idListSet := v.(*schema.Set).List() |
| 121 | + paramMap["IdList"] = helper.InterfacesStringsPoint(idListSet) |
| 122 | + } |
| 123 | + |
| 124 | + if v, ok := d.GetOkExists("filter_region"); ok { |
| 125 | + paramMap["FilterRegion"] = helper.IntUint64(v.(int)) |
| 126 | + } |
| 127 | + |
| 128 | + service := AntiddosService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 129 | + |
| 130 | + var basicDeviceStatus *antiddos.DescribeBasicDeviceStatusResponseParams |
| 131 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 132 | + result, e := service.DescribeAntiddosBasicDeviceStatusByFilter(ctx, paramMap) |
| 133 | + if e != nil { |
| 134 | + return retryError(e) |
| 135 | + } |
| 136 | + basicDeviceStatus = result |
| 137 | + return nil |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + tmpList := make([]map[string]interface{}, 0) |
| 144 | + |
| 145 | + if basicDeviceStatus.Data != nil { |
| 146 | + for _, keyValue := range basicDeviceStatus.Data { |
| 147 | + keyValueMap := map[string]interface{}{} |
| 148 | + if keyValue.Key != nil { |
| 149 | + keyValueMap["key"] = keyValue.Key |
| 150 | + } |
| 151 | + if keyValue.Value != nil { |
| 152 | + keyValueMap["value"] = keyValue.Value |
| 153 | + } |
| 154 | + tmpList = append(tmpList, keyValueMap) |
| 155 | + } |
| 156 | + _ = d.Set("data", tmpList) |
| 157 | + } |
| 158 | + |
| 159 | + if basicDeviceStatus.CLBData != nil { |
| 160 | + for _, keyValue := range basicDeviceStatus.CLBData { |
| 161 | + keyValueMap := map[string]interface{}{} |
| 162 | + if keyValue.Key != nil { |
| 163 | + keyValueMap["key"] = keyValue.Key |
| 164 | + } |
| 165 | + if keyValue.Value != nil { |
| 166 | + keyValueMap["value"] = keyValue.Value |
| 167 | + } |
| 168 | + tmpList = append(tmpList, keyValueMap) |
| 169 | + } |
| 170 | + _ = d.Set("c_l_b_data", tmpList) |
| 171 | + } |
| 172 | + |
| 173 | + d.SetId(helper.BuildToken()) |
| 174 | + output, ok := d.GetOk("result_output_file") |
| 175 | + if ok && output.(string) != "" { |
| 176 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 177 | + return e |
| 178 | + } |
| 179 | + } |
| 180 | + return nil |
| 181 | +} |
0 commit comments