Skip to content

Commit 570d89c

Browse files
author
hellertang
authored
monitor policy supoort change (#759)
1 parent d95d557 commit 570d89c

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

tencentcloud/resource_tc_monitor_alarm_policy.go

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,17 @@ func AlarmPolicyRule() map[string]*schema.Schema {
197197
Type: schema.TypeList,
198198
Optional: true,
199199
MaxItems: 1,
200-
Description: "Filter condition for one single trigger rule.",
200+
Description: "Filter condition for one single trigger rule. Must set it when create tke-xxx rules.",
201201
Elem: &schema.Resource{
202202
Schema: map[string]*schema.Schema{
203203
"type": {
204204
Type: schema.TypeString,
205-
Required: true,
205+
Optional: true,
206206
Description: "Filter condition type. Valid values: DIMENSION (uses dimensions for filtering).",
207207
},
208208
"dimensions": {
209209
Type: schema.TypeString,
210-
Required: true,
210+
Optional: true,
211211
Description: "JSON string generated by serializing the AlarmPolicyDimension two-dimensional array.",
212212
},
213213
},
@@ -253,11 +253,13 @@ func resourceTencentMonitorAlarmPolicy() *schema.Resource {
253253
"monitor_type": {
254254
Type: schema.TypeString,
255255
Required: true,
256+
ForceNew: true,
256257
Description: "The type of monitor.",
257258
},
258259
"namespace": {
259260
Type: schema.TypeString,
260261
Required: true,
262+
ForceNew: true,
261263
Description: "The type of alarm.",
262264
},
263265
"remark": {
@@ -275,6 +277,7 @@ func resourceTencentMonitorAlarmPolicy() *schema.Resource {
275277
"project_id": {
276278
Type: schema.TypeInt,
277279
Optional: true,
280+
ForceNew: true,
278281
Default: -1,
279282
Description: "Project ID. For products with different projects, a value other than -1 must be passed in.",
280283
},
@@ -595,12 +598,6 @@ func resourceTencentMonitorAlarmPolicyRead(d *schema.ResourceData, meta interfac
595598

596599
var rules = make([]interface{}, 0, 100)
597600
for _, rule := range policy.Condition.Rules {
598-
var filter = make([]interface{}, 0, 10)
599-
alarmPolicyFilter := map[string]interface{}{
600-
"type": rule.Filter.Type,
601-
"dimensions": rule.Filter.Dimensions,
602-
}
603-
filter = append(filter, alarmPolicyFilter)
604601

605602
m := map[string]interface{}{
606603
"metric_name": rule.MetricName,
@@ -609,11 +606,20 @@ func resourceTencentMonitorAlarmPolicyRead(d *schema.ResourceData, meta interfac
609606
"value": rule.Value,
610607
"continue_period": rule.ContinuePeriod,
611608
"notice_frequency": rule.NoticeFrequency,
612-
"filter": filter,
613609
"description": rule.Description,
614610
"unit": rule.Unit,
615611
"rule_type": rule.RuleType,
616612
}
613+
if rule.Filter != nil {
614+
var filter = make([]interface{}, 0, 10)
615+
alarmPolicyFilter := map[string]interface{}{
616+
"type": rule.Filter.Type,
617+
"dimensions": rule.Filter.Dimensions,
618+
}
619+
filter = append(filter, alarmPolicyFilter)
620+
m["filter"] = filter
621+
}
622+
617623
rules = append(rules, m)
618624
}
619625

@@ -641,10 +647,19 @@ func resourceTencentMonitorAlarmPolicyRead(d *schema.ResourceData, meta interfac
641647
m["notice_frequency"] = eventRule.NoticeFrequency
642648
m["is_power_notice"] = eventRule.IsPowerNotice
643649
m["notice_frequency"] = eventRule.NoticeFrequency
644-
m["filter"] = filter
645650
m["description"] = eventRule.Description
646651
m["unit"] = eventRule.Unit
647652
m["rule_type"] = eventRule.RuleType
653+
654+
if eventRule.Filter != nil {
655+
var filter = make([]interface{}, 0, 10)
656+
alarmPolicyFilter := map[string]interface{}{
657+
"type": eventRule.Filter.Type,
658+
"dimensions": eventRule.Filter.Dimensions,
659+
}
660+
filter = append(filter, alarmPolicyFilter)
661+
m["filter"] = filter
662+
}
648663
eventConditions = append(eventConditions, m)
649664
}
650665
_ = d.Set("event_conditions", eventConditions)
@@ -679,6 +694,44 @@ func resourceTencentMonitorAlarmPolicyUpdate(d *schema.ResourceData, meta interf
679694
monitorService = MonitorService{client: meta.(*TencentCloudClient).apiV3Conn}
680695
)
681696

697+
if d.HasChange("policy_name") {
698+
request := monitor.NewModifyAlarmPolicyInfoRequest()
699+
request.Module = helper.String("monitor")
700+
request.PolicyId = helper.String(d.Id())
701+
request.Key = helper.String("NAME")
702+
value := d.Get("policy_name").(string)
703+
request.Value = helper.String(value)
704+
705+
if err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
706+
ratelimit.Check(request.GetAction())
707+
if _, err := monitorService.client.UseMonitorClient().ModifyAlarmPolicyInfo(request); err != nil {
708+
return retryError(err, InternalError)
709+
}
710+
return nil
711+
}); err != nil {
712+
return err
713+
}
714+
}
715+
716+
if d.HasChange("remark") {
717+
request := monitor.NewModifyAlarmPolicyInfoRequest()
718+
request.Module = helper.String("monitor")
719+
request.PolicyId = helper.String(d.Id())
720+
request.Key = helper.String("REMARK")
721+
value := d.Get("remark").(string)
722+
request.Value = helper.String(value)
723+
724+
if err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
725+
ratelimit.Check(request.GetAction())
726+
if _, err := monitorService.client.UseMonitorClient().ModifyAlarmPolicyInfo(request); err != nil {
727+
return retryError(err, InternalError)
728+
}
729+
return nil
730+
}); err != nil {
731+
return err
732+
}
733+
}
734+
682735
if d.HasChange("enable") {
683736
request := monitor.NewModifyAlarmPolicyStatusRequest()
684737
request.Module = helper.String("monitor")
@@ -687,10 +740,6 @@ func resourceTencentMonitorAlarmPolicyUpdate(d *schema.ResourceData, meta interf
687740
enable := d.Get("enable").(int)
688741
request.Enable = helper.IntInt64(enable)
689742

690-
//if v, ok := d.GetOk("enable"); ok {
691-
// request. = helper.IntInt64(v.(int))
692-
//}
693-
694743
if err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
695744
ratelimit.Check(request.GetAction())
696745
if _, err := monitorService.client.UseMonitorClient().ModifyAlarmPolicyStatus(request); err != nil {

website/docs/r/monitor_alarm_policy.html.markdown

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ resource "tencentcloud_monitor_alarm_policy" "policy" {
145145

146146
The following arguments are supported:
147147

148-
* `monitor_type` - (Required) The type of monitor.
149-
* `namespace` - (Required) The type of alarm.
148+
* `monitor_type` - (Required, ForceNew) The type of monitor.
149+
* `namespace` - (Required, ForceNew) The type of alarm.
150150
* `policy_name` - (Required) The name of policy.
151151
* `conditions` - (Optional) A list of metric trigger condition.
152152
* `conditon_template_id` - (Optional, ForceNew) ID of trigger condition template.
153153
* `enable` - (Optional) Whether to enable, default is `1`.
154154
* `event_conditions` - (Optional) A list of event trigger condition.
155155
* `notice_ids` - (Optional) List of notification rule IDs.
156-
* `project_id` - (Optional) Project ID. For products with different projects, a value other than -1 must be passed in.
156+
* `project_id` - (Optional, ForceNew) Project ID. For products with different projects, a value other than -1 must be passed in.
157157
* `remark` - (Optional) The remark of policy group.
158158
* `trigger_tasks` - (Optional) Triggered task list.
159159

@@ -166,7 +166,7 @@ The `event_conditions` object supports the following:
166166

167167
* `continue_period` - (Optional) Number of periods.
168168
* `description` - (Optional) Metric display name, which is used in the output parameter.
169-
* `filter` - (Optional) Filter condition for one single trigger rule.
169+
* `filter` - (Optional) Filter condition for one single trigger rule. Must set it when create tke-xxx rules.
170170
* `is_power_notice` - (Optional) Whether the alarm frequency increases exponentially.
171171
* `metric_name` - (Optional) Metric name or event name.
172172
* `notice_frequency` - (Optional) Alarm interval in seconds.
@@ -178,14 +178,14 @@ The `event_conditions` object supports the following:
178178

179179
The `filter` object supports the following:
180180

181-
* `dimensions` - (Required) JSON string generated by serializing the AlarmPolicyDimension two-dimensional array.
182-
* `type` - (Required) Filter condition type. Valid values: DIMENSION (uses dimensions for filtering).
181+
* `dimensions` - (Optional) JSON string generated by serializing the AlarmPolicyDimension two-dimensional array.
182+
* `type` - (Optional) Filter condition type. Valid values: DIMENSION (uses dimensions for filtering).
183183

184184
The `rules` object supports the following:
185185

186186
* `continue_period` - (Optional) Number of periods.
187187
* `description` - (Optional) Metric display name, which is used in the output parameter.
188-
* `filter` - (Optional) Filter condition for one single trigger rule.
188+
* `filter` - (Optional) Filter condition for one single trigger rule. Must set it when create tke-xxx rules.
189189
* `is_power_notice` - (Optional) Whether the alarm frequency increases exponentially.
190190
* `metric_name` - (Optional) Metric name or event name.
191191
* `notice_frequency` - (Optional) Alarm interval in seconds.

0 commit comments

Comments
 (0)