Skip to content

Commit

Permalink
data_source/alicloud_ess_scalinggroup: add attributes of stop_instanc…
Browse files Browse the repository at this point in the history
…e_timeout, desired_capacity, max_instance_lifetime, multi_az_policy, group_type, resource_group_id, spot_instance_remedy, spot_instance_pools, on_demand_percentage_above_base_capacity, on_demand_base_capacity, spot_allocation_strategy, allocation_strategy and az_balance.
  • Loading branch information
fuliu-zln committed Jan 7, 2025
1 parent 4ba86c6 commit 0b2cf05
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 61 deletions.
230 changes: 181 additions & 49 deletions alicloud/data_source_alicloud_ess_scalinggroups.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package alicloud

import (
"fmt"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"regexp"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ess"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -70,10 +72,26 @@ func dataSourceAlicloudEssScalingGroups() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"desired_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"max_instance_lifetime": {
Type: schema.TypeInt,
Computed: true,
},
"max_size": {
Type: schema.TypeInt,
Computed: true,
},
"stop_instance_timeout": {
Type: schema.TypeInt,
Computed: true,
},
"on_demand_base_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"cooldown_time": {
Type: schema.TypeInt,
Computed: true,
Expand All @@ -98,6 +116,10 @@ func dataSourceAlicloudEssScalingGroups() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"resource_group_id": {
Type: schema.TypeString,
Computed: true,
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -114,19 +136,51 @@ func dataSourceAlicloudEssScalingGroups() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"az_balance": {
Type: schema.TypeBool,
Optional: true,
},
"group_type": {
Type: schema.TypeString,
Computed: true,
},
"suspended_processes": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"multi_az_policy": {
Type: schema.TypeString,
Computed: true,
},
"spot_allocation_strategy": {
Type: schema.TypeString,
Computed: true,
},
"group_deletion_protection": {
Type: schema.TypeBool,
Computed: true,
},
"on_demand_percentage_above_base_capacity": {
Type: schema.TypeInt,
Computed: true,
},
"spot_instance_remedy": {
Type: schema.TypeBool,
Computed: true,
},
"allocation_strategy": {
Type: schema.TypeString,
Computed: true,
},
"modification_time": {
Type: schema.TypeString,
Computed: true,
},
"spot_instance_pools": {
Type: schema.TypeInt,
Computed: true,
},
"total_capacity": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -161,40 +215,51 @@ func dataSourceAlicloudEssScalingGroups() *schema.Resource {

func dataSourceAlicloudEssScalingGroupsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := ess.CreateDescribeScalingGroupsRequest()
request.RegionId = client.RegionId
request.PageSize = requests.NewInteger(PageSizeLarge)
request.PageNumber = requests.NewInteger(1)

var allScalingGroups []ess.ScalingGroup
var response map[string]interface{}
conn, err := client.NewEssClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"PageSize": requests.NewInteger(PageSizeLarge),
"PageNumber": requests.NewInteger(1),
"RegionId": client.RegionId,
}

var allScalingGroups []interface{}
for {
raw, err := client.WithEssClient(func(essClient *ess.Client) (interface{}, error) {
return essClient.DescribeScalingGroups(request)
})
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer("DescribeScalingGroups"), nil, StringPointer("POST"), StringPointer("2014-08-28"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ess_scalinggroups", request.GetActionName(), AlibabaCloudSdkGoERROR)
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ess_scalinggroups", "DescribeScalingGroups", AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response, _ := raw.(*ess.DescribeScalingGroupsResponse)
if len(response.ScalingGroups.ScalingGroup) < 1 {

v, err := jsonpath.Get("$.ScalingGroups.ScalingGroup", response)
if err != nil {
return WrapErrorf(err, FailedGetAttributeMsg, "$.ScalingGroups.ScalingGroup", response)
}

addDebug("DescribeScalingGroups", response, request, request)
if len(v.([]interface{})) < 1 {
break
}

allScalingGroups = append(allScalingGroups, response.ScalingGroups.ScalingGroup...)
allScalingGroups = append(allScalingGroups, v.([]interface{})...)

if len(response.ScalingGroups.ScalingGroup) < PageSizeLarge {
if len(v.([]interface{})) < PageSizeLarge {
break
}

if page, err := getNextpageNumber(request.PageNumber); err != nil {
if page, err := getNextpageNumber(requests.Integer(fmt.Sprint(request["PageNumber"]))); err != nil {
return WrapError(err)
} else {
request.PageNumber = page
request["PageNumber"] = page
}
}

var filteredScalingGroupsTemp = make([]ess.ScalingGroup, 0)
var filteredScalingGroupsTemp []interface{}

nameRegex, okNameRegex := d.GetOk("name_regex")
idsMap := make(map[string]string)
Expand All @@ -209,17 +274,19 @@ func dataSourceAlicloudEssScalingGroupsRead(d *schema.ResourceData, meta interfa
}
if okNameRegex || okIds {
for _, group := range allScalingGroups {
var object map[string]interface{}
object = group.(map[string]interface{})
if okNameRegex && nameRegex != "" {
r, err := regexp.Compile(nameRegex.(string))
if err != nil {
return WrapError(err)
}
if r != nil && !r.MatchString(group.ScalingGroupName) {
if r != nil && !r.MatchString(object["ScalingGroupName"].(string)) {
continue
}
}
if okIds && len(idsMap) > 0 {
if _, ok := idsMap[group.ScalingGroupId]; !ok {
if _, ok := idsMap[object["ScalingGroupId"].(string)]; !ok {
continue
}
}
Expand All @@ -231,40 +298,105 @@ func dataSourceAlicloudEssScalingGroupsRead(d *schema.ResourceData, meta interfa
return scalingGroupsDescriptionAttribute(d, filteredScalingGroupsTemp, meta, client)
}

func scalingGroupsDescriptionAttribute(d *schema.ResourceData, scalingGroups []ess.ScalingGroup, meta interface{}, client *connectivity.AliyunClient) error {
func scalingGroupsDescriptionAttribute(d *schema.ResourceData, scalingGroups []interface{}, meta interface{}, client *connectivity.AliyunClient) error {
var ids []string
var names []string
var s = make([]map[string]interface{}, 0)
essService := EssService{client}

for _, scalingGroup := range scalingGroups {
var object map[string]interface{}
object = scalingGroup.(map[string]interface{})
mapping := map[string]interface{}{
"id": scalingGroup.ScalingGroupId,
"name": scalingGroup.ScalingGroupName,
"active_scaling_configuration": scalingGroup.ActiveScalingConfigurationId,
"launch_template_id": scalingGroup.LaunchTemplateId,
"launch_template_version": scalingGroup.LaunchTemplateVersion,
"region_id": scalingGroup.RegionId,
"min_size": scalingGroup.MinSize,
"max_size": scalingGroup.MaxSize,
"cooldown_time": scalingGroup.DefaultCooldown,
"removal_policies": scalingGroup.RemovalPolicies.RemovalPolicy,
"load_balancer_ids": scalingGroup.LoadBalancerIds.LoadBalancerId,
"db_instance_ids": scalingGroup.DBInstanceIds.DBInstanceId,
"vswitch_ids": scalingGroup.VSwitchIds.VSwitchId,
"lifecycle_state": scalingGroup.LifecycleState,
"total_capacity": scalingGroup.TotalCapacity,
"active_capacity": scalingGroup.ActiveCapacity,
"pending_capacity": scalingGroup.PendingCapacity,
"removing_capacity": scalingGroup.RemovingCapacity,
"total_instance_count": scalingGroup.TotalInstanceCount,
"vpc_id": scalingGroup.VpcId,
"vswitch_id": scalingGroup.VSwitchId,
"health_check_type": scalingGroup.HealthCheckType,
"suspended_processes": scalingGroup.SuspendedProcesses.SuspendedProcess,
"group_deletion_protection": scalingGroup.GroupDeletionProtection,
"modification_time": scalingGroup.ModificationTime,
"creation_time": scalingGroup.CreationTime,
"id": object["ScalingGroupId"],
"name": object["ScalingGroupName"],
"active_scaling_configuration": object["ActiveScalingConfigurationId"],
"launch_template_id": object["LaunchTemplateId"],
"launch_template_version": object["LaunchTemplateVersion"],
"region_id": object["RegionId"],
"min_size": object["MinSize"],
"max_size": object["MaxSize"],
"group_type": object["GroupType"],
"cooldown_time": object["DefaultCooldown"],
"stop_instance_timeout": object["StopInstanceTimeout"],
"lifecycle_state": object["LifecycleState"],
"total_capacity": object["TotalCapacity"],
"active_capacity": object["ActiveCapacity"],
"pending_capacity": object["PendingCapacity"],
"removing_capacity": object["RemovingCapacity"],
"total_instance_count": object["TotalInstanceCount"],
"vpc_id": object["VpcId"],
"vswitch_id": object["VSwitchId"],
"health_check_type": object["HealthCheckType"],
"group_deletion_protection": object["GroupDeletionProtection"],
"spot_instance_remedy": object["SpotInstanceRemedy"],
"modification_time": object["ModificationTime"],
"creation_time": object["CreationTime"],
"multi_az_policy": object["MultiAZPolicy"],
"resource_group_id": object["ResourceGroupId"],
}
if object["DesiredCapacity"] != nil {
mapping["desired_capacity"] = object["DesiredCapacity"]
}
if object["AzBalance"] != nil {
mapping["az_balance"] = object["AzBalance"]
}
if object["SpotAllocationStrategy"] != nil {
mapping["spot_allocation_strategy"] = object["SpotAllocationStrategy"]
}
if object["AllocationStrategy"] != nil {
mapping["allocation_strategy"] = object["AllocationStrategy"]
}
if object["OnDemandBaseCapacity"] != nil {
mapping["on_demand_base_capacity"] = object["OnDemandBaseCapacity"]
}
if object["OnDemandPercentageAboveBaseCapacity"] != nil {
mapping["on_demand_percentage_above_base_capacity"] = object["OnDemandPercentageAboveBaseCapacity"]
}
if object["SpotInstancePools"] != nil {
mapping["spot_instance_pools"] = object["SpotInstancePools"]
}
if object["MaxInstanceLifetime"] != nil {
mapping["max_instance_lifetime"] = object["MaxInstanceLifetime"]
}
var dbIds []string
if len(object["DBInstanceIds"].(map[string]interface{})["DBInstanceId"].([]interface{})) > 0 {
for _, v := range object["DBInstanceIds"].(map[string]interface{})["DBInstanceId"].([]interface{}) {
dbIds = append(dbIds, v.(string))
}
mapping["db_instance_ids"] = dbIds
}

var slbIds []string
if len(object["LoadBalancerIds"].(map[string]interface{})["LoadBalancerId"].([]interface{})) > 0 {
for _, v := range object["LoadBalancerIds"].(map[string]interface{})["LoadBalancerId"].([]interface{}) {
slbIds = append(slbIds, v.(string))
}
mapping["loadbalancer_ids"] = slbIds
}

var polices []string
if len(object["RemovalPolicies"].(map[string]interface{})["RemovalPolicy"].([]interface{})) > 0 {
for _, v := range object["RemovalPolicies"].(map[string]interface{})["RemovalPolicy"].([]interface{}) {
polices = append(polices, v.(string))
}
mapping["removal_policies"] = polices
}

var vswitchIds []string
if object["VSwitchIds"] != nil && len(object["VSwitchIds"].(map[string]interface{})["VSwitchId"].([]interface{})) > 0 {
for _, v := range object["VSwitchIds"].(map[string]interface{})["VSwitchId"].([]interface{}) {
vswitchIds = append(vswitchIds, v.(string))
}
mapping["vswitch_ids"] = vswitchIds
}

var suspendedProcesses []string
if object["SuspendedProcesses"] != nil && len(object["SuspendedProcesses"].(map[string]interface{})["SuspendedProcess"].([]interface{})) > 0 {
for _, v := range object["SuspendedProcesses"].(map[string]interface{})["SuspendedProcess"].([]interface{}) {
suspendedProcesses = append(suspendedProcesses, v.(string))
}
mapping["suspended_processes"] = suspendedProcesses
}

listTagResourcesObject, err := essService.ListTagResources(d.Id(), client)
Expand All @@ -273,8 +405,8 @@ func scalingGroupsDescriptionAttribute(d *schema.ResourceData, scalingGroups []e
}
mapping["tags"] = tagsToMap(listTagResourcesObject)

ids = append(ids, scalingGroup.ScalingGroupId)
names = append(names, scalingGroup.ScalingGroupName)
ids = append(ids, object["ScalingGroupId"].(string))
names = append(names, object["ScalingGroupName"].(string))
s = append(s, mapping)
}

Expand Down
Loading

0 comments on commit 0b2cf05

Please sign in to comment.