forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_fc_zones.go
78 lines (70 loc) · 1.93 KB
/
data_source_alicloud_fc_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
package alicloud
import (
"fmt"
"sort"
"github.com/aliyun/fc-go-sdk"
"github.com/gunnerliu/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlicloudFcZones() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudFcZonesRead,
Schema: map[string]*schema.Schema{
"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,
},
},
},
},
},
}
}
func dataSourceAlicloudFcZonesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var clientInfo *fc.Client
raw, err := client.WithFcClient(func(fcClient *fc.Client) (interface{}, error) {
clientInfo = fcClient
return fcClient.GetAccountSettings(fc.NewGetAccountSettingsInput())
})
if err != nil {
return WrapError(fmt.Errorf("[API ERROR] FC GetAccountSettings: %#v", err))
}
addDebug("GetAccountSettings", raw, clientInfo)
out, _ := raw.(*fc.GetAccountSettingsOutput)
if out != nil && len(out.AvailableAZs) > 0 {
sort.Strings(out.AvailableAZs)
var zoneIds []string
var s []map[string]interface{}
for _, zoneId := range out.AvailableAZs {
mapping := map[string]interface{}{"id": zoneId}
s = append(s, mapping)
zoneIds = append(zoneIds, zoneId)
}
d.SetId(dataResourceIdHash(out.AvailableAZs))
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
}