Skip to content

fix(as): [122205033] add new resource #3186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3186.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
tencentcloud_as_load_balancer
```
121 changes: 57 additions & 64 deletions tencentcloud/services/as/resource_tc_as_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,10 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
Description: "ID of a scaling group.",
},

"load_balancer_ids": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of traditional load balancer IDs. The maximum number of traditional load balancers bound to each scaling group is 20. Both LoadBalancerIds and ForwardLoadBalancers can specify at most one at the same time.",
},

"forward_load_balancers": {
Optional: true,
Type: schema.TypeList,
Description: "List of application load balancers. The maximum number of application-type load balancers bound to each scaling group is 100. Both LoadBalancerIds and ForwardLoadBalancers can specify at most one at the same time.",
Description: "List of application load balancers. The maximum number of application-type load balancers bound to each scaling group is 100.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"load_balancer_id": {
Expand Down Expand Up @@ -82,6 +73,7 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Load balancer instance region. Default value is the region of current auto scaling group. The format is the same as the public parameter Region, for example: ap-guangzhou.",
},
},
Expand All @@ -95,23 +87,15 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
defer tccommon.LogElapsed("resource.tencentcloud_as_load_balancer.create")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = as.NewAttachLoadBalancersRequest()
autoScalingGroupId string
)

if v, ok := d.GetOk("auto_scaling_group_id"); ok {
autoScalingGroupId = v.(string)
request.AutoScalingGroupId = helper.String(v.(string))
}

if v, ok := d.GetOk("load_balancer_ids"); ok {
loadBalancerIdsSet := v.(*schema.Set).List()
for i := range loadBalancerIdsSet {
loadBalancerIds := loadBalancerIdsSet[i].(string)
request.LoadBalancerIds = append(request.LoadBalancerIds, &loadBalancerIds)
}
autoScalingGroupId = v.(string)
}

if v, ok := d.GetOk("forward_load_balancers"); ok {
Expand All @@ -121,28 +105,35 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
if v, ok := dMap["load_balancer_id"]; ok {
forwardLoadBalancer.LoadBalancerId = helper.String(v.(string))
}

if v, ok := dMap["listener_id"]; ok {
forwardLoadBalancer.ListenerId = helper.String(v.(string))
}

if v, ok := dMap["target_attributes"]; ok {
for _, item := range v.([]interface{}) {
targetAttributesMap := item.(map[string]interface{})
targetAttribute := as.TargetAttribute{}
if v, ok := targetAttributesMap["port"]; ok {
targetAttribute.Port = helper.IntUint64(v.(int))
}

if v, ok := targetAttributesMap["weight"]; ok {
targetAttribute.Weight = helper.IntUint64(v.(int))
}

forwardLoadBalancer.TargetAttributes = append(forwardLoadBalancer.TargetAttributes, &targetAttribute)
}
}

if v, ok := dMap["location_id"]; ok {
forwardLoadBalancer.LocationId = helper.String(v.(string))
}

if v, ok := dMap["region"]; ok {
forwardLoadBalancer.Region = helper.String(v.(string))
}

request.ForwardLoadBalancers = append(request.ForwardLoadBalancers, &forwardLoadBalancer)
}
}
Expand All @@ -154,8 +145,10 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}

return nil
})

if err != nil {
log.Printf("[CRITAL]%s create as loadBalancer failed, reason:%+v", logId, err)
return err
Expand All @@ -170,13 +163,12 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
defer tccommon.LogElapsed("resource.tencentcloud_as_load_balancer.read")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)

service := AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}

autoScalingGroupId := d.Id()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
autoScalingGroupId = d.Id()
)

loadBalancer, err := service.DescribeAsLoadBalancerById(ctx, autoScalingGroupId)
if err != nil {
Expand All @@ -193,15 +185,10 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
_ = d.Set("auto_scaling_group_id", loadBalancer.AutoScalingGroupId)
}

if loadBalancer.LoadBalancerIdSet != nil {
_ = d.Set("load_balancer_ids", loadBalancer.LoadBalancerIdSet)
}

if loadBalancer.ForwardLoadBalancerSet != nil {
forwardLoadBalancersList := []interface{}{}
forwardLoadBalancersList := make([]map[string]interface{}, 0, len(loadBalancer.ForwardLoadBalancerSet))
for _, forwardLoadBalancers := range loadBalancer.ForwardLoadBalancerSet {
forwardLoadBalancersMap := map[string]interface{}{}

if forwardLoadBalancers.LoadBalancerId != nil {
forwardLoadBalancersMap["load_balancer_id"] = forwardLoadBalancers.LoadBalancerId
}
Expand All @@ -211,10 +198,9 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
}

if forwardLoadBalancers.TargetAttributes != nil {
targetAttributesList := []interface{}{}
targetAttributesList := make([]map[string]interface{}, 0, len(forwardLoadBalancers.TargetAttributes))
for _, targetAttributes := range forwardLoadBalancers.TargetAttributes {
targetAttributesMap := map[string]interface{}{}

if targetAttributes.Port != nil {
targetAttributesMap["port"] = targetAttributes.Port
}
Expand All @@ -226,7 +212,7 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
targetAttributesList = append(targetAttributesList, targetAttributesMap)
}

forwardLoadBalancersMap["target_attributes"] = []interface{}{targetAttributesList}
forwardLoadBalancersMap["target_attributes"] = targetAttributesList
}

if forwardLoadBalancers.LocationId != nil {
Expand All @@ -241,7 +227,6 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
}

_ = d.Set("forward_load_balancers", forwardLoadBalancersList)

}

return nil
Expand All @@ -251,69 +236,76 @@ func resourceTencentCloudAsLoadBalancerUpdate(d *schema.ResourceData, meta inter
defer tccommon.LogElapsed("resource.tencentcloud_as_load_balancer.update")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)

request := as.NewModifyLoadBalancerTargetAttributesRequest()

autoScalingGroupId := d.Id()

request.AutoScalingGroupId = &autoScalingGroupId

immutableArgs := []string{"load_balancer_ids"}
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = as.NewModifyLoadBalancerTargetAttributesRequest()
autoScalingGroupId = d.Id()
)

immutableArgs := []string{"auto_scaling_group_id"}
for _, v := range immutableArgs {
if d.HasChange(v) {
return fmt.Errorf("argument `%s` cannot be changed", v)
}
}

if d.HasChange("forward_load_balancers") {
request.AutoScalingGroupId = &autoScalingGroupId
if v, ok := d.GetOk("forward_load_balancers"); ok {
for _, item := range v.([]interface{}) {
forwardLoadBalancer := as.ForwardLoadBalancer{}
dMap := item.(map[string]interface{})
if v, ok := dMap["load_balancer_id"]; ok {
forwardLoadBalancer.LoadBalancerId = helper.String(v.(string))
}

if v, ok := dMap["listener_id"]; ok {
forwardLoadBalancer.ListenerId = helper.String(v.(string))
}

if v, ok := dMap["target_attributes"]; ok {
for _, item := range v.([]interface{}) {
targetAttributesMap := item.(map[string]interface{})
targetAttribute := as.TargetAttribute{}
if v, ok := targetAttributesMap["port"]; ok {
targetAttribute.Port = helper.IntUint64(v.(int))
}

if v, ok := targetAttributesMap["weight"]; ok {
targetAttribute.Weight = helper.IntUint64(v.(int))
}

forwardLoadBalancer.TargetAttributes = append(forwardLoadBalancer.TargetAttributes, &targetAttribute)
}
}

if v, ok := dMap["location_id"]; ok {
forwardLoadBalancer.LocationId = helper.String(v.(string))
}

if v, ok := dMap["region"]; ok {
forwardLoadBalancer.Region = helper.String(v.(string))
}

request.ForwardLoadBalancers = append(request.ForwardLoadBalancers, &forwardLoadBalancer)
}
}
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().ModifyLoadBalancerTargetAttributes(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().ModifyLoadBalancerTargetAttributes(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}

return nil
})

if err != nil {
log.Printf("[CRITAL]%s update as loadBalancer failed, reason:%+v", logId, err)
return err
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s update as loadBalancer failed, reason:%+v", logId, err)
return err
}

return resourceTencentCloudAsLoadBalancerRead(d, meta)
Expand All @@ -323,11 +315,12 @@ func resourceTencentCloudAsLoadBalancerDelete(d *schema.ResourceData, meta inter
defer tccommon.LogElapsed("resource.tencentcloud_as_load_balancer.delete")()
defer tccommon.InconsistentCheck(d, meta)()

logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)

service := AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
autoScalingGroupId := d.Id()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
autoScalingGroupId = d.Id()
)

if err := service.DeleteAsLoadBalancerById(ctx, autoScalingGroupId); err != nil {
return err
Expand Down
59 changes: 24 additions & 35 deletions tencentcloud/services/as/resource_tc_as_load_balancer.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
Provides a resource to create a as load_balancer
Provides a resource to create a as load balancer

~> **NOTE:** `load_balancer_ids` A list of traditional load balancer IDs, with a maximum of 20 traditional load balancers bound to each scaling group. Only one LoadBalancerIds and ForwardLoadBalancers can be specified simultaneously.
~> **NOTE:** `forward_load_balancers` List of application type load balancers, with a maximum of 100 bound application type load balancers for each scaling group. Only one LoadBalancerIds and ForwardLoadBalancers can be specified simultaneously.
~> **NOTE:** This resource must exclusive in one auto scaling group, do not declare additional rule resources of this auto scaling group elsewhere.

Example Usage
~> **NOTE:** If the `auto_scaling_group_id` field of this resource comes from the `tencentcloud_as_scaling_group` resource, then the `forward_balancer_ids` field of the `tencentcloud_as_scaling_group` resource cannot be set simultaneously with this resource, which may result in conflicts

If use `load_balancer_ids`
~> **NOTE:** `forward_load_balancers` List of application type load balancers, with a maximum of 100 bound application type load balancers for each scaling group.

```hcl
data "tencentcloud_availability_zones_by_product" "zones" {
product = "as"
}
Example Usage

