Skip to content

Commit

Permalink
resource/alicloud_ess_scaling_group: add scaling_policy and max_insta…
Browse files Browse the repository at this point in the history
…nce_lifetime
  • Loading branch information
fuliu-zln authored and ChenHanZhang committed Jul 2, 2024
1 parent 1d165f2 commit e2ba5b4
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
32 changes: 32 additions & 0 deletions alicloud/resource_alicloud_ess_scaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ func resourceAlicloudEssScalingGroup() *schema.Resource {
ValidateFunc: StringInSlice([]string{"ECS", "NONE", "LOAD_BALANCER"}, false),
Optional: true,
},
"scaling_policy": {
Type: schema.TypeString,
Computed: true,
ValidateFunc: StringInSlice([]string{"recycle", "release", "forceRecycle", "forceRelease"}, false),
Optional: true,
},
"max_instance_lifetime": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: IntAtLeast(86400),
},
"default_cooldown": {
Type: schema.TypeInt,
Default: 300,
Expand Down Expand Up @@ -295,6 +306,9 @@ func resourceAliyunEssScalingGroupRead(d *schema.ResourceData, meta interface{})
d.Set("desired_capacity", object["DesiredCapacity"])
d.Set("scaling_group_name", object["ScalingGroupName"])
d.Set("default_cooldown", object["DefaultCooldown"])
if object["MaxInstanceLifetime"] != nil {
d.Set("max_instance_lifetime", object["MaxInstanceLifetime"])
}
d.Set("multi_az_policy", object["MultiAZPolicy"])
d.Set("az_balance", object["AzBalance"])
d.Set("allocation_strategy", object["AllocationStrategy"])
Expand Down Expand Up @@ -386,6 +400,7 @@ func resourceAliyunEssScalingGroupRead(d *schema.ResourceData, meta interface{})
d.Set("launch_template_version", object["LaunchTemplateVersion"])
d.Set("group_type", object["GroupType"])
d.Set("health_check_type", object["HealthCheckType"])
d.Set("scaling_policy", object["ScalingPolicy"])

listTagResourcesObject, err := essService.ListTagResources(d.Id(), client)
if err != nil {
Expand Down Expand Up @@ -435,6 +450,10 @@ func resourceAliyunEssScalingGroupUpdate(d *schema.ResourceData, meta interface{
request["HealthCheckType"] = d.Get("health_check_type").(string)
}

if d.HasChange("scaling_policy") {
request["ScalingPolicy"] = d.Get("scaling_policy").(string)
}

if d.HasChange("min_size") {
request["MinSize"] = requests.NewInteger(d.Get("min_size").(int))
}
Expand All @@ -447,6 +466,11 @@ func resourceAliyunEssScalingGroupUpdate(d *schema.ResourceData, meta interface{
request["DesiredCapacity"] = requests.NewInteger(v.(int))
}
}
if d.HasChange("max_instance_lifetime") {
if v, ok := d.GetOkExists("max_instance_lifetime"); ok {
request["MaxInstanceLifetime"] = requests.NewInteger(v.(int))
}
}
if d.HasChange("default_cooldown") {
request["DefaultCooldown"] = requests.NewInteger(d.Get("default_cooldown").(int))
}
Expand Down Expand Up @@ -662,6 +686,10 @@ func buildAlicloudEssScalingGroupArgs(d *schema.ResourceData, meta interface{})
request["DesiredCapacity"] = v
}

if v, ok := d.GetOk("max_instance_lifetime"); ok {
request["MaxInstanceLifetime"] = v
}

request["OnDemandBaseCapacity"] = d.Get("on_demand_base_capacity")

request["OnDemandPercentageAboveBaseCapacity"] = d.Get("on_demand_percentage_above_base_capacity")
Expand All @@ -678,6 +706,10 @@ func buildAlicloudEssScalingGroupArgs(d *schema.ResourceData, meta interface{})
request["HealthCheckType"] = v
}

if v, ok := d.GetOk("scaling_policy"); ok {
request["ScalingPolicy"] = v
}

if v, ok := d.GetOk("group_deletion_protection"); ok {
request["GroupDeletionProtection"] = v
}
Expand Down
159 changes: 159 additions & 0 deletions alicloud/resource_alicloud_ess_scaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,165 @@ func TestAccAliCloudEssScalingGroup_composable(t *testing.T) {

}

func TestAccAliCloudEssScalingGroup_scalingPolicy_maxInstanceLifetime(t *testing.T) {
rand := acctest.RandIntRange(10000, 999999)
var v ess.ScalingGroup
resourceId := "alicloud_ess_scaling_group.default"

basicMap := map[string]string{
"min_size": "1",
"max_size": "1",
"default_cooldown": "20",
"scaling_group_name": fmt.Sprintf("tf-testAccEssScalingGroup-%d", rand),
"vswitch_ids.#": "2",
"removal_policies.#": "2",
"on_demand_base_capacity": "10",
"spot_instance_pools": "10",
"spot_instance_remedy": "false",
"group_deletion_protection": "false",
"on_demand_percentage_above_base_capacity": "10",
"az_balance": "false",
"allocation_strategy": "priority",
"spot_allocation_strategy": "priority",
}

ra := resourceAttrInit(resourceId, basicMap)
rc := resourceCheckInit(resourceId, &v, func() interface{} {
return &EssService{testAccProvider.Meta().(*connectivity.AliyunClient)}
})
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
name := fmt.Sprintf("tf-testAccEssScalingGroup-%d", rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, resourceEssScalingGroupDependence)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},

// module name
IDRefreshName: resourceId,

Providers: testAccProviders,
CheckDestroy: testAccCheckEssScalingGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"min_size": "1",
"max_size": "1",
"scaling_group_name": "${var.name}",
"default_cooldown": "20",
"vswitch_ids": []string{"${alicloud_vswitch.default.id}", "${alicloud_vswitch.default2.id}"},
"removal_policies": []string{"OldestInstance", "NewestInstance"},
"multi_az_policy": "COMPOSABLE",
"on_demand_base_capacity": "10",
"on_demand_percentage_above_base_capacity": "10",
"spot_instance_pools": "10",
"az_balance": "false",
"allocation_strategy": "priority",
"scaling_policy": "release",
"spot_allocation_strategy": "priority",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(nil),
),
},
{
Config: testAccConfig(map[string]interface{}{
"min_size": "1",
"max_size": "1",
"scaling_policy": "forceRelease",
"max_instance_lifetime": "86400",
"scaling_group_name": "${var.name}",
"default_cooldown": "20",
"vswitch_ids": []string{"${alicloud_vswitch.default.id}", "${alicloud_vswitch.default2.id}"},
"removal_policies": []string{"OldestInstance", "NewestInstance"},
"multi_az_policy": "COMPOSABLE",
"on_demand_base_capacity": "10",
"on_demand_percentage_above_base_capacity": "10",
"spot_instance_pools": "10",
"spot_instance_remedy": "true",
"az_balance": "false",
"allocation_strategy": "priority",
"spot_allocation_strategy": "priority",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"spot_instance_remedy": "true",
"az_balance": "false",
"scaling_policy": "forceRelease",
"max_instance_lifetime": "86400",
"allocation_strategy": "priority",
"spot_allocation_strategy": "priority",
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"min_size": "1",
"max_size": "1",
"scaling_group_name": "${var.name}",
"default_cooldown": "20",
"scaling_policy": "forceRelease",
"max_instance_lifetime": "86411",
"vswitch_ids": []string{"${alicloud_vswitch.default.id}", "${alicloud_vswitch.default2.id}"},
"removal_policies": []string{"OldestInstance", "NewestInstance"},
"multi_az_policy": "COMPOSABLE",
"on_demand_base_capacity": "10",
"on_demand_percentage_above_base_capacity": "10",
"spot_instance_pools": "10",
"spot_instance_remedy": "true",
"group_deletion_protection": "false",
"az_balance": "true",
"allocation_strategy": "lowestPrice",
"spot_allocation_strategy": "lowestPrice",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"group_deletion_protection": "false",
"az_balance": "true",
"scaling_policy": "forceRelease",
"max_instance_lifetime": "86411",
"allocation_strategy": "lowestPrice",
"spot_allocation_strategy": "lowestPrice",
}),
),
},
{
Config: testAccConfig(map[string]interface{}{
"min_size": "1",
"max_size": "1",
"scaling_group_name": "${var.name}",
"default_cooldown": "20",
"scaling_policy": "forceRecycle",
"max_instance_lifetime": REMOVEKEY,
"vswitch_ids": []string{"${alicloud_vswitch.default.id}", "${alicloud_vswitch.default2.id}"},
"removal_policies": []string{"OldestInstance", "NewestInstance"},
"multi_az_policy": "COMPOSABLE",
"on_demand_base_capacity": "10",
"on_demand_percentage_above_base_capacity": "10",
"spot_instance_pools": "10",
"spot_instance_remedy": "true",
"group_deletion_protection": "false",
"az_balance": "true",
"allocation_strategy": "lowestPrice",
"spot_allocation_strategy": "lowestPrice",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"group_deletion_protection": "false",
"az_balance": "true",
"scaling_policy": "forceRecycle",
"max_instance_lifetime": REMOVEKEY,
"allocation_strategy": "lowestPrice",
"spot_allocation_strategy": "lowestPrice",
}),
),
},
},
})

}

