|
| 1 | +/* |
| 2 | +Provides a resource to create a as execute_scaling_policy |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_as_execute_scaling_policy" "execute_scaling_policy" { |
| 8 | + auto_scaling_policy_id = "asp-519acdug" |
| 9 | + honor_cooldown = false |
| 10 | + trigger_source = "API" |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +as execute_scaling_policy can be imported using the id, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +terraform import tencentcloud_as_execute_scaling_policy.execute_scaling_policy execute_scaling_policy_id |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "log" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 29 | + as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419" |
| 30 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 31 | +) |
| 32 | + |
| 33 | +func resourceTencentCloudAsExecuteScalingPolicy() *schema.Resource { |
| 34 | + return &schema.Resource{ |
| 35 | + Create: resourceTencentCloudAsExecuteScalingPolicyCreate, |
| 36 | + Read: resourceTencentCloudAsExecuteScalingPolicyRead, |
| 37 | + Delete: resourceTencentCloudAsExecuteScalingPolicyDelete, |
| 38 | + Importer: &schema.ResourceImporter{ |
| 39 | + State: schema.ImportStatePassthrough, |
| 40 | + }, |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "auto_scaling_policy_id": { |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "Auto-scaling policy ID. This parameter is not available to a target tracking policy.", |
| 47 | + }, |
| 48 | + |
| 49 | + "honor_cooldown": { |
| 50 | + Optional: true, |
| 51 | + ForceNew: true, |
| 52 | + Type: schema.TypeBool, |
| 53 | + Description: "Whether to check if the auto scaling group is in the cooldown period. Default value: false.", |
| 54 | + }, |
| 55 | + |
| 56 | + "trigger_source": { |
| 57 | + Optional: true, |
| 58 | + ForceNew: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Source that triggers the scaling policy. Valid values: API and CLOUD_MONITOR. Default value: API. The value CLOUD_MONITOR is specific to the Cloud Monitor service.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceTencentCloudAsExecuteScalingPolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + defer logElapsed("resource.tencentcloud_as_execute_scaling_policy.create")() |
| 68 | + defer inconsistentCheck(d, meta)() |
| 69 | + |
| 70 | + logId := getLogId(contextNil) |
| 71 | + |
| 72 | + var ( |
| 73 | + request = as.NewExecuteScalingPolicyRequest() |
| 74 | + response = as.NewExecuteScalingPolicyResponse() |
| 75 | + activityId string |
| 76 | + ) |
| 77 | + if v, ok := d.GetOk("auto_scaling_policy_id"); ok { |
| 78 | + request.AutoScalingPolicyId = helper.String(v.(string)) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOkExists("honor_cooldown"); ok { |
| 82 | + request.HonorCooldown = helper.Bool(v.(bool)) |
| 83 | + } |
| 84 | + |
| 85 | + if v, ok := d.GetOk("trigger_source"); ok { |
| 86 | + request.TriggerSource = helper.String(v.(string)) |
| 87 | + } |
| 88 | + |
| 89 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 90 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().ExecuteScalingPolicy(request) |
| 91 | + if e != nil { |
| 92 | + return retryError(e) |
| 93 | + } else { |
| 94 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 95 | + } |
| 96 | + response = result |
| 97 | + return nil |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + log.Printf("[CRITAL]%s operate as executeScalingPolicy failed, reason:%+v", logId, err) |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + activityId = *response.Response.ActivityId |
| 105 | + d.SetId(activityId) |
| 106 | + |
| 107 | + return resourceTencentCloudAsExecuteScalingPolicyRead(d, meta) |
| 108 | +} |
| 109 | + |
| 110 | +func resourceTencentCloudAsExecuteScalingPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 111 | + defer logElapsed("resource.tencentcloud_as_execute_scaling_policy.read")() |
| 112 | + defer inconsistentCheck(d, meta)() |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func resourceTencentCloudAsExecuteScalingPolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 118 | + defer logElapsed("resource.tencentcloud_as_execute_scaling_policy.delete")() |
| 119 | + defer inconsistentCheck(d, meta)() |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
0 commit comments