|
| 1 | +/* |
| 2 | +Use this data source to get the available zones in current region. |
| 3 | +* Must set product param to fetch the product infomations(e.g. => cvm, vpc) |
| 4 | +* By default only `AVAILABLE` zones will be returned, but `UNAVAILABLE` zones can also be fetched when `include_unavailable` is specified. |
| 5 | +Example Usage |
| 6 | +
|
| 7 | +```hcl |
| 8 | +data "tencentcloud_availability_zones_by_product" "all" { |
| 9 | + product="cvm" |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "log" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 21 | + api "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api/v20201106" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudAvailabilityZonesByProduct() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudAvailabilityZonesByProductRead, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + Description: "When specified, only the zone with the exactly name match will be returned.", |
| 34 | + }, |
| 35 | + "product": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + Description: "A string variable indicates that the query will use product infomation.", |
| 39 | + }, |
| 40 | + "include_unavailable": { |
| 41 | + Type: schema.TypeBool, |
| 42 | + Optional: true, |
| 43 | + Description: "A bool variable indicates that the query will include `UNAVAILABLE` zones.", |
| 44 | + }, |
| 45 | + "result_output_file": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + Description: "Used to save results.", |
| 49 | + }, |
| 50 | + |
| 51 | + // Computed values. |
| 52 | + "zones": { |
| 53 | + Type: schema.TypeList, |
| 54 | + Computed: true, |
| 55 | + Description: "A list of zones will be exported and its every element contains the following attributes:", |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "id": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "An internal id for the zone, like `200003`, usually not so useful.", |
| 62 | + }, |
| 63 | + "name": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "The name of the zone, like `ap-guangzhou-3`.", |
| 67 | + }, |
| 68 | + "description": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "The description of the zone, like `Guangzhou Zone 3`.", |
| 72 | + }, |
| 73 | + "state": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "The state of the zone, indicate availability using `AVAILABLE` and `UNAVAILABLE` values.", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func dataSourceTencentCloudAvailabilityZonesByProductRead(d *schema.ResourceData, meta interface{}) error { |
| 86 | + defer logElapsed("data_source.tencentcloud_availability_zones.read")() |
| 87 | + |
| 88 | + logId := getLogId(contextNil) |
| 89 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 90 | + apiService := APIService{ |
| 91 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 92 | + } |
| 93 | + |
| 94 | + var name string |
| 95 | + var product string |
| 96 | + var includeUnavailable = false |
| 97 | + if v, ok := d.GetOk("name"); ok { |
| 98 | + name = v.(string) |
| 99 | + } |
| 100 | + if v, ok := d.GetOk("product"); ok { |
| 101 | + product = v.(string) |
| 102 | + } |
| 103 | + if v, ok := d.GetOkExists("include_unavailable"); ok { |
| 104 | + includeUnavailable = v.(bool) |
| 105 | + } |
| 106 | + |
| 107 | + var zones []*api.ZoneInfo |
| 108 | + var errRet error |
| 109 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 110 | + zones, errRet = apiService.DescribeZonesWithProduct(ctx, product) |
| 111 | + if errRet != nil { |
| 112 | + return retryError(errRet, InternalError) |
| 113 | + } |
| 114 | + return nil |
| 115 | + }) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + zoneList := make([]map[string]interface{}, 0, len(zones)) |
| 121 | + ids := make([]string, 0, len(zones)) |
| 122 | + for _, zone := range zones { |
| 123 | + if name != "" && name != *zone.Zone { |
| 124 | + continue |
| 125 | + } |
| 126 | + if !includeUnavailable && *zone.ZoneState == ZONE_STATE_UNAVAILABLE { |
| 127 | + continue |
| 128 | + } |
| 129 | + mapping := map[string]interface{}{ |
| 130 | + "id": zone.ZoneId, |
| 131 | + "name": zone.Zone, |
| 132 | + "description": zone.ZoneName, |
| 133 | + "state": zone.ZoneState, |
| 134 | + } |
| 135 | + zoneList = append(zoneList, mapping) |
| 136 | + ids = append(ids, *zone.ZoneId) |
| 137 | + } |
| 138 | + |
| 139 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 140 | + err = d.Set("zones", zoneList) |
| 141 | + if err != nil { |
| 142 | + log.Printf("[CRITAL]%s provider set zones list fail, reason:%s\n ", logId, err.Error()) |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + output, ok := d.GetOk("result_output_file") |
| 147 | + if ok && output.(string) != "" { |
| 148 | + if err := writeToFile(output.(string), zoneList); err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
0 commit comments