|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cynosdb instance_slow_queries |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +Query slow queries of instance |
| 7 | +```hcl |
| 8 | +variable "cynosdb_cluster_id" { |
| 9 | + default = "default_cynosdb_cluster" |
| 10 | +} |
| 11 | +
|
| 12 | +data "tencentcloud_cynosdb_instance_slow_queries" "instance_slow_queries" { |
| 13 | + instance_id = var.cynosdb_cluster_id |
| 14 | + start_time = "2023-06-20 23:19:03" |
| 15 | + end_time = "2023-06-30 23:19:03" |
| 16 | + username = "keep_dts" |
| 17 | + host = "%%" |
| 18 | + database = "tf_ci_test" |
| 19 | + order_by = "QueryTime" |
| 20 | + order_by_type = "desc" |
| 21 | +} |
| 22 | +``` |
| 23 | +
|
| 24 | +Query slow queries by time range |
| 25 | +```hcl |
| 26 | +variable "cynosdb_cluster_id" { |
| 27 | + default = "default_cynosdb_cluster" |
| 28 | +} |
| 29 | +
|
| 30 | +data "tencentcloud_cynosdb_instance_slow_queries" "instance_slow_queries" { |
| 31 | + instance_id = var.cynosdb_cluster_id |
| 32 | + start_time = "2023-06-20 23:19:03" |
| 33 | + end_time = "2023-06-30 23:19:03" |
| 34 | + order_by = "QueryTime" |
| 35 | + order_by_type = "desc" |
| 36 | +} |
| 37 | +``` |
| 38 | +
|
| 39 | +Query slow queries by user and db name |
| 40 | +```hcl |
| 41 | +variable "cynosdb_cluster_id" { |
| 42 | + default = "default_cynosdb_cluster" |
| 43 | +} |
| 44 | +
|
| 45 | +data "tencentcloud_cynosdb_instance_slow_queries" "instance_slow_queries" { |
| 46 | + instance_id = var.cynosdb_cluster_id |
| 47 | + username = "keep_dts" |
| 48 | + host = "%%" |
| 49 | + database = "tf_ci_test" |
| 50 | + order_by = "QueryTime" |
| 51 | + order_by_type = "desc" |
| 52 | +} |
| 53 | +``` |
| 54 | +*/ |
| 55 | +package tencentcloud |
| 56 | + |
| 57 | +import ( |
| 58 | + "context" |
| 59 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 60 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 61 | + cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" |
| 62 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 63 | +) |
| 64 | + |
| 65 | +func dataSourceTencentCloudCynosdbInstanceSlowQueries() *schema.Resource { |
| 66 | + return &schema.Resource{ |
| 67 | + Read: dataSourceTencentCloudCynosdbInstanceSlowQueriesRead, |
| 68 | + Schema: map[string]*schema.Schema{ |
| 69 | + "instance_id": { |
| 70 | + Required: true, |
| 71 | + Type: schema.TypeString, |
| 72 | + Description: "Instance ID.", |
| 73 | + }, |
| 74 | + |
| 75 | + "start_time": { |
| 76 | + Optional: true, |
| 77 | + Type: schema.TypeString, |
| 78 | + Description: "Earliest transaction start time.", |
| 79 | + }, |
| 80 | + |
| 81 | + "end_time": { |
| 82 | + Optional: true, |
| 83 | + Type: schema.TypeString, |
| 84 | + Description: "Latest transaction start time.", |
| 85 | + }, |
| 86 | + |
| 87 | + "username": { |
| 88 | + Optional: true, |
| 89 | + Type: schema.TypeString, |
| 90 | + Description: "user name.", |
| 91 | + }, |
| 92 | + |
| 93 | + "host": { |
| 94 | + Optional: true, |
| 95 | + Type: schema.TypeString, |
| 96 | + Description: "Client host.", |
| 97 | + }, |
| 98 | + |
| 99 | + "database": { |
| 100 | + Optional: true, |
| 101 | + Type: schema.TypeString, |
| 102 | + Description: "Database name.", |
| 103 | + }, |
| 104 | + |
| 105 | + "order_by": { |
| 106 | + Optional: true, |
| 107 | + Type: schema.TypeString, |
| 108 | + Description: "Sort field, optional values: QueryTime, LockTime, RowsExamined, RowsSent.", |
| 109 | + }, |
| 110 | + |
| 111 | + "order_by_type": { |
| 112 | + Optional: true, |
| 113 | + Type: schema.TypeString, |
| 114 | + Description: "Sort type, optional values: asc, desc.", |
| 115 | + }, |
| 116 | + |
| 117 | + "slow_queries": { |
| 118 | + Computed: true, |
| 119 | + Type: schema.TypeList, |
| 120 | + Description: "Slow query records.", |
| 121 | + Elem: &schema.Resource{ |
| 122 | + Schema: map[string]*schema.Schema{ |
| 123 | + "timestamp": { |
| 124 | + Type: schema.TypeInt, |
| 125 | + Computed: true, |
| 126 | + Description: "Execution timestamp.", |
| 127 | + }, |
| 128 | + "query_time": { |
| 129 | + Type: schema.TypeFloat, |
| 130 | + Computed: true, |
| 131 | + Description: "Execution time in seconds.", |
| 132 | + }, |
| 133 | + "sql_text": { |
| 134 | + Type: schema.TypeString, |
| 135 | + Computed: true, |
| 136 | + Description: "SQL statement.", |
| 137 | + }, |
| 138 | + "user_host": { |
| 139 | + Type: schema.TypeString, |
| 140 | + Computed: true, |
| 141 | + Description: "Client host.", |
| 142 | + }, |
| 143 | + "user_name": { |
| 144 | + Type: schema.TypeString, |
| 145 | + Computed: true, |
| 146 | + Description: "user name.", |
| 147 | + }, |
| 148 | + "database": { |
| 149 | + Type: schema.TypeString, |
| 150 | + Computed: true, |
| 151 | + Description: "Database name.", |
| 152 | + }, |
| 153 | + "lock_time": { |
| 154 | + Type: schema.TypeFloat, |
| 155 | + Computed: true, |
| 156 | + Description: "Lock duration in seconds.", |
| 157 | + }, |
| 158 | + "rows_examined": { |
| 159 | + Type: schema.TypeInt, |
| 160 | + Computed: true, |
| 161 | + Description: "Scan Rows.", |
| 162 | + }, |
| 163 | + "rows_sent": { |
| 164 | + Type: schema.TypeInt, |
| 165 | + Computed: true, |
| 166 | + Description: "Return the number of rows.", |
| 167 | + }, |
| 168 | + "sql_template": { |
| 169 | + Type: schema.TypeString, |
| 170 | + Computed: true, |
| 171 | + Description: "SQL template.", |
| 172 | + }, |
| 173 | + "sql_md5": { |
| 174 | + Type: schema.TypeString, |
| 175 | + Computed: true, |
| 176 | + Description: "SQL statement md5.", |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + |
| 182 | + "result_output_file": { |
| 183 | + Type: schema.TypeString, |
| 184 | + Optional: true, |
| 185 | + Description: "Used to save results.", |
| 186 | + }, |
| 187 | + }, |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +func dataSourceTencentCloudCynosdbInstanceSlowQueriesRead(d *schema.ResourceData, meta interface{}) error { |
| 192 | + defer logElapsed("data_source.tencentcloud_cynosdb_instance_slow_queries.read")() |
| 193 | + defer inconsistentCheck(d, meta)() |
| 194 | + |
| 195 | + logId := getLogId(contextNil) |
| 196 | + |
| 197 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 198 | + |
| 199 | + paramMap := make(map[string]interface{}) |
| 200 | + if v, ok := d.GetOk("instance_id"); ok { |
| 201 | + paramMap["InstanceId"] = helper.String(v.(string)) |
| 202 | + } |
| 203 | + |
| 204 | + if v, ok := d.GetOk("start_time"); ok { |
| 205 | + paramMap["StartTime"] = helper.String(v.(string)) |
| 206 | + } |
| 207 | + |
| 208 | + if v, ok := d.GetOk("end_time"); ok { |
| 209 | + paramMap["EndTime"] = helper.String(v.(string)) |
| 210 | + } |
| 211 | + |
| 212 | + if v, ok := d.GetOk("username"); ok { |
| 213 | + paramMap["Username"] = helper.String(v.(string)) |
| 214 | + } |
| 215 | + |
| 216 | + if v, ok := d.GetOk("host"); ok { |
| 217 | + paramMap["Host"] = helper.String(v.(string)) |
| 218 | + } |
| 219 | + |
| 220 | + if v, ok := d.GetOk("database"); ok { |
| 221 | + paramMap["Database"] = helper.String(v.(string)) |
| 222 | + } |
| 223 | + |
| 224 | + if v, ok := d.GetOk("order_by"); ok { |
| 225 | + paramMap["OrderBy"] = helper.String(v.(string)) |
| 226 | + } |
| 227 | + |
| 228 | + if v, ok := d.GetOk("order_by_type"); ok { |
| 229 | + paramMap["OrderByType"] = helper.String(v.(string)) |
| 230 | + } |
| 231 | + |
| 232 | + service := CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 233 | + |
| 234 | + var slowQueries []*cynosdb.SlowQueriesItem |
| 235 | + |
| 236 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 237 | + result, e := service.DescribeCynosdbInstanceSlowQueriesByFilter(ctx, paramMap) |
| 238 | + if e != nil { |
| 239 | + return retryError(e) |
| 240 | + } |
| 241 | + slowQueries = result |
| 242 | + return nil |
| 243 | + }) |
| 244 | + if err != nil { |
| 245 | + return err |
| 246 | + } |
| 247 | + |
| 248 | + ids := make([]string, 0, len(slowQueries)) |
| 249 | + tmpList := make([]map[string]interface{}, 0, len(slowQueries)) |
| 250 | + |
| 251 | + if slowQueries != nil { |
| 252 | + for _, slowQueriesItem := range slowQueries { |
| 253 | + slowQueriesItemMap := map[string]interface{}{} |
| 254 | + |
| 255 | + if slowQueriesItem.Timestamp != nil { |
| 256 | + slowQueriesItemMap["timestamp"] = slowQueriesItem.Timestamp |
| 257 | + } |
| 258 | + |
| 259 | + if slowQueriesItem.QueryTime != nil { |
| 260 | + slowQueriesItemMap["query_time"] = slowQueriesItem.QueryTime |
| 261 | + } |
| 262 | + |
| 263 | + if slowQueriesItem.SqlText != nil { |
| 264 | + slowQueriesItemMap["sql_text"] = slowQueriesItem.SqlText |
| 265 | + } |
| 266 | + |
| 267 | + if slowQueriesItem.UserHost != nil { |
| 268 | + slowQueriesItemMap["user_host"] = slowQueriesItem.UserHost |
| 269 | + } |
| 270 | + |
| 271 | + if slowQueriesItem.UserName != nil { |
| 272 | + slowQueriesItemMap["user_name"] = slowQueriesItem.UserName |
| 273 | + } |
| 274 | + |
| 275 | + if slowQueriesItem.Database != nil { |
| 276 | + slowQueriesItemMap["database"] = slowQueriesItem.Database |
| 277 | + } |
| 278 | + |
| 279 | + if slowQueriesItem.LockTime != nil { |
| 280 | + slowQueriesItemMap["lock_time"] = slowQueriesItem.LockTime |
| 281 | + } |
| 282 | + |
| 283 | + if slowQueriesItem.RowsExamined != nil { |
| 284 | + slowQueriesItemMap["rows_examined"] = slowQueriesItem.RowsExamined |
| 285 | + } |
| 286 | + |
| 287 | + if slowQueriesItem.RowsSent != nil { |
| 288 | + slowQueriesItemMap["rows_sent"] = slowQueriesItem.RowsSent |
| 289 | + } |
| 290 | + |
| 291 | + if slowQueriesItem.SqlTemplate != nil { |
| 292 | + slowQueriesItemMap["sql_template"] = slowQueriesItem.SqlTemplate |
| 293 | + } |
| 294 | + |
| 295 | + if slowQueriesItem.SqlMd5 != nil { |
| 296 | + slowQueriesItemMap["sql_md5"] = slowQueriesItem.SqlMd5 |
| 297 | + } |
| 298 | + |
| 299 | + ids = append(ids, *slowQueriesItem.SqlMd5) |
| 300 | + tmpList = append(tmpList, slowQueriesItemMap) |
| 301 | + } |
| 302 | + |
| 303 | + _ = d.Set("slow_queries", tmpList) |
| 304 | + } |
| 305 | + |
| 306 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 307 | + output, ok := d.GetOk("result_output_file") |
| 308 | + if ok && output.(string) != "" { |
| 309 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 310 | + return e |
| 311 | + } |
| 312 | + } |
| 313 | + return nil |
| 314 | +} |
0 commit comments