|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of waf user_clb_regions |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_waf_user_clb_regions" "example" {} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "strconv" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 19 | + waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudWafUserClbRegions() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudWafUserClbRegionsRead, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "data": { |
| 27 | + Computed: true, |
| 28 | + Type: schema.TypeSet, |
| 29 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 30 | + Description: "Region list(ap-xxx format).", |
| 31 | + }, |
| 32 | + "rich_datas": { |
| 33 | + Computed: true, |
| 34 | + Type: schema.TypeList, |
| 35 | + Description: "Detail info for region.", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + Description: "Region ID.", |
| 42 | + }, |
| 43 | + "text": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Description: "Chinese description for region.", |
| 47 | + }, |
| 48 | + "value": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + Description: "English description for region.", |
| 52 | + }, |
| 53 | + "code": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Region code.", |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + "result_output_file": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Optional: true, |
| 64 | + Description: "Used to save results.", |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func dataSourceTencentCloudWafUserClbRegionsRead(d *schema.ResourceData, meta interface{}) error { |
| 71 | + defer logElapsed("data_source.tencentcloud_waf_user_clb_regions.read")() |
| 72 | + defer inconsistentCheck(d, meta)() |
| 73 | + |
| 74 | + var ( |
| 75 | + logId = getLogId(contextNil) |
| 76 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 77 | + service = WafService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 78 | + userClbRegions *waf.DescribeUserClbWafRegionsResponseParams |
| 79 | + ) |
| 80 | + |
| 81 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 82 | + result, e := service.DescribeWafUserClbRegionsByFilter(ctx) |
| 83 | + if e != nil { |
| 84 | + return retryError(e) |
| 85 | + } |
| 86 | + |
| 87 | + userClbRegions = result |
| 88 | + return nil |
| 89 | + }) |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + if userClbRegions.Data != nil { |
| 96 | + _ = d.Set("data", userClbRegions.Data) |
| 97 | + } |
| 98 | + |
| 99 | + if userClbRegions.RichDatas != nil { |
| 100 | + tmpList := make([]map[string]interface{}, 0, len(userClbRegions.RichDatas)) |
| 101 | + for _, clbWafRegionItem := range userClbRegions.RichDatas { |
| 102 | + clbWafRegionItemMap := map[string]interface{}{} |
| 103 | + |
| 104 | + if clbWafRegionItem.Id != nil { |
| 105 | + clbWafRegionItemMap["id"] = clbWafRegionItem.Id |
| 106 | + } |
| 107 | + |
| 108 | + if clbWafRegionItem.Text != nil { |
| 109 | + clbWafRegionItemMap["text"] = clbWafRegionItem.Text |
| 110 | + } |
| 111 | + |
| 112 | + if clbWafRegionItem.Value != nil { |
| 113 | + clbWafRegionItemMap["value"] = clbWafRegionItem.Value |
| 114 | + } |
| 115 | + |
| 116 | + if clbWafRegionItem.Code != nil { |
| 117 | + clbWafRegionItemMap["code"] = clbWafRegionItem.Code |
| 118 | + } |
| 119 | + |
| 120 | + tmpList = append(tmpList, clbWafRegionItemMap) |
| 121 | + } |
| 122 | + |
| 123 | + _ = d.Set("rich_datas", tmpList) |
| 124 | + } |
| 125 | + |
| 126 | + d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) |
| 127 | + output, ok := d.GetOk("result_output_file") |
| 128 | + if ok && output.(string) != "" { |
| 129 | + if e := writeToFile(output.(string), d); e != nil { |
| 130 | + return e |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
0 commit comments