|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cfs available_zone |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cfs_available_zone" "available_zone" {} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 17 | + cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719" |
| 18 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 19 | +) |
| 20 | + |
| 21 | +func dataSourceTencentCloudCfsAvailableZone() *schema.Resource { |
| 22 | + return &schema.Resource{ |
| 23 | + Read: dataSourceTencentCloudCfsAvailableZoneRead, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "region_zones": { |
| 26 | + Computed: true, |
| 27 | + Type: schema.TypeList, |
| 28 | + Description: "Information such as resource availability in each AZ and the supported storage classes and protocols.", |
| 29 | + Elem: &schema.Resource{ |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "region": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + Description: "Region name, such as `ap-beijing`.", |
| 35 | + }, |
| 36 | + "region_name": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + Description: "Region name, such as `bj`.", |
| 40 | + }, |
| 41 | + "region_status": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + Description: "Region availability. If a region has at least one AZ where resources are purchasable, this value will be AVAILABLE; otherwise, it will be UNAVAILABLE.", |
| 45 | + }, |
| 46 | + "zones": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Computed: true, |
| 49 | + Description: "Array of AZs.", |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: map[string]*schema.Schema{ |
| 52 | + "zone": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "AZ name.", |
| 56 | + }, |
| 57 | + "zone_id": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Computed: true, |
| 60 | + Description: "AZ ID.", |
| 61 | + }, |
| 62 | + "zone_cn_name": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "Chinese name of an AZ.", |
| 66 | + }, |
| 67 | + "types": { |
| 68 | + Type: schema.TypeList, |
| 69 | + Computed: true, |
| 70 | + Description: "Array of classes.", |
| 71 | + Elem: &schema.Resource{ |
| 72 | + Schema: map[string]*schema.Schema{ |
| 73 | + "protocols": { |
| 74 | + Type: schema.TypeList, |
| 75 | + Computed: true, |
| 76 | + Description: "Protocol and sale details.", |
| 77 | + Elem: &schema.Resource{ |
| 78 | + Schema: map[string]*schema.Schema{ |
| 79 | + "sale_status": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + Description: " Sale status. Valid values: sale_out (sold out), saling (purchasable), no_saling (non-purchasable).", |
| 83 | + }, |
| 84 | + "protocol": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: "Protocol type. Valid values: NFS, CIFS.", |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + "type": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "Storage class. Valid values: SD (standard storage) and HP (high-performance storage).", |
| 96 | + }, |
| 97 | + "prepayment": { |
| 98 | + Type: schema.TypeBool, |
| 99 | + Computed: true, |
| 100 | + Description: "Indicates whether prepaid is supported. true: yes; false: no.", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + "zone_name": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Computed: true, |
| 108 | + Description: "Chinese and English names of an AZ.", |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + "region_cn_name": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + Description: "Region chinese name, such as `Guangzhou`.", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + |
| 122 | + "result_output_file": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Optional: true, |
| 125 | + Description: "Used to save results.", |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func dataSourceTencentCloudCfsAvailableZoneRead(d *schema.ResourceData, meta interface{}) error { |
| 132 | + defer logElapsed("data_source.tencentcloud_cfs_available_zone.read")() |
| 133 | + defer inconsistentCheck(d, meta)() |
| 134 | + |
| 135 | + logId := getLogId(contextNil) |
| 136 | + |
| 137 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 138 | + |
| 139 | + service := CfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 140 | + |
| 141 | + var regionZones []*cfs.AvailableRegion |
| 142 | + |
| 143 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 144 | + result, e := service.DescribeCfsAvailableZoneByFilter(ctx) |
| 145 | + if e != nil { |
| 146 | + return retryError(e) |
| 147 | + } |
| 148 | + regionZones = result |
| 149 | + return nil |
| 150 | + }) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + ids := make([]string, 0, len(regionZones)) |
| 156 | + tmpList := make([]map[string]interface{}, 0, len(regionZones)) |
| 157 | + |
| 158 | + for _, availableRegion := range regionZones { |
| 159 | + availableRegionMap := map[string]interface{}{} |
| 160 | + |
| 161 | + if availableRegion.Region != nil { |
| 162 | + availableRegionMap["region"] = availableRegion.Region |
| 163 | + } |
| 164 | + |
| 165 | + if availableRegion.RegionName != nil { |
| 166 | + availableRegionMap["region_name"] = availableRegion.RegionName |
| 167 | + } |
| 168 | + |
| 169 | + if availableRegion.RegionStatus != nil { |
| 170 | + availableRegionMap["region_status"] = availableRegion.RegionStatus |
| 171 | + } |
| 172 | + |
| 173 | + if availableRegion.Zones != nil { |
| 174 | + zonesList := []interface{}{} |
| 175 | + for _, zones := range availableRegion.Zones { |
| 176 | + zonesMap := map[string]interface{}{} |
| 177 | + |
| 178 | + if zones.Zone != nil { |
| 179 | + zonesMap["zone"] = zones.Zone |
| 180 | + } |
| 181 | + |
| 182 | + if zones.ZoneId != nil { |
| 183 | + zonesMap["zone_id"] = zones.ZoneId |
| 184 | + } |
| 185 | + |
| 186 | + if zones.ZoneCnName != nil { |
| 187 | + zonesMap["zone_cn_name"] = zones.ZoneCnName |
| 188 | + } |
| 189 | + |
| 190 | + if zones.Types != nil { |
| 191 | + typesList := []interface{}{} |
| 192 | + for _, types := range zones.Types { |
| 193 | + typesMap := map[string]interface{}{} |
| 194 | + |
| 195 | + if types.Protocols != nil { |
| 196 | + protocolsList := []interface{}{} |
| 197 | + for _, protocols := range types.Protocols { |
| 198 | + protocolsMap := map[string]interface{}{} |
| 199 | + |
| 200 | + if protocols.SaleStatus != nil { |
| 201 | + protocolsMap["sale_status"] = protocols.SaleStatus |
| 202 | + } |
| 203 | + |
| 204 | + if protocols.Protocol != nil { |
| 205 | + protocolsMap["protocol"] = protocols.Protocol |
| 206 | + } |
| 207 | + |
| 208 | + protocolsList = append(protocolsList, protocolsMap) |
| 209 | + } |
| 210 | + |
| 211 | + typesMap["protocols"] = protocolsList |
| 212 | + } |
| 213 | + |
| 214 | + if types.Type != nil { |
| 215 | + typesMap["type"] = types.Type |
| 216 | + } |
| 217 | + |
| 218 | + if types.Prepayment != nil { |
| 219 | + typesMap["prepayment"] = types.Prepayment |
| 220 | + } |
| 221 | + |
| 222 | + typesList = append(typesList, typesMap) |
| 223 | + } |
| 224 | + |
| 225 | + zonesMap["types"] = typesList |
| 226 | + } |
| 227 | + |
| 228 | + if zones.ZoneName != nil { |
| 229 | + zonesMap["zone_name"] = zones.ZoneName |
| 230 | + } |
| 231 | + |
| 232 | + zonesList = append(zonesList, zonesMap) |
| 233 | + } |
| 234 | + |
| 235 | + availableRegionMap["zones"] = zonesList |
| 236 | + } |
| 237 | + |
| 238 | + if availableRegion.RegionCnName != nil { |
| 239 | + availableRegionMap["region_cn_name"] = availableRegion.RegionCnName |
| 240 | + } |
| 241 | + ids = append(ids, *availableRegion.Region) |
| 242 | + tmpList = append(tmpList, availableRegionMap) |
| 243 | + } |
| 244 | + |
| 245 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 246 | + err = d.Set("region_zones", tmpList) |
| 247 | + if err != nil { |
| 248 | + return err |
| 249 | + } |
| 250 | + output, ok := d.GetOk("result_output_file") |
| 251 | + if ok && output.(string) != "" { |
| 252 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 253 | + return e |
| 254 | + } |
| 255 | + } |
| 256 | + return nil |
| 257 | +} |
0 commit comments