forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_gpdb_zones.go
114 lines (106 loc) · 2.96 KB
/
data_source_alicloud_gpdb_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package alicloud
import (
"fmt"
"sort"
"strings"
"github.com/aliyun/alibaba-cloud-sdk-go/services/gpdb"
"github.com/gunnerliu/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlicloudGpdbZones() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudGpdbZonesRead,
Schema: map[string]*schema.Schema{
"multi": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"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},
},
},
},
},
},
}
}
func dataSourceAlicloudGpdbZonesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
multi := d.Get("multi").(bool)
var zoneIds []string
request := gpdb.CreateDescribeRegionsRequest()
request.RegionId = client.RegionId
raw, err := client.WithGpdbClient(func(gpdbClient *gpdb.Client) (interface{}, error) {
return gpdbClient.DescribeRegions(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_gpdb_zones", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response, _ := raw.(*gpdb.DescribeRegionsResponse)
if len(response.Regions.Region) <= 0 {
return WrapError(fmt.Errorf("[ERROR] There is no available region for gpdb."))
}
for _, r := range response.Regions.Region {
for _, zoneId := range r.Zones.Zone {
if multi && strings.Contains(zoneId.ZoneId, MULTI_IZ_SYMBOL) && r.RegionId == string(client.Region) {
zoneIds = append(zoneIds, zoneId.ZoneId)
continue
}
if !multi && !strings.Contains(zoneId.ZoneId, MULTI_IZ_SYMBOL) && r.RegionId == string(client.Region) {
zoneIds = append(zoneIds, zoneId.ZoneId)
continue
}
}
}
if len(zoneIds) > 0 {
sort.Strings(zoneIds)
}
var s []map[string]interface{}
if !multi {
for _, zoneId := range zoneIds {
mapping := map[string]interface{}{"id": zoneId}
s = append(s, mapping)
}
} else {
for _, zoneId := range zoneIds {
mapping := map[string]interface{}{
"id": zoneId,
"multi_zone_ids": splitMultiZoneId(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
}