data "tencentcloud_images" "image" {
image_type = ["PUBLIC_IMAGE"]
os_name = "TencentOS Server 3.2 (Final)"
```hcl
variable "availability_zone" {
default = "ap-guangzhou-6"
}

// create vpc
resource "tencentcloud_vpc" "vpc" {
name = "vpc-example"
cidr_block = "10.0.0.0/16"
name = "vpc"
}

// create subnet
resource "tencentcloud_subnet" "subnet" {
vpc_id = tencentcloud_vpc.vpc.id
name = "subnet-example"
cidr_block = "10.0.0.0/16"
availability_zone = data.tencentcloud_availability_zones_by_product.zones.zones.0.name
availability_zone = var.availability_zone
name = "subnet"
cidr_block = "10.0.1.0/24"
is_multicast = false
}


resource "tencentcloud_as_scaling_config" "example" {
configuration_name = "tf-example"
image_id = data.tencentcloud_images.image.images.0.image_id
instance_types = ["SA1.SMALL1", "SA2.SMALL1", "SA2.SMALL2", "SA2.SMALL4"]
image_id = "img-eb30mz89"
instance_types = ["S6.MEDIUM4"]
instance_name_settings {
instance_name = "test-ins-name"
instance_name = "demo-ins-name"
}
}

Expand All @@ -49,19 +49,17 @@ resource "tencentcloud_as_scaling_group" "example" {

resource "tencentcloud_clb_instance" "example" {
network_type = "INTERNAL"
clb_name = "clb-example"
project_id = 0
clb_name = "tf-example"
vpc_id = tencentcloud_vpc.vpc.id
subnet_id = tencentcloud_subnet.subnet.id

tags = {
test = "tf"
createBy = "Terraform"
}
}

resource "tencentcloud_clb_listener" "example" {
clb_id = tencentcloud_clb_instance.example.id
listener_name = "listener-example"
listener_name = "tf-example"
port = 80
protocol = "HTTP"
}
Expand All @@ -73,15 +71,6 @@ resource "tencentcloud_clb_listener_rule" "example" {
url = "/bar"
}

resource "tencentcloud_as_load_balancer" "example" {
auto_scaling_group_id = tencentcloud_as_scaling_group.example.id
load_balancer_ids = [tencentcloud_clb_instance.example.id]
}
```

If use `forward_load_balancers`

```hcl
resource "tencentcloud_as_load_balancer" "example" {
auto_scaling_group_id = tencentcloud_as_scaling_group.example.id

Expand All @@ -100,8 +89,8 @@ resource "tencentcloud_as_load_balancer" "example" {

Import

as load_balancer can be imported using the id, e.g.
as load balancer can be imported using the id, e.g.

```
terraform import tencentcloud_as_load_balancer.load_balancer auto_scaling_group_id
terraform import tencentcloud_as_load_balancer.example asg-bpp4uol2
```
Loading
Loading