Skip to content

Commit b0f8cdd

Browse files
authored
Merge pull request #550 from ChuGuai/master
add tcp/udp listener target group attachment
2 parents 84003cf + d4c89c3 commit b0f8cdd

8 files changed

+221
-97
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: 17 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,13 @@ 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 || protocol == CLB_LISTENER_PROTOCOL_TCPSSL {
272+
targetType := CLB_TARGET_TYPE_NODE
273+
request.TargetType = &targetType
274+
}
259275

260276
if v, ok := d.GetOk("session_expire_time"); ok {
261277
if !(protocol == CLB_LISTENER_PROTOCOL_TCP || protocol == CLB_LISTENER_PROTOCOL_UDP) {
@@ -358,6 +374,7 @@ func resourceTencentCloudClbListenerRead(d *schema.ResourceData, meta interface{
358374
_ = d.Set("clb_id", clbId)
359375
_ = d.Set("listener_id", instance.ListenerId)
360376
_ = d.Set("listener_name", instance.ListenerName)
377+
_ = d.Set("target_type", instance.TargetType)
361378
_ = d.Set("port", instance.Port)
362379
_ = d.Set("protocol", instance.Protocol)
363380
_ = 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: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ resource "tencentcloud_clb_target_group_attachment" "group" {
3434
clb_id = tencentcloud_clb_instance.clb_basic.id
3535
listener_id = tencentcloud_clb_listener.listener_basic.id
3636
rule_id = tencentcloud_clb_listener_rule.rule_basic.id
37-
targrt_group_id = tencentcloud_clb_target_group.test.id
37+
target_group_id = tencentcloud_clb_target_group.test.id
3838
}
3939
```
4040
@@ -62,6 +62,7 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
6262
return &schema.Resource{
6363
Create: resourceTencentCloudClbTargetGroupAttachmentCreate,
6464
Read: resourceTencentCloudClbTargetGroupAttachmentRead,
65+
Update: resourceTencentCloudClbTargetGroupAttachmentUpdate,
6566
Delete: resourceTencentCloudClbTargetGroupAttachmentDelete,
6667
Importer: &schema.ResourceImporter{
6768
State: schema.ImportStatePassthrough,
@@ -80,40 +81,61 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
8081
Description: "ID of the CLB listener.",
8182
},
8283
"targrt_group_id": {
84+
Type: schema.TypeString,
85+
Optional: true,
86+
Description: "ID of the CLB target group.",
87+
Deprecated: "It has been deprecated from version 1.47.1. Use `target_group_id` instead.",
88+
},
89+
"target_group_id": {
8390
Type: schema.TypeString,
8491
ForceNew: true,
85-
Required: true,
92+
Optional: true,
8693
Description: "ID of the CLB target group.",
8794
},
8895
"rule_id": {
8996
Type: schema.TypeString,
90-
Required: true,
97+
Optional: true,
9198
ForceNew: true,
9299
Description: "ID of the CLB listener rule.",
93100
},
94101
},
95102
}
96103
}
97-
98104
func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
99105
defer logElapsed("resource.tencentcloud_clb_target_group_attachment.create")()
100106

101107
var (
102108
clbService = ClbService{
103109
client: meta.(*TencentCloudClient).apiV3Conn,
104110
}
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
111+
logId = getLogId(contextNil)
112+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
113+
locationId string
114+
listenerId = d.Get("listener_id").(string)
115+
clbId = d.Get("clb_id").(string)
116+
targetGroupId string
117+
118+
targetInfos []*clb.TargetGroupInfo
119+
instance *clb.LoadBalancer
120+
has bool
121+
err error
116122
)
123+
if v, ok := d.GetOk("rule_id"); ok {
124+
locationId = v.(string)
125+
}
126+
vTarget, eHas := d.GetOk("target_group_id")
127+
vTargrt, rHas := d.GetOk("targrt_group_id")
128+
129+
if eHas || rHas {
130+
if rHas {
131+
targetGroupId = vTargrt.(string)
132+
}
133+
if eHas {
134+
targetGroupId = vTarget.(string)
135+
}
136+
} else {
137+
return fmt.Errorf("'target_group_id' or 'targrt_group_id' at least set one, please use 'target_group_id'")
138+
}
117139

118140
//check listenerId
119141
checkErr := ListenerIdCheck(listenerId)
@@ -125,26 +147,6 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
125147
if checkErr != nil {
126148
return checkErr
127149
}
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-
}
148150

149151
//check target group
150152
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
@@ -158,7 +160,7 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
158160
return err
159161
}
160162
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
161-
targetInfos, err = clbService.DescribeTargetGroups(ctx, targrtGroupId, nil)
163+
targetInfos, err = clbService.DescribeTargetGroups(ctx, targetGroupId, nil)
162164
if err != nil {
163165
return retryError(err, InternalError)
164166
}
@@ -171,22 +173,22 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
171173
return fmt.Errorf("CLB instance needs to be in the same VPC as the backend target group")
172174
}
173175

174-
err = clbService.AssociateTargetGroups(ctx, listenerId, clbId, targrtGroupId, locationId)
176+
err = clbService.AssociateTargetGroups(ctx, listenerId, clbId, targetGroupId, locationId)
175177
if err != nil {
176178
return err
177179
}
178180

179181
// wait status
180-
has, err = clbService.DescribeAssociateTargetGroups(ctx, []string{targrtGroupId, listenerId, clbId, locationId})
182+
has, err = clbService.DescribeAssociateTargetGroups(ctx, []string{targetGroupId, listenerId, clbId, locationId})
181183
if err != nil {
182184
return err
183185
}
184186
if !has {
185-
return fmt.Errorf("AssociateTargetGroups faild, targrtGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s",
186-
targrtGroupId, listenerId, clbId, locationId)
187+
return fmt.Errorf("AssociateTargetGroups faild, targetGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s",
188+
targetGroupId, listenerId, clbId, locationId)
187189
}
188190

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

191193
return resourceTencentCloudClbTargetGroupAttachmentRead(d, meta)
192194
}
@@ -207,7 +209,7 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
207209

208210
ids := strings.Split(id, FILED_SP)
209211
if len(ids) != 4 {
210-
return fmt.Errorf("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id, rule_id")
212+
return fmt.Errorf("CLB target group attachment id is clb_id#listener_id#target_group_id#rule_id(only required for 7 layer CLB)")
211213
}
212214

213215
has, err := clbService.DescribeAssociateTargetGroups(ctx, ids)
@@ -219,14 +221,21 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
219221
return nil
220222
}
221223

222-
_ = d.Set("targrt_group_id", ids[0])
224+
_ = d.Set("target_group_id", ids[0])
223225
_ = d.Set("listener_id", ids[1])
224226
_ = d.Set("clb_id", ids[2])
225-
_ = d.Set("rule_id", ids[3])
227+
if ids[3] != "" {
228+
_ = d.Set("rule_id", ids[3])
229+
}
226230

227231
return nil
228232
}
229233

234+
func resourceTencentCloudClbTargetGroupAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
235+
defer logElapsed("resource.tencentcloud_clb_target_group_attachment.update")()
236+
return resourceTencentCloudClbTargetGroupAttachmentRead(d, meta)
237+
}
238+
230239
func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
231240
defer logElapsed("resource.tencentcloud_clb_target_group_attachment.delete")()
232241

@@ -243,7 +252,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
243252

244253
ids := strings.Split(id, FILED_SP)
245254
if len(ids) != 4 {
246-
return fmt.Errorf("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id, rule_id")
255+
return fmt.Errorf("CLB target group attachment id is clb_id#listener_id#target_group_id#rule_id(only required for 7 layer CLB)")
247256
}
248257

249258
if err := clbService.DisassociateTargetGroups(ctx, ids[0], ids[1], ids[2], ids[3]); err != nil {
@@ -264,12 +273,18 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
264273
if rule.LocationId != nil {
265274
originLocationId = *rule.LocationId
266275
}
267-
268-
if originListenerId == ids[1] && originClbId == ids[2] && originLocationId == ids[3] {
276+
if *rule.Protocol == CLB_LISTENER_PROTOCOL_TCP || *rule.Protocol == CLB_LISTENER_PROTOCOL_UDP || *rule.Protocol == CLB_LISTENER_PROTOCOL_TCPSSL {
277+
if originListenerId == ids[1] && originClbId == ids[2] {
278+
return resource.RetryableError(
279+
fmt.Errorf("rule association target group instance still exist. [targetGroupId=%s, listenerId=%s, cldId=%s]",
280+
ids[0], ids[1], ids[2]))
281+
}
282+
} else if originListenerId == ids[1] && originClbId == ids[2] && originLocationId == ids[3] {
269283
return resource.RetryableError(
270284
fmt.Errorf("rule association target group instance still exist. [targetGroupId=%s, listenerId=%s, cldId=%s, ruleId=%s]",
271285
ids[0], ids[1], ids[2], ids[3]))
272286
}
287+
273288
}
274289
}
275290
return nil

0 commit comments

Comments
 (0)