-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_ckafka_region.go
154 lines (133 loc) · 4.11 KB
/
data_source_tc_ckafka_region.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ckafka
import (
"errors"
"strconv"
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"
ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudCkafkaRegion() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCkafkaRegionRead,
Schema: map[string]*schema.Schema{
"result": {
Computed: true,
Type: schema.TypeList,
Description: "Return a list of region enumeration results.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"region_id": {
Type: schema.TypeInt,
Computed: true,
Description: "region ID.",
},
"region_name": {
Type: schema.TypeString,
Computed: true,
Description: "geographical name.",
},
"area_name": {
Type: schema.TypeString,
Computed: true,
Description: "area name.",
},
"region_code": {
Type: schema.TypeString,
Computed: true,
Description: "Region Code.",
},
"region_code_v3": {
Type: schema.TypeString,
Computed: true,
Description: "Region Code(V3 version).",
},
"support": {
Type: schema.TypeString,
Computed: true,
Description: "NONE: The default value does not support any special models CVM: Supports CVM types.",
},
"ipv6": {
Type: schema.TypeInt,
Computed: true,
Description: "Whether to support ipv6, 0: means not supported, 1: means supported.",
},
"multi_zone": {
Type: schema.TypeInt,
Computed: true,
Description: "Whether to support cross-availability zones, 0: means not supported, 1: means supported.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudCkafkaRegionRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_ckafka_region.read")()
defer tccommon.InconsistentCheck(d, meta)()
var result []*ckafka.Region
request := ckafka.NewDescribeRegionRequest()
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
response, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCkafkaClient().DescribeRegion(request)
if e != nil {
return tccommon.RetryError(e)
}
if response == nil || response.Response == nil {
return tccommon.RetryError(errors.New("Response is null"))
}
result = response.Response.Result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(result))
tmpList := make([]map[string]interface{}, 0, len(result))
if result != nil {
for _, region := range result {
regionMap := map[string]interface{}{}
if region.RegionId != nil {
regionMap["region_id"] = region.RegionId
}
if region.RegionName != nil {
regionMap["region_name"] = region.RegionName
}
if region.AreaName != nil {
regionMap["area_name"] = region.AreaName
}
if region.RegionCode != nil {
regionMap["region_code"] = region.RegionCode
}
if region.RegionCodeV3 != nil {
regionMap["region_code_v3"] = region.RegionCodeV3
}
if region.Support != nil {
regionMap["support"] = region.Support
}
if region.Ipv6 != nil {
regionMap["ipv6"] = region.Ipv6
}
if region.MultiZone != nil {
regionMap["multi_zone"] = region.MultiZone
}
ids = append(ids, strconv.FormatInt(*region.RegionId, 10))
tmpList = append(tmpList, regionMap)
}
_ = d.Set("result", tmpList)
}
d.SetId(helper.DataResourceIdsHash(ids))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}