|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dbbrain diag_event |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_dbbrain_diag_history" "diag_history" { |
| 8 | + instance_id = "%s" |
| 9 | + start_time = "%s" |
| 10 | + end_time = "%s" |
| 11 | + product = "mysql" |
| 12 | +} |
| 13 | +
|
| 14 | +data "tencentcloud_dbbrain_diag_event" "diag_event" { |
| 15 | + instance_id = "%s" |
| 16 | + event_id = data.tencentcloud_dbbrain_diag_history.diag_history.events.0.event_id |
| 17 | + product = "mysql" |
| 18 | +} |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 28 | + dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527" |
| 29 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 30 | +) |
| 31 | + |
| 32 | +func dataSourceTencentCloudDbbrainDiagEvent() *schema.Resource { |
| 33 | + return &schema.Resource{ |
| 34 | + Read: dataSourceTencentCloudDbbrainDiagEventRead, |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "instance_id": { |
| 37 | + Required: true, |
| 38 | + Type: schema.TypeString, |
| 39 | + Description: "isntance id.", |
| 40 | + }, |
| 41 | + |
| 42 | + "event_id": { |
| 43 | + Optional: true, |
| 44 | + Computed: true, |
| 45 | + Type: schema.TypeInt, |
| 46 | + Description: "Event ID. Obtain it through `Get Instance Diagnosis History DescribeDBDiagHistory`.", |
| 47 | + }, |
| 48 | + |
| 49 | + "product": { |
| 50 | + Optional: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Service product type, supported values include: `mysql` - cloud database MySQL, `cynosdb` - cloud database CynosDB for MySQL, the default is `mysql`.", |
| 53 | + }, |
| 54 | + |
| 55 | + "diag_item": { |
| 56 | + Computed: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "diagnostic item.", |
| 59 | + }, |
| 60 | + |
| 61 | + "diag_type": { |
| 62 | + Computed: true, |
| 63 | + Type: schema.TypeString, |
| 64 | + Description: "Diagnostic type.", |
| 65 | + }, |
| 66 | + |
| 67 | + "explanation": { |
| 68 | + Computed: true, |
| 69 | + Type: schema.TypeString, |
| 70 | + Description: "Diagnostic event details, output is empty if there is no additional explanatory information.", |
| 71 | + }, |
| 72 | + |
| 73 | + "outline": { |
| 74 | + Computed: true, |
| 75 | + Type: schema.TypeString, |
| 76 | + Description: "Diagnostic summary.", |
| 77 | + }, |
| 78 | + |
| 79 | + "problem": { |
| 80 | + Computed: true, |
| 81 | + Type: schema.TypeString, |
| 82 | + Description: "Diagnosed problem.", |
| 83 | + }, |
| 84 | + |
| 85 | + "severity": { |
| 86 | + Computed: true, |
| 87 | + Type: schema.TypeInt, |
| 88 | + 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.", |
| 89 | + }, |
| 90 | + |
| 91 | + "start_time": { |
| 92 | + Computed: true, |
| 93 | + Type: schema.TypeString, |
| 94 | + Description: "Starting time.", |
| 95 | + }, |
| 96 | + |
| 97 | + "suggestions": { |
| 98 | + Computed: true, |
| 99 | + Type: schema.TypeString, |
| 100 | + Description: "A diagnostic suggestion, or empty if there is no suggestion.", |
| 101 | + }, |
| 102 | + |
| 103 | + "metric": { |
| 104 | + Computed: true, |
| 105 | + Type: schema.TypeString, |
| 106 | + Description: "reserved text. Note: This field may return null, indicating that no valid value can be obtained.", |
| 107 | + }, |
| 108 | + |
| 109 | + "end_time": { |
| 110 | + Computed: true, |
| 111 | + Type: schema.TypeString, |
| 112 | + Description: "End Time.", |
| 113 | + }, |
| 114 | + |
| 115 | + "result_output_file": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Optional: true, |
| 118 | + Description: "Used to save results.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func dataSourceTencentCloudDbbrainDiagEventRead(d *schema.ResourceData, meta interface{}) error { |
| 125 | + defer logElapsed("data_source.tencentcloud_dbbrain_diag_event.read")() |
| 126 | + defer inconsistentCheck(d, meta)() |
| 127 | + |
| 128 | + logId := getLogId(contextNil) |
| 129 | + |
| 130 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 131 | + var id string |
| 132 | + |
| 133 | + paramMap := make(map[string]interface{}) |
| 134 | + if v, ok := d.GetOk("instance_id"); ok { |
| 135 | + paramMap["instance_id"] = helper.String(v.(string)) |
| 136 | + id = v.(string) |
| 137 | + } |
| 138 | + |
| 139 | + if v, _ := d.GetOk("event_id"); v != nil { |
| 140 | + paramMap["event_id"] = helper.IntInt64(v.(int)) |
| 141 | + } |
| 142 | + |
| 143 | + if v, ok := d.GetOk("product"); ok { |
| 144 | + paramMap["product"] = helper.String(v.(string)) |
| 145 | + } |
| 146 | + |
| 147 | + var result *dbbrain.DescribeDBDiagEventResponseParams |
| 148 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 149 | + |
| 150 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 151 | + var e error |
| 152 | + result, e = service.DescribeDbbrainDiagEventByFilter(ctx, paramMap) |
| 153 | + if e != nil { |
| 154 | + return retryError(e) |
| 155 | + } |
| 156 | + return nil |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + if result != nil { |
| 163 | + if result.DiagItem != nil { |
| 164 | + _ = d.Set("diag_item", result.DiagItem) |
| 165 | + } |
| 166 | + |
| 167 | + if result.DiagType != nil { |
| 168 | + _ = d.Set("diag_type", result.DiagType) |
| 169 | + } |
| 170 | + |
| 171 | + if result.EventId != nil { |
| 172 | + _ = d.Set("event_id", result.EventId) |
| 173 | + } |
| 174 | + |
| 175 | + if result.Explanation != nil { |
| 176 | + _ = d.Set("explanation", result.Explanation) |
| 177 | + } |
| 178 | + |
| 179 | + if result.Outline != nil { |
| 180 | + _ = d.Set("outline", result.Outline) |
| 181 | + } |
| 182 | + |
| 183 | + if result.Problem != nil { |
| 184 | + _ = d.Set("problem", result.Problem) |
| 185 | + } |
| 186 | + |
| 187 | + if result.Severity != nil { |
| 188 | + _ = d.Set("severity", result.Severity) |
| 189 | + } |
| 190 | + |
| 191 | + if result.StartTime != nil { |
| 192 | + _ = d.Set("start_time", result.StartTime) |
| 193 | + } |
| 194 | + |
| 195 | + if result.Suggestions != nil { |
| 196 | + _ = d.Set("suggestions", result.Suggestions) |
| 197 | + } |
| 198 | + |
| 199 | + if result.Metric != nil { |
| 200 | + _ = d.Set("metric", result.Metric) |
| 201 | + } |
| 202 | + |
| 203 | + if result.EndTime != nil { |
| 204 | + _ = d.Set("end_time", result.EndTime) |
| 205 | + } |
| 206 | + |
| 207 | + } |
| 208 | + |
| 209 | + d.SetId(id) |
| 210 | + output, ok := d.GetOk("result_output_file") |
| 211 | + if ok && output.(string) != "" { |
| 212 | + if e := writeToFile(output.(string), result); e != nil { |
| 213 | + return e |
| 214 | + } |
| 215 | + } |
| 216 | + return nil |
| 217 | +} |
0 commit comments