Skip to content

Commit e030927

Browse files
author
“guojunchu”
committed
add tcp/udp listener target group attachment
1 parent eb56eef commit e030927

8 files changed

+136
-96
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
## 1.47.1 (Unreleased)
2+
3+
ENHANCEMENTS:
4+
* Resource: `tencentcloud_clb_listener` add computed argument `target_type`.
5+
6+
DEPRECATED:
7+
* Resource: `tencentcloud_clb_target_group_attachment`: optional argument `targrt_group_id` is no longer supported, replace by `target_group_id`.
8+
29
## 1.47.0 (November 13, 2020)
310

411
ENHANCEMENTS:

tencentcloud/resource_tc_clb_listener.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ resource "tencentcloud_clb_listener" "TCP_listener" {
2929
health_check_unhealth_num = 3
3030
session_expire_time = 30
3131
scheduler = "WRR"
32+
target_type = "TARGETGROUP"
3233
}
3334
```
3435
@@ -64,6 +65,7 @@ resource "tencentcloud_clb_listener" "TCPSSL_listener" {
6465
health_check_health_num = 3
6566
health_check_unhealth_num = 3
6667
scheduler = "WRR"
68+
target_type = "TARGETGROUP"
6769
}
6870
```
6971
Import
@@ -196,6 +198,13 @@ func resourceTencentCloudClbListener() *schema.Resource {
196198
Optional: true,
197199
Description: "Indicates whether SNI is enabled, and only supported with protocol 'HTTPS'. If enabled, you can set a certificate for each rule in `tencentcloud_clb_listener_rule`, otherwise all rules have a certificate.",
198200
},
201+
"target_type": {
202+
Type: schema.TypeString,
203+
Optional: true,
204+
ForceNew: true,
205+
ValidateFunc: validateAllowedStringValue([]string{CLB_TARGET_TYPE_NODE, CLB_TARGET_TYPE_TARGETGROUP}),
206+
Description: "Backend target type. Valid values: `NODE`, `TARGETGROUP`. `NODE` means to bind ordinary nodes, `TARGETGROUP` means to bind target group. NOTES: TCP/UDP/TCP_SSL listener must configuration, HTTP/HTTPS listener needs to be configured in tencentcloud_clb_listener_rule.",
207+
},
199208
//computed
200209
"listener_id": {
201210
Type: schema.TypeString,
@@ -256,6 +265,12 @@ func resourceTencentCloudClbListenerCreate(d *schema.ResourceData, meta interfac
256265
scheduler = v.(string)
257266
request.Scheduler = helper.String(scheduler)
258267
}
268+
if v, ok := d.GetOk("target_type"); ok {
269+
targetType := v.(string)
270+
request.TargetType = &targetType
271+
} else if protocol == CLB_LISTENER_PROTOCOL_TCP || protocol == CLB_LISTENER_PROTOCOL_UDP {
272+
return fmt.Errorf("[TECENT_TERRAFORM_CHECK][CLB listener][Create] check: protocol TCP/UDP must set target_type")
273+
}
259274

260275
if v, ok := d.GetOk("session_expire_time"); ok {
261276
if !(protocol == CLB_LISTENER_PROTOCOL_TCP || protocol == CLB_LISTENER_PROTOCOL_UDP) {
@@ -358,6 +373,7 @@ func resourceTencentCloudClbListenerRead(d *schema.ResourceData, meta interface{
358373
_ = d.Set("clb_id", clbId)
359374
_ = d.Set("listener_id", instance.ListenerId)
360375
_ = d.Set("listener_name", instance.ListenerName)
376+
_ = d.Set("target_type", instance.TargetType)
361377
_ = d.Set("port", instance.Port)
362378
_ = d.Set("protocol", instance.Protocol)
363379
_ = d.Set("session_expire_time", instance.SessionExpireTime)

tencentcloud/resource_tc_clb_listener_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ resource "tencentcloud_clb_listener" "listener_basic" {
263263
listener_name = "listener_basic"
264264
session_expire_time = 30
265265
scheduler = "WRR"
266+
target_type = "TARGETGROUP"
266267
}
267268
`
268269

@@ -284,6 +285,7 @@ resource "tencentcloud_clb_listener" "listener_tcp" {
284285
health_check_unhealth_num = 2
285286
session_expire_time = 30
286287
scheduler = "WRR"
288+
target_type = "TARGETGROUP"
287289
}
288290
`
289291

@@ -305,6 +307,7 @@ resource "tencentcloud_clb_listener" "listener_tcp"{
305307
health_check_unhealth_num = 3
306308
session_expire_time = 60
307309
scheduler = "WRR"
310+
target_type = "TARGETGROUP"
308311
}
309312
`
310313

@@ -327,6 +330,7 @@ resource "tencentcloud_clb_listener" "listener_tcpssl" {
327330
health_check_health_num = 2
328331
health_check_unhealth_num = 2
329332
scheduler = "WRR"
333+
target_type = "TARGETGROUP"
330334
}
331335
`
332336
const testAccClbListener_tcpssl_update = `
@@ -348,6 +352,7 @@ resource "tencentcloud_clb_listener" "listener_tcpssl"{
348352
health_check_health_num = 3
349353
health_check_unhealth_num = 3
350354
scheduler = "WRR"
355+
target_type = "TARGETGROUP"
351356
}
352357
`
353358
const testAccClbListener_https = `

tencentcloud/resource_tc_clb_target_group_attachment.go

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
Provides a resource to create a CLB target group attachment is bound to the load balancing listener or forwarding rule.
33
4+
~> **NOTE:** Required argument `targrt_group_id` is no longer supported, replace by `target_group_id`.
5+
46
Example Usage
57
68
```hcl
@@ -34,7 +36,7 @@ resource "tencentcloud_clb_target_group_attachment" "group" {
3436
clb_id = tencentcloud_clb_instance.clb_basic.id
3537
listener_id = tencentcloud_clb_listener.listener_basic.id
3638
rule_id = tencentcloud_clb_listener_rule.rule_basic.id
37-
targrt_group_id = tencentcloud_clb_target_group.test.id
39+
target_group_id = tencentcloud_clb_target_group.test.id
3840
}
3941
```
4042
@@ -52,6 +54,7 @@ import (
5254
"context"
5355
"fmt"
5456
"strings"
57+
"time"
5558

5659
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
5760
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -80,41 +83,50 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
8083
Description: "ID of the CLB listener.",
8184
},
8285
"targrt_group_id": {
86+
Type: schema.TypeString,
87+
ForceNew: true,
88+
Optional: true,
89+
Description: "ID of the CLB target group.",
90+
Deprecated: "It has been deprecated from version 1.47.1. Use `target_group_id` instead.",
91+
},
92+
"target_group_id": {
8393
Type: schema.TypeString,
8494
ForceNew: true,
8595
Required: true,
8696
Description: "ID of the CLB target group.",
8797
},
8898
"rule_id": {
8999
Type: schema.TypeString,
90-
Required: true,
100+
Optional: true,
91101
ForceNew: true,
92102
Description: "ID of the CLB listener rule.",
93103
},
94104
},
95105
}
96106
}
97-
98107
func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
99108
defer logElapsed("resource.tencentcloud_clb_target_group_attachment.create")()
100109

101110
var (
102111
clbService = ClbService{
103112
client: meta.(*TencentCloudClient).apiV3Conn,
104113
}
105-
logId = getLogId(contextNil)
106-
ctx = context.WithValue(context.TODO(), logIdKey, logId)
107-
listenerId = d.Get("listener_id").(string)
108-
clbId = d.Get("clb_id").(string)
109-
targrtGroupId = d.Get("targrt_group_id").(string)
110-
locationId = d.Get("rule_id").(string)
111-
targetInfos []*clb.TargetGroupInfo
112-
instance *clb.LoadBalancer
113-
listener *clb.Listener
114-
isRuleExist, has bool
115-
err error
116-
)
114+
logId = getLogId(contextNil)
115+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
116+
locationId string
117+
listenerId = d.Get("listener_id").(string)
118+
clbId = d.Get("clb_id").(string)
119+
targetGroupId = d.Get("target_group_id").(string)
117120

121+
targetInfos []*clb.TargetGroupInfo
122+
instance *clb.LoadBalancer
123+
//listener *clb.Listener
124+
has bool
125+
err error
126+
)
127+
if v, ok := d.GetOk("rule_id"); ok {
128+
locationId = v.(string)
129+
}
118130
//check listenerId
119131
checkErr := ListenerIdCheck(listenerId)
120132
if checkErr != nil {
@@ -125,26 +137,6 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
125137
if checkErr != nil {
126138
return checkErr
127139
}
128-
//check rule
129-
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
130-
listener, err = clbService.DescribeListenerById(ctx, listenerId, clbId)
131-
if err != nil {
132-
return retryError(err, InternalError)
133-
}
134-
return nil
135-
})
136-
if err != nil {
137-
return err
138-
}
139-
for _, rule := range listener.Rules {
140-
if locationId == *rule.LocationId && (rule.TargetType != nil && *rule.TargetType == CLB_TARGET_TYPE_TARGETGROUP) {
141-
isRuleExist = true
142-
break
143-
}
144-
}
145-
if !isRuleExist {
146-
return fmt.Errorf("rule bound to the listener of the CLB instance does not exist or the rule not in targetgroup mode")
147-
}
148140

149141
//check target group
150142
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
@@ -158,7 +150,7 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
158150
return err
159151
}
160152
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
161-
targetInfos, err = clbService.DescribeTargetGroups(ctx, targrtGroupId, nil)
153+
targetInfos, err = clbService.DescribeTargetGroups(ctx, targetGroupId, nil)
162154
if err != nil {
163155
return retryError(err, InternalError)
164156
}
@@ -171,22 +163,22 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
171163
return fmt.Errorf("CLB instance needs to be in the same VPC as the backend target group")
172164
}
173165

174-
err = clbService.AssociateTargetGroups(ctx, listenerId, clbId, targrtGroupId, locationId)
166+
err = clbService.AssociateTargetGroups(ctx, listenerId, clbId, targetGroupId, locationId)
175167
if err != nil {
176168
return err
177169
}
178170

179171
// wait status
180-
has, err = clbService.DescribeAssociateTargetGroups(ctx, []string{targrtGroupId, listenerId, clbId, locationId})
172+
has, err = clbService.DescribeAssociateTargetGroups(ctx, []string{targetGroupId, listenerId, clbId, locationId})
181173
if err != nil {
182174
return err
183175
}
184176
if !has {
185-
return fmt.Errorf("AssociateTargetGroups faild, targrtGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s",
186-
targrtGroupId, listenerId, clbId, locationId)
177+
return fmt.Errorf("AssociateTargetGroups faild, targetGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s",
178+
targetGroupId, listenerId, clbId, locationId)
187179
}
188180

189-
d.SetId(strings.Join([]string{targrtGroupId, listenerId, clbId, locationId}, FILED_SP))
181+
d.SetId(strings.Join([]string{targetGroupId, listenerId, clbId, locationId}, FILED_SP))
190182

191183
return resourceTencentCloudClbTargetGroupAttachmentRead(d, meta)
192184
}
@@ -207,7 +199,7 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
207199

208200
ids := strings.Split(id, FILED_SP)
209201
if len(ids) != 4 {
210-
return fmt.Errorf("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id, rule_id")
202+
return fmt.Errorf("CLB target group attachment id must contains clb_id, listernrt_id, target_group_id, rule_id")
211203
}
212204

213205
has, err := clbService.DescribeAssociateTargetGroups(ctx, ids)
@@ -219,10 +211,12 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
219211
return nil
220212
}
221213

222-
_ = d.Set("targrt_group_id", ids[0])
214+
_ = d.Set("target_group_id", ids[0])
223215
_ = d.Set("listener_id", ids[1])
224216
_ = d.Set("clb_id", ids[2])
225-
_ = d.Set("rule_id", ids[3])
217+
if ids[3] != "" {
218+
_ = d.Set("rule_id", ids[3])
219+
}
226220

227221
return nil
228222
}
@@ -243,12 +237,13 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
243237

244238
ids := strings.Split(id, FILED_SP)
245239
if len(ids) != 4 {
246-
return fmt.Errorf("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id, rule_id")
240+
return fmt.Errorf("CLB target group attachment id must contains clb_id, listernrt_id, target_group_id, rule_id")
247241
}
248242

249243
if err := clbService.DisassociateTargetGroups(ctx, ids[0], ids[1], ids[2], ids[3]); err != nil {
250244
return err
251245
}
246+
time.Sleep(10 * time.Second)
252247

253248
// check status
254249
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
@@ -265,7 +260,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
265260
originLocationId = *rule.LocationId
266261
}
267262

268-
if originListenerId == ids[1] && originClbId == ids[2] && originLocationId == ids[3] {
263+
if originListenerId == ids[1] && originClbId == ids[2] || originLocationId == ids[3] {
269264
return resource.RetryableError(
270265
fmt.Errorf("rule association target group instance still exist. [targetGroupId=%s, listenerId=%s, cldId=%s, ruleId=%s]",
271266
ids[0], ids[1], ids[2], ids[3]))

0 commit comments

Comments
 (0)