Skip to content

Commit 9b61710

Browse files
authored
fix: add global notification (#1222)
* fix: add global notification * feat: add instructions * feat: Import support * fix: Modify instruction * fix: Modify create resource logic * fix: modify resource create * feat: add ut * fix: make fmt Co-authored-by: arunma <arunma@tencent.com>
1 parent 54ff0c6 commit 9b61710

File tree

7 files changed

+581
-0
lines changed

7 files changed

+581
-0
lines changed

tencentcloud/internal/helper/transform.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ func InterfacesIntInt64Point(configured []interface{}) []*int64 {
107107
return vs
108108
}
109109

110+
func InterfacesUint64Point(configured []interface{}) []*uint64 {
111+
vs := make([]*uint64, 0, len(configured))
112+
for _, v := range configured {
113+
vs = append(vs, Uint64(v.(uint64)))
114+
}
115+
return vs
116+
}
117+
110118
// StringsInterfaces Flatten to an array of raw strings and returns a []interface{}
111119
func StringsInterfaces(list []*string) []interface{} {
112120
vs := make([]interface{}, 0, len(list))

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ Monitor
437437
tencentcloud_monitor_tmp_tke_config
438438
tencentcloud_monitor_alarm_notice
439439
tencentcloud_monitor_tmp_tke_record_rule_yaml
440+
tencentcloud_monitor_tmp_tke_global_notification
440441
441442
PostgreSQL
442443
Data Source
@@ -1102,6 +1103,7 @@ func Provider() terraform.ResourceProvider {
11021103
"tencentcloud_monitor_tmp_tke_alert_policy": resourceTencentCloudMonitorTmpTkeAlertPolicy(),
11031104
"tencentcloud_monitor_tmp_tke_config": resourceTencentCloudMonitorTmpTkeConfig(),
11041105
"tencentcloud_monitor_tmp_tke_record_rule_yaml": resourceTencentCloudMonitorTmpTkeRecordRuleYaml(),
1106+
"tencentcloud_monitor_tmp_tke_global_notification": resourceTencentCloudMonitorTmpTkeGlobalNotification(),
11051107
"tencentcloud_mongodb_standby_instance": resourceTencentCloudMongodbStandbyInstance(),
11061108
"tencentcloud_elasticsearch_instance": resourceTencentCloudElasticsearchInstance(),
11071109
"tencentcloud_postgresql_instance": resourceTencentCloudPostgresqlInstance(),
Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
/*
2+
Provides a resource to create a tmp tke global notification
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_monitor_tmp_tke_global_notification" "tmpGlobalNotification" {
8+
instance_id = xxx
9+
notification{
10+
enabled = true
11+
type = "webhook"
12+
web_hook = ""
13+
alert_manager = {
14+
url = ""
15+
cluster_type = ""
16+
cluster_id = ""
17+
}
18+
repeat_interval = ""
19+
time_range_start = ""
20+
time_range_end = ""
21+
notify_way = ["SMS", "EMAIL"]
22+
receiver_groups = [""]
23+
phone_notify_order = []
24+
phone_circle_times = xxx
25+
phone_inner_interval = xxx
26+
phone_circle_interval = xxx
27+
phone_arrive_notice = false
28+
}
29+
}
30+
31+
*/
32+
package tencentcloud
33+
34+
import (
35+
"context"
36+
"fmt"
37+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
38+
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
39+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
40+
)
41+
42+
func resourceTencentCloudMonitorTmpTkeGlobalNotification() *schema.Resource {
43+
return &schema.Resource{
44+
Read: resourceTencentCloudMonitorTmpTkeGlobalNotificationRead,
45+
Create: resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate,
46+
Update: resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate,
47+
Delete: resourceTencentCloudMonitorTmpTkeGlobalNotificationDelete,
48+
Importer: &schema.ResourceImporter{
49+
State: schema.ImportStatePassthrough,
50+
},
51+
Schema: map[string]*schema.Schema{
52+
"instance_id": {
53+
Type: schema.TypeString,
54+
Required: true,
55+
Description: "Instance Id.",
56+
},
57+
58+
"notification": {
59+
Type: schema.TypeList,
60+
MaxItems: 1,
61+
Required: true,
62+
Description: "Alarm notification channels.",
63+
Elem: &schema.Resource{
64+
Schema: map[string]*schema.Schema{
65+
"enabled": {
66+
Type: schema.TypeBool,
67+
Required: true,
68+
Description: "Alarm notification switch.",
69+
},
70+
"type": {
71+
Type: schema.TypeString,
72+
Required: true,
73+
ValidateFunc: validateAllowedStringValue([]string{"amp", "webhook", "alertmanager"}),
74+
Description: "Alarm notification type, Valid values: `amp`, `webhook`, `alertmanager`.",
75+
},
76+
"web_hook": {
77+
Type: schema.TypeString,
78+
Optional: true,
79+
Description: "Web hook, if Type is `webhook`, this field is required.",
80+
},
81+
"alert_manager": {
82+
Type: schema.TypeMap,
83+
Optional: true,
84+
Description: "Alert manager, if Type is `alertmanager`, this field is required.",
85+
Elem: &schema.Resource{
86+
Schema: map[string]*schema.Schema{
87+
"url": {
88+
Type: schema.TypeString,
89+
Required: true,
90+
Description: "Alert manager url.",
91+
},
92+
"cluster_type": {
93+
Type: schema.TypeString,
94+
Optional: true,
95+
Description: "Cluster type.",
96+
},
97+
"cluster_id": {
98+
Type: schema.TypeString,
99+
Optional: true,
100+
Description: "Cluster id.",
101+
},
102+
},
103+
},
104+
},
105+
"repeat_interval": {
106+
Type: schema.TypeString,
107+
Optional: true,
108+
Description: "Convergence time.",
109+
},
110+
"time_range_start": {
111+
Type: schema.TypeString,
112+
Optional: true,
113+
Description: "Effective start time.",
114+
},
115+
"time_range_end": {
116+
Type: schema.TypeString,
117+
Optional: true,
118+
Description: "Effective end time.",
119+
},
120+
"notify_way": {
121+
Type: schema.TypeSet,
122+
Elem: &schema.Schema{
123+
Type: schema.TypeString,
124+
ValidateFunc: validateAllowedStringValue([]string{"SMS", "EMAIL", "CALL", "WECHAT"}),
125+
},
126+
Optional: true,
127+
Description: "Alarm notification method, Valid values: `SMS`, `EMAIL`, `CALL`, `WECHAT`.",
128+
},
129+
"receiver_groups": {
130+
Type: schema.TypeSet,
131+
Elem: &schema.Schema{
132+
Type: schema.TypeString,
133+
},
134+
Optional: true,
135+
Description: "Alarm receiving group(user group).",
136+
},
137+
"phone_notify_order": {
138+
Type: schema.TypeSet,
139+
Elem: &schema.Schema{
140+
Type: schema.TypeInt,
141+
},
142+
Optional: true,
143+
Description: "Phone alert sequence, NotifyWay is `CALL`, and this parameter is used.",
144+
},
145+
"phone_circle_times": {
146+
Type: schema.TypeInt,
147+
Optional: true,
148+
Description: "Number of phone alerts (user group), NotifyWay is `CALL`, and this parameter is used.",
149+
},
150+
"phone_inner_interval": {
151+
Type: schema.TypeInt,
152+
Optional: true,
153+
Description: "Interval between telephone alarm rounds, NotifyWay is `CALL`, and this parameter is used.",
154+
},
155+
"phone_circle_interval": {
156+
Type: schema.TypeInt,
157+
Optional: true,
158+
Description: "Telephone alarm off-wheel interval, NotifyWay is `CALL`, and this parameter is used.",
159+
},
160+
"phone_arrive_notice": {
161+
Type: schema.TypeBool,
162+
Optional: true,
163+
Description: "Phone Alarm Reach Notification, NotifyWay is `CALL`, and this parameter is used.",
164+
},
165+
},
166+
},
167+
},
168+
},
169+
}
170+
}
171+
172+
func resourceTencentCloudMonitorTmpTkeGlobalNotificationCreate(d *schema.ResourceData, meta interface{}) error {
173+
defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.create")()
174+
defer inconsistentCheck(d, meta)()
175+
176+
instanceId := ""
177+
if v, ok := d.GetOk("instance_id"); ok {
178+
instanceId = v.(string)
179+
}
180+
181+
d.SetId(instanceId)
182+
183+
return resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d, meta)
184+
}
185+
186+
func resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d *schema.ResourceData, meta interface{}) error {
187+
defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.read")()
188+
defer inconsistentCheck(d, meta)()
189+
190+
logId := getLogId(contextNil)
191+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
192+
193+
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
194+
195+
instanceId := d.Id()
196+
197+
globalNotification, err := service.DescribeTkeTmpGlobalNotification(ctx, instanceId)
198+
199+
if err != nil {
200+
return err
201+
}
202+
203+
if globalNotification == nil {
204+
d.SetId("")
205+
return fmt.Errorf("resource `global_notification` %s does not exist", instanceId)
206+
}
207+
208+
if *globalNotification.Enabled {
209+
_ = d.Set("instance_id", instanceId)
210+
alertManager := make(map[string]interface{})
211+
if globalNotification.AlertManager != nil {
212+
alertManager = map[string]interface{}{
213+
"url": globalNotification.AlertManager.Url,
214+
"cluster_type": globalNotification.AlertManager.ClusterType,
215+
"cluster_id": globalNotification.AlertManager.ClusterId,
216+
}
217+
}
218+
219+
var notifications []map[string]interface{}
220+
notification := make(map[string]interface{})
221+
notification["enabled"] = globalNotification.Enabled
222+
notification["type"] = globalNotification.Type
223+
notification["web_hook"] = globalNotification.WebHook
224+
notification["alert_manager"] = alertManager
225+
notification["repeat_interval"] = globalNotification.RepeatInterval
226+
notification["time_range_start"] = globalNotification.TimeRangeStart
227+
notification["time_range_end"] = globalNotification.TimeRangeEnd
228+
notification["notify_way"] = globalNotification.NotifyWay
229+
notification["receiver_groups"] = globalNotification.ReceiverGroups
230+
notification["phone_notify_order"] = globalNotification.PhoneNotifyOrder
231+
notification["phone_circle_times"] = globalNotification.PhoneCircleTimes
232+
notification["phone_inner_interval"] = globalNotification.PhoneInnerInterval
233+
notification["phone_circle_interval"] = globalNotification.PhoneCircleInterval
234+
notification["phone_arrive_notice"] = globalNotification.PhoneArriveNotice
235+
notifications = append(notifications, notification)
236+
_ = d.Set("notification", notifications)
237+
}
238+
239+
return nil
240+
}
241+
242+
func resourceTencentCloudMonitorTmpTkeGlobalNotificationUpdate(d *schema.ResourceData, meta interface{}) error {
243+
defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.update")()
244+
defer inconsistentCheck(d, meta)()
245+
246+
logId := getLogId(contextNil)
247+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
248+
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
249+
250+
if d.HasChange("notification") {
251+
notification := tke.PrometheusNotificationItem{}
252+
if dMap, ok := helper.InterfacesHeadMap(d, "notification"); ok {
253+
if v, ok := dMap["enabled"]; ok {
254+
notification.Enabled = helper.Bool(v.(bool))
255+
}
256+
if v, ok := dMap["type"]; ok {
257+
notification.Type = helper.String(v.(string))
258+
}
259+
if v, ok := dMap["web_hook"]; ok {
260+
notification.WebHook = helper.String(v.(string))
261+
}
262+
if v, ok := helper.InterfacesHeadMap(d, "alert_manager"); ok {
263+
alertManager := tke.PrometheusAlertManagerConfig{}
264+
if vv, ok := v["url"]; ok {
265+
alertManager.Url = helper.String(vv.(string))
266+
}
267+
if vv, ok := v["cluster_type"]; ok {
268+
alertManager.ClusterType = helper.String(vv.(string))
269+
}
270+
if vv, ok := v["cluster_id"]; ok {
271+
alertManager.ClusterId = helper.String(vv.(string))
272+
}
273+
notification.AlertManager = &alertManager
274+
}
275+
276+
if v, ok := dMap["repeat_interval"]; ok {
277+
notification.RepeatInterval = helper.String(v.(string))
278+
}
279+
if v, ok := dMap["time_range_start"]; ok {
280+
notification.TimeRangeStart = helper.String(v.(string))
281+
}
282+
if v, ok := dMap["time_range_end"]; ok {
283+
notification.TimeRangeEnd = helper.String(v.(string))
284+
}
285+
if v, ok := dMap["notify_way"]; ok {
286+
for _, vv := range v.(*schema.Set).List() {
287+
if vv == "CALL" {
288+
if v, ok := dMap["receiver_groups"]; ok {
289+
notification.ReceiverGroups = helper.Strings(v.([]string))
290+
}
291+
if v, ok := dMap["phone_notify_order"]; ok {
292+
notification.PhoneNotifyOrder = helper.InterfacesUint64Point(v.([]interface{}))
293+
}
294+
if v, ok := dMap["phone_circle_times"]; ok {
295+
notification.PhoneCircleTimes = helper.Int64(v.(int64))
296+
}
297+
if v, ok := dMap["phone_inner_interval"]; ok {
298+
notification.PhoneInnerInterval = helper.Int64(v.(int64))
299+
}
300+
if v, ok := dMap["phone_circle_interval"]; ok {
301+
notification.PhoneCircleInterval = helper.Int64(v.(int64))
302+
}
303+
if v, ok := dMap["phone_arrive_notice"]; ok {
304+
notification.PhoneArriveNotice = helper.Bool(v.(bool))
305+
}
306+
}
307+
notification.NotifyWay = append(notification.NotifyWay, helper.String(vv.(string)))
308+
}
309+
}
310+
}
311+
312+
if _, err := service.ModifyTkeTmpGlobalNotification(ctx, d.Id(), notification); err != nil {
313+
return err
314+
}
315+
}
316+
317+
return resourceTencentCloudMonitorTmpTkeGlobalNotificationRead(d, meta)
318+
}
319+
320+
func resourceTencentCloudMonitorTmpTkeGlobalNotificationDelete(d *schema.ResourceData, meta interface{}) error {
321+
defer logElapsed("resource.tencentcloud_monitor_tmp_tke_global_notification.delete")()
322+
defer inconsistentCheck(d, meta)()
323+
324+
logId := getLogId(contextNil)
325+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
326+
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
327+
328+
id := d.Id()
329+
notification := tke.PrometheusNotificationItem{
330+
// Turning off the alarm notification function is to delete the alarm notification
331+
Enabled: helper.Bool(false),
332+
Type: helper.String(""),
333+
}
334+
335+
if _, err := service.ModifyTkeTmpGlobalNotification(ctx, id, notification); err != nil {
336+
return err
337+
}
338+
339+
return nil
340+
}

0 commit comments

Comments
 (0)