|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cynosdb zone |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cynosdb_zone" "zone" { |
| 8 | + include_virtual_zones = true |
| 9 | + show_permission = true |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 20 | + cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudCynosdbZone() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudCynosdbZoneRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "include_virtual_zones": { |
| 29 | + Optional: true, |
| 30 | + Type: schema.TypeBool, |
| 31 | + Description: "Is virtual zone included.", |
| 32 | + }, |
| 33 | + |
| 34 | + "show_permission": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeBool, |
| 37 | + Description: "Whether to display all available zones under the region and display the permissions of each available zone of the user.", |
| 38 | + }, |
| 39 | + |
| 40 | + "region_set": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeList, |
| 43 | + Description: "Information of region.", |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "region": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Description: "Region in English.", |
| 50 | + }, |
| 51 | + "region_id": { |
| 52 | + Type: schema.TypeInt, |
| 53 | + Computed: true, |
| 54 | + Description: "Region ID.", |
| 55 | + }, |
| 56 | + "region_zh": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Region name in Chinese.", |
| 60 | + }, |
| 61 | + "zone_set": { |
| 62 | + Type: schema.TypeList, |
| 63 | + Computed: true, |
| 64 | + Description: "List of available zones for sale.", |
| 65 | + Elem: &schema.Resource{ |
| 66 | + Schema: map[string]*schema.Schema{ |
| 67 | + "zone": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Computed: true, |
| 70 | + Description: "Zone name in English.", |
| 71 | + }, |
| 72 | + "zone_id": { |
| 73 | + Type: schema.TypeInt, |
| 74 | + Computed: true, |
| 75 | + Description: "ZoneId.", |
| 76 | + }, |
| 77 | + "zone_zh": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Zone name in Chinesee.", |
| 81 | + }, |
| 82 | + "is_support_serverless": { |
| 83 | + Type: schema.TypeInt, |
| 84 | + Computed: true, |
| 85 | + Description: "Does it support serverless clusters, 0:Not supported 1:Support.", |
| 86 | + }, |
| 87 | + "is_support_normal": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Computed: true, |
| 90 | + Description: "Does it support normal clusters, 0:Not supported 1:Support.", |
| 91 | + }, |
| 92 | + "physical_zone": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "Physical zone.", |
| 96 | + }, |
| 97 | + "has_permission": { |
| 98 | + Type: schema.TypeBool, |
| 99 | + Computed: true, |
| 100 | + Description: "Whether the user have zone permissionsNote: This field may return null, indicating that no valid value can be obtained.", |
| 101 | + }, |
| 102 | + "is_whole_rdma_zone": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Computed: true, |
| 105 | + Description: "Is zone Rdma.", |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + "db_type": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Computed: true, |
| 113 | + Description: "Database type.", |
| 114 | + }, |
| 115 | + "modules": { |
| 116 | + Type: schema.TypeList, |
| 117 | + Computed: true, |
| 118 | + Description: "Regional module support.", |
| 119 | + Elem: &schema.Resource{ |
| 120 | + Schema: map[string]*schema.Schema{ |
| 121 | + "is_disable": { |
| 122 | + Type: schema.TypeString, |
| 123 | + Computed: true, |
| 124 | + Description: "Is zone on sale, optional values: yes, no.", |
| 125 | + }, |
| 126 | + "module_name": { |
| 127 | + Type: schema.TypeString, |
| 128 | + Computed: true, |
| 129 | + Description: "Module name.", |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + |
| 138 | + "result_output_file": { |
| 139 | + Type: schema.TypeString, |
| 140 | + Optional: true, |
| 141 | + Description: "Used to save results.", |
| 142 | + }, |
| 143 | + }, |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func dataSourceTencentCloudCynosdbZoneRead(d *schema.ResourceData, meta interface{}) error { |
| 148 | + defer logElapsed("data_source.tencentcloud_cynosdb_zone.read")() |
| 149 | + defer inconsistentCheck(d, meta)() |
| 150 | + |
| 151 | + logId := getLogId(contextNil) |
| 152 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 153 | + |
| 154 | + paramMap := make(map[string]interface{}) |
| 155 | + if v, _ := d.GetOk("include_virtual_zones"); v != nil { |
| 156 | + paramMap["IncludeVirtualZones"] = helper.Bool(v.(bool)) |
| 157 | + } |
| 158 | + |
| 159 | + if v, _ := d.GetOk("show_permission"); v != nil { |
| 160 | + paramMap["ShowPermission"] = helper.Bool(v.(bool)) |
| 161 | + } |
| 162 | + |
| 163 | + service := CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 164 | + |
| 165 | + var regionSet []*cynosdb.SaleRegion |
| 166 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 167 | + result, e := service.DescribeCynosdbZoneByFilter(ctx, paramMap) |
| 168 | + if e != nil { |
| 169 | + return retryError(e) |
| 170 | + } |
| 171 | + regionSet = result |
| 172 | + return nil |
| 173 | + }) |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + |
| 178 | + ids := make([]string, 0, len(regionSet)) |
| 179 | + tmpList := make([]map[string]interface{}, 0, len(regionSet)) |
| 180 | + if regionSet != nil { |
| 181 | + for _, saleRegion := range regionSet { |
| 182 | + saleRegionMap := map[string]interface{}{} |
| 183 | + |
| 184 | + if saleRegion.Region != nil { |
| 185 | + saleRegionMap["region"] = saleRegion.Region |
| 186 | + } |
| 187 | + |
| 188 | + if saleRegion.RegionId != nil { |
| 189 | + saleRegionMap["region_id"] = saleRegion.RegionId |
| 190 | + } |
| 191 | + |
| 192 | + if saleRegion.RegionZh != nil { |
| 193 | + saleRegionMap["region_zh"] = saleRegion.RegionZh |
| 194 | + } |
| 195 | + |
| 196 | + if saleRegion.ZoneSet != nil { |
| 197 | + zoneSetList := []interface{}{} |
| 198 | + for _, zoneSet := range saleRegion.ZoneSet { |
| 199 | + zoneSetMap := map[string]interface{}{} |
| 200 | + |
| 201 | + if zoneSet.Zone != nil { |
| 202 | + zoneSetMap["zone"] = zoneSet.Zone |
| 203 | + } |
| 204 | + |
| 205 | + if zoneSet.ZoneId != nil { |
| 206 | + zoneSetMap["zone_id"] = zoneSet.ZoneId |
| 207 | + } |
| 208 | + |
| 209 | + if zoneSet.ZoneZh != nil { |
| 210 | + zoneSetMap["zone_zh"] = zoneSet.ZoneZh |
| 211 | + } |
| 212 | + |
| 213 | + if zoneSet.IsSupportServerless != nil { |
| 214 | + zoneSetMap["is_support_serverless"] = zoneSet.IsSupportServerless |
| 215 | + } |
| 216 | + |
| 217 | + if zoneSet.IsSupportNormal != nil { |
| 218 | + zoneSetMap["is_support_normal"] = zoneSet.IsSupportNormal |
| 219 | + } |
| 220 | + |
| 221 | + if zoneSet.PhysicalZone != nil { |
| 222 | + zoneSetMap["physical_zone"] = zoneSet.PhysicalZone |
| 223 | + } |
| 224 | + |
| 225 | + if zoneSet.HasPermission != nil { |
| 226 | + zoneSetMap["has_permission"] = zoneSet.HasPermission |
| 227 | + } |
| 228 | + |
| 229 | + if zoneSet.IsWholeRdmaZone != nil { |
| 230 | + zoneSetMap["is_whole_rdma_zone"] = zoneSet.IsWholeRdmaZone |
| 231 | + } |
| 232 | + |
| 233 | + zoneSetList = append(zoneSetList, zoneSetMap) |
| 234 | + } |
| 235 | + |
| 236 | + saleRegionMap["zone_set"] = zoneSetList |
| 237 | + } |
| 238 | + |
| 239 | + if saleRegion.DbType != nil { |
| 240 | + saleRegionMap["db_type"] = saleRegion.DbType |
| 241 | + } |
| 242 | + |
| 243 | + if saleRegion.Modules != nil { |
| 244 | + modulesList := []interface{}{} |
| 245 | + for _, modules := range saleRegion.Modules { |
| 246 | + modulesMap := map[string]interface{}{} |
| 247 | + |
| 248 | + if modules.IsDisable != nil { |
| 249 | + modulesMap["is_disable"] = modules.IsDisable |
| 250 | + } |
| 251 | + |
| 252 | + if modules.ModuleName != nil { |
| 253 | + modulesMap["module_name"] = modules.ModuleName |
| 254 | + } |
| 255 | + |
| 256 | + modulesList = append(modulesList, modulesMap) |
| 257 | + } |
| 258 | + |
| 259 | + saleRegionMap["modules"] = modulesList |
| 260 | + } |
| 261 | + |
| 262 | + ids = append(ids, *saleRegion.Region) |
| 263 | + tmpList = append(tmpList, saleRegionMap) |
| 264 | + } |
| 265 | + |
| 266 | + _ = d.Set("region_set", tmpList) |
| 267 | + } |
| 268 | + |
| 269 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 270 | + output, ok := d.GetOk("result_output_file") |
| 271 | + if ok && output.(string) != "" { |
| 272 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 273 | + return e |
| 274 | + } |
| 275 | + } |
| 276 | + return nil |
| 277 | +} |
0 commit comments