func TestAccAliCloudEssScalingGroup_vpc(t *testing.T) {
rand := acctest.RandIntRange(10000, 999999)
var v ess.ScalingGroup
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/ess_scaling_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ The following arguments are supported:
* `launch_template_version` - (Optional, Available since v1.159.0) The version number of the launch template. Valid values are the version number, `Latest`, or `Default`, Default value: `Default`.
* `group_type` - (Optional, ForceNew, Available since v1.164.0) Resource type within scaling group. Optional values: ECS, ECI. Default to ECS.
* `health_check_type` - (Optional, Available since v1.193.0) Resource type within scaling group. Optional values: ECS, NONE, LOAD_BALANCER. Default to ECS.
* `scaling_policy` - (Optional, Available since v1.226.0) The reclaim mode of the scaling group. Optional values: recycle, release, forceRecycle, forceRelease.
* `max_instance_lifetime` - (Optional, Available since v1.226.0) The maximum life span of an instance in the scaling group. Unit: seconds.
* `tags` - (Optional, Available since v1.160.0) A mapping of tags to assign to the resource.
- Key: It can be up to 64 characters in length. It cannot begin with "aliyun", "acs:", "http://", or "https://". It cannot be a null string.
- Value: It can be up to 128 characters in length. It cannot begin with "aliyun", "acs:", "http://", or "https://". It can be a null string.
Expand Down

0 comments on commit e2ba5b4

Please sign in to comment.