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