1
+ /*
2
+ Use this data source to get the available zones in the current region. By default only `AVAILABLE` zones will be returned, but `UNAVAILABLE` zones can also be fetched when `include_unavailable` is specified.
3
+
4
+ Example Usage
5
+
6
+ ```hcl
7
+ data "tencentcloud_availability_zones" "my_favourite_zone" {
8
+ name = "ap-guangzhou-3"
9
+ }
10
+ ```
11
+ */
1
12
package tencentcloud
2
13
3
14
import (
4
- "encoding/json"
5
- "errors"
6
- "fmt"
15
+ "context"
7
16
"log"
8
17
18
+ "github.com/hashicorp/terraform/helper/resource"
9
19
"github.com/hashicorp/terraform/helper/schema"
10
- )
11
-
12
- const (
13
- // tencentCloudApiAvailibilityZoneStateAvailable = "AVAILABLE"
14
- tencentCloudApiAvailibilityZoneStateUnavailable = "UNAVAILABLE"
20
+ cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
15
21
)
16
22
17
23
func dataSourceTencentCloudAvailabilityZones () * schema.Resource {
@@ -20,38 +26,47 @@ func dataSourceTencentCloudAvailabilityZones() *schema.Resource {
20
26
21
27
Schema : map [string ]* schema.Schema {
22
28
"name" : {
23
- Type : schema .TypeString ,
24
- Optional : true ,
25
- ForceNew : true ,
29
+ Type : schema .TypeString ,
30
+ Optional : true ,
31
+ Description : "When specified, only the zone with the exactly name match will return." ,
26
32
},
27
-
28
33
"include_unavailable" : {
29
- Type : schema .TypeBool ,
30
- Optional : true ,
31
- ForceNew : true ,
34
+ Type : schema .TypeBool ,
35
+ Optional : true ,
36
+ Description : "A bool variable indicates that the query will include `UNAVAILABLE` zones." ,
37
+ },
38
+ "result_output_file" : {
39
+ Type : schema .TypeString ,
40
+ Optional : true ,
41
+ Description : "Used to save results." ,
32
42
},
33
43
34
44
// Computed values.
35
45
"zones" : {
36
- Type : schema .TypeList ,
37
- Computed : true ,
46
+ Type : schema .TypeList ,
47
+ Computed : true ,
48
+ Description : "A list of zones will be exported and its every element contains the following attributes:" ,
38
49
Elem : & schema.Resource {
39
50
Schema : map [string ]* schema.Schema {
40
51
"id" : {
41
- Type : schema .TypeString ,
42
- Computed : true ,
52
+ Type : schema .TypeString ,
53
+ Computed : true ,
54
+ Description : "An internal id for the zone, like `200003`, usually not so useful for end user." ,
43
55
},
44
56
"name" : {
45
- Type : schema .TypeString ,
46
- Computed : true ,
57
+ Type : schema .TypeString ,
58
+ Computed : true ,
59
+ Description : "The english name for the zone, like `ap-guangzhou-3`." ,
47
60
},
48
61
"description" : {
49
- Type : schema .TypeString ,
50
- Computed : true ,
62
+ Type : schema .TypeString ,
63
+ Computed : true ,
64
+ Description : "The description for the zone, unfortunately only Chinese characters at this stage." ,
51
65
},
52
66
"state" : {
53
- Type : schema .TypeString ,
54
- Computed : true ,
67
+ Type : schema .TypeString ,
68
+ Computed : true ,
69
+ Description : "The state for the zone, indicate availability using `AVAILABLE` and `UNAVAILABLE` values." ,
55
70
},
56
71
},
57
72
},
@@ -62,106 +77,66 @@ func dataSourceTencentCloudAvailabilityZones() *schema.Resource {
62
77
63
78
func dataSourceTencentCloudAvailabilityZonesRead (d * schema.ResourceData , meta interface {}) error {
64
79
defer logElapsed ("data_source.tencentcloud_availability_zones.read" )()
65
-
66
- client := meta .(* TencentCloudClient ).commonConn
67
-
68
- params := map [string ]string {
69
- "Version" : "2017-03-12" ,
70
- "Action" : "DescribeZones" ,
80
+ logId := getLogId (contextNil )
81
+ ctx := context .WithValue (context .TODO (), "logId" , logId )
82
+ cvmService := CvmService {
83
+ client : meta .(* TencentCloudClient ).apiV3Conn ,
71
84
}
72
85
73
- log . Printf ( "[DEBUG] tencentcloud_instance_types - param: %v" , params )
74
- response , err := client . SendRequest ( "cvm" , params )
75
- if err != nil {
76
- return err
86
+ var name string
87
+ var includeUnavailable = false
88
+ if v , ok := d . GetOk ( "name" ); ok {
89
+ name = v .( string )
77
90
}
78
-
79
- type Zone struct {
80
- Zone string `json:"Zone"`
81
- ZoneName string `json:"ZoneName"`
82
- ZoneId string `json:"ZoneId"`
83
- ZoneState string `json:"ZoneState"`
91
+ if v , ok := d .GetOkExists ("include_unavailable" ); ok {
92
+ includeUnavailable = v .(bool )
84
93
}
85
- var jsonresp struct {
86
- Response struct {
87
- Error struct {
88
- Code string `json:"Code"`
89
- Message string `json:"Message"`
90
- }
91
- RequestId string `json:"RequestId"`
92
- ZoneSet []Zone
94
+
95
+ var zones []* cvm.ZoneInfo
96
+ var errRet error
97
+ err := resource .Retry (readRetryTimeout , func () * resource.RetryError {
98
+ zones , errRet = cvmService .DescribeZones (ctx )
99
+ if errRet != nil {
100
+ return retryError (errRet , "InternalError" )
93
101
}
94
- }
95
- err = json . Unmarshal ([] byte ( response ), & jsonresp )
102
+ return nil
103
+ } )
96
104
if err != nil {
97
105
return err
98
106
}
99
- if jsonresp .Response .Error .Code != "" {
100
- return fmt .Errorf (
101
- "tencentcloud_availability_zones got error, code:%v, message:%v" ,
102
- jsonresp .Response .Error .Code ,
103
- jsonresp .Response .Error .Message ,
104
- )
105
- }
106
-
107
- var (
108
- resultZoneList []Zone
109
- )
110
- zoneList := jsonresp .Response .ZoneSet
111
- if len (zoneList ) == 0 {
112
- return errors .New ("No avalability zones found" )
113
- }
114
-
115
- name , nameOk := d .GetOk ("name" )
116
- includeUnavailable , includeUnavailableOk := d .GetOk ("include_unavailable" )
117
- for _ , zone := range zoneList {
118
- log .Printf (
119
- "[DEBUG] tencentcloud_availability_zones - Zone found id: %v, name:% v, description: %v, state: %v" ,
120
- zone .ZoneId ,
121
- zone .Zone ,
122
- zone .ZoneName ,
123
- zone .ZoneState ,
124
- )
125
107
126
- if zone .ZoneState == tencentCloudApiAvailibilityZoneStateUnavailable {
127
- if ! includeUnavailableOk || ! includeUnavailable .(bool ) {
128
- continue
129
- }
108
+ zoneList := make ([]map [string ]interface {}, 0 , len (zones ))
109
+ ids := make ([]string , 0 , len (zones ))
110
+ for _ , zone := range zones {
111
+ if name != "" && name != * zone .Zone {
112
+ continue
130
113
}
131
-
132
- if nameOk {
133
- zoneName := name .(string )
134
- if zone .Zone == zoneName {
135
- resultZoneList = append (resultZoneList , zone )
136
- }
114
+ if ! includeUnavailable && * zone .ZoneState == ZONE_STATE_UNAVAILABLE {
137
115
continue
138
116
}
139
- resultZoneList = append (resultZoneList , zone )
140
- }
141
117
142
- if len (resultZoneList ) == 0 {
143
- return errors .New ("No avalability zones found" )
118
+ mapping := map [string ]interface {}{
119
+ "id" : zone .ZoneId ,
120
+ "name" : zone .Zone ,
121
+ "description" : zone .ZoneName ,
122
+ "state" : zone .ZoneState ,
123
+ }
124
+ zoneList = append (zoneList , mapping )
125
+ ids = append (ids , * zone .ZoneId )
144
126
}
145
127
146
- var (
147
- result []map [string ]interface {}
148
- resultIds []string
149
- )
150
-
151
- for _ , zone := range resultZoneList {
152
- m := make (map [string ]interface {})
153
- m ["id" ] = zone .ZoneId
154
- m ["name" ] = zone .Zone
155
- m ["description" ] = zone .ZoneName
156
- m ["state" ] = zone .ZoneState
157
- result = append (result , m )
158
- resultIds = append (resultIds , zone .ZoneId )
159
- }
160
- id := dataResourceIdsHash (resultIds )
161
- d .SetId (id )
162
- log .Printf ("[DEBUG] tencentcloud_availability_zones - instances[0]: %#v" , result [0 ])
163
- if err := d .Set ("zones" , result ); err != nil {
128
+ d .SetId (dataResourceIdsHash (ids ))
129
+ err = d .Set ("zones" , zoneList )
130
+ if err != nil {
131
+ log .Printf ("[CRITAL]%s provider set zone list fail, reason:%s\n " , logId , err .Error ())
164
132
return err
165
133
}
134
+
135
+ output , ok := d .GetOk ("result_output_file" )
136
+ if ok && output .(string ) != "" {
137
+ if err := writeToFile (output .(string ), zoneList ); err != nil {
138
+ return err
139
+ }
140
+ }
166
141
return nil
167
142
}
0 commit comments