|
| 1 | +package crs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" |
| 8 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 9 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceTencentCloudRedisClusters() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceTencentCloudRedisClustersRead, |
| 15 | + Schema: map[string]*schema.Schema{ |
| 16 | + "redis_cluster_ids": { |
| 17 | + Optional: true, |
| 18 | + Type: schema.TypeSet, |
| 19 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 20 | + Description: "Redis Cluster Ids.", |
| 21 | + }, |
| 22 | + "status": { |
| 23 | + Optional: true, |
| 24 | + Type: schema.TypeSet, |
| 25 | + Elem: &schema.Schema{Type: schema.TypeInt}, |
| 26 | + Description: "Cluster status: 1- In process, 2- Running, 3- Isolated.", |
| 27 | + }, |
| 28 | + "project_ids": { |
| 29 | + Optional: true, |
| 30 | + Type: schema.TypeSet, |
| 31 | + Elem: &schema.Schema{Type: schema.TypeInt}, |
| 32 | + Description: "Project Ids.", |
| 33 | + }, |
| 34 | + "auto_renew_flag": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeSet, |
| 37 | + Elem: &schema.Schema{Type: schema.TypeInt}, |
| 38 | + Description: "Renewal mode: 0- default state (manual renewal); 1- Automatic renewal; 2- Clearly stating that automatic renewal is not allowed.", |
| 39 | + }, |
| 40 | + "cluster_name": { |
| 41 | + Optional: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "Cluster name.", |
| 44 | + }, |
| 45 | + "dedicated_cluster_id": { |
| 46 | + Optional: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "Dedicated cluster Id.", |
| 49 | + }, |
| 50 | + // computed |
| 51 | + "resources": { |
| 52 | + Computed: true, |
| 53 | + Type: schema.TypeList, |
| 54 | + Description: ".", |
| 55 | + Elem: &schema.Resource{ |
| 56 | + Schema: map[string]*schema.Schema{ |
| 57 | + "app_id": { |
| 58 | + Type: schema.TypeInt, |
| 59 | + Computed: true, |
| 60 | + Description: "User's Appid.", |
| 61 | + }, |
| 62 | + "region_id": { |
| 63 | + Type: schema.TypeInt, |
| 64 | + Computed: true, |
| 65 | + Description: "Region Id.", |
| 66 | + }, |
| 67 | + "zone_id": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Computed: true, |
| 70 | + Description: "zone Id.", |
| 71 | + }, |
| 72 | + "redis_cluster_id": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "Redis Cluster Id.", |
| 76 | + }, |
| 77 | + "pay_mode": { |
| 78 | + Type: schema.TypeInt, |
| 79 | + Computed: true, |
| 80 | + Description: "Billing mode, 1-annual and monthly package, 0-quantity based billing.", |
| 81 | + }, |
| 82 | + "project_id": { |
| 83 | + Type: schema.TypeInt, |
| 84 | + Computed: true, |
| 85 | + Description: "Project Id.", |
| 86 | + }, |
| 87 | + "auto_renew_flag": { |
| 88 | + Type: schema.TypeInt, |
| 89 | + Computed: true, |
| 90 | + Description: "Renewal mode: 0- default state (manual renewal); 1- Automatic renewal; 2- Clearly stating that automatic renewal is not allowed.", |
| 91 | + }, |
| 92 | + "cluster_name": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "Cluster name.", |
| 96 | + }, |
| 97 | + "start_time": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "Instance create time.", |
| 101 | + }, |
| 102 | + "end_time": { |
| 103 | + Type: schema.TypeString, |
| 104 | + Computed: true, |
| 105 | + Description: "Instance expiration time.", |
| 106 | + }, |
| 107 | + "status": { |
| 108 | + Type: schema.TypeInt, |
| 109 | + Computed: true, |
| 110 | + Description: "Cluster status: 1- In process, 2- Running, 3- Isolated.", |
| 111 | + }, |
| 112 | + "base_bundles": { |
| 113 | + Type: schema.TypeList, |
| 114 | + Computed: true, |
| 115 | + Description: "Basic Control Resource Package.", |
| 116 | + Elem: &schema.Resource{ |
| 117 | + Schema: map[string]*schema.Schema{ |
| 118 | + "resource_bundle_name": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Computed: true, |
| 121 | + Description: "Resource bundle name.", |
| 122 | + }, |
| 123 | + "available_memory": { |
| 124 | + Type: schema.TypeInt, |
| 125 | + Computed: true, |
| 126 | + Description: "Saleable memory, unit: GB.", |
| 127 | + }, |
| 128 | + "count": { |
| 129 | + Type: schema.TypeInt, |
| 130 | + Computed: true, |
| 131 | + Description: "Resource bundle count.", |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + "resource_bundles": { |
| 137 | + Type: schema.TypeList, |
| 138 | + Computed: true, |
| 139 | + Description: "List of Resource Packages.", |
| 140 | + Elem: &schema.Resource{ |
| 141 | + Schema: map[string]*schema.Schema{ |
| 142 | + "resource_bundle_name": { |
| 143 | + Type: schema.TypeString, |
| 144 | + Computed: true, |
| 145 | + Description: "Resource bundle name.", |
| 146 | + }, |
| 147 | + "available_memory": { |
| 148 | + Type: schema.TypeInt, |
| 149 | + Computed: true, |
| 150 | + Description: "Saleable memory, unit: GB.", |
| 151 | + }, |
| 152 | + "count": { |
| 153 | + Type: schema.TypeInt, |
| 154 | + Computed: true, |
| 155 | + Description: "Resource bundle count.", |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + "dedicated_cluster_id": { |
| 161 | + Type: schema.TypeString, |
| 162 | + Computed: true, |
| 163 | + Description: "Dedicated cluster Id.", |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + "result_output_file": { |
| 169 | + Type: schema.TypeString, |
| 170 | + Optional: true, |
| 171 | + Description: "Used to save results.", |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func dataSourceTencentCloudRedisClustersRead(d *schema.ResourceData, meta interface{}) error { |
| 178 | + defer tccommon.LogElapsed("data_source.tencentcloud_redis_clusters.read")() |
| 179 | + defer tccommon.InconsistentCheck(d, meta)() |
| 180 | + |
| 181 | + var ( |
| 182 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 183 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 184 | + service = RedisService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 185 | + ) |
| 186 | + |
| 187 | + paramMap := make(map[string]interface{}) |
| 188 | + if v, ok := d.GetOk("redis_cluster_ids"); ok { |
| 189 | + redisClusterIdsSet := v.(*schema.Set).List() |
| 190 | + paramMap["RedisClusterIds"] = helper.InterfacesStringsPoint(redisClusterIdsSet) |
| 191 | + } |
| 192 | + |
| 193 | + if v, ok := d.GetOk("status"); ok { |
| 194 | + statusList := make([]*int64, 0) |
| 195 | + statusSet := v.(*schema.Set).List() |
| 196 | + for i := range statusSet { |
| 197 | + status := statusSet[i].(int) |
| 198 | + statusList = append(statusList, helper.IntInt64(status)) |
| 199 | + } |
| 200 | + |
| 201 | + paramMap["Status"] = statusList |
| 202 | + } |
| 203 | + |
| 204 | + if v, ok := d.GetOk("project_ids"); ok { |
| 205 | + projectIdsList := make([]*int64, 0) |
| 206 | + projectIdsSet := v.(*schema.Set).List() |
| 207 | + for i := range projectIdsSet { |
| 208 | + projectIds := projectIdsSet[i].(int) |
| 209 | + projectIdsList = append(projectIdsList, helper.IntInt64(projectIds)) |
| 210 | + } |
| 211 | + |
| 212 | + paramMap["ProjectIds"] = projectIdsList |
| 213 | + } |
| 214 | + |
| 215 | + if v, ok := d.GetOk("auto_renew_flag"); ok { |
| 216 | + autoRenewFlagList := make([]*int64, 0) |
| 217 | + autoRenewFlagSet := v.(*schema.Set).List() |
| 218 | + for i := range autoRenewFlagSet { |
| 219 | + autoRenewFlag := autoRenewFlagSet[i].(int) |
| 220 | + autoRenewFlagList = append(autoRenewFlagList, helper.IntInt64(autoRenewFlag)) |
| 221 | + } |
| 222 | + |
| 223 | + paramMap["AutoRenewFlag"] = autoRenewFlagList |
| 224 | + } |
| 225 | + |
| 226 | + if v, ok := d.GetOk("cluster_name"); ok { |
| 227 | + paramMap["ClusterName"] = helper.String(v.(string)) |
| 228 | + } |
| 229 | + |
| 230 | + if v, ok := d.GetOk("search_key"); ok { |
| 231 | + paramMap["SearchKey"] = helper.String(v.(string)) |
| 232 | + } |
| 233 | + |
| 234 | + if v, ok := d.GetOk("dedicated_cluster_id"); ok { |
| 235 | + paramMap["DedicatedClusterId"] = helper.String(v.(string)) |
| 236 | + } |
| 237 | + |
| 238 | + var resources []*redis.CDCResource |
| 239 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 240 | + result, e := service.DescribeRedisClustersByFilter(ctx, paramMap) |
| 241 | + if e != nil { |
| 242 | + return tccommon.RetryError(e) |
| 243 | + } |
| 244 | + |
| 245 | + resources = result |
| 246 | + return nil |
| 247 | + }) |
| 248 | + |
| 249 | + if err != nil { |
| 250 | + return err |
| 251 | + } |
| 252 | + |
| 253 | + ids := make([]string, 0, len(resources)) |
| 254 | + tmpList := make([]map[string]interface{}, 0, len(resources)) |
| 255 | + if resources != nil { |
| 256 | + for _, cDCResource := range resources { |
| 257 | + cDCResourceMap := map[string]interface{}{} |
| 258 | + if cDCResource.AppId != nil { |
| 259 | + cDCResourceMap["app_id"] = cDCResource.AppId |
| 260 | + } |
| 261 | + |
| 262 | + if cDCResource.RegionId != nil { |
| 263 | + cDCResourceMap["region_id"] = cDCResource.RegionId |
| 264 | + } |
| 265 | + |
| 266 | + if cDCResource.ZoneId != nil { |
| 267 | + cDCResourceMap["zone_id"] = cDCResource.ZoneId |
| 268 | + } |
| 269 | + |
| 270 | + if cDCResource.RedisClusterId != nil { |
| 271 | + cDCResourceMap["redis_cluster_id"] = cDCResource.RedisClusterId |
| 272 | + } |
| 273 | + |
| 274 | + if cDCResource.PayMode != nil { |
| 275 | + cDCResourceMap["pay_mode"] = cDCResource.PayMode |
| 276 | + } |
| 277 | + |
| 278 | + if cDCResource.ProjectId != nil { |
| 279 | + cDCResourceMap["project_id"] = cDCResource.ProjectId |
| 280 | + } |
| 281 | + |
| 282 | + if cDCResource.AutoRenewFlag != nil { |
| 283 | + cDCResourceMap["auto_renew_flag"] = cDCResource.AutoRenewFlag |
| 284 | + } |
| 285 | + |
| 286 | + if cDCResource.ClusterName != nil { |
| 287 | + cDCResourceMap["cluster_name"] = cDCResource.ClusterName |
| 288 | + } |
| 289 | + |
| 290 | + if cDCResource.StartTime != nil { |
| 291 | + cDCResourceMap["start_time"] = cDCResource.StartTime |
| 292 | + } |
| 293 | + |
| 294 | + if cDCResource.EndTime != nil { |
| 295 | + cDCResourceMap["end_time"] = cDCResource.EndTime |
| 296 | + } |
| 297 | + |
| 298 | + if cDCResource.Status != nil { |
| 299 | + cDCResourceMap["status"] = cDCResource.Status |
| 300 | + } |
| 301 | + |
| 302 | + if cDCResource.BaseBundles != nil { |
| 303 | + baseBundlesList := []interface{}{} |
| 304 | + for _, baseBundles := range cDCResource.BaseBundles { |
| 305 | + baseBundlesMap := map[string]interface{}{} |
| 306 | + if baseBundles.ResourceBundleName != nil { |
| 307 | + baseBundlesMap["resource_bundle_name"] = baseBundles.ResourceBundleName |
| 308 | + } |
| 309 | + |
| 310 | + if baseBundles.AvailableMemory != nil { |
| 311 | + baseBundlesMap["available_memory"] = baseBundles.AvailableMemory |
| 312 | + } |
| 313 | + |
| 314 | + if baseBundles.Count != nil { |
| 315 | + baseBundlesMap["count"] = baseBundles.Count |
| 316 | + } |
| 317 | + |
| 318 | + baseBundlesList = append(baseBundlesList, baseBundlesMap) |
| 319 | + } |
| 320 | + |
| 321 | + cDCResourceMap["base_bundles"] = baseBundlesList |
| 322 | + } |
| 323 | + |
| 324 | + if cDCResource.ResourceBundles != nil { |
| 325 | + resourceBundlesList := []interface{}{} |
| 326 | + for _, resourceBundles := range cDCResource.ResourceBundles { |
| 327 | + resourceBundlesMap := map[string]interface{}{} |
| 328 | + if resourceBundles.ResourceBundleName != nil { |
| 329 | + resourceBundlesMap["resource_bundle_name"] = resourceBundles.ResourceBundleName |
| 330 | + } |
| 331 | + |
| 332 | + if resourceBundles.AvailableMemory != nil { |
| 333 | + resourceBundlesMap["available_memory"] = resourceBundles.AvailableMemory |
| 334 | + } |
| 335 | + |
| 336 | + if resourceBundles.Count != nil { |
| 337 | + resourceBundlesMap["count"] = resourceBundles.Count |
| 338 | + } |
| 339 | + |
| 340 | + resourceBundlesList = append(resourceBundlesList, resourceBundlesMap) |
| 341 | + } |
| 342 | + |
| 343 | + cDCResourceMap["resource_bundles"] = resourceBundlesList |
| 344 | + } |
| 345 | + |
| 346 | + if cDCResource.DedicatedClusterId != nil { |
| 347 | + cDCResourceMap["dedicated_cluster_id"] = cDCResource.DedicatedClusterId |
| 348 | + } |
| 349 | + |
| 350 | + ids = append(ids, *cDCResource.DedicatedClusterId) |
| 351 | + tmpList = append(tmpList, cDCResourceMap) |
| 352 | + } |
| 353 | + |
| 354 | + _ = d.Set("resources", tmpList) |
| 355 | + } |
| 356 | + |
| 357 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 358 | + output, ok := d.GetOk("result_output_file") |
| 359 | + if ok && output.(string) != "" { |
| 360 | + if e := tccommon.WriteToFile(output.(string), tmpList); e != nil { |
| 361 | + return e |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + return nil |
| 366 | +} |
0 commit comments