Skip to content

update eip & zones #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

ENHANCEMENTS:

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

## 1.20.0 (September 24, 2019)
Expand Down
194 changes: 86 additions & 108 deletions tencentcloud/data_source_tc_availability_zones.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/*
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.

Example Usage

```hcl
data "tencentcloud_availability_zones" "my_favourite_zone" {
name = "ap-guangzhou-3"
}
```
*/
package tencentcloud

import (
"encoding/json"
"errors"
"fmt"
"context"
"log"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

const (
// tencentCloudApiAvailibilityZoneStateAvailable = "AVAILABLE"
tencentCloudApiAvailibilityZoneStateUnavailable = "UNAVAILABLE"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
)

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

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Description: "When specified, only the zone with the exactly name match will return.",
},

"include_unavailable": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Type: schema.TypeBool,
Optional: true,
Description: "A bool variable indicates that the query will include `UNAVAILABLE` zones.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},

// Computed values.
"zones": {
Type: schema.TypeList,
Computed: true,
Type: schema.TypeList,
Computed: true,
Description: "A list of zones will be exported and its every element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "An internal id for the zone, like `200003`, usually not so useful for end user.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "The english name for the zone, like `ap-guangzhou-3`.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "The description for the zone, unfortunately only Chinese characters at this stage.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "The state for the zone, indicate availability using `AVAILABLE` and `UNAVAILABLE` values.",
},
},
},
Expand All @@ -62,106 +77,69 @@ func dataSourceTencentCloudAvailabilityZones() *schema.Resource {

func dataSourceTencentCloudAvailabilityZonesRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_availability_zones.read")()

client := meta.(*TencentCloudClient).commonConn

params := map[string]string{
"Version": "2017-03-12",
"Action": "DescribeZones",
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), "logId", logId)
cvmService := CvmService{
client: meta.(*TencentCloudClient).apiV3Conn,
}

log.Printf("[DEBUG] tencentcloud_instance_types - param: %v", params)
response, err := client.SendRequest("cvm", params)
if err != nil {
return err
var name string
var includeUnavailable = true
if v, ok := d.GetOk("name"); ok {
name = v.(string)
}

type Zone struct {
Zone string `json:"Zone"`
ZoneName string `json:"ZoneName"`
ZoneId string `json:"ZoneId"`
ZoneState string `json:"ZoneState"`
if v, ok := d.GetOkExists("include_unavailable"); ok {
includeUnavailable = v.(bool)
}
var jsonresp struct {
Response struct {
Error struct {
Code string `json:"Code"`
Message string `json:"Message"`
}
RequestId string `json:"RequestId"`
ZoneSet []Zone

var zones []*cvm.ZoneInfo
var errRet error
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
zones, errRet = cvmService.DescribeZones(ctx)
if errRet != nil {
return retryError(errRet, "InternalError")
}
}
err = json.Unmarshal([]byte(response), &jsonresp)
return nil
})
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
}
zoneList := make([]map[string]interface{}, 0, len(zones))
ids := make([]string, 0, len(zones))
for _, zone := range zones {
flag := true
if name != "" && name != *zone.Zone {
flag = false
}
if !includeUnavailable && *zone.ZoneState == ZONE_STATE_UNAVAILABLE {
flag = false
}

if nameOk {
zoneName := name.(string)
if zone.Zone == zoneName {
resultZoneList = append(resultZoneList, zone)
if flag {
mapping := map[string]interface{}{
"id": zone.ZoneId,
"name": zone.Zone,
"description": zone.ZoneName,
"state": zone.ZoneState,
}
continue
zoneList = append(zoneList, mapping)
ids = append(ids, *zone.ZoneId)
}
resultZoneList = append(resultZoneList, zone)
}

if len(resultZoneList) == 0 {
return errors.New("No avalability zones found")
d.SetId(dataResourceIdsHash(ids))
err = d.Set("zones", zoneList)
if err != nil {
log.Printf("[CRITAL]%s provider set zone list fail, reason:%s\n ", logId, err.Error())
return err
}

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
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if err := writeToFile(output.(string), zoneList); err != nil {
return err
}
}
return nil
}
3 changes: 3 additions & 0 deletions tencentcloud/extension_cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const (
CVM_PLACEMENT_GROUP_TYPE_HOST = "HOST"
CVM_PLACEMENT_GROUP_TYPE_SW = "SW"
CVM_PLACEMENT_GROUP_TYPE_RACK = "RACK"

ZONE_STATE_AVAILABLE = "AVAILABLE"
ZONE_STATE_UNAVAILABLE = "UNAVAILABLE"
)

var CVM_CHARGE_TYPE = []string{
Expand Down
28 changes: 28 additions & 0 deletions tencentcloud/extension_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,32 @@ const (
EIP_STATUS_UNBIND = "UNBIND"
EIP_STATUS_OFFLINING = "OFFLINING"
EIP_STATUS_BIND_ENI = "BIND_ENI"

EIP_TYPE_EIP = "EIP"
EIP_TYPE_ANYCAST = "AnycastEIP"

EIP_ANYCAST_ZONE_GLOBAL = "ANYCAST_ZONE_GLOBAL"
EIP_ANYCAST_ZONE_OVERSEAS = "ANYCAST_ZONE_OVERSEAS"

EIP_INTERNET_PROVIDER_BGP = "BGP"
EIP_INTERNET_PROVIDER_CMCC = "CMCC"
EIP_INTERNET_PROVIDER_CTCC = "CTCC"
EIP_INTERNET_PROVIDER_CUCC = "CUCC"
)

var EIP_INTERNET_PROVIDER = []string{
EIP_INTERNET_PROVIDER_BGP,
EIP_INTERNET_PROVIDER_CMCC,
EIP_INTERNET_PROVIDER_CTCC,
EIP_INTERNET_PROVIDER_CUCC,
}

var EIP_TYPE = []string{
EIP_TYPE_EIP,
EIP_TYPE_ANYCAST,
}

var EIP_ANYCAST_ZONE = []string{
EIP_ANYCAST_ZONE_GLOBAL,
EIP_ANYCAST_ZONE_OVERSEAS,
}
Loading