Skip to content

fix(clb): [125668619] tencentcloud_clb_attachment optmize targets code logic #3437

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 2 commits into from
Jul 10, 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/3437.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_clb_attachment: optmize `targets` code logic
```
15 changes: 10 additions & 5 deletions tencentcloud/services/clb/extension_clb.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,16 @@ const (
)

const (
CLB_BACKEND_TYPE_CVM = "CVM"
CLB_BACKEND_TYPE_ENI = "ENI"
CLB_BACKEND_TYPE_NAT = "NAT"
CLB_BACKEND_TYPE_CCN = "CCN"
CLB_BACKEND_TYPE_SRV = "SRV"
CLB_BACKEND_TYPE_CVM = "CVM"
CLB_BACKEND_TYPE_ENI = "ENI"
CLB_BACKEND_TYPE_NAT = "NAT"
CLB_BACKEND_TYPE_CCN = "CCN"
CLB_BACKEND_TYPE_SRV = "SRV"
CLB_BACKEND_TYPE_MS = "MANAGED_SERVICE"
CLB_BACKEND_TYPE_EVM = "EVM"
CLB_BACKEND_TYPE_GR = "GLOBALROUTE"
CLB_BACKEND_TYPE_IPDC = "IPDC"
CLB_BACKEND_TYPE_PVGW = "PVGW"
)

const (
Expand Down
14 changes: 11 additions & 3 deletions tencentcloud/services/clb/resource_tc_clb_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func resourceTencentCloudClbServerAttachmentCreate(d *schema.ResourceData, meta
} else if domain != "" && url != "" {
id = fmt.Sprintf("%s,%s#%v#%v", domain, url, d.Get("listener_id"), d.Get("clb_id"))
} else {
// only api support for now
id = fmt.Sprintf("%s#%v#%v", "", d.Get("listener_id"), d.Get("clb_id"))
}

Expand Down Expand Up @@ -265,18 +266,22 @@ func resourceTencentCloudClbServerAttachmentRead(d *schema.ResourceData, meta in
_ = d.Set("listener_id", listenerId)
_ = d.Set("protocol_type", instance.Protocol)

if locationId != "" {
_ = d.Set("rule_id", locationId)
}

if domain != "" && url != "" {
_ = d.Set("domain", domain)
_ = d.Set("url", url)
}

var onlineTargets []*clb.Backend
if *instance.Protocol == CLB_LISTENER_PROTOCOL_HTTP || *instance.Protocol == CLB_LISTENER_PROTOCOL_HTTPS {
_ = d.Set("rule_id", locationId)
if len(instance.Rules) > 0 {
for _, loc := range instance.Rules {
if locationId == "" || locationId == *loc.LocationId {
if (locationId == *loc.LocationId) || (domain == *loc.Domain && url == *loc.Url) {
onlineTargets = loc.Targets
break
}
}
}
Expand All @@ -296,7 +301,10 @@ func resourceTencentCloudClbServerAttachmentRead(d *schema.ResourceData, meta in

targets = append(targets, target)
} else if *onlineTarget.Type == CLB_BACKEND_TYPE_ENI || *onlineTarget.Type == CLB_BACKEND_TYPE_NAT ||
*onlineTarget.Type == CLB_BACKEND_TYPE_CCN || *onlineTarget.Type == CLB_BACKEND_TYPE_SRV {
*onlineTarget.Type == CLB_BACKEND_TYPE_CCN || *onlineTarget.Type == CLB_BACKEND_TYPE_SRV ||
*onlineTarget.Type == CLB_BACKEND_TYPE_MS || *onlineTarget.Type == CLB_BACKEND_TYPE_EVM ||
*onlineTarget.Type == CLB_BACKEND_TYPE_GR || *onlineTarget.Type == CLB_BACKEND_TYPE_IPDC ||
*onlineTarget.Type == CLB_BACKEND_TYPE_PVGW {
target := map[string]interface{}{
"weight": int(*onlineTarget.Weight),
"port": int(*onlineTarget.Port),
Expand Down
22 changes: 13 additions & 9 deletions tencentcloud/services/clb/resource_tc_clb_attachment.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Provides a resource to create a CLB attachment.

Example Usage

Bind a Cvm instance by using `rule_id`
Bind a Cvm instance by using rule_id

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -20,7 +20,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```

Bind a Cvm instance by using `domian` and `url`
Bind a Cvm instance by using domian and url

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -37,7 +37,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```

Bind multiple Cvm instances by using `rule_id`
Bind multiple Cvm instances by using rule_id

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -59,7 +59,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```

Bind multiple Cvm instances by using `domian` and `url`
Bind multiple Cvm instances by using domian and url

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -82,7 +82,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```

Bind backend target is ENI by using `rule_id`
Bind backend target is ENI by using rule_id

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -98,14 +98,14 @@ resource "tencentcloud_clb_attachment" "example" {
}
```

Bind backend target is ENI by using `domian` and `url`
Bind backend target is ENI by using domian and url

```hcl
resource "tencentcloud_clb_attachment" "example" {
clb_id = "lb-k2zjp9lv"
listener_id = "lbl-hh141sn9"
domain = "test.com"
url = "/"
url = "/path"

targets {
eni_ip = "172.16.16.52"
Expand All @@ -119,10 +119,14 @@ Import

CLB attachment can be imported using the id, e.g.

If use rule_id

```
$ terraform import tencentcloud_clb_attachment.example loc-4xxr2cy7#lbl-hh141sn9#lb-7a0t6zqb
```

Or
If use domain & url

$ terraform import tencentcloud_clb_attachment.example test.com,/#lbl-hh141sn9#lb-7a0t6zqb
```
$ terraform import tencentcloud_clb_attachment.example test.com,/path#lbl-hh141sn9#lb-7a0t6zqb
```
22 changes: 14 additions & 8 deletions website/docs/r/clb_attachment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Provides a resource to create a CLB attachment.

## Example Usage

### Bind a Cvm instance by using rule_id

```hcl
resource "tencentcloud_clb_attachment" "example" {
clb_id = "lb-k2zjp9lv"
Expand All @@ -29,7 +31,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```


### Bind a Cvm instance by using domian and url

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -46,7 +48,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```


### Bind multiple Cvm instances by using rule_id

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -68,7 +70,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```


### Bind multiple Cvm instances by using domian and url

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -91,7 +93,7 @@ resource "tencentcloud_clb_attachment" "example" {
}
```


### Bind backend target is ENI by using rule_id

```hcl
resource "tencentcloud_clb_attachment" "example" {
Expand All @@ -107,14 +109,14 @@ resource "tencentcloud_clb_attachment" "example" {
}
```


### Bind backend target is ENI by using domian and url

```hcl
resource "tencentcloud_clb_attachment" "example" {
clb_id = "lb-k2zjp9lv"
listener_id = "lbl-hh141sn9"
domain = "test.com"
url = "/"
url = "/path"

targets {
eni_ip = "172.16.16.52"
Expand Down Expand Up @@ -154,11 +156,15 @@ In addition to all arguments above, the following attributes are exported:

CLB attachment can be imported using the id, e.g.

If use rule_id

```
$ terraform import tencentcloud_clb_attachment.example loc-4xxr2cy7#lbl-hh141sn9#lb-7a0t6zqb
```

Or
If use domain & url

$ terraform import tencentcloud_clb_attachment.example test.com,/#lbl-hh141sn9#lb-7a0t6zqb
```
$ terraform import tencentcloud_clb_attachment.example test.com,/path#lbl-hh141sn9#lb-7a0t6zqb
```

Loading