Skip to content

Commit 5c7c96d

Browse files
authored
feat: asg - support ServiceSettings (#860)
* feat: asg - support ServiceSettings * fix: asConfig testcase - public ip assigned * fix: asg_testcase set asg belong to existed network
1 parent 8f19eae commit 5c7c96d

6 files changed

+112
-52
lines changed

tencentcloud/extension_as.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,8 @@ const (
170170
MultiZoneSubnetPolicyPriority = "PRIORITY"
171171
MultiZoneSubnetPolicyEquality = "EQUALITY"
172172
)
173+
174+
const (
175+
SCALING_MODE_CLASSIC = "CLASSIC_SCALING"
176+
SCALING_MODE_WAKE_UP_STOPPED = "WAKE_UP_STOPPED_SCALING"
177+
)

tencentcloud/resource_tc_as_scaling_config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,16 @@ func resourceTencentCloudAsScalingConfigRead(d *schema.ResourceData, meta interf
458458
_ = d.Set("cam_role_name", *config.CamRoleName)
459459
if config.InstanceNameSettings != nil {
460460
settings := make([]map[string]interface{}, 0)
461-
settings = append(settings, map[string]interface{}{
461+
setting := map[string]interface{}{
462462
"instance_name": config.InstanceNameSettings.InstanceName,
463463
"instance_name_style": config.InstanceNameSettings.InstanceNameStyle,
464-
})
465-
_ = d.Set("instance_name_settings", settings)
464+
}
465+
name, nameOk := setting["instance_name"].(string)
466+
style, styleOk := setting["instance_name_style"].(string)
467+
if nameOk && name != "" || styleOk && style != "" {
468+
settings = append(settings, setting)
469+
_ = d.Set("instance_name_settings", settings)
470+
}
466471
}
467472

468473
if config.SystemDisk.DiskType != nil {

tencentcloud/resource_tc_as_scaling_config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestAccTencentCloudAsScalingConfig_full(t *testing.T) {
8080
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "data_disk.0.disk_type", "CLOUD_SSD"),
8181
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "data_disk.0.disk_size", "100"),
8282
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "internet_max_bandwidth_out", "20"),
83-
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "public_ip_assigned", "false"),
83+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "public_ip_assigned", "true"),
8484
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "enhanced_security_service", "true"),
8585
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "enhanced_monitor_service", "true"),
8686
resource.TestCheckResourceAttr("tencentcloud_as_scaling_config.launch_configuration", "user_data", "dGVzdA=="),
@@ -197,7 +197,7 @@ resource "tencentcloud_as_scaling_config" "launch_configuration" {
197197
198198
internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR"
199199
internet_max_bandwidth_out = 20
200-
public_ip_assigned = false
200+
public_ip_assigned = true
201201
password = "test123#"
202202
enhanced_security_service = true
203203
enhanced_monitor_service = true

tencentcloud/resource_tc_as_scaling_group.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ func resourceTencentCloudAsScalingGroup() *schema.Resource {
194194
ValidateFunc: validateAllowedStringValue([]string{SCALING_GROUP_RETRY_POLICY_IMMEDIATE_RETRY,
195195
SCALING_GROUP_RETRY_POLICY_INCREMENTAL_INTERVALS}),
196196
},
197+
"scaling_mode": {
198+
Type: schema.TypeString,
199+
Optional: true,
200+
Description: "Indicates scaling mode which creates and terminates instances (classic method), or method first tries to start stopped instances (wake up stopped) to perform scaling operations. Available values: `CLASSIC_SCALING`, `WAKE_UP_STOPPED_SCALING`. Default: `CLASSIC_SCALING`.",
201+
},
202+
// Service Settings
203+
"replace_monitor_unhealthy": {
204+
Type: schema.TypeBool,
205+
Optional: true,
206+
Description: "Enables unhealthy instance replacement. If set to `true`, AS will replace instances that are flagged as unhealthy by Cloud Monitor.",
207+
},
208+
"replace_load_balancer_unhealthy": {
209+
Type: schema.TypeBool,
210+
Optional: true,
211+
Description: "Enable unhealthy instance replacement. If set to `true`, AS will replace instances that are found unhealthy in the CLB health check.",
212+
},
197213
"tags": {
198214
Type: schema.TypeMap,
199215
Optional: true,
@@ -314,6 +330,24 @@ func resourceTencentCloudAsScalingGroupCreate(d *schema.ResourceData, meta inter
314330
request.MultiZoneSubnetPolicy = helper.String(v.(string))
315331
}
316332

333+
var (
334+
scalingMode = d.Get("scaling_mode").(string)
335+
replaceMonitorUnhealthy = d.Get("replace_monitor_unhealthy").(bool)
336+
replaceLBUnhealthy = d.Get("replace_load_balancer_unhealthy").(bool)
337+
)
338+
339+
if scalingMode != "" || replaceMonitorUnhealthy || replaceLBUnhealthy {
340+
if scalingMode == "" {
341+
scalingMode = SCALING_MODE_CLASSIC
342+
}
343+
344+
request.ServiceSettings = &as.ServiceSettings{
345+
ScalingMode: &scalingMode,
346+
ReplaceMonitorUnhealthy: &replaceMonitorUnhealthy,
347+
ReplaceLoadBalancerUnhealthy: &replaceLBUnhealthy,
348+
}
349+
}
350+
317351
var id string
318352
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
319353
ratelimit.Check(request.GetAction())
@@ -419,7 +453,21 @@ func resourceTencentCloudAsScalingGroupRead(d *schema.ResourceData, meta interfa
419453
_ = d.Set("termination_policies", helper.StringsInterfaces(scalingGroup.TerminationPolicySet))
420454
_ = d.Set("retry_policy", scalingGroup.RetryPolicy)
421455
_ = d.Set("create_time", scalingGroup.CreatedTime)
422-
_ = d.Set("multi_zone_subnet_policy", scalingGroup.MultiZoneSubnetPolicy)
456+
if v, ok := d.GetOk("multi_zone_subnet_policy"); ok && v.(string) != "" {
457+
_ = d.Set("multi_zone_subnet_policy", scalingGroup.MultiZoneSubnetPolicy)
458+
}
459+
460+
if v := d.Get("scaling_mode"); v != "" {
461+
_ = d.Set("scaling_mode", v.(string))
462+
}
463+
464+
if v, ok := d.GetOk("replace_monitor_unhealthy"); ok {
465+
_ = d.Set("replace_monitor_unhealthy", v.(bool))
466+
}
467+
468+
if v, ok := d.GetOk("replace_load_balancer_unhealthy"); ok {
469+
_ = d.Set("replace_load_balancer_unhealthy", v.(bool))
470+
}
423471

424472
if scalingGroup.ForwardLoadBalancerSet != nil && len(scalingGroup.ForwardLoadBalancerSet) > 0 {
425473
forwardLoadBalancers := make([]map[string]interface{}, 0, len(scalingGroup.ForwardLoadBalancerSet))
@@ -538,6 +586,23 @@ func resourceTencentCloudAsScalingGroupUpdate(d *schema.ResourceData, meta inter
538586
request.MultiZoneSubnetPolicy = helper.String(d.Get("multi_zone_subnet_policy").(string))
539587
}
540588

589+
if d.HasChange("scaling_mode") ||
590+
d.HasChange("replace_monitor_unhealthy") ||
591+
d.HasChange("replace_load_balancer_unhealthy") {
592+
updateAttrs = append(updateAttrs, "scaling_mode", "replace_monitor_unhealthy", "replace_load_balancer_unhealthy")
593+
scalingMode := d.Get("scaling_mode").(string)
594+
if scalingMode == "" {
595+
scalingMode = SCALING_MODE_CLASSIC
596+
}
597+
replaceMonitor := d.Get("replace_monitor_unhealthy").(bool)
598+
replaceLB := d.Get("replace_load_balancer_unhealthy").(bool)
599+
request.ServiceSettings = &as.ServiceSettings{
600+
ScalingMode: &scalingMode,
601+
ReplaceMonitorUnhealthy: &replaceMonitor,
602+
ReplaceLoadBalancerUnhealthy: &replaceLB,
603+
}
604+
}
605+
541606
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
542607
ratelimit.Check(request.GetAction())
543608

tencentcloud/resource_tc_as_scaling_group_test.go

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ func TestAccTencentCloudAsScalingGroup_full(t *testing.T) {
120120
resource.TestCheckResourceAttr("tencentcloud_as_scaling_group.scaling_group", "termination_policies.#", "1"),
121121
resource.TestCheckResourceAttr("tencentcloud_as_scaling_group.scaling_group", "termination_policies.0", "OLDEST_INSTANCE"),
122122
resource.TestCheckResourceAttr("tencentcloud_as_scaling_group.scaling_group", "retry_policy", "IMMEDIATE_RETRY"),
123+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_group.scaling_group", "scaling_mode", "WAKE_UP_STOPPED_SCALING"),
124+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_group.scaling_group", "replace_monitor_unhealthy", "true"),
125+
resource.TestCheckResourceAttr("tencentcloud_as_scaling_group.scaling_group", "replace_load_balancer_unhealthy", "true"),
123126
resource.TestCheckNoResourceAttr("tencentcloud_as_scaling_group.scaling_group", "tags.test"),
124127
resource.TestCheckResourceAttr("tencentcloud_as_scaling_group.scaling_group", "tags.abc", "abc"),
125128
),
@@ -176,63 +179,47 @@ func testAccCheckAsScalingGroupDestroy(s *terraform.State) error {
176179
}
177180

178181
func testAccAsScalingGroup_basic() string {
179-
return `
180-
resource "tencentcloud_vpc" "vpc" {
181-
name = "tf-as-vpc"
182-
cidr_block = "10.2.0.0/16"
183-
}
184-
185-
resource "tencentcloud_subnet" "subnet" {
186-
vpc_id = tencentcloud_vpc.vpc.id
187-
name = "tf-as-subnet"
188-
cidr_block = "10.2.11.0/24"
189-
availability_zone = "ap-guangzhou-3"
190-
}
191-
182+
return fmt.Sprintf(`
192183
resource "tencentcloud_as_scaling_config" "launch_configuration" {
193184
configuration_name = "tf-as-configuration-basic"
194185
image_id = "img-9qabwvbn"
195186
instance_types = ["SA1.SMALL1"]
187+
instance_name_settings {
188+
instance_name = "test-ins-name"
189+
# instance_name_style
190+
}
196191
}
197192
198193
resource "tencentcloud_as_scaling_group" "scaling_group" {
199194
scaling_group_name = "tf-as-group-basic"
200195
configuration_id = tencentcloud_as_scaling_config.launch_configuration.id
201196
max_size = 1
202197
min_size = 0
203-
vpc_id = tencentcloud_vpc.vpc.id
204-
subnet_ids = [tencentcloud_subnet.subnet.id]
198+
vpc_id = "%s"
199+
subnet_ids = ["%s"]
205200
}
206-
`
201+
`, defaultVpcId, defaultSubnetId)
207202
}
208203

209204
func testAccAsScalingGroup_full() string {
210-
return `
211-
resource "tencentcloud_vpc" "vpc" {
212-
name = "tf-as-vpc"
213-
cidr_block = "10.2.0.0/16"
214-
}
215-
216-
resource "tencentcloud_subnet" "subnet" {
217-
vpc_id = tencentcloud_vpc.vpc.id
218-
name = "tf-as-subnet"
219-
cidr_block = "10.2.11.0/24"
220-
availability_zone = "ap-guangzhou-3"
221-
}
205+
return fmt.Sprintf(`
222206
223207
resource "tencentcloud_as_scaling_config" "launch_configuration" {
224208
configuration_name = "tf-as-configuration-full"
225209
image_id = "img-9qabwvbn"
226210
instance_types = ["SA1.SMALL1"]
211+
instance_name_settings {
212+
instance_name = "test-ins-name-full"
213+
}
227214
}
228215
229216
resource "tencentcloud_as_scaling_group" "scaling_group" {
230217
scaling_group_name = "tf-as-group-full"
231218
configuration_id = tencentcloud_as_scaling_config.launch_configuration.id
232219
max_size = 1
233220
min_size = 0
234-
vpc_id = tencentcloud_vpc.vpc.id
235-
subnet_ids = [tencentcloud_subnet.subnet.id]
221+
vpc_id = "%s"
222+
subnet_ids = ["%s"]
236223
project_id = 0
237224
default_cooldown = 400
238225
desired_capacity = 1
@@ -243,45 +230,40 @@ resource "tencentcloud_as_scaling_group" "scaling_group" {
243230
"test" = "test"
244231
}
245232
}
246-
`
233+
`, defaultVpcId, defaultSubnetId)
247234
}
248235

249236
func testAccAsScalingGroup_update() string {
250-
return `
251-
resource "tencentcloud_vpc" "vpc" {
252-
name = "tf-as-vpc"
253-
cidr_block = "10.2.0.0/16"
254-
}
255-
256-
resource "tencentcloud_subnet" "subnet" {
257-
vpc_id = tencentcloud_vpc.vpc.id
258-
name = "tf-as-subnet"
259-
cidr_block = "10.2.11.0/24"
260-
availability_zone = "ap-guangzhou-3"
261-
}
237+
return fmt.Sprintf(`
262238
263239
resource "tencentcloud_as_scaling_config" "launch_configuration" {
264240
configuration_name = "tf-as-configuration-full"
265241
image_id = "img-9qabwvbn"
266242
instance_types = ["SA1.SMALL1"]
243+
instance_name_settings {
244+
instance_name = "test-ins-name-full"
245+
}
267246
}
268247
269248
resource "tencentcloud_as_scaling_group" "scaling_group" {
270249
scaling_group_name = "tf-as-group-update"
271250
configuration_id = tencentcloud_as_scaling_config.launch_configuration.id
272251
max_size = 2
273252
min_size = 0
274-
vpc_id = tencentcloud_vpc.vpc.id
275-
subnet_ids = [tencentcloud_subnet.subnet.id]
253+
vpc_id = "%s"
254+
subnet_ids = ["%s"]
276255
project_id = 0
277256
default_cooldown = 300
278257
desired_capacity = 0
279258
termination_policies = ["OLDEST_INSTANCE"]
280259
retry_policy = "IMMEDIATE_RETRY"
260+
scaling_mode = "WAKE_UP_STOPPED_SCALING"
261+
replace_monitor_unhealthy = true
262+
replace_load_balancer_unhealthy = true
281263
282264
tags = {
283265
"abc" = "abc"
284266
}
285267
}
286-
`
268+
`, defaultVpcId, defaultSubnetId)
287269
}

website/docs/r/as_scaling_group.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ The following arguments are supported:
5555
* `load_balancer_ids` - (Optional) ID list of traditional load balancers.
5656
* `multi_zone_subnet_policy` - (Optional) Multi zone or subnet strategy, Valid values: PRIORITY and EQUALITY.
5757
* `project_id` - (Optional) Specifies to which project the scaling group belongs.
58+
* `replace_load_balancer_unhealthy` - (Optional) Enable unhealthy instance replacement. If set to `true`, AS will replace instances that are found unhealthy in the CLB health check.
59+
* `replace_monitor_unhealthy` - (Optional) Enables unhealthy instance replacement. If set to `true`, AS will replace instances that are flagged as unhealthy by Cloud Monitor.
5860
* `retry_policy` - (Optional) Available values for retry policies. Valid values: IMMEDIATE_RETRY and INCREMENTAL_INTERVALS.
61+
* `scaling_mode` - (Optional) Indicates scaling mode which creates and terminates instances (classic method), or method first tries to start stopped instances (wake up stopped) to perform scaling operations. Available values: `CLASSIC_SCALING`, `WAKE_UP_STOPPED_SCALING`. Default: `CLASSIC_SCALING`.
5962
* `subnet_ids` - (Optional) ID list of subnet, and for VPC it is required.
6063
* `tags` - (Optional) Tags of a scaling group.
6164
* `termination_policies` - (Optional) Available values for termination policies. Valid values: OLDEST_INSTANCE and NEWEST_INSTANCE.

0 commit comments

Comments
 (0)