|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of Cynosdb clusters. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cynosdb_clusters" "foo" { |
| 8 | + cluster_id = "cynosdbmysql-dzj5l8gz" |
| 9 | + project_id = 0 |
| 10 | + db_type = "MYSQL" |
| 11 | + cluster_name = "test" |
| 12 | +} |
| 13 | +``` |
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func dataSourceTencentCloudCynosdbClusters() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Read: dataSourceTencentCloudCynosdbClustersRead, |
| 30 | + |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "db_type": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Description: "Type of CynosDB, and available values include `MYSQL`, `POSTGRESQL`.", |
| 36 | + }, |
| 37 | + "cluster_id": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Description: "ID of the cluster to be queried.", |
| 41 | + }, |
| 42 | + "project_id": { |
| 43 | + Type: schema.TypeInt, |
| 44 | + Optional: true, |
| 45 | + Description: "ID of the project to be queried.", |
| 46 | + }, |
| 47 | + "cluster_name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + Description: "Name of the cluster to be queried.", |
| 51 | + }, |
| 52 | + "result_output_file": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Optional: true, |
| 55 | + Description: "Used to save results.", |
| 56 | + }, |
| 57 | + "cluster_list": { |
| 58 | + Type: schema.TypeList, |
| 59 | + Computed: true, |
| 60 | + Description: "A list of clusters. Each element contains the following attributes:", |
| 61 | + Elem: &schema.Resource{ |
| 62 | + Schema: map[string]*schema.Schema{ |
| 63 | + "project_id": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: "ID of the project.", |
| 67 | + }, |
| 68 | + "available_zone": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "The available zone of the CynosDB Cluster.", |
| 72 | + }, |
| 73 | + "vpc_id": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "ID of the VPC.", |
| 77 | + }, |
| 78 | + "subnet_id": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "ID of the subnet within this VPC.", |
| 82 | + }, |
| 83 | + "port": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Computed: true, |
| 86 | + Description: "Port of CynosDB cluster.", |
| 87 | + }, |
| 88 | + "db_type": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: "Type of CynosDB, and available values include `MYSQL`.", |
| 92 | + }, |
| 93 | + "db_version": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "Version of CynosDB, which is related to `db_type`. For `MYSQL`, available value is `5.7`.", |
| 97 | + }, |
| 98 | + "cluster_limit": { |
| 99 | + Type: schema.TypeInt, |
| 100 | + Computed: true, |
| 101 | + Description: "Storage limit of CynosDB cluster instance, unit in GB.", |
| 102 | + }, |
| 103 | + "cluster_name": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "Name of CynosDB cluster.", |
| 107 | + }, |
| 108 | + "cluster_id": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + Description: "ID of CynosDB cluster.", |
| 112 | + }, |
| 113 | + // payment |
| 114 | + "charge_type": { |
| 115 | + Type: schema.TypeString, |
| 116 | + Computed: true, |
| 117 | + Description: "The charge type of instance. Valid values are `PREPAID` and `POSTPAID_BY_HOUR`. Default value is `POSTPAID_BY_HOUR`.", |
| 118 | + }, |
| 119 | + "auto_renew_flag": { |
| 120 | + Type: schema.TypeInt, |
| 121 | + Computed: true, |
| 122 | + Description: "Auto renew flag. Valid values are `0`(MANUAL_RENEW), `1`(AUTO_RENEW). Only works for PREPAID cluster.", |
| 123 | + }, |
| 124 | + "cluster_status": { |
| 125 | + Type: schema.TypeString, |
| 126 | + Computed: true, |
| 127 | + Description: "Status of the Cynosdb cluster.", |
| 128 | + }, |
| 129 | + "create_time": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + Description: "Creation time of the CynosDB cluster.", |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func dataSourceTencentCloudCynosdbClustersRead(d *schema.ResourceData, meta interface{}) error { |
| 142 | + defer logElapsed("data_source.tencentcloud_cynosdb_clusters.read")() |
| 143 | + |
| 144 | + logId := getLogId(contextNil) |
| 145 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 146 | + |
| 147 | + params := make(map[string]string) |
| 148 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 149 | + params["ClusterId"] = v.(string) |
| 150 | + } |
| 151 | + if v, ok := d.GetOk("cluster_name"); ok { |
| 152 | + params["ClusterName"] = v.(string) |
| 153 | + } |
| 154 | + if v, ok := d.GetOkExists("project_id"); ok { |
| 155 | + params["ProjectId"] = fmt.Sprintf("%d", v.(int)) |
| 156 | + } |
| 157 | + clusterType := "" |
| 158 | + if v, ok := d.GetOk("cluster_type"); ok { |
| 159 | + clusterType = v.(string) |
| 160 | + } |
| 161 | + |
| 162 | + cynosdbService := CynosdbService{ |
| 163 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 164 | + } |
| 165 | + |
| 166 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 167 | + clusters, e := cynosdbService.DescribeClusters(ctx, params) |
| 168 | + if e != nil { |
| 169 | + return retryError(e) |
| 170 | + } |
| 171 | + ids := make([]string, 0, len(clusters)) |
| 172 | + clusterList := make([]map[string]interface{}, 0, len(clusters)) |
| 173 | + for _, cluster := range clusters { |
| 174 | + if clusterType != "" && clusterType != *cluster.DbType { |
| 175 | + continue |
| 176 | + } |
| 177 | + mapping := map[string]interface{}{ |
| 178 | + "cluster_id": cluster.ClusterId, |
| 179 | + "cluster_name": cluster.ClusterName, |
| 180 | + "cluster_limit": cluster.StorageLimit, |
| 181 | + "db_type": cluster.DbType, |
| 182 | + "available_zone": cluster.Zone, |
| 183 | + "project_id": cluster.ProjectID, |
| 184 | + "create_time": cluster.CreateTime, |
| 185 | + "cluster_status": cluster.Status, |
| 186 | + "auto_renew_flag": cluster.RenewFlag, |
| 187 | + "port": cluster.Vport, |
| 188 | + "vpc_id": cluster.VpcId, |
| 189 | + "subnet_id": cluster.SubnetId, |
| 190 | + "db_version": cluster.DbVersion, |
| 191 | + "charge_type": CYNOSDB_CHARGE_TYPE[*cluster.PayMode], |
| 192 | + } |
| 193 | + |
| 194 | + clusterList = append(clusterList, mapping) |
| 195 | + ids = append(ids, *cluster.ClusterId) |
| 196 | + } |
| 197 | + |
| 198 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 199 | + if e = d.Set("cluster_list", clusterList); e != nil { |
| 200 | + log.Printf("[CRITAL]%s provider set cluster list fail, reason:%s\n ", logId, e.Error()) |
| 201 | + return resource.NonRetryableError(e) |
| 202 | + } |
| 203 | + |
| 204 | + output, ok := d.GetOk("result_output_file") |
| 205 | + if ok && output.(string) != "" { |
| 206 | + if e := writeToFile(output.(string), clusterList); e != nil { |
| 207 | + return resource.NonRetryableError(e) |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return nil |
| 212 | + }) |
| 213 | + if err != nil { |
| 214 | + log.Printf("[CRITAL]%s read cynosdb clusters failed, reason:%s\n ", logId, err.Error()) |
| 215 | + return err |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments