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 929c720a35..400e208fa3 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -437,6 +437,7 @@ Monitor tencentcloud_monitor_tmp_tke_config tencentcloud_monitor_alarm_notice tencentcloud_monitor_tmp_tke_record_rule_yaml + tencentcloud_monitor_tmp_tke_global_notification PostgreSQL Data Source @@ -1102,6 +1103,7 @@ func Provider() terraform.ResourceProvider { "tencentcloud_monitor_tmp_tke_alert_policy": resourceTencentCloudMonitorTmpTkeAlertPolicy(), "tencentcloud_monitor_tmp_tke_config": resourceTencentCloudMonitorTmpTkeConfig(), "tencentcloud_monitor_tmp_tke_record_rule_yaml": resourceTencentCloudMonitorTmpTkeRecordRuleYaml(), + "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..5b59f03430 --- /dev/null +++ b/tencentcloud/resource_tc_monitor_tmp_tke_global_notification.go @@ -0,0 +1,340 @@ +/* +Provides a resource to create a tmp tke global notification + +Example Usage + +```hcl +resource "tencentcloud_monitor_tmp_tke_global_notification" "tmpGlobalNotification" { + instance_id = xxx + 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 = xxx + phone_inner_interval = xxx + phone_circle_interval = xxx + phone_arrive_notice = false + } +} + +*/ +package tencentcloud + +import ( + "context" + "fmt" + "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" +) + +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.TypeMap, + Optional: true, + 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 { + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.create")() + defer inconsistentCheck(d, meta)() + + instanceId := "" + if v, ok := d.GetOk("instance_id"); ok { + instanceId = v.(string) + } + + d.SetId(instanceId) + + return resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d, meta) +} + +func resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d *schema.ResourceData, meta interface{}) error { + 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() + + globalNotification, err := service.DescribeTkeTmpGlobalNotification(ctx, instanceId) + + if err != nil { + return err + } + + if globalNotification == nil { + d.SetId("") + return fmt.Errorf("resource `global_notification` %s does not exist", instanceId) + } + + 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) + } + + return nil +} + +func resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d *schema.ResourceData, meta interface{}) error { + 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("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 { + 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/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 + } +} +` diff --git a/tencentcloud/service_tencentcloud_tke.go b/tencentcloud/service_tencentcloud_tke.go index a5d92d34ca..543943d0b4 100644 --- a/tencentcloud/service_tencentcloud_tke.go +++ b/tencentcloud/service_tencentcloud_tke.go @@ -2029,3 +2029,61 @@ 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) + 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 +} 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..31524aaeb5 --- /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, 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. +* `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 b50e5322b9..f9e89dabaa 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -1160,6 +1160,9 @@