This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_tc_as_scaling_policy.go
283 lines (259 loc) · 10.6 KB
/
resource_tc_as_scaling_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Provides a resource for an AS (Auto scaling) policy.
Example Usage
```hcl
resource "tencentcloud_as_scaling_policy" "scaling_policy" {
scaling_group_id = "asg-n32ymck2"
policy_name = "tf-as-scaling-policy"
adjustment_type = "EXACT_CAPACITY"
adjustment_value = 0
comparison_operator = "GREATER_THAN"
metric_name = "CPU_UTILIZATION"
threshold = 80
period = 300
continuous_time = 10
statistic = "AVERAGE"
cooldown = 360
}
```
*/
package tencentcloud
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func resourceTencentCloudAsScalingPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudAsScalingPolicyCreate,
Read: resourceTencentCloudAsScalingPolicyRead,
Update: resourceTencentCloudAsScalingPolicyUpdate,
Delete: resourceTencentCloudAsScalingPolicyDelete,
Schema: map[string]*schema.Schema{
"scaling_group_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of a scaling group.",
},
"policy_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of a policy used to define a reaction when an alarm is triggered.",
},
"adjustment_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAllowedStringValue(SCALING_GROUP_ADJUSTMENT_TYPE),
Description: "Specifies whether the adjustment is an absolute number or a percentage of the current capacity. Available values include CHANGE_IN_CAPACITY, EXACT_CAPACITY and PERCENT_CHANGE_IN_CAPACITY.",
},
"adjustment_value": {
Type: schema.TypeInt,
Required: true,
Description: "Define the number of instances by which to scale.For CHANGE_IN_CAPACITY type or PERCENT_CHANGE_IN_CAPACITY, a positive increment adds to the current capacity and a negative value removes from the current capacity. For EXACT_CAPACITY type, it defines an absolute number of the existing Auto Scaling group size.",
},
"comparison_operator": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAllowedStringValue(SCALING_GROUP_COMPARISON_OPERATOR),
Description: "Comparison operator, of which valid values can be GREATER_THAN, GREATER_THAN_OR_EQUAL_TO, LESS_THAN, LESS_THAN_OR_EQUAL_TO, EQUAL_TO and NOT_EQUAL_TO.",
},
"metric_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAllowedStringValue(SCALING_GROUP_METRIC_NAME),
Description: "Name of an indicator, which can be CPU_UTILIZATION, MEM_UTILIZATION, LAN_TRAFFIC_OUT, LAN_TRAFFIC_IN, WAN_TRAFFIC_OUT and WAN_TRAFFIC_IN.",
},
"threshold": {
Type: schema.TypeInt,
Required: true,
Description: "Alarm threshold.",
},
"period": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateAllowedIntValue([]int{60, 300}),
Description: "Time period in second, of which valid values can be 60 and 300.",
},
"continuous_time": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateIntegerInRange(1, 10),
Description: "Retry times (1~10).",
},
"statistic": {
Type: schema.TypeString,
Optional: true,
Default: SCALING_GROUP_STATISTIC_AVERAGE,
ValidateFunc: validateAllowedStringValue(SCALING_GROUP_STATISTIC),
Description: "Statistic types, include AVERAGE, MAXIMUM and MINIMUM. Default is AVERAGE.",
},
"cooldown": {
Type: schema.TypeInt,
Optional: true,
Default: 300,
Description: "Cooldwon time in second. Default is 300.",
},
"notification_user_group_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "An ID group of users to be notified when an alarm is triggered.",
},
},
}
}
func resourceTencentCloudAsScalingPolicyCreate(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_as_scaling_policy.create")()
logId := getLogId(contextNil)
request := as.NewCreateScalingPolicyRequest()
request.AutoScalingGroupId = helper.String(d.Get("scaling_group_id").(string))
request.ScalingPolicyName = helper.String(d.Get("policy_name").(string))
request.AdjustmentType = helper.String(d.Get("adjustment_type").(string))
adjustMentValue := int64(d.Get("adjustment_value").(int))
request.AdjustmentValue = &adjustMentValue
request.MetricAlarm = &as.MetricAlarm{}
request.MetricAlarm.ComparisonOperator = helper.String(d.Get("comparison_operator").(string))
request.MetricAlarm.MetricName = helper.String(d.Get("metric_name").(string))
request.MetricAlarm.Threshold = helper.IntUint64(d.Get("threshold").(int))
request.MetricAlarm.Period = helper.IntUint64(d.Get("period").(int))
request.MetricAlarm.ContinuousTime = helper.IntUint64(d.Get("continuous_time").(int))
if v, ok := d.GetOk("statistic"); ok {
request.MetricAlarm.Statistic = helper.String(v.(string))
}
if v, ok := d.GetOk("cooldown"); ok {
request.Cooldown = helper.IntUint64(v.(int))
}
if v, ok := d.GetOk("notification_user_group_ids"); ok {
notificationUserGroupIds := v.([]interface{})
request.NotificationUserGroupIds = make([]*string, 0, len(notificationUserGroupIds))
for _, value := range notificationUserGroupIds {
request.NotificationUserGroupIds = append(request.NotificationUserGroupIds, helper.String(value.(string)))
}
}
response, err := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().CreateScalingPolicy(request)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
logId, request.GetAction(), request.ToJsonString(), err.Error())
return err
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
if response.Response.AutoScalingPolicyId == nil {
return fmt.Errorf("scaling policy id is nil")
}
d.SetId(*response.Response.AutoScalingPolicyId)
return resourceTencentCloudAsScalingPolicyRead(d, meta)
}
func resourceTencentCloudAsScalingPolicyRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_as_scaling_policy.read")()
defer inconsistentCheck(d, meta)()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
scalingPolicyId := d.Id()
asService := AsService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
scalingPolicy, has, e := asService.DescribeScalingPolicyById(ctx, scalingPolicyId)
if e != nil {
return retryError(e)
}
if has == 0 {
d.SetId("")
return nil
}
_ = d.Set("scaling_group_id", *scalingPolicy.AutoScalingGroupId)
_ = d.Set("policy_name", *scalingPolicy.ScalingPolicyName)
_ = d.Set("adjustment_type", *scalingPolicy.AdjustmentType)
_ = d.Set("adjustment_value", *scalingPolicy.AdjustmentValue)
_ = d.Set("comparison_operator", *scalingPolicy.MetricAlarm.ComparisonOperator)
_ = d.Set("metric_name", *scalingPolicy.MetricAlarm.MetricName)
_ = d.Set("threshold", *scalingPolicy.MetricAlarm.Threshold)
_ = d.Set("period", *scalingPolicy.MetricAlarm.Period)
_ = d.Set("continuous_time", *scalingPolicy.MetricAlarm.ContinuousTime)
_ = d.Set("statistic", *scalingPolicy.MetricAlarm.Statistic)
_ = d.Set("cooldown", *scalingPolicy.Cooldown)
if scalingPolicy.NotificationUserGroupIds != nil {
_ = d.Set("notification_user_group_ids", helper.StringsInterfaces(scalingPolicy.NotificationUserGroupIds))
}
return nil
})
if err != nil {
return err
}
return nil
}
func resourceTencentCloudAsScalingPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_as_scaling_policy.update")()
logId := getLogId(contextNil)
request := as.NewModifyScalingPolicyRequest()
scalingPolicyId := d.Id()
request.AutoScalingPolicyId = &scalingPolicyId
if d.HasChange("policy_name") {
request.ScalingPolicyName = helper.String(d.Get("policy_name").(string))
}
if d.HasChange("adjustment_type") {
request.AdjustmentType = helper.String(d.Get("adjustment_type").(string))
}
if d.HasChange("adjustment_value") {
adjustmentValue := int64(d.Get("adjustment_value").(int))
request.AdjustmentValue = &adjustmentValue
}
request.MetricAlarm = &as.MetricAlarm{}
if d.HasChange("comparison_operator") {
request.MetricAlarm.ComparisonOperator = helper.String(d.Get("comparison_operator").(string))
}
if d.HasChange("metric_name") {
request.MetricAlarm.MetricName = helper.String(d.Get("metric_name").(string))
}
if d.HasChange("threshold") {
request.MetricAlarm.Threshold = helper.IntUint64(d.Get("threshold").(int))
}
if d.HasChange("period") {
request.MetricAlarm.Period = helper.IntUint64(d.Get("period").(int))
}
if d.HasChange("continuous_time") {
request.MetricAlarm.ContinuousTime = helper.IntUint64(d.Get("continuous_time").(int))
}
if d.HasChange("statistic") {
request.MetricAlarm.Statistic = helper.String(d.Get("statistic").(string))
}
if d.HasChange("cooldown") {
request.Cooldown = helper.IntUint64(d.Get("cooldown").(int))
}
if d.HasChange("notification_user_group_ids") {
notificationUserGroupIds := d.Get("notification_user_group_ids").([]interface{})
request.NotificationUserGroupIds = make([]*string, 0, len(notificationUserGroupIds))
for _, value := range notificationUserGroupIds {
request.NotificationUserGroupIds = append(request.NotificationUserGroupIds, helper.String(value.(string)))
}
}
response, err := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().ModifyScalingPolicy(request)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
logId, request.GetAction(), request.ToJsonString(), err.Error())
return err
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
return nil
}
func resourceTencentCloudAsScalingPolicyDelete(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("resource.tencentcloud_as_scaling_policy.delete")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
scalingPolicyId := d.Id()
asService := AsService{
client: meta.(*TencentCloudClient).apiV3Conn,
}
err := asService.DeleteScalingPolicy(ctx, scalingPolicyId)
if err != nil {
return err
}
return nil
}