-
Notifications
You must be signed in to change notification settings - Fork 533
/
data_source_alicloud_hbase_zones.go
98 lines (91 loc) · 2.64 KB
/
data_source_alicloud_hbase_zones.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
package alicloud
import (
"fmt"
"sort"
"github.com/aliyun/alibaba-cloud-sdk-go/services/hbase"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlicloudHBaseZones() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudHBaseZonesRead,
Schema: map[string]*schema.Schema{
"multi": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Removed: "Field 'multi' has been removed from provider version 1.99.0.",
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"zones": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"multi_zone_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Removed: "Field 'multi_zone_ids' has been removed from provider version 1.99.0.",
},
},
},
},
},
}
}
func dataSourceAlicloudHBaseZonesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var zoneIds []string
request := hbase.CreateDescribeRegionsRequest()
request.RegionId = client.RegionId
raw, err := client.WithHbaseClient(func(hbaseClient *hbase.Client) (interface{}, error) {
return hbaseClient.DescribeRegions(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_hbase_zones", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
regions, _ := raw.(*hbase.DescribeRegionsResponse)
if len(regions.Regions.Region) <= 0 {
return WrapError(fmt.Errorf("[ERROR] There is no available region for HBase."))
}
for _, r := range regions.Regions.Region {
for _, zonid := range r.Zones.Zone {
if r.RegionId == string(client.Region) {
zoneIds = append(zoneIds, zonid.Id)
}
}
}
if len(zoneIds) > 0 {
sort.Strings(zoneIds)
}
var s []map[string]interface{}
for _, zoneId := range zoneIds {
mapping := map[string]interface{}{"id": zoneId}
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(zoneIds))
if err := d.Set("zones", s); err != nil {
return WrapError(err)
}
if err := d.Set("ids", zoneIds); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}