Skip to content

Commit 60d806d

Browse files
authored
Merge pull request #158 from oliverpei/master
update eip & zones
2 parents 79a2109 + 102bf42 commit 60d806d

14 files changed

+345
-179
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
ENHANCEMENTS:
44

5+
* Data Source: `tencentcloud_availability_zones` refactor logic with api3.0 .
56
* Data Source: `tencentcloud_as_scaling_groups` add optional argument `tags` and attribute `tags` for `scaling_group_list`.
7+
* Resource: `tencentcloud_eip` add optional argument `type`, `anycast_zone`, `internet_service_provider`, etc.
68
* Resource: `tencentcloud_as_scaling_group` add optional argument `tags`.
79

810
## 1.20.0 (September 24, 2019)
Lines changed: 84 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
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+
*/
112
package tencentcloud
213

314
import (
4-
"encoding/json"
5-
"errors"
6-
"fmt"
15+
"context"
716
"log"
817

18+
"github.com/hashicorp/terraform/helper/resource"
919
"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"
1521
)
1622

1723
func dataSourceTencentCloudAvailabilityZones() *schema.Resource {
@@ -20,38 +26,47 @@ func dataSourceTencentCloudAvailabilityZones() *schema.Resource {
2026

2127
Schema: map[string]*schema.Schema{
2228
"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.",
2632
},
27-
2833
"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.",
3242
},
3343

3444
// Computed values.
3545
"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:",
3849
Elem: &schema.Resource{
3950
Schema: map[string]*schema.Schema{
4051
"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.",
4355
},
4456
"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`.",
4760
},
4861
"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.",
5165
},
5266
"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.",
5570
},
5671
},
5772
},
@@ -62,106 +77,66 @@ func dataSourceTencentCloudAvailabilityZones() *schema.Resource {
6277

6378
func dataSourceTencentCloudAvailabilityZonesRead(d *schema.ResourceData, meta interface{}) error {
6479
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,
7184
}
7285

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)
7790
}
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)
8493
}
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")
93101
}
94-
}
95-
err = json.Unmarshal([]byte(response), &jsonresp)
102+
return nil
103+
})
96104
if err != nil {
97105
return err
98106
}
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-
)
125107

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
130113
}
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 {
137115
continue
138116
}
139-
resultZoneList = append(resultZoneList, zone)
140-
}
141117

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)
144126
}
145127

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())
164132
return err
165133
}
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+
}
166141
return nil
167142
}

tencentcloud/extension_cvm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const (
2828
CVM_PLACEMENT_GROUP_TYPE_HOST = "HOST"
2929
CVM_PLACEMENT_GROUP_TYPE_SW = "SW"
3030
CVM_PLACEMENT_GROUP_TYPE_RACK = "RACK"
31+
32+
ZONE_STATE_AVAILABLE = "AVAILABLE"
33+
ZONE_STATE_UNAVAILABLE = "UNAVAILABLE"
3134
)
3235

3336
var CVM_CHARGE_TYPE = []string{

tencentcloud/extension_vpc.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,36 @@ const (
3434
EIP_STATUS_UNBIND = "UNBIND"
3535
EIP_STATUS_OFFLINING = "OFFLINING"
3636
EIP_STATUS_BIND_ENI = "BIND_ENI"
37+
38+
EIP_TYPE_EIP = "EIP"
39+
EIP_TYPE_ANYCAST = "AnycastEIP"
40+
41+
EIP_ANYCAST_ZONE_GLOBAL = "ANYCAST_ZONE_GLOBAL"
42+
EIP_ANYCAST_ZONE_OVERSEAS = "ANYCAST_ZONE_OVERSEAS"
43+
44+
EIP_INTERNET_PROVIDER_BGP = "BGP"
45+
EIP_INTERNET_PROVIDER_CMCC = "CMCC"
46+
EIP_INTERNET_PROVIDER_CTCC = "CTCC"
47+
EIP_INTERNET_PROVIDER_CUCC = "CUCC"
3748
)
3849

50+
var EIP_INTERNET_PROVIDER = []string{
51+
EIP_INTERNET_PROVIDER_BGP,
52+
EIP_INTERNET_PROVIDER_CMCC,
53+
EIP_INTERNET_PROVIDER_CTCC,
54+
EIP_INTERNET_PROVIDER_CUCC,
55+
}
56+
57+
var EIP_TYPE = []string{
58+
EIP_TYPE_EIP,
59+
EIP_TYPE_ANYCAST,
60+
}
61+
62+
var EIP_ANYCAST_ZONE = []string{
63+
EIP_ANYCAST_ZONE_GLOBAL,
64+
EIP_ANYCAST_ZONE_OVERSEAS,
65+
}
66+
3967
// ENI
4068
const (
4169
ENI_DESCRIBE_LIMIT = 100

0 commit comments

Comments
 (0)