|
| 1 | +/* |
| 2 | +Provides a resource for bind objects to a alarm policy resource. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_instances" "instances" { |
| 8 | +} |
| 9 | +resource "tencentcloud_monitor_alarm_policy" "policy" { |
| 10 | + policy_name = "hello" |
| 11 | + monitor_type = "MT_QCE" |
| 12 | + enable = 1 |
| 13 | + project_id = 1244035 |
| 14 | + namespace = "cvm_device" |
| 15 | +
|
| 16 | + conditions { |
| 17 | + is_union_rule = 1 |
| 18 | + rules { |
| 19 | + metric_name = "CpuUsage" |
| 20 | + period = 60 |
| 21 | + operator = "ge" |
| 22 | + value = "89.9" |
| 23 | + continue_period = 1 |
| 24 | + notice_frequency = 3600 |
| 25 | + is_power_notice = 0 |
| 26 | + } |
| 27 | + } |
| 28 | +
|
| 29 | + event_conditions { |
| 30 | + metric_name = "ping_unreachable" |
| 31 | + } |
| 32 | +
|
| 33 | + event_conditions { |
| 34 | + metric_name = "guest_reboot" |
| 35 | + } |
| 36 | +
|
| 37 | + notice_ids = ["notice-l9ziyxw6"] |
| 38 | +
|
| 39 | + trigger_tasks { |
| 40 | + type = "AS" |
| 41 | + task_config = "{\"Region\":\"ap-guangzhou\",\"Group\":\"asg-0z312312x\",\"Policy\":\"asp-ganig28\"}" |
| 42 | + } |
| 43 | +
|
| 44 | +} |
| 45 | +
|
| 46 | +#for cvm |
| 47 | +resource "tencentcloud_monitor_policy_binding_object" "binding" { |
| 48 | + policy_id = tencentcloud_monitor_alarm_policy.policy.id |
| 49 | +
|
| 50 | + dimensions { |
| 51 | + dimensions_json = "{\"unInstanceId\":\"${data.tencentcloud_instances.instances.instance_list[0].instance_id}\"}" |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | +
|
| 56 | +*/ |
| 57 | +package tencentcloud |
| 58 | + |
| 59 | +import ( |
| 60 | + "context" |
| 61 | + "encoding/json" |
| 62 | + "fmt" |
| 63 | + "time" |
| 64 | + |
| 65 | + "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" |
| 66 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 67 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 68 | + monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" |
| 69 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 70 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit" |
| 71 | +) |
| 72 | + |
| 73 | +func resourceTencentMonitorPolicyBindingObject() *schema.Resource { |
| 74 | + return &schema.Resource{ |
| 75 | + Create: resourceTencentMonitorPolicyBindingObjectCreate, |
| 76 | + Read: resourceTencentMonitorPolicyBindingObjectRead, |
| 77 | + Delete: resourceTencentMonitorPolicyBindingObjectDelete, |
| 78 | + Schema: map[string]*schema.Schema{ |
| 79 | + "policy_id": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Required: true, |
| 82 | + ForceNew: true, |
| 83 | + Description: "Alarm policy ID for binding objects.", |
| 84 | + }, |
| 85 | + "dimensions": { |
| 86 | + Type: schema.TypeSet, |
| 87 | + Required: true, |
| 88 | + ForceNew: true, |
| 89 | + Description: "A list objects. Each element contains the following attributes:", |
| 90 | + Set: func(v interface{}) int { |
| 91 | + vmap := v.(map[string]interface{}) |
| 92 | + hashMap := map[string]interface{}{} |
| 93 | + if vmap["dimensions_json"] != nil { |
| 94 | + hashMap["dimensions_json"] = vmap["dimensions_json"] |
| 95 | + } |
| 96 | + b, _ := json.Marshal(hashMap) |
| 97 | + return hashcode.String(string(b)) |
| 98 | + }, |
| 99 | + Elem: &schema.Resource{ |
| 100 | + Schema: map[string]*schema.Schema{ |
| 101 | + "dimensions_json": { |
| 102 | + Type: schema.TypeString, |
| 103 | + Required: true, |
| 104 | + ForceNew: true, |
| 105 | + Description: `Represents a collection of dimensions of an object instance, json format.eg:'{"unInstanceId":"ins-ot3cq4bi"}'.`, |
| 106 | + }, |
| 107 | + "unique_id": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "Object unique ID.", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func resourceTencentMonitorPolicyBindingObjectCreate(d *schema.ResourceData, meta interface{}) error { |
| 120 | + defer logElapsed("resource.tencentcloud_monitor_binding_object.create")() |
| 121 | + |
| 122 | + var ( |
| 123 | + logId = getLogId(contextNil) |
| 124 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 125 | + monitorService = MonitorService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 126 | + request = monitor.NewBindingPolicyObjectRequest() |
| 127 | + idSeeds []string |
| 128 | + policyId = d.Get("policy_id").(string) |
| 129 | + ) |
| 130 | + |
| 131 | + info, err := monitorService.DescribeAlarmPolicyById(ctx, policyId) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + if info == nil { |
| 136 | + return fmt.Errorf("alarm policy %s not exist", policyId) |
| 137 | + } |
| 138 | + request.GroupId = helper.Int64(0) |
| 139 | + request.PolicyId = &policyId |
| 140 | + dimensions := d.Get("dimensions").(*schema.Set).List() |
| 141 | + |
| 142 | + request.Dimensions = make([]*monitor.BindingPolicyObjectDimension, 0, len(dimensions)) |
| 143 | + |
| 144 | + idSeeds = append(idSeeds, fmt.Sprintf("%s", policyId)) |
| 145 | + |
| 146 | + for _, v := range dimensions { |
| 147 | + m := v.(map[string]interface{}) |
| 148 | + var dimension monitor.BindingPolicyObjectDimension |
| 149 | + var dimensionsJson = m["dimensions_json"].(string) |
| 150 | + var region = MonitorRegionMap[monitorService.client.Region] |
| 151 | + |
| 152 | + if region == "" { |
| 153 | + return fmt.Errorf("monitor not support region `%s` bind", monitorService.client.Region) |
| 154 | + } |
| 155 | + idSeeds = append(idSeeds, dimensionsJson, region) |
| 156 | + dimension.Dimensions = &dimensionsJson |
| 157 | + dimension.Region = ®ion |
| 158 | + request.Dimensions = append(request.Dimensions, &dimension) |
| 159 | + } |
| 160 | + |
| 161 | + request.Module = helper.String("monitor") |
| 162 | + if err = resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 163 | + ratelimit.Check(request.GetAction()) |
| 164 | + if _, err = monitorService.client.UseMonitorClient().BindingPolicyObject(request); err != nil { |
| 165 | + return retryError(err, InternalError) |
| 166 | + } |
| 167 | + return nil |
| 168 | + }); err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + d.SetId(helper.DataResourceIdsHash(idSeeds)) |
| 173 | + time.Sleep(3 * time.Second) |
| 174 | + |
| 175 | + objects, err := monitorService.DescribeBindingAlarmPolicyObjectList(ctx, policyId) |
| 176 | + |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + successDimensionsJsonMap := make(map[string]bool) |
| 182 | + bindingFails := make([]string, 0, len(request.Dimensions)) |
| 183 | + for _, v := range objects { |
| 184 | + successDimensionsJsonMap[*v.Dimensions] = true |
| 185 | + } |
| 186 | + for _, v := range request.Dimensions { |
| 187 | + if !successDimensionsJsonMap[*v.Dimensions] { |
| 188 | + bindingFails = append(bindingFails, *v.Dimensions) |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + if len(bindingFails) > 0 { |
| 193 | + return fmt.Errorf("bind objects to policy has partial failure,Please check if it is an instance of this region `%s`,[%s]", |
| 194 | + monitorService.client.Region, helper.SliceFieldSerialize(bindingFails)) |
| 195 | + } |
| 196 | + |
| 197 | + return resourceTencentMonitorPolicyBindingObjectRead(d, meta) |
| 198 | +} |
| 199 | + |
| 200 | +func resourceTencentMonitorPolicyBindingObjectRead(d *schema.ResourceData, meta interface{}) error { |
| 201 | + defer logElapsed("resource.tencentcloud_monitor_policy_binding_object.read")() |
| 202 | + defer inconsistentCheck(d, meta)() |
| 203 | + var ( |
| 204 | + logId = getLogId(contextNil) |
| 205 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 206 | + monitorService = MonitorService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 207 | + policyId = d.Get("policy_id").(string) |
| 208 | + ) |
| 209 | + |
| 210 | + info, err := monitorService.DescribeAlarmPolicyById(ctx, policyId) |
| 211 | + if err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + if info == nil { |
| 215 | + return fmt.Errorf("alarm policy %s not exist", policyId) |
| 216 | + } |
| 217 | + |
| 218 | + objects, err := monitorService.DescribeBindingAlarmPolicyObjectList(ctx, policyId) |
| 219 | + |
| 220 | + if err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + |
| 224 | + getUniqueId := func(dimensionsJson string) (has bool, uniqueId string) { |
| 225 | + for _, item := range objects { |
| 226 | + if *item.Dimensions == dimensionsJson { |
| 227 | + uniqueId = *item.UniqueId |
| 228 | + has = true |
| 229 | + return |
| 230 | + } |
| 231 | + } |
| 232 | + return |
| 233 | + } |
| 234 | + |
| 235 | + dimensions := d.Get("dimensions").(*schema.Set).List() |
| 236 | + newDimensions := make([]interface{}, 0, len(dimensions)) |
| 237 | + |
| 238 | + for _, v := range dimensions { |
| 239 | + m := v.(map[string]interface{}) |
| 240 | + var dimensionsJson = m["dimensions_json"].(string) |
| 241 | + var has, uniqueId = getUniqueId(dimensionsJson) |
| 242 | + if has { |
| 243 | + newDimensions = append(newDimensions, map[string]interface{}{ |
| 244 | + "dimensions_json": dimensionsJson, |
| 245 | + "unique_id": uniqueId, |
| 246 | + }) |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + if len(newDimensions) == 0 { |
| 251 | + d.SetId("") |
| 252 | + return nil |
| 253 | + } |
| 254 | + |
| 255 | + return d.Set("dimensions", newDimensions) |
| 256 | +} |
| 257 | + |
| 258 | +func resourceTencentMonitorPolicyBindingObjectDelete(d *schema.ResourceData, meta interface{}) error { |
| 259 | + defer logElapsed("resource.tencentcloud_monitor_binding_object.delete")() |
| 260 | + |
| 261 | + var ( |
| 262 | + logId = getLogId(contextNil) |
| 263 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 264 | + monitorService = MonitorService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 265 | + policyId = d.Get("policy_id").(string) |
| 266 | + ) |
| 267 | + |
| 268 | + info, err := monitorService.DescribeAlarmPolicyById(ctx, policyId) |
| 269 | + if err != nil { |
| 270 | + return err |
| 271 | + } |
| 272 | + if info == nil { |
| 273 | + return fmt.Errorf("alarm policy %s not exist", policyId) |
| 274 | + } |
| 275 | + |
| 276 | + objects, err := monitorService.DescribeBindingAlarmPolicyObjectList(ctx, policyId) |
| 277 | + |
| 278 | + if err != nil { |
| 279 | + return err |
| 280 | + } |
| 281 | + getUniqueId := func(dimensionsJson string) (has bool, uniqueId string) { |
| 282 | + for _, item := range objects { |
| 283 | + if *item.Dimensions == dimensionsJson { |
| 284 | + uniqueId = *item.UniqueId |
| 285 | + has = true |
| 286 | + return |
| 287 | + } |
| 288 | + } |
| 289 | + return |
| 290 | + } |
| 291 | + |
| 292 | + dimensions := d.Get("dimensions").(*schema.Set).List() |
| 293 | + uniqueIds := make([]*string, 0, len(dimensions)) |
| 294 | + for _, v := range dimensions { |
| 295 | + m := v.(map[string]interface{}) |
| 296 | + var dimensionsJson = m["dimensions_json"].(string) |
| 297 | + var has, uniqueId = getUniqueId(dimensionsJson) |
| 298 | + if has { |
| 299 | + uniqueIds = append(uniqueIds, &uniqueId) |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + if len(uniqueIds) == 0 { |
| 304 | + d.SetId("") |
| 305 | + return nil |
| 306 | + } |
| 307 | + |
| 308 | + var ( |
| 309 | + request = monitor.NewUnBindingPolicyObjectRequest() |
| 310 | + ) |
| 311 | + |
| 312 | + request.Module = helper.String("monitor") |
| 313 | + request.GroupId = helper.Int64(0) |
| 314 | + request.PolicyId = &policyId |
| 315 | + request.UniqueId = uniqueIds |
| 316 | + |
| 317 | + if err = resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 318 | + ratelimit.Check(request.GetAction()) |
| 319 | + if _, err = monitorService.client.UseMonitorClient().UnBindingPolicyObject(request); err != nil { |
| 320 | + return retryError(err, InternalError) |
| 321 | + } |
| 322 | + return nil |
| 323 | + }); err != nil { |
| 324 | + return err |
| 325 | + } |
| 326 | + return nil |
| 327 | +} |
0 commit comments