|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dbbrain health_scores |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dbbrain_health_scores" "health_scores" { |
| 8 | + instance_id = "" |
| 9 | + time = "" |
| 10 | + product = "" |
| 11 | + } |
| 12 | +``` |
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 21 | + dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudDbbrainHealthScores() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudDbbrainHealthScoresRead, |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "instance_id": { |
| 30 | + Required: true, |
| 31 | + Type: schema.TypeString, |
| 32 | + Description: "The ID of the instance whose health score needs to be obtained.", |
| 33 | + }, |
| 34 | + |
| 35 | + "time": { |
| 36 | + Required: true, |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "The time to obtain the health score, the time format is as follows: 2019-09-10 12:13:14.", |
| 39 | + }, |
| 40 | + |
| 41 | + "product": { |
| 42 | + Required: true, |
| 43 | + Type: schema.TypeString, |
| 44 | + Description: "Service product type, supported values include: mysql - cloud database MySQL, cynosdb - cloud database TDSQL-C for MySQL, the default is mysql.", |
| 45 | + }, |
| 46 | + |
| 47 | + "data": { |
| 48 | + Computed: true, |
| 49 | + Type: schema.TypeList, |
| 50 | + Description: "Health score and abnormal deduction items.", |
| 51 | + Elem: &schema.Resource{ |
| 52 | + Schema: map[string]*schema.Schema{ |
| 53 | + "issue_types": { |
| 54 | + Type: schema.TypeList, |
| 55 | + Computed: true, |
| 56 | + Description: "Exception details.", |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "issue_type": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Index classification: AVAILABILITY: availability, MAINTAINABILITY: maintainability, PERFORMANCE, performance, RELIABILITY reliability.", |
| 63 | + }, |
| 64 | + "events": { |
| 65 | + Type: schema.TypeList, |
| 66 | + Computed: true, |
| 67 | + Description: "unusual event.", |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "event_id": { |
| 71 | + Type: schema.TypeInt, |
| 72 | + Computed: true, |
| 73 | + Description: "Event ID.", |
| 74 | + }, |
| 75 | + "diag_type": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Diagnostic type.", |
| 79 | + }, |
| 80 | + "start_time": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "Starting time.", |
| 84 | + }, |
| 85 | + "end_time": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: "End Time.", |
| 89 | + }, |
| 90 | + "outline": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + Description: "overview.", |
| 94 | + }, |
| 95 | + "severity": { |
| 96 | + Type: schema.TypeInt, |
| 97 | + Computed: true, |
| 98 | + Description: "severity. The severity is divided into 5 levels, according to the degree of impact from high to low: 1: Fatal, 2: Serious, 3: Warning, 4: Prompt, 5: Healthy.", |
| 99 | + }, |
| 100 | + "score_lost": { |
| 101 | + Type: schema.TypeInt, |
| 102 | + Computed: true, |
| 103 | + Description: "Points deducted.", |
| 104 | + }, |
| 105 | + "metric": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Computed: true, |
| 108 | + Description: "reserved text.", |
| 109 | + }, |
| 110 | + "count": { |
| 111 | + Type: schema.TypeInt, |
| 112 | + Computed: true, |
| 113 | + Description: "Number of alerts.", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + "total_count": { |
| 119 | + Type: schema.TypeInt, |
| 120 | + Computed: true, |
| 121 | + Description: "The total number of abnormal events.", |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + "events_total_count": { |
| 127 | + Type: schema.TypeInt, |
| 128 | + Computed: true, |
| 129 | + Description: "The total number of abnormal events.", |
| 130 | + }, |
| 131 | + "health_score": { |
| 132 | + Type: schema.TypeInt, |
| 133 | + Computed: true, |
| 134 | + Description: "Health score.", |
| 135 | + }, |
| 136 | + "health_level": { |
| 137 | + Type: schema.TypeString, |
| 138 | + Computed: true, |
| 139 | + Description: "Health level, such as: HEALTH, SUB_HEALTH, RISK, HIGH_RISK.", |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + |
| 145 | + "result_output_file": { |
| 146 | + Type: schema.TypeString, |
| 147 | + Optional: true, |
| 148 | + Description: "Used to save results.", |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func dataSourceTencentCloudDbbrainHealthScoresRead(d *schema.ResourceData, meta interface{}) error { |
| 155 | + defer logElapsed("data_source.tencentcloud_dbbrain_health_scores.read")() |
| 156 | + defer inconsistentCheck(d, meta)() |
| 157 | + |
| 158 | + logId := getLogId(contextNil) |
| 159 | + var instanceId string |
| 160 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 161 | + |
| 162 | + paramMap := make(map[string]interface{}) |
| 163 | + if v, ok := d.GetOk("instance_id"); ok { |
| 164 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 165 | + instanceId = v.(string) |
| 166 | + } |
| 167 | + |
| 168 | + if v, ok := d.GetOk("time"); ok { |
| 169 | + paramMap["time"] = helper.String(v.(string)) |
| 170 | + } |
| 171 | + |
| 172 | + if v, ok := d.GetOk("product"); ok { |
| 173 | + paramMap["product"] = helper.String(v.(string)) |
| 174 | + } |
| 175 | + |
| 176 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 177 | + |
| 178 | + var data *dbbrain.HealthScoreInfo |
| 179 | + |
| 180 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 181 | + result, e := service.DescribeDbbrainHealthScoresByFilter(ctx, paramMap) |
| 182 | + if e != nil { |
| 183 | + return retryError(e) |
| 184 | + } |
| 185 | + data = result |
| 186 | + return nil |
| 187 | + }) |
| 188 | + if err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + |
| 192 | + healthScoreInfoMap := map[string]interface{}{} |
| 193 | + if data != nil { |
| 194 | + if data.IssueTypes != nil { |
| 195 | + issueTypesList := []interface{}{} |
| 196 | + for _, issueTypes := range data.IssueTypes { |
| 197 | + issueTypesMap := map[string]interface{}{} |
| 198 | + |
| 199 | + if issueTypes.IssueType != nil { |
| 200 | + issueTypesMap["issue_type"] = issueTypes.IssueType |
| 201 | + } |
| 202 | + |
| 203 | + if issueTypes.Events != nil { |
| 204 | + eventsList := []interface{}{} |
| 205 | + for _, events := range issueTypes.Events { |
| 206 | + eventsMap := map[string]interface{}{} |
| 207 | + |
| 208 | + if events.EventId != nil { |
| 209 | + eventsMap["event_id"] = events.EventId |
| 210 | + } |
| 211 | + |
| 212 | + if events.DiagType != nil { |
| 213 | + eventsMap["diag_type"] = events.DiagType |
| 214 | + } |
| 215 | + |
| 216 | + if events.StartTime != nil { |
| 217 | + eventsMap["start_time"] = events.StartTime |
| 218 | + } |
| 219 | + |
| 220 | + if events.EndTime != nil { |
| 221 | + eventsMap["end_time"] = events.EndTime |
| 222 | + } |
| 223 | + |
| 224 | + if events.Outline != nil { |
| 225 | + eventsMap["outline"] = events.Outline |
| 226 | + } |
| 227 | + |
| 228 | + if events.Severity != nil { |
| 229 | + eventsMap["severity"] = events.Severity |
| 230 | + } |
| 231 | + |
| 232 | + if events.ScoreLost != nil { |
| 233 | + eventsMap["score_lost"] = events.ScoreLost |
| 234 | + } |
| 235 | + |
| 236 | + if events.Metric != nil { |
| 237 | + eventsMap["metric"] = events.Metric |
| 238 | + } |
| 239 | + |
| 240 | + if events.Count != nil { |
| 241 | + eventsMap["count"] = events.Count |
| 242 | + } |
| 243 | + |
| 244 | + eventsList = append(eventsList, eventsMap) |
| 245 | + } |
| 246 | + |
| 247 | + issueTypesMap["events"] = eventsList |
| 248 | + } |
| 249 | + |
| 250 | + if issueTypes.TotalCount != nil { |
| 251 | + issueTypesMap["total_count"] = issueTypes.TotalCount |
| 252 | + } |
| 253 | + |
| 254 | + issueTypesList = append(issueTypesList, issueTypesMap) |
| 255 | + } |
| 256 | + |
| 257 | + healthScoreInfoMap["issue_types"] = issueTypesList |
| 258 | + } |
| 259 | + |
| 260 | + if data.EventsTotalCount != nil { |
| 261 | + healthScoreInfoMap["events_total_count"] = data.EventsTotalCount |
| 262 | + } |
| 263 | + |
| 264 | + if data.HealthScore != nil { |
| 265 | + healthScoreInfoMap["health_score"] = data.HealthScore |
| 266 | + } |
| 267 | + |
| 268 | + if data.HealthLevel != nil { |
| 269 | + healthScoreInfoMap["health_level"] = data.HealthLevel |
| 270 | + } |
| 271 | + |
| 272 | + _ = d.Set("data", []interface{}{healthScoreInfoMap}) |
| 273 | + } |
| 274 | + |
| 275 | + d.SetId(instanceId) |
| 276 | + output, ok := d.GetOk("result_output_file") |
| 277 | + if ok && output.(string) != "" { |
| 278 | + if e := writeToFile(output.(string), healthScoreInfoMap); e != nil { |
| 279 | + return e |
| 280 | + } |
| 281 | + } |
| 282 | + return nil |
| 283 | +} |
0 commit comments