Skip to content

Commit 6b981e4

Browse files
tongyimingmikatong
andauthored
fix(as): [128680543]support target tracking policy (#3615)
* support target tracking policy * add changelog --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent f85032c commit 6b981e4

File tree

5 files changed

+258
-45
lines changed

5 files changed

+258
-45
lines changed

.changelog/3615.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_as_scaling_policy: support target tracking policy.
3+
```

tencentcloud/services/as/resource_tc_as_scaling_policy.go

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,56 +35,56 @@ func ResourceTencentCloudAsScalingPolicy() *schema.Resource {
3535
},
3636
"adjustment_type": {
3737
Type: schema.TypeString,
38-
Required: true,
38+
Optional: true,
3939
ValidateFunc: tccommon.ValidateAllowedStringValue(SCALING_GROUP_ADJUSTMENT_TYPE),
4040
Description: "Specifies whether the adjustment is an absolute number or a percentage of the current capacity. Valid values: `CHANGE_IN_CAPACITY`, `EXACT_CAPACITY` and `PERCENT_CHANGE_IN_CAPACITY`.",
4141
},
4242
"adjustment_value": {
4343
Type: schema.TypeInt,
44-
Required: true,
44+
Optional: true,
4545
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.",
4646
},
4747
"comparison_operator": {
4848
Type: schema.TypeString,
49-
Required: true,
49+
Optional: true,
5050
ValidateFunc: tccommon.ValidateAllowedStringValue(SCALING_GROUP_COMPARISON_OPERATOR),
5151
Description: "Comparison operator. Valid values: `GREATER_THAN`, `GREATER_THAN_OR_EQUAL_TO`, `LESS_THAN`, `LESS_THAN_OR_EQUAL_TO`, `EQUAL_TO` and `NOT_EQUAL_TO`.",
5252
},
5353
"metric_name": {
5454
Type: schema.TypeString,
55-
Required: true,
55+
Optional: true,
5656
ValidateFunc: tccommon.ValidateAllowedStringValue(SCALING_GROUP_METRIC_NAME),
5757
Description: "Name of an indicator. Valid values: `CPU_UTILIZATION`, `MEM_UTILIZATION`, `LAN_TRAFFIC_OUT`, `LAN_TRAFFIC_IN`, `WAN_TRAFFIC_OUT` and `WAN_TRAFFIC_IN`.",
5858
},
5959
"threshold": {
6060
Type: schema.TypeInt,
61-
Required: true,
61+
Optional: true,
6262
Description: "Alarm threshold.",
6363
},
6464
"period": {
6565
Type: schema.TypeInt,
66-
Required: true,
66+
Optional: true,
6767
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{60, 300}),
6868
Description: "Time period in second. Valid values: `60` and `300`.",
6969
},
7070
"continuous_time": {
7171
Type: schema.TypeInt,
72-
Required: true,
72+
Optional: true,
7373
ValidateFunc: tccommon.ValidateIntegerInRange(1, 10),
7474
Description: "Retry times. Valid value ranges: (1~10).",
7575
},
7676
"statistic": {
7777
Type: schema.TypeString,
7878
Optional: true,
79-
Default: SCALING_GROUP_STATISTIC_AVERAGE,
79+
Computed: true,
8080
ValidateFunc: tccommon.ValidateAllowedStringValue(SCALING_GROUP_STATISTIC),
8181
Description: "Statistic types. Valid values: `AVERAGE`, `MAXIMUM` and `MINIMUM`. Default is `AVERAGE`.",
8282
},
8383
"cooldown": {
8484
Type: schema.TypeInt,
8585
Optional: true,
86-
Default: 300,
87-
Description: "Cooldwon time in second. Default is `30`0.",
86+
Computed: true,
87+
Description: "Cooldwon time in second. Default is `300`.",
8888
},
8989
"notification_user_group_ids": {
9090
Type: schema.TypeList,
@@ -113,6 +113,7 @@ func ResourceTencentCloudAsScalingPolicy() *schema.Resource {
113113
"estimated_instance_warmup": {
114114
Type: schema.TypeInt,
115115
Optional: true,
116+
Computed: true,
116117
Description: "Instance warm-up time, in seconds, applicable only to target tracking strategies. Value range is 0-3600, with a default warm-up time of 300 seconds.",
117118
},
118119
"disable_scale_in": {
@@ -132,18 +133,40 @@ func resourceTencentCloudAsScalingPolicyCreate(d *schema.ResourceData, meta inte
132133
request := as.NewCreateScalingPolicyRequest()
133134
request.AutoScalingGroupId = helper.String(d.Get("scaling_group_id").(string))
134135
request.ScalingPolicyName = helper.String(d.Get("policy_name").(string))
135-
request.AdjustmentType = helper.String(d.Get("adjustment_type").(string))
136-
adjustMentValue := int64(d.Get("adjustment_value").(int))
137-
request.AdjustmentValue = &adjustMentValue
138-
request.MetricAlarm = &as.MetricAlarm{}
139-
request.MetricAlarm.ComparisonOperator = helper.String(d.Get("comparison_operator").(string))
140-
request.MetricAlarm.MetricName = helper.String(d.Get("metric_name").(string))
141-
request.MetricAlarm.Threshold = helper.IntUint64(d.Get("threshold").(int))
142-
request.MetricAlarm.Period = helper.IntUint64(d.Get("period").(int))
143-
request.MetricAlarm.ContinuousTime = helper.IntUint64(d.Get("continuous_time").(int))
144-
136+
if v, ok := d.GetOk("adjustment_type"); ok {
137+
request.AdjustmentType = helper.String(v.(string))
138+
}
139+
if v, ok := d.GetOkExists("adjustment_value"); ok {
140+
request.AdjustmentValue = helper.IntInt64(v.(int))
141+
}
142+
metricAlarm := &as.MetricAlarm{}
143+
var hasMetricAlarm bool
144+
if v, ok := d.GetOk("comparison_operator"); ok {
145+
metricAlarm.ComparisonOperator = helper.String(v.(string))
146+
hasMetricAlarm = true
147+
}
148+
if v, ok := d.GetOk("metric_name"); ok {
149+
metricAlarm.MetricName = helper.String(v.(string))
150+
hasMetricAlarm = true
151+
}
152+
if v, ok := d.GetOkExists("threshold"); ok {
153+
metricAlarm.Threshold = helper.IntUint64(v.(int))
154+
hasMetricAlarm = true
155+
}
156+
if v, ok := d.GetOkExists("period"); ok {
157+
metricAlarm.Period = helper.IntUint64(v.(int))
158+
hasMetricAlarm = true
159+
}
160+
if v, ok := d.GetOkExists("continuous_time"); ok {
161+
metricAlarm.ContinuousTime = helper.IntUint64(v.(int))
162+
hasMetricAlarm = true
163+
}
145164
if v, ok := d.GetOk("statistic"); ok {
146-
request.MetricAlarm.Statistic = helper.String(v.(string))
165+
metricAlarm.Statistic = helper.String(v.(string))
166+
hasMetricAlarm = true
167+
}
168+
if hasMetricAlarm {
169+
request.MetricAlarm = metricAlarm
147170
}
148171
if v, ok := d.GetOk("cooldown"); ok {
149172
request.Cooldown = helper.IntUint64(v.(int))
@@ -286,21 +309,43 @@ func resourceTencentCloudAsScalingPolicyUpdate(d *schema.ResourceData, meta inte
286309
request.ScalingPolicyName = helper.String(d.Get("policy_name").(string))
287310
}
288311
if d.HasChange("adjustment_type") {
289-
request.AdjustmentType = helper.String(d.Get("adjustment_type").(string))
312+
if v, ok := d.GetOk("adjustment_type"); ok {
313+
request.AdjustmentType = helper.String(v.(string))
314+
}
290315
}
291316
if d.HasChange("adjustment_value") {
292-
adjustmentValue := int64(d.Get("adjustment_value").(int))
293-
request.AdjustmentValue = &adjustmentValue
294-
}
295-
request.MetricAlarm = &as.MetricAlarm{}
296-
297-
//these two parameter must pass together
298-
request.MetricAlarm.ComparisonOperator = helper.String(d.Get("comparison_operator").(string))
299-
request.MetricAlarm.Threshold = helper.IntUint64(d.Get("threshold").(int))
300-
request.MetricAlarm.MetricName = helper.String(d.Get("metric_name").(string))
301-
request.MetricAlarm.Period = helper.IntUint64(d.Get("period").(int))
302-
request.MetricAlarm.ContinuousTime = helper.IntUint64(d.Get("continuous_time").(int))
303-
request.MetricAlarm.Statistic = helper.String(d.Get("statistic").(string))
317+
if v, ok := d.GetOkExists("adjustment_value"); ok {
318+
request.AdjustmentValue = helper.IntInt64(v.(int))
319+
}
320+
}
321+
322+
if d.HasChange("comparison_operator") || d.HasChange("threshold") || d.HasChange("metric_name") || d.HasChange("period") || d.HasChange("continuous_time") || d.HasChange("statistic") {
323+
request.MetricAlarm = &as.MetricAlarm{}
324+
325+
if v, ok := d.GetOk("comparison_operator"); ok {
326+
request.MetricAlarm.ComparisonOperator = helper.String(v.(string))
327+
}
328+
329+
if v, ok := d.GetOkExists("threshold"); ok {
330+
request.MetricAlarm.Threshold = helper.IntUint64(v.(int))
331+
}
332+
333+
if v, ok := d.GetOk("metric_name"); ok {
334+
request.MetricAlarm.MetricName = helper.String(v.(string))
335+
}
336+
337+
if v, ok := d.GetOkExists("period"); ok {
338+
request.MetricAlarm.Period = helper.IntUint64(v.(int))
339+
}
340+
341+
if v, ok := d.GetOkExists("continuous_time"); ok {
342+
request.MetricAlarm.ContinuousTime = helper.IntUint64(v.(int))
343+
}
344+
345+
if v, ok := d.GetOk("statistic"); ok {
346+
request.MetricAlarm.Statistic = helper.String(v.(string))
347+
}
348+
}
304349

305350
if d.HasChange("cooldown") {
306351
request.Cooldown = helper.IntUint64(d.Get("cooldown").(int))

tencentcloud/services/as/resource_tc_as_scaling_policy.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Provides a resource for an AS (Auto scaling) policy.
22

33
Example Usage
44

5+
Create Simple policy
6+
57
```hcl
68
data "tencentcloud_availability_zones_by_product" "zones" {
79
product = "as"
@@ -55,4 +57,55 @@ resource "tencentcloud_as_scaling_policy" "example" {
5557
statistic = "AVERAGE"
5658
cooldown = 360
5759
}
60+
```
61+
62+
Create a Target tracking policy
63+
64+
```hcl
65+
data "tencentcloud_availability_zones_by_product" "zones" {
66+
product = "as"
67+
}
68+
69+
data "tencentcloud_images" "image" {
70+
image_type = ["PUBLIC_IMAGE"]
71+
os_name = "TencentOS Server 3.2 (Final)"
72+
}
73+
74+
resource "tencentcloud_vpc" "vpc" {
75+
name = "vpc-example"
76+
cidr_block = "10.0.0.0/16"
77+
}
78+
79+
resource "tencentcloud_subnet" "subnet" {
80+
vpc_id = tencentcloud_vpc.vpc.id
81+
name = "subnet-example"
82+
cidr_block = "10.0.0.0/16"
83+
availability_zone = data.tencentcloud_availability_zones_by_product.zones.zones.0.name
84+
}
85+
86+
resource "tencentcloud_as_scaling_config" "example" {
87+
configuration_name = "tf-example"
88+
image_id = data.tencentcloud_images.image.images.0.image_id
89+
instance_types = ["SA1.SMALL1", "SA2.SMALL1", "SA2.SMALL2", "SA2.SMALL4"]
90+
instance_name_settings {
91+
instance_name = "test-ins-name"
92+
}
93+
}
94+
95+
resource "tencentcloud_as_scaling_group" "example" {
96+
scaling_group_name = "tf-example"
97+
configuration_id = tencentcloud_as_scaling_config.example.id
98+
max_size = 1
99+
min_size = 0
100+
vpc_id = tencentcloud_vpc.vpc.id
101+
subnet_ids = [tencentcloud_subnet.subnet.id]
102+
}
103+
104+
resource "tencentcloud_as_scaling_policy" "example" {
105+
scaling_group_id = tencentcloud_as_scaling_group.scaling_group.id
106+
policy_name = "tf-as-scaling-policy"
107+
policy_type = "TARGET_TRACKING"
108+
predefined_metric_type = "ASG_AVG_CPU_UTILIZATION"
109+
target_value = 80
110+
}
58111
```

tencentcloud/services/as/resource_tc_as_scaling_policy_test.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1414
)
1515

16-
func TestAccTencentCloudAsScalingPolicy(t *testing.T) {
16+
func TestAccTencentCloudAsScalingPolicy_Basic(t *testing.T) {
1717
t.Parallel()
1818
resource.Test(t, resource.TestCase{
1919
PreCheck: func() { tcacctest.AccPreCheck(t) },
@@ -58,6 +58,28 @@ func TestAccTencentCloudAsScalingPolicy(t *testing.T) {
5858
})
5959
}
6060

61+
func TestAccTencentCloudAsScalingPolicy_TargetTracking(t *testing.T) {
62+
t.Parallel()
63+
resource.Test(t, resource.TestCase{
64+
PreCheck: func() { tcacctest.AccPreCheck(t) },
65+
Providers: tcacctest.AccProviders,
66+
CheckDestroy: testAccCheckAsScalingPolicyDestroy,
67+
Steps: []resource.TestStep{
68+
{
69+
Config: TargetTrackingPolicy,
70+
Check: resource.ComposeAggregateTestCheckFunc(
71+
testAccCheckAsScalingPolicyExists("tencentcloud_as_scaling_policy.scaling_policy"),
72+
resource.TestCheckResourceAttrSet("tencentcloud_as_scaling_policy.scaling_policy", "scaling_group_id"),
73+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_policy.scaling_policy", "policy_name", "tf-as-scaling-policy"),
74+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_policy.scaling_policy", "policy_type", "TARGET_TRACKING"),
75+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_policy.scaling_policy", "predefined_metric_type", "ASG_AVG_CPU_UTILIZATION"),
76+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_policy.scaling_policy", "target_value", "80"),
77+
),
78+
},
79+
},
80+
})
81+
}
82+
6183
func testAccCheckAsScalingPolicyExists(n string) resource.TestCheckFunc {
6284
return func(s *terraform.State) error {
6385
logId := tccommon.GetLogId(tccommon.ContextNil)
@@ -119,7 +141,7 @@ resource "tencentcloud_subnet" "subnet" {
119141
120142
resource "tencentcloud_as_scaling_config" "launch_configuration" {
121143
configuration_name = "tf-as-configuration-policy"
122-
image_id = "img-9qabwvbn"
144+
image_id = "img-9qrfy1xt"
123145
instance_types = [data.tencentcloud_instance_types.default.instance_types.0.instance_type]
124146
}
125147
@@ -164,7 +186,7 @@ resource "tencentcloud_subnet" "subnet" {
164186
165187
resource "tencentcloud_as_scaling_config" "launch_configuration" {
166188
configuration_name = "tf-as-configuration-policy"
167-
image_id = "img-9qabwvbn"
189+
image_id = "img-9qrfy1xt"
168190
instance_types = [data.tencentcloud_instance_types.default.instance_types.0.instance_type]
169191
}
170192
@@ -192,3 +214,40 @@ resource "tencentcloud_as_scaling_policy" "scaling_policy" {
192214
}
193215
`
194216
}
217+
218+
const TargetTrackingPolicy = tcacctest.DefaultAsVariable + `
219+
resource "tencentcloud_vpc" "vpc" {
220+
name = "tf-as-vpc"
221+
cidr_block = "10.2.0.0/16"
222+
}
223+
224+
resource "tencentcloud_subnet" "subnet" {
225+
vpc_id = tencentcloud_vpc.vpc.id
226+
name = "tf-as-subnet"
227+
cidr_block = "10.2.11.0/24"
228+
availability_zone = var.availability_zone
229+
}
230+
231+
resource "tencentcloud_as_scaling_config" "launch_configuration" {
232+
configuration_name = "tf-as-configuration-policy"
233+
image_id = "img-9qrfy1xt"
234+
instance_types = [data.tencentcloud_instance_types.default.instance_types.0.instance_type]
235+
}
236+
237+
resource "tencentcloud_as_scaling_group" "scaling_group" {
238+
scaling_group_name = "tf-as-scaling-group-policy"
239+
configuration_id = tencentcloud_as_scaling_config.launch_configuration.id
240+
max_size = 1
241+
min_size = 0
242+
vpc_id = tencentcloud_vpc.vpc.id
243+
subnet_ids = [tencentcloud_subnet.subnet.id]
244+
}
245+
246+
resource "tencentcloud_as_scaling_policy" "scaling_policy" {
247+
scaling_group_id = tencentcloud_as_scaling_group.scaling_group.id
248+
policy_name = "tf-as-scaling-policy"
249+
policy_type = "TARGET_TRACKING"
250+
predefined_metric_type = "ASG_AVG_CPU_UTILIZATION"
251+
target_value = 80
252+
}
253+
`

0 commit comments

Comments
 (0)