/
data_source_tc_availability_zones.go
165 lines (147 loc) · 3.61 KB
/
data_source_tc_availability_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
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
155
156
157
158
159
160
161
162
163
164
165
package tencentcloud
import (
"encoding/json"
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
)
const (
tencentCloudApiAvailibilityZoneStateAvailable = "AVAILABLE"
tencentCloudApiAvailibilityZoneStateUnavailable = "UNAVAILABLE"
)
func dataSourceTencentCloudAvailabilityZones() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudAvailabilityZonesRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"include_unavailable": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
// Computed values.
"zones": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceTencentCloudAvailabilityZonesRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*TencentCloudClient).commonConn
params := map[string]string{
"Version": "2017-03-12",
"Action": "DescribeZones",
}
log.Printf("[DEBUG] tencentcloud_instance_types - param: %v", params)
response, err := client.SendRequest("cvm", params)
if err != nil {
return err
}
type Zone struct {
Zone string `json:"Zone"`
ZoneName string `json:"ZoneName"`
ZoneId string `json:"ZoneId"`
ZoneState string `json:"ZoneState"`
}
var jsonresp struct {
Response struct {
Error struct {
Code string `json:"Code"`
Message string `json:"Message"`
}
RequestId string `json:"RequestId"`
ZoneSet []Zone
}
}
err = json.Unmarshal([]byte(response), &jsonresp)
if err != nil {
return err
}
if jsonresp.Response.Error.Code != "" {
return fmt.Errorf(
"tencentcloud_availability_zones got error, code:%v, message:%v",
jsonresp.Response.Error.Code,
jsonresp.Response.Error.Message,
)
}
var (
resultZoneList []Zone
)
zoneList := jsonresp.Response.ZoneSet
if len(zoneList) == 0 {
return errors.New("No avalability zones found")
}
name, nameOk := d.GetOk("name")
includeUnavailable, includeUnavailableOk := d.GetOk("include_unavailable")
for _, zone := range zoneList {
log.Printf(
"[DEBUG] tencentcloud_availability_zones - Zone found id: %v, name:% v, description: %v, state: %v",
zone.ZoneId,
zone.Zone,
zone.ZoneName,
zone.ZoneState,
)
if zone.ZoneState == tencentCloudApiAvailibilityZoneStateUnavailable {
if !includeUnavailableOk || !includeUnavailable.(bool) {
continue
}
}
if nameOk {
zoneName := name.(string)
if zone.Zone == zoneName {
resultZoneList = append(resultZoneList, zone)
}
continue
}
resultZoneList = append(resultZoneList, zone)
}
if len(resultZoneList) == 0 {
return errors.New("No avalability zones found")
}
var (
result []map[string]interface{}
resultIds []string
)
for _, zone := range resultZoneList {
m := make(map[string]interface{})
m["id"] = zone.ZoneId
m["name"] = zone.Zone
m["description"] = zone.ZoneName
m["state"] = zone.ZoneState
result = append(result, m)
resultIds = append(resultIds, zone.ZoneId)
}
id := dataResourceIdsHash(resultIds)
d.SetId(id)
log.Printf("[DEBUG] tencentcloud_availability_zones - instances[0]: %#v", result[0])
if err := d.Set("zones", result); err != nil {
return err
}
return nil
}