From 13d17e23449e0b0e7bb5c3ce575bc625f9878b9b Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 18 Aug 2022 00:51:27 +0800 Subject: [PATCH 1/8] fix: add global notification --- tencentcloud/internal/helper/transform.go | 8 + tencentcloud/provider.go | 2 + ..._tc_monitor_tmp_tke_global_notification.go | 424 ++++++++++++++++++ tencentcloud/service_tencentcloud_tke.go | 58 +++ 4 files changed, 492 insertions(+) create mode 100644 tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go diff --git a/tencentcloud/internal/helper/transform.go b/tencentcloud/internal/helper/transform.go index 4175a6abd6..54a5d1a1c0 100644 --- a/tencentcloud/internal/helper/transform.go +++ b/tencentcloud/internal/helper/transform.go @@ -107,6 +107,14 @@ func InterfacesIntInt64Point(configured []interface{}) []*int64 { return vs } +func InterfacesUint64Point(configured []interface{}) []*uint64 { + vs := make([]*uint64, 0, len(configured)) + for _, v := range configured { + vs = append(vs, Uint64(v.(uint64))) + } + return vs +} + // StringsInterfaces Flatten to an array of raw strings and returns a []interface{} func StringsInterfaces(list []*string) []interface{} { vs := make([]interface{}, 0, len(list)) diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 83ef85d498..5f7fa9a846 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -432,6 +432,7 @@ Monitor tencentcloud_monitor_tmp_recording_rule tencentcloud_monitor_tmp_tke_template tencentcloud_monitor_tmp_tke_alert_policy + tencentcloud_monitor_tmp_tke_global_notification PostgreSQL Data Source @@ -1082,6 +1083,7 @@ func Provider() terraform.ResourceProvider { "tencentcloud_monitor_tmp_recording_rule": resourceTencentCloudMonitorTmpRecordingRule(), "tencentcloud_monitor_tmp_tke_template": resourceTencentCloudMonitorTmpTkeTemplate(), "tencentcloud_monitor_tmp_tke_alert_policy": resourceTencentCloudMonitorTmpTkeAlertPolicy(), + "tencentcloud_monitor_tmp_tke_global_notification": resourceTencentCloudMonitorTmpTkeGlobalNotification(), "tencentcloud_mongodb_standby_instance": resourceTencentCloudMongodbStandbyInstance(), "tencentcloud_elasticsearch_instance": resourceTencentCloudElasticsearchInstance(), "tencentcloud_postgresql_instance": resourceTencentCloudPostgresqlInstance(), diff --git a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go new file mode 100644 index 0000000000..b51e587a7f --- /dev/null +++ b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go @@ -0,0 +1,424 @@ +/* +Provides a resource to create a tmp tke global notification + +Example Usage + +```hcl +resource "tencentcloud_monitor_tmp_tke_global_notification" "tmpGlobalNotification" { + instance_id = `+ defaultPrometheusId +` + notification{ + enabled = true + type = "webhook" + web_hook = "" + alert_manager{ + url = "" + cluster_type = "" + cluster_id = "" + } + repeat_interval = "" + time_range_start = "" + time_range_end = "" + notify_way = ["SMS", "EMAIL"] + receiver_groups = [""] + phone_notify_order = [] + phone_circle_times = 5 + phone_inner_interval = 1 + phone_circle_interval = 1 + phone_arrive_notice = false + } +} + +*/ +package tencentcloud + +import ( + "context" + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" + "log" +) + +func resourceTencentCloudMonitorTmpTkeGlobalNotification() *schema.Resource { + return &schema.Resource{ + Read: resourceTencentCloudMonitorTmpTkeGlobalNotificationRead, + Create: resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate, + Update: resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate, + Delete: resourceTencentCloudMonitorTmpTkeGlobalNotificationDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Required: true, + Description: "Instance Id.", + }, + + "notification": { + Type: schema.TypeList, + MaxItems: 1, + Required: true, + Description: "Alarm notification channels.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "enabled": { + Type: schema.TypeBool, + Required: true, + Description: "Alarm notification switch.", + }, + "type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateAllowedStringValue([]string{"amp", "webhook", "alertmanager"}), + Description: "Alarm notification type, Valid values: `amp`, `webhook`, `alertmanager`.", + }, + "web_hook": { + Type: schema.TypeString, + Optional: true, + Description: "Web hook, if Type is `webhook`, this field is required.", + }, + "alert_manager": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Description: "Alert manager, if Type is `alertmanager`, this field is required.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "url": { + Type: schema.TypeString, + Required: true, + Description: "Alert manager url.", + }, + "cluster_type": { + Type: schema.TypeString, + Optional: true, + Description: "Cluster type.", + }, + "cluster_id": { + Type: schema.TypeString, + Optional: true, + Description: "Cluster id.", + }, + }, + }, + }, + "repeat_interval": { + Type: schema.TypeString, + Optional: true, + Description: "Convergence time.", + }, + "time_range_start": { + Type: schema.TypeString, + Optional: true, + Description: "Effective start time.", + }, + "time_range_end": { + Type: schema.TypeString, + Optional: true, + Description: "Effective end time.", + }, + "notify_way": { + Type: schema.TypeSet, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validateAllowedStringValue([]string{"SMS", "EMAIL", "CALL", "WECHAT"}), + }, + Optional: true, + Description: "Alarm notification method, Valid values: `SMS`, `EMAIL`, `CALL`, `WECHAT`.", + }, + "receiver_groups": { + Type: schema.TypeSet, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + Description: "Alarm receiving group(user group).", + }, + "phone_notify_order": { + Type: schema.TypeSet, + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + Optional: true, + Description: "Phone alert sequence, NotifyWay is `CALL`, and this parameter is used.", + }, + "phone_circle_times": { + Type: schema.TypeInt, + Optional: true, + Description: "Number of phone alerts (user group), NotifyWay is `CALL`, and this parameter is used.", + }, + "phone_inner_interval": { + Type: schema.TypeInt, + Optional: true, + Description: "Interval between telephone alarm rounds, NotifyWay is `CALL`, and this parameter is used.", + }, + "phone_circle_interval": { + Type: schema.TypeInt, + Optional: true, + Description: "Telephone alarm off-wheel interval, NotifyWay is `CALL`, and this parameter is used.", + }, + "phone_arrive_notice": { + Type: schema.TypeBool, + Optional: true, + Description: "Phone Alarm Reach Notification, NotifyWay is `CALL`, and this parameter is used.", + }, + }, + }, + }, + }, + } +} + +func resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG]-- create") + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.create")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + + request := tke.NewCreatePrometheusGlobalNotificationRequest() + + instanceId := "" + if v, ok := d.GetOk("instance_id"); ok { + instanceId = v.(string) + request.InstanceId = helper.String(v.(string)) + } + + notification := tke.PrometheusNotificationItem{} + if dMap, ok := helper.InterfacesHeadMap(d, "notification"); ok { + if v, ok := dMap["enabled"]; ok { + notification.Enabled = helper.Bool(v.(bool)) + } + if v, ok := dMap["type"]; ok { + notification.Type = helper.String(v.(string)) + } + if v, ok := dMap["web_hook"]; ok { + notification.WebHook = helper.String(v.(string)) + } + if v, ok := helper.InterfacesHeadMap(d, "alert_manager"); ok { + alertManager := tke.PrometheusAlertManagerConfig{} + if vv, ok := v["url"]; ok { + alertManager.Url = helper.String(vv.(string)) + } + if vv, ok := v["cluster_type"]; ok { + alertManager.ClusterType = helper.String(vv.(string)) + } + if vv, ok := v["cluster_id"]; ok { + alertManager.ClusterId = helper.String(vv.(string)) + } + notification.AlertManager = &alertManager + } + + if v, ok := dMap["repeat_interval"]; ok { + notification.RepeatInterval = helper.String(v.(string)) + } + if v, ok := dMap["time_range_start"]; ok { + notification.TimeRangeStart = helper.String(v.(string)) + } + if v, ok := dMap["time_range_end"]; ok { + notification.TimeRangeEnd = helper.String(v.(string)) + } + if v, ok := dMap["notify_way"]; ok && v != nil { + for _, vv := range v.(*schema.Set).List() { + if vv == "CALL" { + if v, ok := dMap["receiver_groups"]; ok { + notification.ReceiverGroups = helper.Strings(v.([]string)) + } + if v, ok := dMap["phone_notify_order"]; ok { + notification.PhoneNotifyOrder = helper.InterfacesUint64Point(v.([]interface{})) + } + if v, ok := dMap["phone_circle_times"]; ok { + notification.PhoneCircleTimes = helper.Int64(v.(int64)) + } + if v, ok := dMap["phone_inner_interval"]; ok { + notification.PhoneInnerInterval = helper.Int64(v.(int64)) + } + if v, ok := dMap["phone_circle_interval"]; ok { + notification.PhoneCircleInterval = helper.Int64(v.(int64)) + } + if v, ok := dMap["phone_arrive_notice"]; ok { + notification.PhoneArriveNotice = helper.Bool(v.(bool)) + } + } + notification.NotifyWay = append(notification.NotifyWay, helper.String(vv.(string))) + } + } + request.Notification = ¬ification + + } + + // When an instance is created, the alarm monitoring empty data will be created by default + ctx := context.WithValue(context.TODO(), logIdKey, logId) + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} + read, e := service.DescribeTkeTmpGlobalNotification(ctx, instanceId) + if e != nil { + return e + } + + log.Printf("[DEBUG]-- create -> Modify, %v", read) + if read != nil { + result, e := service.ModifyTkeTmpGlobalNotification(ctx, instanceId, notification) + if e != nil { + return e + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + } else { + log.Printf("[DEBUG]-- create, %v", read) + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTkeClient().CreatePrometheusGlobalNotification(request) + if e != nil { + return retryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + return nil + }) + + if err != nil { + log.Printf("[CRITAL]%s create tke global notification failed, reason:%+v", logId, err) + return err + } + } + + d.SetId(instanceId) + + return resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d, meta) +} + +func resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG]-- read") + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.read")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} + + instanceId := d.Id() + + template, err := service.DescribeTkeTmpGlobalNotification(ctx, instanceId) + + if err != nil { + return err + } + + if template == nil { + d.SetId("") + return fmt.Errorf("resource `global_notification` %s does not exist", instanceId) + } + + return nil +} + +func resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG]-- Update") + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.update")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} + + if d.HasChange("instance_id") { + return fmt.Errorf("`instance_id` do not support change now.") + } + + if d.HasChange("notification") { + notification := tke.PrometheusNotificationItem{} + if dMap, ok := helper.InterfacesHeadMap(d, "notification"); ok { + if v, ok := dMap["enabled"]; ok { + notification.Enabled = helper.Bool(v.(bool)) + } + if v, ok := dMap["type"]; ok { + notification.Type = helper.String(v.(string)) + } + if v, ok := dMap["web_hook"]; ok { + notification.WebHook = helper.String(v.(string)) + } + if v, ok := helper.InterfacesHeadMap(d, "alert_manager"); ok { + alertManager := tke.PrometheusAlertManagerConfig{} + if vv, ok := v["url"]; ok { + alertManager.Url = helper.String(vv.(string)) + } + if vv, ok := v["cluster_type"]; ok { + alertManager.ClusterType = helper.String(vv.(string)) + } + if vv, ok := v["cluster_id"]; ok { + alertManager.ClusterId = helper.String(vv.(string)) + } + notification.AlertManager = &alertManager + } + + if v, ok := dMap["repeat_interval"]; ok { + notification.RepeatInterval = helper.String(v.(string)) + } + if v, ok := dMap["time_range_start"]; ok { + notification.TimeRangeStart = helper.String(v.(string)) + } + if v, ok := dMap["time_range_end"]; ok { + notification.TimeRangeEnd = helper.String(v.(string)) + } + if v, ok := dMap["notify_way"]; ok { + for _, vv := range v.(*schema.Set).List() { + if vv == "CALL" { + if v, ok := dMap["receiver_groups"]; ok { + notification.ReceiverGroups = helper.Strings(v.([]string)) + } + if v, ok := dMap["phone_notify_order"]; ok { + notification.PhoneNotifyOrder = helper.InterfacesUint64Point(v.([]interface{})) + } + if v, ok := dMap["phone_circle_times"]; ok { + notification.PhoneCircleTimes = helper.Int64(v.(int64)) + } + if v, ok := dMap["phone_inner_interval"]; ok { + notification.PhoneInnerInterval = helper.Int64(v.(int64)) + } + if v, ok := dMap["phone_circle_interval"]; ok { + notification.PhoneCircleInterval = helper.Int64(v.(int64)) + } + if v, ok := dMap["phone_arrive_notice"]; ok { + notification.PhoneArriveNotice = helper.Bool(v.(bool)) + } + } + notification.NotifyWay = append(notification.NotifyWay, helper.String(vv.(string))) + } + } + } + + if _, err := service.ModifyTkeTmpGlobalNotification(ctx, d.Id(), notification); err != nil { + return err + } + } + + return resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d, meta) +} + +func resourceTencentCloudMonitorTmpTkeGlobalNotificationDelete(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG]-- Delete") + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.delete")() + defer inconsistentCheck(d, meta)() + + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} + + id := d.Id() + notification := tke.PrometheusNotificationItem{ + // Turning off the alarm notification function is to delete the alarm notification + Enabled: helper.Bool(false), + Type: helper.String(""), + } + + if _, err := service.ModifyTkeTmpGlobalNotification(ctx, id, notification); err != nil { + return err + } + + return nil +} diff --git a/tencentcloud/service_tencentcloud_tke.go b/tencentcloud/service_tencentcloud_tke.go index effb6dea65..e472458cb0 100644 --- a/tencentcloud/service_tencentcloud_tke.go +++ b/tencentcloud/service_tencentcloud_tke.go @@ -1838,3 +1838,61 @@ func (me *TkeService) DeleteTkeTmpAlertPolicyById(ctx context.Context, tmpAlertP return } + +func (me *TkeService) DescribeTkeTmpGlobalNotification(ctx context.Context, instanceId string) (tmpNotification *tke.PrometheusNotificationItem, errRet error) { + var ( + logId = getLogId(ctx) + request = tke.NewDescribePrometheusGlobalNotificationRequest() + ) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", + logId, "query object", request.ToJsonString(), errRet.Error()) + } + }() + request.InstanceId = &instanceId + + response, err := me.client.UseTkeClient().DescribePrometheusGlobalNotification(request) + if err != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", + logId, request.GetAction(), request.ToJsonString(), err.Error()) + errRet = err + return + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", + logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if response.Response.Notification != nil && response.Response.RequestId != nil { + tmpNotification = response.Response.Notification + return + } + + return +} + +func (me *TkeService) ModifyTkeTmpGlobalNotification(ctx context.Context, instanceId string, notification tke.PrometheusNotificationItem) (response *tke.ModifyPrometheusGlobalNotificationResponse, errRet error) { + logId := getLogId(ctx) + + request := tke.NewModifyPrometheusGlobalNotificationRequest() + request.InstanceId = &instanceId + request.Notification = ¬ification + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", + logId, "delete object", request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + response, err := me.client.UseTkeClient().ModifyPrometheusGlobalNotification(request) + if err != nil { + errRet = err + return nil, err + } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", + logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + return +} From 8711ee178da9a05678f38ffa1e3a80a02e9064b6 Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 18 Aug 2022 09:25:25 +0800 Subject: [PATCH 2/8] feat: add instructions --- ..._tc_monitor_tmp_tke_global_notification.go | 2 +- ..._tmp_tke_global_notification.html.markdown | 55 +++++++++++++++++++ website/tencentcloud.erb | 3 + 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 website/docs/r/monitor_tmp_tke_global_notification.html.markdown diff --git a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go index b51e587a7f..833256e08c 100644 --- a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go +++ b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go @@ -135,7 +135,7 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotification() *schema.Resource { Type: schema.TypeString, }, Optional: true, - Description: "Alarm receiving group(user group).", + Description: "Alarm receiving group(user group).", }, "phone_notify_order": { Type: schema.TypeSet, diff --git a/website/docs/r/monitor_tmp_tke_global_notification.html.markdown b/website/docs/r/monitor_tmp_tke_global_notification.html.markdown new file mode 100644 index 0000000000..f1959a3662 --- /dev/null +++ b/website/docs/r/monitor_tmp_tke_global_notification.html.markdown @@ -0,0 +1,55 @@ +--- +subcategory: "Monitor" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_monitor_tmp_tke_global_notification" +sidebar_current: "docs-tencentcloud-resource-monitor_tmp_tke_global_notification" +description: |- + Provides a resource to create a tmp tke global notification +--- + +# tencentcloud_monitor_tmp_tke_global_notification + +Provides a resource to create a tmp tke global notification + +## Example Usage + + + +## Argument Reference + +The following arguments are supported: + +* `instance_id` - (Required, String) Instance Id. +* `notification` - (Required, List) Alarm notification channels. + +The `alert_manager` object supports the following: + +* `url` - (Required, String) Alert manager url. +* `cluster_id` - (Optional, String) Cluster id. +* `cluster_type` - (Optional, String) Cluster type. + +The `notification` object supports the following: + +* `enabled` - (Required, Bool) Alarm notification switch. +* `type` - (Required, String) Alarm notification type, Valid values: `amp`, `webhook`, `alertmanager`. +* `alert_manager` - (Optional, List) Alert manager, if Type is `alertmanager`, this field is required. +* `notify_way` - (Optional, Set) Alarm notification method, Valid values: `SMS`, `EMAIL`, `CALL`, `WECHAT`. +* `phone_arrive_notice` - (Optional, Bool) Phone Alarm Reach Notification, NotifyWay is `CALL`, and this parameter is used. +* `phone_circle_interval` - (Optional, Int) Telephone alarm off-wheel interval, NotifyWay is `CALL`, and this parameter is used. +* `phone_circle_times` - (Optional, Int) Number of phone alerts (user group), NotifyWay is `CALL`, and this parameter is used. +* `phone_inner_interval` - (Optional, Int) Interval between telephone alarm rounds, NotifyWay is `CALL`, and this parameter is used. +* `phone_notify_order` - (Optional, Set) Phone alert sequence, NotifyWay is `CALL`, and this parameter is used. +* `receiver_groups` - (Optional, Set) Alarm receiving group(user group). +* `repeat_interval` - (Optional, String) Convergence time. +* `time_range_end` - (Optional, String) Effective end time. +* `time_range_start` - (Optional, String) Effective start time. +* `web_hook` - (Optional, String) Web hook, if Type is `webhook`, this field is required. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - ID of the resource. + + + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 74d644e480..d45df81a0b 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -1145,6 +1145,9 @@
  • tencentcloud_monitor_tmp_tke_alert_policy
  • +
  • + tencentcloud_monitor_tmp_tke_global_notification +
  • tencentcloud_monitor_tmp_tke_template
  • From 38d26cc3087067631832959e421ea1829deaca51 Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 18 Aug 2022 16:29:51 +0800 Subject: [PATCH 3/8] feat: Import support --- ..._tc_monitor_tmp_tke_global_notification.go | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go index 833256e08c..9feb199c54 100644 --- a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go +++ b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go @@ -81,9 +81,8 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotification() *schema.Resource { Description: "Web hook, if Type is `webhook`, this field is required.", }, "alert_manager": { - Type: schema.TypeList, + Type: schema.TypeMap, Optional: true, - MaxItems: 1, Description: "Alert manager, if Type is `alertmanager`, this field is required.", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -303,17 +302,46 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d *schema.ResourceD instanceId := d.Id() - template, err := service.DescribeTkeTmpGlobalNotification(ctx, instanceId) + globalNotification, err := service.DescribeTkeTmpGlobalNotification(ctx, instanceId) if err != nil { return err } - if template == nil { + if globalNotification == nil { d.SetId("") return fmt.Errorf("resource `global_notification` %s does not exist", instanceId) } + _ = d.Set("instance_id", instanceId) + alertManager := make(map[string]interface{}) + if globalNotification.AlertManager != nil { + alertManager = map[string]interface{}{ + "url": globalNotification.AlertManager.Url, + "cluster_type": globalNotification.AlertManager.ClusterType, + "cluster_id": globalNotification.AlertManager.ClusterId, + } + } + + var notifications []map[string]interface{} + notification := make(map[string]interface{}) + notification["enabled"] = globalNotification.Enabled + notification["type"] = globalNotification.Type + notification["web_hook"] = globalNotification.WebHook + notification["alert_manager"] = alertManager + notification["repeat_interval"] = globalNotification.RepeatInterval + notification["time_range_start"] = globalNotification.TimeRangeStart + notification["time_range_end"] = globalNotification.TimeRangeEnd + notification["notify_way"] = globalNotification.NotifyWay + notification["receiver_groups"] = globalNotification.ReceiverGroups + notification["phone_notify_order"] = globalNotification.PhoneNotifyOrder + notification["phone_circle_times"] = globalNotification.PhoneCircleTimes + notification["phone_inner_interval"] = globalNotification.PhoneInnerInterval + notification["phone_circle_interval"] = globalNotification.PhoneCircleInterval + notification["phone_arrive_notice"] = globalNotification.PhoneArriveNotice + notifications = append(notifications, notification) + _ = d.Set("notification", notifications) + return nil } From f64df707cee5825967badec44d11fadad778af47 Mon Sep 17 00:00:00 2001 From: arunma Date: Thu, 18 Aug 2022 16:32:47 +0800 Subject: [PATCH 4/8] fix: Modify instruction --- .../docs/r/monitor_tmp_tke_global_notification.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/monitor_tmp_tke_global_notification.html.markdown b/website/docs/r/monitor_tmp_tke_global_notification.html.markdown index f1959a3662..31524aaeb5 100644 --- a/website/docs/r/monitor_tmp_tke_global_notification.html.markdown +++ b/website/docs/r/monitor_tmp_tke_global_notification.html.markdown @@ -32,7 +32,7 @@ The `notification` object supports the following: * `enabled` - (Required, Bool) Alarm notification switch. * `type` - (Required, String) Alarm notification type, Valid values: `amp`, `webhook`, `alertmanager`. -* `alert_manager` - (Optional, List) Alert manager, if Type is `alertmanager`, this field is required. +* `alert_manager` - (Optional, Map) Alert manager, if Type is `alertmanager`, this field is required. * `notify_way` - (Optional, Set) Alarm notification method, Valid values: `SMS`, `EMAIL`, `CALL`, `WECHAT`. * `phone_arrive_notice` - (Optional, Bool) Phone Alarm Reach Notification, NotifyWay is `CALL`, and this parameter is used. * `phone_circle_interval` - (Optional, Int) Telephone alarm off-wheel interval, NotifyWay is `CALL`, and this parameter is used. From f41ae60b6410473f766f100e0d6af297bfdb5cbd Mon Sep 17 00:00:00 2001 From: arunma Date: Fri, 19 Aug 2022 10:42:01 +0800 Subject: [PATCH 5/8] fix: Modify create resource logic --- ..._tc_monitor_tmp_tke_global_notification.go | 103 +++++++----------- 1 file changed, 38 insertions(+), 65 deletions(-) diff --git a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go index 9feb199c54..9e36335bfb 100644 --- a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go +++ b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go @@ -5,12 +5,12 @@ Example Usage ```hcl resource "tencentcloud_monitor_tmp_tke_global_notification" "tmpGlobalNotification" { - instance_id = `+ defaultPrometheusId +` + instance_id = xxx notification{ enabled = true type = "webhook" web_hook = "" - alert_manager{ + alert_manager = { url = "" cluster_type = "" cluster_id = "" @@ -21,9 +21,9 @@ resource "tencentcloud_monitor_tmp_tke_global_notification" "tmpGlobalNotificati notify_way = ["SMS", "EMAIL"] receiver_groups = [""] phone_notify_order = [] - phone_circle_times = 5 - phone_inner_interval = 1 - phone_circle_interval = 1 + phone_circle_times = xxx + phone_inner_interval = xxx + phone_circle_interval = xxx phone_arrive_notice = false } } @@ -34,11 +34,11 @@ package tencentcloud import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "log" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" - "log" ) func resourceTencentCloudMonitorTmpTkeGlobalNotification() *schema.Resource { @@ -172,7 +172,6 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotification() *schema.Resource { } func resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate(d *schema.ResourceData, meta interface{}) error { - log.Printf("[DEBUG]-- create") defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.create")() defer inconsistentCheck(d, meta)() @@ -252,37 +251,12 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate(d *schema.Resourc // When an instance is created, the alarm monitoring empty data will be created by default ctx := context.WithValue(context.TODO(), logIdKey, logId) service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} - read, e := service.DescribeTkeTmpGlobalNotification(ctx, instanceId) + result, e := service.ModifyTkeTmpGlobalNotification(ctx, instanceId, notification) if e != nil { return e - } - - log.Printf("[DEBUG]-- create -> Modify, %v", read) - if read != nil { - result, e := service.ModifyTkeTmpGlobalNotification(ctx, instanceId, notification) - if e != nil { - return e - } else { - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", - logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) - } } else { - log.Printf("[DEBUG]-- create, %v", read) - err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { - result, e := meta.(*TencentCloudClient).apiV3Conn.UseTkeClient().CreatePrometheusGlobalNotification(request) - if e != nil { - return retryError(e) - } else { - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", - logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) - } - return nil - }) - - if err != nil { - log.Printf("[CRITAL]%s create tke global notification failed, reason:%+v", logId, err) - return err - } + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) } d.SetId(instanceId) @@ -291,7 +265,6 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate(d *schema.Resourc } func resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d *schema.ResourceData, meta interface{}) error { - log.Printf("[DEBUG]-- read") defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.read")() defer inconsistentCheck(d, meta)() @@ -313,40 +286,41 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d *schema.ResourceD return fmt.Errorf("resource `global_notification` %s does not exist", instanceId) } - _ = d.Set("instance_id", instanceId) - alertManager := make(map[string]interface{}) - if globalNotification.AlertManager != nil { - alertManager = map[string]interface{}{ - "url": globalNotification.AlertManager.Url, - "cluster_type": globalNotification.AlertManager.ClusterType, - "cluster_id": globalNotification.AlertManager.ClusterId, + if *globalNotification.Enabled { + _ = d.Set("instance_id", instanceId) + alertManager := make(map[string]interface{}) + if globalNotification.AlertManager != nil { + alertManager = map[string]interface{}{ + "url": globalNotification.AlertManager.Url, + "cluster_type": globalNotification.AlertManager.ClusterType, + "cluster_id": globalNotification.AlertManager.ClusterId, + } } - } - var notifications []map[string]interface{} - notification := make(map[string]interface{}) - notification["enabled"] = globalNotification.Enabled - notification["type"] = globalNotification.Type - notification["web_hook"] = globalNotification.WebHook - notification["alert_manager"] = alertManager - notification["repeat_interval"] = globalNotification.RepeatInterval - notification["time_range_start"] = globalNotification.TimeRangeStart - notification["time_range_end"] = globalNotification.TimeRangeEnd - notification["notify_way"] = globalNotification.NotifyWay - notification["receiver_groups"] = globalNotification.ReceiverGroups - notification["phone_notify_order"] = globalNotification.PhoneNotifyOrder - notification["phone_circle_times"] = globalNotification.PhoneCircleTimes - notification["phone_inner_interval"] = globalNotification.PhoneInnerInterval - notification["phone_circle_interval"] = globalNotification.PhoneCircleInterval - notification["phone_arrive_notice"] = globalNotification.PhoneArriveNotice - notifications = append(notifications, notification) - _ = d.Set("notification", notifications) + var notifications []map[string]interface{} + notification := make(map[string]interface{}) + notification["enabled"] = globalNotification.Enabled + notification["type"] = globalNotification.Type + notification["web_hook"] = globalNotification.WebHook + notification["alert_manager"] = alertManager + notification["repeat_interval"] = globalNotification.RepeatInterval + notification["time_range_start"] = globalNotification.TimeRangeStart + notification["time_range_end"] = globalNotification.TimeRangeEnd + notification["notify_way"] = globalNotification.NotifyWay + notification["receiver_groups"] = globalNotification.ReceiverGroups + notification["phone_notify_order"] = globalNotification.PhoneNotifyOrder + notification["phone_circle_times"] = globalNotification.PhoneCircleTimes + notification["phone_inner_interval"] = globalNotification.PhoneInnerInterval + notification["phone_circle_interval"] = globalNotification.PhoneCircleInterval + notification["phone_arrive_notice"] = globalNotification.PhoneArriveNotice + notifications = append(notifications, notification) + _ = d.Set("notification", notifications) + } return nil } func resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d *schema.ResourceData, meta interface{}) error { - log.Printf("[DEBUG]-- Update") defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.update")() defer inconsistentCheck(d, meta)() @@ -429,7 +403,6 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d *schema.Resourc } func resourceTencentCloudMonitorTmpTkeGlobalNotificationDelete(d *schema.ResourceData, meta interface{}) error { - log.Printf("[DEBUG]-- Delete") defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.delete")() defer inconsistentCheck(d, meta)() From 012d598277f307dddc9814660175fd854f4acd85 Mon Sep 17 00:00:00 2001 From: arunma Date: Fri, 19 Aug 2022 16:27:23 +0800 Subject: [PATCH 6/8] fix: modify resource create --- ..._tc_monitor_tmp_tke_global_notification.go | 87 +------------------ 1 file changed, 1 insertion(+), 86 deletions(-) diff --git a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go index 9e36335bfb..5b59f03430 100644 --- a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go +++ b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go @@ -34,8 +34,6 @@ package tencentcloud import ( "context" "fmt" - "log" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" @@ -175,93 +173,14 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate(d *schema.Resourc defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.create")() defer inconsistentCheck(d, meta)() - logId := getLogId(contextNil) - - request := tke.NewCreatePrometheusGlobalNotificationRequest() - instanceId := "" if v, ok := d.GetOk("instance_id"); ok { instanceId = v.(string) - request.InstanceId = helper.String(v.(string)) - } - - notification := tke.PrometheusNotificationItem{} - if dMap, ok := helper.InterfacesHeadMap(d, "notification"); ok { - if v, ok := dMap["enabled"]; ok { - notification.Enabled = helper.Bool(v.(bool)) - } - if v, ok := dMap["type"]; ok { - notification.Type = helper.String(v.(string)) - } - if v, ok := dMap["web_hook"]; ok { - notification.WebHook = helper.String(v.(string)) - } - if v, ok := helper.InterfacesHeadMap(d, "alert_manager"); ok { - alertManager := tke.PrometheusAlertManagerConfig{} - if vv, ok := v["url"]; ok { - alertManager.Url = helper.String(vv.(string)) - } - if vv, ok := v["cluster_type"]; ok { - alertManager.ClusterType = helper.String(vv.(string)) - } - if vv, ok := v["cluster_id"]; ok { - alertManager.ClusterId = helper.String(vv.(string)) - } - notification.AlertManager = &alertManager - } - - if v, ok := dMap["repeat_interval"]; ok { - notification.RepeatInterval = helper.String(v.(string)) - } - if v, ok := dMap["time_range_start"]; ok { - notification.TimeRangeStart = helper.String(v.(string)) - } - if v, ok := dMap["time_range_end"]; ok { - notification.TimeRangeEnd = helper.String(v.(string)) - } - if v, ok := dMap["notify_way"]; ok && v != nil { - for _, vv := range v.(*schema.Set).List() { - if vv == "CALL" { - if v, ok := dMap["receiver_groups"]; ok { - notification.ReceiverGroups = helper.Strings(v.([]string)) - } - if v, ok := dMap["phone_notify_order"]; ok { - notification.PhoneNotifyOrder = helper.InterfacesUint64Point(v.([]interface{})) - } - if v, ok := dMap["phone_circle_times"]; ok { - notification.PhoneCircleTimes = helper.Int64(v.(int64)) - } - if v, ok := dMap["phone_inner_interval"]; ok { - notification.PhoneInnerInterval = helper.Int64(v.(int64)) - } - if v, ok := dMap["phone_circle_interval"]; ok { - notification.PhoneCircleInterval = helper.Int64(v.(int64)) - } - if v, ok := dMap["phone_arrive_notice"]; ok { - notification.PhoneArriveNotice = helper.Bool(v.(bool)) - } - } - notification.NotifyWay = append(notification.NotifyWay, helper.String(vv.(string))) - } - } - request.Notification = ¬ification - - } - - // When an instance is created, the alarm monitoring empty data will be created by default - ctx := context.WithValue(context.TODO(), logIdKey, logId) - service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} - result, e := service.ModifyTkeTmpGlobalNotification(ctx, instanceId, notification) - if e != nil { - return e - } else { - log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", - logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) } d.SetId(instanceId) - return resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d, meta) + return resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d, meta) } func resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d *schema.ResourceData, meta interface{}) error { @@ -328,10 +247,6 @@ func resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d *schema.Resourc ctx := context.WithValue(context.TODO(), logIdKey, logId) service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} - if d.HasChange("instance_id") { - return fmt.Errorf("`instance_id` do not support change now.") - } - if d.HasChange("notification") { notification := tke.PrometheusNotificationItem{} if dMap, ok := helper.InterfacesHeadMap(d, "notification"); ok { From 22e59f0a2bd9fe0ac9d16d8b5edf8d3c49f6d219 Mon Sep 17 00:00:00 2001 From: arunma Date: Fri, 19 Aug 2022 17:18:42 +0800 Subject: [PATCH 7/8] feat: add ut --- ...onitor_tmp_tke_global_notification_test.go | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tencentcloud/resource_tc_monitor_tmp_tke_global_notification_test.go diff --git a/tencentcloud/resource_tc_monitor_tmp_tke_global_notification_test.go b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification_test.go new file mode 100644 index 0000000000..76da88f52b --- /dev/null +++ b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification_test.go @@ -0,0 +1,115 @@ +package tencentcloud + +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +// go test -i; go test -test.run TestAccTencentCloudMonitorTmpTkeGlobalNotification_basic -v +func TestAccTencentCloudMonitorTmpTkeGlobalNotification_basic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_COMMON) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckTmpTkeGlobalNotificationDestroy, + Steps: []resource.TestStep{ + { + Config: testTmpTkeGlobalNotification_basic, + Check: resource.ComposeTestCheckFunc( + testAccCheckTmpTkeGlobalNotificationExists("tencentcloud_monitor_tmp_tke_global_notification.basic"), + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_global_notification.basic", "notification.0.enabled", "true"), + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_global_notification.basic", "notification.0.type", "webhook"), + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_global_notification.basic", "notification.0.notify_way.#", "2"), + resource.TestCheckResourceAttr("tencentcloud_monitor_tmp_tke_global_notification.basic", "notification.0.phone_arrive_notice", "false"), + ), + }, + { + ResourceName: "tencentcloud_monitor_tmp_tke_global_notification.basic", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckTmpTkeGlobalNotificationDestroy(s *terraform.State) error { + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + service := TkeService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn} + for _, rs := range s.RootModule().Resources { + if rs.Type != "tencentcloud_monitor_tmp_tke_global_notification" { + continue + } + + tmpGlobalNotification, err := service.DescribeTkeTmpGlobalNotification(ctx, rs.Primary.ID) + if *tmpGlobalNotification.Enabled { + return fmt.Errorf("global notification %s still exists", rs.Primary.ID) + } + if err != nil { + return err + } + } + return nil +} + +func testAccCheckTmpTkeGlobalNotificationExists(r string) resource.TestCheckFunc { + return func(s *terraform.State) error { + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + rs, ok := s.RootModule().Resources[r] + if !ok { + return fmt.Errorf("resource %s is not found", r) + } + if rs.Primary.ID == "" { + return fmt.Errorf("instance id is not set") + } + + tkeService := TkeService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn} + tmpGlobalNotification, err := tkeService.DescribeTkeTmpGlobalNotification(ctx, rs.Primary.ID) + if !*tmpGlobalNotification.Enabled { + return fmt.Errorf("global notification %s is not found", rs.Primary.ID) + } + if err != nil { + return err + } + + return nil + } +} + +const testTmpTkeGlobalNotificationVar = ` +variable "prometheus_id" { +default = "` + defaultPrometheusId + `" +} +` + +const testTmpTkeGlobalNotification_basic = testTmpTkeGlobalNotificationVar + ` +resource "tencentcloud_monitor_tmp_tke_global_notification" "basic" { + instance_id = var.prometheus_id + notification { + enabled = true + type = "webhook" + alert_manager = { + "cluster_id" = "" + "cluster_type" = "" + "url" = "" + } + web_hook = "" + repeat_interval = "5m" + time_range_start = "00:00:00" + time_range_end = "23:59:59" + notify_way = ["SMS", "EMAIL"] + receiver_groups = [] + phone_notify_order = [] + phone_circle_times = 0 + phone_inner_interval = 0 + phone_circle_interval = 0 + phone_arrive_notice = false + } +} +` From 669d07382d2a3a308153f49127b692cf4a93c043 Mon Sep 17 00:00:00 2001 From: arunma Date: Fri, 19 Aug 2022 17:40:46 +0800 Subject: [PATCH 8/8] fix: make fmt --- tencentcloud/service_tencentcloud_tke.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tencentcloud/service_tencentcloud_tke.go b/tencentcloud/service_tencentcloud_tke.go index 85b75f9643..543943d0b4 100644 --- a/tencentcloud/service_tencentcloud_tke.go +++ b/tencentcloud/service_tencentcloud_tke.go @@ -2030,7 +2030,6 @@ func (me *TkeService) DescribePrometheusRecordRuleByName(ctx context.Context, id return response, nil } - func (me *TkeService) DescribeTkeTmpGlobalNotification(ctx context.Context, instanceId string) (tmpNotification *tke.PrometheusNotificationItem, errRet error) { var ( logId = getLogId(ctx)