From ea5ba345f2e9d2c9250194a8f482c874fb6366f3 Mon Sep 17 00:00:00 2001 From: LH1029 <1099270795@qq.com> Date: Tue, 26 Jul 2022 17:59:12 +0800 Subject: [PATCH 01/19] feat: add datasource alarm-notices --- go.mod | 2 +- go.sum | 2 + .../data_source_tc_monitor_alarm_notices.go | 193 ++++++++++++++++++ ...ta_source_tc_monitor_alarm_notices_test.go | 33 +++ tencentcloud/provider.go | 2 + .../d/monitor_alarm_notices.html.markdown | 54 +++++ website/tencentcloud.erb | 3 + 7 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 tencentcloud/data_source_tc_monitor_alarm_notices.go create mode 100644 tencentcloud/data_source_tc_monitor_alarm_notices_test.go create mode 100644 website/docs/d/monitor_alarm_notices.html.markdown diff --git a/go.mod b/go.mod index 2fe0741ede..87f4d5d71c 100644 --- a/go.mod +++ b/go.mod @@ -67,5 +67,5 @@ require ( github.com/tencentyun/cos-go-sdk-v5 v0.7.33 github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1 github.com/zclconf/go-cty v1.4.2 // indirect - golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect ) diff --git a/go.sum b/go.sum index 2afbddc74c..8c206abe23 100644 --- a/go.sum +++ b/go.sum @@ -683,6 +683,8 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= diff --git a/tencentcloud/data_source_tc_monitor_alarm_notices.go b/tencentcloud/data_source_tc_monitor_alarm_notices.go new file mode 100644 index 0000000000..3ec557ee47 --- /dev/null +++ b/tencentcloud/data_source_tc_monitor_alarm_notices.go @@ -0,0 +1,193 @@ +/* +Use this data source to Interlude notification list. + +Example Usage + +```hcl +data "tencentcloud_monitor_alarm_notices" "notices" { + module = "monitor" + pagenumber = 1 + pagesize = 20 + order = "DESC" + +} +``` + +*/ +package tencentcloud + +import ( + "crypto/md5" + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit" +) + +func dataSourceTencentMonitorAlarmNotices() *schema.Resource { + return &schema.Resource{ + Read: dataSourceTencentMonitorAlarmNoticesRead, + Schema: map[string]*schema.Schema{ + "module": { + Type: schema.TypeString, + Required: true, + Description: "Module name, fill in 'monitor' here.", + }, + "pagenumber": { + Type: schema.TypeInt, + Required: true, + Description: "Page number minimum 1.", + }, + "pagesize": { + Type: schema.TypeInt, + Required: true, + Description: "Page size 1-200.", + }, + "order": { + Type: schema.TypeString, + Required: true, + Description: "Sort by update time ASC=forward order DESC=reverse order.", + }, + "result_output_file": { + Type: schema.TypeString, + Optional: true, + Description: "Used to store results.", + }, + + "notices": { + Type: schema.TypeList, + Optional: true, + Description: "Alarm notification template list.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "notices_id": { + Type: schema.TypeString, + Optional: true, + Description: "Alarm notification template ID.", + }, + "notices_name": { + Type: schema.TypeString, + Optional: true, + Description: "Alarm notification template name.", + }, + "updated_at": { + Type: schema.TypeString, + Optional: true, + Description: "Last modified time.", + }, + "updated_by": { + Type: schema.TypeString, + Optional: true, + Description: "Last Modified By.", + }, + "notice_type": { + Type: schema.TypeString, + Optional: true, + Description: "Alarm notification type ALARM=Notification not restored OK=Notification restored ALL.", + }, + "user_notices": { + Type: schema.TypeList, + Optional: true, + Description: "Alarm notification template list.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "receiver_type": { + Type: schema.TypeString, + Optional: true, + Description: "Recipient Type USER=User GROUP=User Group.", + }, + "start_time": { + Type: schema.TypeInt, + Optional: true, + Description: "The number of seconds since the notification start time 00:00:00 (value range 0-86399).", + }, + "endtime": { + Type: schema.TypeInt, + Optional: true, + Description: "The number of seconds since the notification start time 00:00:00 (value range 0-86399).", + }, + }, + }, + }, + "is_preset": { + Type: schema.TypeInt, + Optional: true, + Default: 1, + Description: "Whether it is the system default notification template 0=No 1=Yes.", + }, + "notice_language": { + Type: schema.TypeString, + Optional: true, + Description: "Notification language zh-CN=Chinese en-US=English.", + }, + }, + }, + }, + }, + } +} + +func dataSourceTencentMonitorAlarmNoticesRead(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("data_source.tencentcloud_monitor_alarm_notices.read")() + + var ( + monitorService = MonitorService{client: meta.(*TencentCloudClient).apiV3Conn} + request = monitor.NewDescribeAlarmNoticesRequest() + response *monitor.DescribeAlarmNoticesResponse + err error + notices []interface{} + ) + request.Module = helper.String(d.Get("module").(string)) + request.PageNumber = helper.IntInt64(d.Get("pagenumber").(int)) + request.PageSize = helper.IntInt64(d.Get("pagesize").(int)) + request.Order = helper.String(d.Get("order").(string)) + + if err = resource.Retry(readRetryTimeout, func() *resource.RetryError { + ratelimit.Check(request.GetAction()) + if response, err = monitorService.client.UseMonitorClient().DescribeAlarmNotices(request); err != nil { + return retryError(err, InternalError) + } + return nil + }); err != nil { + return err + } + + for _, noticesItem := range response.Response.Notices { + noticesItemMap := map[string]interface{}{ + "notices_id": noticesItem.Id, + "notices_name": noticesItem.Name, + "updated_at": noticesItem.UpdatedAt, + "updated_by": noticesItem.UpdatedBy, + "notice_type": noticesItem.NoticeType, + "is_preset": noticesItem.IsPreset, + "notice_language": noticesItem.NoticeLanguage, + } + + user_noticesItems := make([]interface{}, 0, 100) + for _, user_noticesItem := range noticesItem.UserNotices { + user_noticesItems = append(user_noticesItems, map[string]interface{}{ + "receiver_type": user_noticesItem.ReceiverType, + "start_time": user_noticesItem.StartTime, + "endtime": user_noticesItem.EndTime, + }) + } + noticesItemMap["user_notices"] = user_noticesItems + notices = append(notices, noticesItemMap) + } + + md := md5.New() + _, _ = md.Write([]byte(request.ToJsonString())) + id := fmt.Sprintf("%x", md.Sum(nil)) + d.SetId(id) + + if err = d.Set("notices", notices); err != nil { + return err + } + if output, ok := d.GetOk("result_output_file"); ok { + return writeToFile(output.(string), notices) + } + return nil +} diff --git a/tencentcloud/data_source_tc_monitor_alarm_notices_test.go b/tencentcloud/data_source_tc_monitor_alarm_notices_test.go new file mode 100644 index 0000000000..6090d63b97 --- /dev/null +++ b/tencentcloud/data_source_tc_monitor_alarm_notices_test.go @@ -0,0 +1,33 @@ +package tencentcloud + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccAlarmNoticesDatasourceBasic(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAlarmNotices(), + Check: resource.ComposeTestCheckFunc( + testAccCheckTencentCloudDataSourceID("data.tencentcloud_monitor_alarm_notices.notices"), + resource.TestCheckResourceAttr("data.tencentcloud_monitor_alarm_notices.notices", "notices.#", "1"), + ), + }, + }, + }) +} + +func testAccDataSourceAlarmNotices() string { + return `data "tencentcloud_monitor_alarm_notices" "notices" { + module = "monitor" + pagenumber = 1 + pagesize = 20 + order = "DESC" +}` +} diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 4e3a7b1eeb..fffc5ff189 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -418,6 +418,7 @@ Monitor tencentcloud_monitor_binding_objects tencentcloud_monitor_policy_groups tencentcloud_monitor_product_namespace + tencentcloud_monitor_alarm_notices Resource tencentcloud_monitor_policy_group @@ -891,6 +892,7 @@ func Provider() terraform.ResourceProvider { "tencentcloud_monitor_binding_objects": dataSourceTencentMonitorBindingObjects(), "tencentcloud_monitor_policy_groups": dataSourceTencentMonitorPolicyGroups(), "tencentcloud_monitor_product_namespace": dataSourceTencentMonitorProductNamespace(), + "tencentcloud_monitor_alarm_notices": dataSourceTencentMonitorAlarmNotices(), "tencentcloud_elasticsearch_instances": dataSourceTencentCloudElasticsearchInstances(), "tencentcloud_postgresql_instances": dataSourceTencentCloudPostgresqlInstances(), "tencentcloud_postgresql_specinfos": dataSourceTencentCloudPostgresqlSpecinfos(), diff --git a/website/docs/d/monitor_alarm_notices.html.markdown b/website/docs/d/monitor_alarm_notices.html.markdown new file mode 100644 index 0000000000..5dcab5d671 --- /dev/null +++ b/website/docs/d/monitor_alarm_notices.html.markdown @@ -0,0 +1,54 @@ +--- +subcategory: "Monitor" +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_monitor_alarm_notices" +sidebar_current: "docs-tencentcloud-datasource-monitor_alarm_notices" +description: |- + Use this data source to Interlude notification list. +--- + +# tencentcloud_monitor_alarm_notices + +Use this data source to Interlude notification list. + +## Example Usage + +```hcl +data "tencentcloud_monitor_alarm_notices" "notices" { + module = "monitor" + pagenumber = 1 + pagesize = 20 + order = "DESC" + +} +``` + +## Argument Reference + +The following arguments are supported: + +* `module` - (Required, String) Module name, fill in 'monitor' here. +* `order` - (Required, String) Sort by update time ASC=forward order DESC=reverse order. +* `pagenumber` - (Required, Int) Page number minimum 1. +* `pagesize` - (Required, Int) Page size 1-200. +* `notices` - (Optional, List) Alarm notification template list. +* `result_output_file` - (Optional, String) Used to store results. + +The `notices` object supports the following: + +* `is_preset` - (Optional, Int) Whether it is the system default notification template 0=No 1=Yes. +* `notice_language` - (Optional, String) Notification language zh-CN=Chinese en-US=English. +* `notice_type` - (Optional, String) Alarm notification type ALARM=Notification not restored OK=Notification restored ALL. +* `notices_id` - (Optional, String) Alarm notification template ID. +* `notices_name` - (Optional, String) Alarm notification template name. +* `updated_at` - (Optional, String) Last modified time. +* `updated_by` - (Optional, String) Last Modified By. +* `user_notices` - (Optional, List) Alarm notification template list. + +The `user_notices` object supports the following: + +* `endtime` - (Optional, Int) The number of seconds since the notification start time 00:00:00 (value range 0-86399). +* `receiver_type` - (Optional, String) Recipient Type USER=User GROUP=User Group. +* `start_time` - (Optional, Int) The number of seconds since the notification start time 00:00:00 (value range 0-86399). + + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 1a10275836..20ab3737de 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -1089,6 +1089,9 @@
  • Data Sources