Skip to content

Commit d4a56d5

Browse files
authored
fix(as): [122205033] add new resource (#3186)
* add * add * add * add
1 parent ca755c1 commit d4a56d5

8 files changed

+104
-109
lines changed

.changelog/3186.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_as_load_balancer
3+
```

tencentcloud/services/as/resource_tc_as_load_balancer.go

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,10 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
3030
Description: "ID of a scaling group.",
3131
},
3232

33-
"load_balancer_ids": {
34-
Optional: true,
35-
Type: schema.TypeSet,
36-
Elem: &schema.Schema{
37-
Type: schema.TypeString,
38-
},
39-
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.",
40-
},
41-
4233
"forward_load_balancers": {
4334
Optional: true,
4435
Type: schema.TypeList,
45-
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.",
36+
Description: "List of application load balancers. The maximum number of application-type load balancers bound to each scaling group is 100.",
4637
Elem: &schema.Resource{
4738
Schema: map[string]*schema.Schema{
4839
"load_balancer_id": {
@@ -82,6 +73,7 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
8273
"region": {
8374
Type: schema.TypeString,
8475
Optional: true,
76+
Computed: true,
8577
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.",
8678
},
8779
},
@@ -95,23 +87,15 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
9587
defer tccommon.LogElapsed("resource.tencentcloud_as_load_balancer.create")()
9688
defer tccommon.InconsistentCheck(d, meta)()
9789

98-
logId := tccommon.GetLogId(tccommon.ContextNil)
99-
10090
var (
91+
logId = tccommon.GetLogId(tccommon.ContextNil)
10192
request = as.NewAttachLoadBalancersRequest()
10293
autoScalingGroupId string
10394
)
95+
10496
if v, ok := d.GetOk("auto_scaling_group_id"); ok {
105-
autoScalingGroupId = v.(string)
10697
request.AutoScalingGroupId = helper.String(v.(string))
107-
}
108-
109-
if v, ok := d.GetOk("load_balancer_ids"); ok {
110-
loadBalancerIdsSet := v.(*schema.Set).List()
111-
for i := range loadBalancerIdsSet {
112-
loadBalancerIds := loadBalancerIdsSet[i].(string)
113-
request.LoadBalancerIds = append(request.LoadBalancerIds, &loadBalancerIds)
114-
}
98+
autoScalingGroupId = v.(string)
11599
}
116100

117101
if v, ok := d.GetOk("forward_load_balancers"); ok {
@@ -121,28 +105,35 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
121105
if v, ok := dMap["load_balancer_id"]; ok {
122106
forwardLoadBalancer.LoadBalancerId = helper.String(v.(string))
123107
}
108+
124109
if v, ok := dMap["listener_id"]; ok {
125110
forwardLoadBalancer.ListenerId = helper.String(v.(string))
126111
}
112+
127113
if v, ok := dMap["target_attributes"]; ok {
128114
for _, item := range v.([]interface{}) {
129115
targetAttributesMap := item.(map[string]interface{})
130116
targetAttribute := as.TargetAttribute{}
131117
if v, ok := targetAttributesMap["port"]; ok {
132118
targetAttribute.Port = helper.IntUint64(v.(int))
133119
}
120+
134121
if v, ok := targetAttributesMap["weight"]; ok {
135122
targetAttribute.Weight = helper.IntUint64(v.(int))
136123
}
124+
137125
forwardLoadBalancer.TargetAttributes = append(forwardLoadBalancer.TargetAttributes, &targetAttribute)
138126
}
139127
}
128+
140129
if v, ok := dMap["location_id"]; ok {
141130
forwardLoadBalancer.LocationId = helper.String(v.(string))
142131
}
132+
143133
if v, ok := dMap["region"]; ok {
144134
forwardLoadBalancer.Region = helper.String(v.(string))
145135
}
136+
146137
request.ForwardLoadBalancers = append(request.ForwardLoadBalancers, &forwardLoadBalancer)
147138
}
148139
}
@@ -154,8 +145,10 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
154145
} else {
155146
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
156147
}
148+
157149
return nil
158150
})
151+
159152
if err != nil {
160153
log.Printf("[CRITAL]%s create as loadBalancer failed, reason:%+v", logId, err)
161154
return err
@@ -170,13 +163,12 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
170163
defer tccommon.LogElapsed("resource.tencentcloud_as_load_balancer.read")()
171164
defer tccommon.InconsistentCheck(d, meta)()
172165

173-
logId := tccommon.GetLogId(tccommon.ContextNil)
174-
175-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
176-
177-
service := AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
178-
179-
autoScalingGroupId := d.Id()
166+
var (
167+
logId = tccommon.GetLogId(tccommon.ContextNil)
168+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
169+
service = AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
170+
autoScalingGroupId = d.Id()
171+
)
180172

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

196-
if loadBalancer.LoadBalancerIdSet != nil {
197-
_ = d.Set("load_balancer_ids", loadBalancer.LoadBalancerIdSet)
198-
}
199-
200188
if loadBalancer.ForwardLoadBalancerSet != nil {
201-
forwardLoadBalancersList := []interface{}{}
189+
forwardLoadBalancersList := make([]map[string]interface{}, 0, len(loadBalancer.ForwardLoadBalancerSet))
202190
for _, forwardLoadBalancers := range loadBalancer.ForwardLoadBalancerSet {
203191
forwardLoadBalancersMap := map[string]interface{}{}
204-
205192
if forwardLoadBalancers.LoadBalancerId != nil {
206193
forwardLoadBalancersMap["load_balancer_id"] = forwardLoadBalancers.LoadBalancerId
207194
}
@@ -211,10 +198,9 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
211198
}
212199

213200
if forwardLoadBalancers.TargetAttributes != nil {
214-
targetAttributesList := []interface{}{}
201+
targetAttributesList := make([]map[string]interface{}, 0, len(forwardLoadBalancers.TargetAttributes))
215202
for _, targetAttributes := range forwardLoadBalancers.TargetAttributes {
216203
targetAttributesMap := map[string]interface{}{}
217-
218204
if targetAttributes.Port != nil {
219205
targetAttributesMap["port"] = targetAttributes.Port
220206
}
@@ -226,7 +212,7 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
226212
targetAttributesList = append(targetAttributesList, targetAttributesMap)
227213
}
228214

229-
forwardLoadBalancersMap["target_attributes"] = []interface{}{targetAttributesList}
215+
forwardLoadBalancersMap["target_attributes"] = targetAttributesList
230216
}
231217

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

243229
_ = d.Set("forward_load_balancers", forwardLoadBalancersList)
244-
245230
}
246231

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

254-
logId := tccommon.GetLogId(tccommon.ContextNil)
255-
256-
request := as.NewModifyLoadBalancerTargetAttributesRequest()
257-
258-
autoScalingGroupId := d.Id()
259-
260-
request.AutoScalingGroupId = &autoScalingGroupId
261-
262-
immutableArgs := []string{"load_balancer_ids"}
239+
var (
240+
logId = tccommon.GetLogId(tccommon.ContextNil)
241+
request = as.NewModifyLoadBalancerTargetAttributesRequest()
242+
autoScalingGroupId = d.Id()
243+
)
263244

245+
immutableArgs := []string{"auto_scaling_group_id"}
264246
for _, v := range immutableArgs {
265247
if d.HasChange(v) {
266248
return fmt.Errorf("argument `%s` cannot be changed", v)
267249
}
268250
}
269251

270252
if d.HasChange("forward_load_balancers") {
253+
request.AutoScalingGroupId = &autoScalingGroupId
271254
if v, ok := d.GetOk("forward_load_balancers"); ok {
272255
for _, item := range v.([]interface{}) {
273256
forwardLoadBalancer := as.ForwardLoadBalancer{}
274257
dMap := item.(map[string]interface{})
275258
if v, ok := dMap["load_balancer_id"]; ok {
276259
forwardLoadBalancer.LoadBalancerId = helper.String(v.(string))
277260
}
261+
278262
if v, ok := dMap["listener_id"]; ok {
279263
forwardLoadBalancer.ListenerId = helper.String(v.(string))
280264
}
265+
281266
if v, ok := dMap["target_attributes"]; ok {
282267
for _, item := range v.([]interface{}) {
283268
targetAttributesMap := item.(map[string]interface{})
284269
targetAttribute := as.TargetAttribute{}
285270
if v, ok := targetAttributesMap["port"]; ok {
286271
targetAttribute.Port = helper.IntUint64(v.(int))
287272
}
273+
288274
if v, ok := targetAttributesMap["weight"]; ok {
289275
targetAttribute.Weight = helper.IntUint64(v.(int))
290276
}
277+
291278
forwardLoadBalancer.TargetAttributes = append(forwardLoadBalancer.TargetAttributes, &targetAttribute)
292279
}
293280
}
281+
294282
if v, ok := dMap["location_id"]; ok {
295283
forwardLoadBalancer.LocationId = helper.String(v.(string))
296284
}
285+
297286
if v, ok := dMap["region"]; ok {
298287
forwardLoadBalancer.Region = helper.String(v.(string))
299288
}
289+
300290
request.ForwardLoadBalancers = append(request.ForwardLoadBalancers, &forwardLoadBalancer)
301291
}
302292
}
303-
}
304293

305-
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
306-
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().ModifyLoadBalancerTargetAttributes(request)
307-
if e != nil {
308-
return tccommon.RetryError(e)
309-
} else {
310-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
294+
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
295+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().ModifyLoadBalancerTargetAttributes(request)
296+
if e != nil {
297+
return tccommon.RetryError(e)
298+
} else {
299+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
300+
}
301+
302+
return nil
303+
})
304+
305+
if err != nil {
306+
log.Printf("[CRITAL]%s update as loadBalancer failed, reason:%+v", logId, err)
307+
return err
311308
}
312-
return nil
313-
})
314-
if err != nil {
315-
log.Printf("[CRITAL]%s update as loadBalancer failed, reason:%+v", logId, err)
316-
return err
317309
}
318310

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

326-
logId := tccommon.GetLogId(tccommon.ContextNil)
327-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
328-
329-
service := AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
330-
autoScalingGroupId := d.Id()
318+
var (
319+
logId = tccommon.GetLogId(tccommon.ContextNil)
320+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
321+
service = AsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
322+
autoScalingGroupId = d.Id()
323+
)
331324

332325
if err := service.DeleteAsLoadBalancerById(ctx, autoScalingGroupId); err != nil {
333326
return err
Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
Provides a resource to create a as load_balancer
1+
Provides a resource to create a as load balancer
22

3-
~> **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.
4-
~> **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.
3+
~> **NOTE:** This resource must exclusive in one auto scaling group, do not declare additional rule resources of this auto scaling group elsewhere.
54

6-
Example Usage
5+
~> **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
76

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

10-
```hcl
11-
data "tencentcloud_availability_zones_by_product" "zones" {
12-
product = "as"
13-
}
9+
Example Usage
1410

15-
data "tencentcloud_images" "image" {
16-
image_type = ["PUBLIC_IMAGE"]
17-
os_name = "TencentOS Server 3.2 (Final)"
11+
```hcl
12+
variable "availability_zone" {
13+
default = "ap-guangzhou-6"
1814
}
1915
16+
// create vpc
2017
resource "tencentcloud_vpc" "vpc" {
21-
name = "vpc-example"
2218
cidr_block = "10.0.0.0/16"
19+
name = "vpc"
2320
}
2421
22+
// create subnet
2523
resource "tencentcloud_subnet" "subnet" {
2624
vpc_id = tencentcloud_vpc.vpc.id
27-
name = "subnet-example"
28-
cidr_block = "10.0.0.0/16"
29-
availability_zone = data.tencentcloud_availability_zones_by_product.zones.zones.0.name
25+
availability_zone = var.availability_zone
26+
name = "subnet"
27+
cidr_block = "10.0.1.0/24"
28+
is_multicast = false
3029
}
3130
31+
3232
resource "tencentcloud_as_scaling_config" "example" {
3333
configuration_name = "tf-example"
34-
image_id = data.tencentcloud_images.image.images.0.image_id
35-
instance_types = ["SA1.SMALL1", "SA2.SMALL1", "SA2.SMALL2", "SA2.SMALL4"]
34+
image_id = "img-eb30mz89"
35+
instance_types = ["S6.MEDIUM4"]
3636
instance_name_settings {
37-
instance_name = "test-ins-name"
37+
instance_name = "demo-ins-name"
3838
}
3939
}
4040
@@ -49,19 +49,17 @@ resource "tencentcloud_as_scaling_group" "example" {
4949
5050
resource "tencentcloud_clb_instance" "example" {
5151
network_type = "INTERNAL"
52-
clb_name = "clb-example"
53-
project_id = 0
52+
clb_name = "tf-example"
5453
vpc_id = tencentcloud_vpc.vpc.id
5554
subnet_id = tencentcloud_subnet.subnet.id
56-
5755
tags = {
58-
test = "tf"
56+
createBy = "Terraform"
5957
}
6058
}
6159
6260
resource "tencentcloud_clb_listener" "example" {
6361
clb_id = tencentcloud_clb_instance.example.id
64-
listener_name = "listener-example"
62+
listener_name = "tf-example"
6563
port = 80
6664
protocol = "HTTP"
6765
}
@@ -73,15 +71,6 @@ resource "tencentcloud_clb_listener_rule" "example" {
7371
url = "/bar"
7472
}
7573
76-
resource "tencentcloud_as_load_balancer" "example" {
77-
auto_scaling_group_id = tencentcloud_as_scaling_group.example.id
78-
load_balancer_ids = [tencentcloud_clb_instance.example.id]
79-
}
80-
```
81-
82-
If use `forward_load_balancers`
83-
84-
```hcl
8574
resource "tencentcloud_as_load_balancer" "example" {
8675
auto_scaling_group_id = tencentcloud_as_scaling_group.example.id
8776
@@ -100,8 +89,8 @@ resource "tencentcloud_as_load_balancer" "example" {
10089

10190
Import
10291

103-
as load_balancer can be imported using the id, e.g.
92+
as load balancer can be imported using the id, e.g.
10493

10594
```
106-
terraform import tencentcloud_as_load_balancer.load_balancer auto_scaling_group_id
95+
terraform import tencentcloud_as_load_balancer.example asg-bpp4uol2
10796
```

0 commit comments

Comments
 (0)