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