forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_vpn_gateway_zones.go
121 lines (111 loc) · 3.46 KB
/
data_source_alicloud_vpn_gateway_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
115
116
117
118
119
120
121
// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you!
package alicloud
import (
"fmt"
"time"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/gunnerliu/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAliCloudVPNGatewayZones() *schema.Resource {
return &schema.Resource{
Read: dataSourceAliCloudVPNGatewayZoneRead,
Schema: map[string]*schema.Schema{
"ids": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"spec": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"zones": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"zone_name": {
Type: schema.TypeString,
Computed: true,
},
"zone_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceAliCloudVPNGatewayZoneRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
action := "DescribeVpnGatewayAvailableZones"
conn, err := client.NewVpngatewayClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["RegionId"] = client.RegionId
if v, ok := d.GetOk("spec"); ok {
query["Spec"] = StringPointer(v.(string))
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("GET"), StringPointer("2016-04-28"), StringPointer("AK"), query, nil, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
resp, _ := jsonpath.Get("$.AvailableZoneIdList", response)
s := make([]map[string]interface{}, 0)
ids := make([]string, 0)
availableZoneIdList1Raw := resp.([]interface{})
zonesMaps := make([]map[string]interface{}, 0)
if availableZoneIdList1Raw != nil {
for _, availableZoneIdListChild1Raw := range availableZoneIdList1Raw {
zonesMap := make(map[string]interface{})
availableZoneIdListChild1Raw := availableZoneIdListChild1Raw.(map[string]interface{})
zonesMap["zone_id"] = availableZoneIdListChild1Raw["ZoneId"]
zonesMap["zone_name"] = availableZoneIdListChild1Raw["ZoneName"]
ids = append(ids, fmt.Sprint(availableZoneIdListChild1Raw["ZoneId"]))
zonesMaps = append(zonesMaps, zonesMap)
}
}
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
if err := d.Set("zones", zonesMaps); err != nil {
return WrapError(err)
}
d.SetId(dataResourceIdHash(ids))
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}