|
| 1 | +package apm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + apmv20210622 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622" |
| 12 | + |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudApmPrometheusRule() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudApmPrometheusRuleCreate, |
| 20 | + Read: resourceTencentCloudApmPrometheusRuleRead, |
| 21 | + Update: resourceTencentCloudApmPrometheusRuleUpdate, |
| 22 | + Delete: resourceTencentCloudApmPrometheusRuleDelete, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "name": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + Description: "Metric match rule name.", |
| 28 | + }, |
| 29 | + |
| 30 | + "service_name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + Description: "Applications where the rule takes effect. input an empty string for all applications.", |
| 34 | + }, |
| 35 | + |
| 36 | + "metric_match_type": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Required: true, |
| 39 | + Description: "Match type: 0 - precision match, 1 - prefix match, 2 - suffix match.", |
| 40 | + }, |
| 41 | + |
| 42 | + "metric_name_rule": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + Description: "Specifies the rule for customer-defined metric names with cache hit.", |
| 46 | + }, |
| 47 | + |
| 48 | + "instance_id": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + Description: "Business system ID.", |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func resourceTencentCloudApmPrometheusRuleCreate(d *schema.ResourceData, meta interface{}) error { |
| 58 | + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.create")() |
| 59 | + defer tccommon.InconsistentCheck(d, meta)() |
| 60 | + |
| 61 | + var ( |
| 62 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 63 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 64 | + request = apmv20210622.NewCreateApmPrometheusRuleRequest() |
| 65 | + response = apmv20210622.NewCreateApmPrometheusRuleResponse() |
| 66 | + instanceId string |
| 67 | + ruleId string |
| 68 | + ) |
| 69 | + |
| 70 | + if v, ok := d.GetOk("name"); ok { |
| 71 | + request.Name = helper.String(v.(string)) |
| 72 | + } |
| 73 | + |
| 74 | + if v, ok := d.GetOk("service_name"); ok { |
| 75 | + request.ServiceName = helper.String(v.(string)) |
| 76 | + } |
| 77 | + |
| 78 | + if v, ok := d.GetOkExists("metric_match_type"); ok { |
| 79 | + request.MetricMatchType = helper.IntInt64(v.(int)) |
| 80 | + } |
| 81 | + |
| 82 | + if v, ok := d.GetOk("metric_name_rule"); ok { |
| 83 | + request.MetricNameRule = helper.String(v.(string)) |
| 84 | + } |
| 85 | + |
| 86 | + if v, ok := d.GetOk("instance_id"); ok { |
| 87 | + request.InstanceId = helper.String(v.(string)) |
| 88 | + instanceId = v.(string) |
| 89 | + } |
| 90 | + |
| 91 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 92 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmClient().CreateApmPrometheusRuleWithContext(ctx, request) |
| 93 | + if e != nil { |
| 94 | + return tccommon.RetryError(e) |
| 95 | + } else { |
| 96 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 97 | + } |
| 98 | + |
| 99 | + if result == nil || result.Response == nil { |
| 100 | + return resource.NonRetryableError(fmt.Errorf("Create apm prometheus rule failed, Response is nil.")) |
| 101 | + } |
| 102 | + |
| 103 | + response = result |
| 104 | + return nil |
| 105 | + }) |
| 106 | + |
| 107 | + if reqErr != nil { |
| 108 | + log.Printf("[CRITAL]%s create apm prometheus rule failed, reason:%+v", logId, reqErr) |
| 109 | + return reqErr |
| 110 | + } |
| 111 | + |
| 112 | + if response.Response.InanceId == nil { |
| 113 | + return fmt.Errorf("InstanceId is nil.") |
| 114 | + } |
| 115 | + |
| 116 | + instanceId = *response.Response.InstanceId |
| 117 | + |
| 118 | + d.SetId(strings.Join([]string{instanceId, ruleId}, tccommon.FILED_SP)) |
| 119 | + return resourceTencentCloudApmPrometheusRuleRead(d, meta) |
| 120 | +} |
| 121 | + |
| 122 | +func resourceTencentCloudApmPrometheusRuleRead(d *schema.ResourceData, meta interface{}) error { |
| 123 | + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.read")() |
| 124 | + defer tccommon.InconsistentCheck(d, meta)() |
| 125 | + |
| 126 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 127 | + |
| 128 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 129 | + |
| 130 | + service := ApmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 131 | + |
| 132 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 133 | + if len(idSplit) != 2 { |
| 134 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 135 | + } |
| 136 | + instanceId := idSplit[0] |
| 137 | + ruleId := idSplit[1] |
| 138 | + |
| 139 | + respData, err := service.DescribeApmPrometheusRuleById(ctx) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + if respData == nil { |
| 145 | + d.SetId("") |
| 146 | + log.Printf("[WARN]%s resource `apm_prometheus_rule` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 147 | + return nil |
| 148 | + } |
| 149 | + apmPrometheusRulesList := make([]map[string]interface{}, 0, len(respData.ApmPrometheusRules)) |
| 150 | + if respData.ApmPrometheusRules != nil { |
| 151 | + for _, apmPrometheusRules := range respData.ApmPrometheusRules { |
| 152 | + apmPrometheusRulesMap := map[string]interface{}{} |
| 153 | + |
| 154 | + if apmPrometheusRules.Id != nil { |
| 155 | + apmPrometheusRulesMap["id"] = apmPrometheusRules.Id |
| 156 | + } |
| 157 | + |
| 158 | + if apmPrometheusRules.Name != nil { |
| 159 | + apmPrometheusRulesMap["name"] = apmPrometheusRules.Name |
| 160 | + } |
| 161 | + |
| 162 | + if apmPrometheusRules.ServiceName != nil { |
| 163 | + apmPrometheusRulesMap["service_name"] = apmPrometheusRules.ServiceName |
| 164 | + } |
| 165 | + |
| 166 | + if apmPrometheusRules.Status != nil { |
| 167 | + apmPrometheusRulesMap["status"] = apmPrometheusRules.Status |
| 168 | + } |
| 169 | + |
| 170 | + if apmPrometheusRules.MetricNameRule != nil { |
| 171 | + apmPrometheusRulesMap["metric_name_rule"] = apmPrometheusRules.MetricNameRule |
| 172 | + } |
| 173 | + |
| 174 | + if apmPrometheusRules.MetricMatchType != nil { |
| 175 | + apmPrometheusRulesMap["metric_match_type"] = apmPrometheusRules.MetricMatchType |
| 176 | + } |
| 177 | + |
| 178 | + apmPrometheusRulesList = append(apmPrometheusRulesList, apmPrometheusRulesMap) |
| 179 | + } |
| 180 | + |
| 181 | + _ = d.Set("apm_prometheus_rules", apmPrometheusRulesList) |
| 182 | + } |
| 183 | + |
| 184 | + _ = instanceId |
| 185 | + _ = ruleId |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func resourceTencentCloudApmPrometheusRuleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 190 | + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.update")() |
| 191 | + defer tccommon.InconsistentCheck(d, meta)() |
| 192 | + |
| 193 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 194 | + |
| 195 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 196 | + |
| 197 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 198 | + if len(idSplit) != 2 { |
| 199 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 200 | + } |
| 201 | + instanceId := idSplit[0] |
| 202 | + ruleId := idSplit[1] |
| 203 | + |
| 204 | + needChange := false |
| 205 | + mutableArgs := []string{"id", "instance_id", "name", "status", "service_name", "metric_match_type", "metric_name_rule"} |
| 206 | + for _, v := range mutableArgs { |
| 207 | + if d.HasChange(v) { |
| 208 | + needChange = true |
| 209 | + break |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + if needChange { |
| 214 | + request := apmv20210622.NewModifyApmPrometheusRuleRequest() |
| 215 | + |
| 216 | + if v, ok := d.GetOkExists("id"); ok { |
| 217 | + request.Id = helper.IntInt64(v.(int)) |
| 218 | + } |
| 219 | + |
| 220 | + if v, ok := d.GetOk("instance_id"); ok { |
| 221 | + request.InstanceId = helper.String(v.(string)) |
| 222 | + } |
| 223 | + |
| 224 | + if v, ok := d.GetOk("name"); ok { |
| 225 | + request.Name = helper.String(v.(string)) |
| 226 | + } |
| 227 | + |
| 228 | + if v, ok := d.GetOkExists("status"); ok { |
| 229 | + request.Status = helper.IntUint64(v.(int)) |
| 230 | + } |
| 231 | + |
| 232 | + if v, ok := d.GetOk("service_name"); ok { |
| 233 | + request.ServiceName = helper.String(v.(string)) |
| 234 | + } |
| 235 | + |
| 236 | + if v, ok := d.GetOkExists("metric_match_type"); ok { |
| 237 | + request.MetricMatchType = helper.IntInt64(v.(int)) |
| 238 | + } |
| 239 | + |
| 240 | + if v, ok := d.GetOk("metric_name_rule"); ok { |
| 241 | + request.MetricNameRule = helper.String(v.(string)) |
| 242 | + } |
| 243 | + |
| 244 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 245 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmV20210622Client().ModifyApmPrometheusRuleWithContext(ctx, request) |
| 246 | + if e != nil { |
| 247 | + return tccommon.RetryError(e) |
| 248 | + } else { |
| 249 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 250 | + } |
| 251 | + return nil |
| 252 | + }) |
| 253 | + if reqErr != nil { |
| 254 | + log.Printf("[CRITAL]%s update apm prometheus rule failed, reason:%+v", logId, reqErr) |
| 255 | + return reqErr |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + _ = instanceId |
| 260 | + _ = ruleId |
| 261 | + return resourceTencentCloudApmPrometheusRuleRead(d, meta) |
| 262 | +} |
| 263 | + |
| 264 | +func resourceTencentCloudApmPrometheusRuleDelete(d *schema.ResourceData, meta interface{}) error { |
| 265 | + defer tccommon.LogElapsed("resource.tencentcloud_apm_prometheus_rule.delete")() |
| 266 | + defer tccommon.InconsistentCheck(d, meta)() |
| 267 | + |
| 268 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 269 | + ctx := tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 270 | + |
| 271 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 272 | + if len(idSplit) != 2 { |
| 273 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 274 | + } |
| 275 | + instanceId := idSplit[0] |
| 276 | + ruleId := idSplit[1] |
| 277 | + |
| 278 | + var ( |
| 279 | + request = apmv20210622.NewModifyApmPrometheusRuleRequest() |
| 280 | + response = apmv20210622.NewModifyApmPrometheusRuleResponse() |
| 281 | + ) |
| 282 | + |
| 283 | + if v, ok := d.GetOkExists("id"); ok { |
| 284 | + request.Id = helper.IntInt64(v.(int)) |
| 285 | + } |
| 286 | + |
| 287 | + if v, ok := d.GetOk("instance_id"); ok { |
| 288 | + request.InstanceId = helper.String(v.(string)) |
| 289 | + } |
| 290 | + |
| 291 | + if v, ok := d.GetOk("name"); ok { |
| 292 | + request.Name = helper.String(v.(string)) |
| 293 | + } |
| 294 | + |
| 295 | + if v, ok := d.GetOkExists("status"); ok { |
| 296 | + request.Status = helper.IntUint64(v.(int)) |
| 297 | + } |
| 298 | + |
| 299 | + if v, ok := d.GetOk("service_name"); ok { |
| 300 | + request.ServiceName = helper.String(v.(string)) |
| 301 | + } |
| 302 | + |
| 303 | + if v, ok := d.GetOkExists("metric_match_type"); ok { |
| 304 | + request.MetricMatchType = helper.IntInt64(v.(int)) |
| 305 | + } |
| 306 | + |
| 307 | + if v, ok := d.GetOk("metric_name_rule"); ok { |
| 308 | + request.MetricNameRule = helper.String(v.(string)) |
| 309 | + } |
| 310 | + |
| 311 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 312 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseApmV20210622Client().ModifyApmPrometheusRuleWithContext(ctx, request) |
| 313 | + if e != nil { |
| 314 | + return tccommon.RetryError(e) |
| 315 | + } else { |
| 316 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 317 | + } |
| 318 | + response = result |
| 319 | + return nil |
| 320 | + }) |
| 321 | + if reqErr != nil { |
| 322 | + log.Printf("[CRITAL]%s delete apm prometheus rule failed, reason:%+v", logId, reqErr) |
| 323 | + return reqErr |
| 324 | + } |
| 325 | + |
| 326 | + _ = response |
| 327 | + _ = instanceId |
| 328 | + _ = ruleId |
| 329 | + return nil |
| 330 | +} |
0 commit comments