@@ -131,6 +131,87 @@ resource "tencentcloud_monitor_alarm_policy" "policy" {
131131}
132132```
133133
134+ cvm_device alarm policy binding cvm by tag
135+ ```
136+ resource "tencentcloud_monitor_alarm_policy" "policy" {
137+ enable = 1
138+ monitor_type = "MT_QCE"
139+ namespace = "cvm_device"
140+ notice_ids = [
141+ "notice-l9ziyxw6",
142+ ]
143+ policy_name = "policy"
144+ project_id = 0
145+
146+ conditions {
147+ is_union_rule = 0
148+
149+ rules {
150+ continue_period = 5
151+ description = "CPUUtilization"
152+ is_power_notice = 0
153+ metric_name = "CpuUsage"
154+ notice_frequency = 7200
155+ operator = "gt"
156+ period = 60
157+ rule_type = "STATIC"
158+ unit = "%"
159+ value = "95"
160+ }
161+ rules {
162+ continue_period = 5
163+ description = "PublicBandwidthUtilization"
164+ is_power_notice = 0
165+ metric_name = "Outratio"
166+ notice_frequency = 7200
167+ operator = "gt"
168+ period = 60
169+ rule_type = "STATIC"
170+ unit = "%"
171+ value = "95"
172+ }
173+ rules {
174+ continue_period = 5
175+ description = "MemoryUtilization"
176+ is_power_notice = 0
177+ metric_name = "MemUsage"
178+ notice_frequency = 7200
179+ operator = "gt"
180+ period = 60
181+ rule_type = "STATIC"
182+ unit = "%"
183+ value = "95"
184+ }
185+ rules {
186+ continue_period = 5
187+ description = "DiskUtilization"
188+ is_power_notice = 0
189+ metric_name = "CvmDiskUsage"
190+ notice_frequency = 7200
191+ operator = "gt"
192+ period = 60
193+ rule_type = "STATIC"
194+ unit = "%"
195+ value = "95"
196+ }
197+ }
198+
199+ event_conditions {
200+ continue_period = 0
201+ description = "DiskReadonly"
202+ is_power_notice = 0
203+ metric_name = "disk_readonly"
204+ notice_frequency = 0
205+ period = 0
206+ }
207+
208+ policy_tag {
209+ key = "test-tag"
210+ value = "unit-test"
211+ }
212+ }
213+ ```
214+
134215Import
135216
136217Alarm policy instance can be imported, e.g.
@@ -349,6 +430,26 @@ func resourceTencentMonitorAlarmPolicy() *schema.Resource {
349430 },
350431 },
351432 },
433+ "policy_tag" : {
434+ Type : schema .TypeList ,
435+ Optional : true ,
436+ ForceNew : true ,
437+ Description : "Policy tag to bind object." ,
438+ Elem : & schema.Resource {
439+ Schema : map [string ]* schema.Schema {
440+ "key" : {
441+ Type : schema .TypeString ,
442+ Required : true ,
443+ Description : "Tag key." ,
444+ },
445+ "value" : {
446+ Type : schema .TypeString ,
447+ Required : true ,
448+ Description : "Tag value." ,
449+ },
450+ },
451+ },
452+ },
352453 // compute
353454 "create_time" : {
354455 Type : schema .TypeString ,
@@ -387,8 +488,9 @@ func resourceTencentMonitorAlarmPolicyCreate(d *schema.ResourceData, meta interf
387488 // request.Enable = helper.IntInt64(v.(int))
388489 //}
389490
390- if v , ok := d .GetOk ("project_id" ); ok {
391- request .ProjectId = helper .IntInt64 (v .(int ))
491+ projectId := d .Get ("project_id" ).(int )
492+ if projectId != - 1 {
493+ request .ProjectId = helper .IntInt64 (projectId )
392494 }
393495
394496 if v , ok := d .GetOk ("conditon_template_id" ); ok {
@@ -532,6 +634,7 @@ func resourceTencentMonitorAlarmPolicyCreate(d *schema.ResourceData, meta interf
532634 request .TriggerTasks = tasks
533635 }
534636
637+ var groupId * string
535638 var policyId * string
536639 if err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
537640 ratelimit .Check (request .GetAction ())
@@ -545,11 +648,48 @@ func resourceTencentMonitorAlarmPolicyCreate(d *schema.ResourceData, meta interf
545648 logId , request .GetAction (), request .ToJsonString (), response .ToJsonString ())
546649
547650 policyId = response .Response .PolicyId
651+ groupId = response .Response .OriginId
548652 return nil
549653 }); err != nil {
550654 return err
551655 }
552656 d .SetId (fmt .Sprintf ("%s" , * policyId ))
657+
658+ // binding tag
659+ if v , ok := d .GetOk ("policy_tag" ); ok {
660+ request := monitor .NewBindingPolicyTagRequest ()
661+
662+ request .Module = helper .String ("monitor" )
663+ request .PolicyId = helper .String (* policyId )
664+ request .ServiceType = helper .String (d .Get ("namespace" ).(string ))
665+ request .GroupId = helper .String (* groupId )
666+ tagSet := make ([]* monitor.PolicyTag , 0 , 10 )
667+ for _ , item := range v .([]interface {}) {
668+ m := item .(map [string ]interface {})
669+ tagInfo := monitor.PolicyTag {
670+ Key : helper .String (m ["key" ].(string )),
671+ Value : helper .String (m ["value" ].(string )),
672+ }
673+ tagSet = append (tagSet , & tagInfo )
674+ }
675+ request .Tag = tagSet [0 ]
676+
677+ if err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
678+ ratelimit .Check (request .GetAction ())
679+ response , err := monitorService .client .UseMonitorClient ().BindingPolicyTag (request )
680+ if err != nil {
681+ log .Printf ("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n " ,
682+ logId , request .GetAction (), request .ToJsonString (), err .Error ())
683+ return retryError (err , InternalError )
684+ }
685+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " ,
686+ logId , request .GetAction (), request .ToJsonString (), response .ToJsonString ())
687+ return nil
688+ }); err != nil {
689+ return err
690+ }
691+ }
692+
553693 return resourceTencentMonitorAlarmPolicyRead (d , meta )
554694}
555695
@@ -683,6 +823,15 @@ func resourceTencentMonitorAlarmPolicyRead(d *schema.ResourceData, meta interfac
683823 }
684824 errs = append (errs , d .Set ("trigger_tasks" , triggerTasks ))
685825
826+ tagSets := make ([]map [string ]interface {}, 0 , len (policy .TagInstances ))
827+ for _ , item := range policy .TagInstances {
828+ tagSets = append (tagSets , map [string ]interface {}{
829+ "key" : item .Key ,
830+ "value" : item .Value ,
831+ })
832+ }
833+ _ = d .Set ("policy_tag" , tagSets )
834+
686835 if len (errs ) > 0 {
687836 return errs [0 ]
688837 } else {
0 commit comments