Skip to content

fix(clb): [121921107] add new resource #3163

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 3 commits into from
Feb 27, 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/3163.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
tencentcloud_clb_customized_config_attachment
```
1 change: 1 addition & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,7 @@ func Provider() *schema.Provider {
"tencentcloud_clb_log_topic": clb.ResourceTencentCloudClbLogTopic(),
"tencentcloud_clb_customized_config": clb.ResourceTencentCloudClbCustomizedConfig(),
"tencentcloud_clb_customized_config_v2": clb.ResourceTencentCloudClbCustomizedConfigV2(),
"tencentcloud_clb_customized_config_attachment": clb.ResourceTencentCloudClbCustomizedConfigAttachment(),
"tencentcloud_clb_snat_ip": clb.ResourceTencentCloudClbSnatIp(),
"tencentcloud_clb_function_targets_attachment": clb.ResourceTencentCloudClbFunctionTargetsAttachment(),
"tencentcloud_clb_instance_mix_ip_target_config": clb.ResourceTencentCloudClbInstanceMixIpTargetConfig(),
Expand Down
1 change: 1 addition & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ tencentcloud_clb_log_set
tencentcloud_clb_log_topic
tencentcloud_clb_customized_config
tencentcloud_clb_customized_config_v2
tencentcloud_clb_customized_config_attachment
tencentcloud_clb_snat_ip
tencentcloud_clb_function_targets_attachment
tencentcloud_clb_instance_sla_config
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,364 @@
package clb

import (
"context"
"fmt"
"log"

"github.com/pkg/errors"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"

"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
)

func ResourceTencentCloudClbCustomizedConfigAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudClbCustomizedConfigAttachmentCreate,
Read: resourceTencentCloudClbCustomizedConfigAttachmentRead,
Update: resourceTencentCloudClbCustomizedConfigAttachmentUpdate,
Delete: resourceTencentCloudClbCustomizedConfigAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"config_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of Customized Config.",
},
"bind_list": {
Type: schema.TypeSet,
Required: true,
Description: "Associated server or location.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"load_balancer_id": {
Type: schema.TypeString,
Required: true,
Description: "Clb ID.",
},
"listener_id": {
Type: schema.TypeString,
Required: true,
Description: "Listener ID.",
},
"domain": {
Type: schema.TypeString,
Required: true,
Description: "Domain.",
},
"location_id": {
Type: schema.TypeString,
Optional: true,
Description: "Location ID.",
},
},
},
},
},
}
}

func resourceTencentCloudClbCustomizedConfigAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.create")()

var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = clb.NewAssociateCustomizedConfigRequest()
configId string
)

if v, ok := d.GetOk("config_id"); ok {
request.UconfigId = helper.String(v.(string))
configId = v.(string)
}

if v, ok := d.GetOk("bind_list"); ok {
for _, item := range v.(*schema.Set).List() {
bindItem := clb.BindItem{}
dMap := item.(map[string]interface{})
if v, ok := dMap["load_balancer_id"]; ok {
bindItem.LoadBalancerId = helper.String(v.(string))
}

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

if v, ok := dMap["domain"]; ok {
bindItem.Domain = helper.String(v.(string))
}

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

request.BindList = append(request.BindList, &bindItem)
}
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().AssociateCustomizedConfig(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())
if result == nil || result.Response == nil || result.Response.RequestId == nil {
return resource.NonRetryableError(fmt.Errorf("Associate CLB Customized Config Failed, Response is nil."))
}

requestId := *result.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return tccommon.RetryError(errors.WithStack(retryErr))
}
}

return nil
})

if err != nil {
log.Printf("[CRITAL]%s Associate CLB Customized Config Failed, reason:%+v", logId, err)
return err
}

d.SetId(configId)

return resourceTencentCloudClbCustomizedConfigAttachmentRead(d, meta)
}

func resourceTencentCloudClbCustomizedConfigAttachmentRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.read")()

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

bindList, err := clbService.DescribeDescribeCustomizedConfigAssociateListById(ctx, configId)
if err != nil {
return err
}

if bindList == nil || len(bindList) == 0 {

Check failure on line 150 in tencentcloud/services/clb/resource_tc_clb_customized_config_attachment.go

View workflow job for this annotation

GitHub Actions / golangci-lint

S1009: should omit nil check; len() for []*github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317.BindDetailItem is defined as zero (gosimple)
d.SetId("")
return fmt.Errorf("resource `tencentcloud_clb_customized_config_attachment` %s does not exist", configId)
}

_ = d.Set("config_id", configId)

tmpList := make([]map[string]interface{}, 0, len(bindList))
for _, item := range bindList {
dMap := make(map[string]interface{})
if item.LoadBalancerId != nil {
dMap["load_balancer_id"] = *item.LoadBalancerId
}

if item.ListenerId != nil {
dMap["listener_id"] = *item.ListenerId
}

if item.Domain != nil {
dMap["domain"] = *item.Domain
}

if item.LocationId != nil {
dMap["location_id"] = *item.LocationId
}

tmpList = append(tmpList, dMap)
}

_ = d.Set("bind_list", tmpList)

return nil
}

func resourceTencentCloudClbCustomizedConfigAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.delete")()

var (
logId = tccommon.GetLogId(tccommon.ContextNil)
id = d.Id()
)

if d.HasChange("bind_list") {
oldInterface, newInterface := d.GetChange("bind_list")
olds := oldInterface.(*schema.Set)
news := newInterface.(*schema.Set)
remove := olds.Difference(news).List()
add := news.Difference(olds).List()
if len(remove) > 0 {
request := clb.NewDisassociateCustomizedConfigRequest()
for _, item := range remove {
bindItem := clb.BindItem{}
dMap := item.(map[string]interface{})
if v, ok := dMap["load_balancer_id"]; ok {
bindItem.LoadBalancerId = helper.String(v.(string))
}

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

if v, ok := dMap["domain"]; ok {
bindItem.Domain = helper.String(v.(string))
}

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

request.BindList = append(request.BindList, &bindItem)
}

request.UconfigId = helper.String(id)
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().DisassociateCustomizedConfig(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())
if result == nil || result.Response == nil || result.Response.RequestId == nil {
return resource.NonRetryableError(fmt.Errorf("Disassociate CLB Customized Config Failed, Response is nil."))
}

requestId := *result.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return tccommon.RetryError(errors.WithStack(retryErr))
}
}

return nil
})

if err != nil {
log.Printf("[CRITAL]%s Disassociate CLB Customized Config Failed, reason:%+v", logId, err)
return err
}
}

if len(add) > 0 {
request := clb.NewAssociateCustomizedConfigRequest()
request.UconfigId = helper.String(id)
for _, item := range add {
bindItem := clb.BindItem{}
dMap := item.(map[string]interface{})
if v, ok := dMap["load_balancer_id"]; ok {
bindItem.LoadBalancerId = helper.String(v.(string))
}

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

if v, ok := dMap["domain"]; ok {
bindItem.Domain = helper.String(v.(string))
}

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

request.BindList = append(request.BindList, &bindItem)
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().AssociateCustomizedConfig(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())
if result == nil || result.Response == nil || result.Response.RequestId == nil {
return resource.NonRetryableError(fmt.Errorf("Associate CLB Customized Config Failed, Response is nil."))
}

requestId := *result.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return tccommon.RetryError(errors.WithStack(retryErr))
}
}

return nil
})

if err != nil {
log.Printf("[CRITAL]%s Associate CLB Customized Config Failed, reason:%+v", logId, err)
return err
}
}
}

return resourceTencentCloudClbCustomizedConfigAttachmentRead(d, meta)
}

func resourceTencentCloudClbCustomizedConfigAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.delete")()

var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = clb.NewDisassociateCustomizedConfigRequest()
id = d.Id()
)

request.UconfigId = helper.String(id)
if v, ok := d.GetOk("bind_list"); ok {
for _, item := range v.(*schema.Set).List() {
bindItem := clb.BindItem{}
dMap := item.(map[string]interface{})
if v, ok := dMap["load_balancer_id"]; ok {
bindItem.LoadBalancerId = helper.String(v.(string))
}

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

if v, ok := dMap["domain"]; ok {
bindItem.Domain = helper.String(v.(string))
}

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

request.BindList = append(request.BindList, &bindItem)
}
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().DisassociateCustomizedConfig(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())
if result == nil || result.Response == nil || result.Response.RequestId == nil {
return resource.NonRetryableError(fmt.Errorf("Disassociate CLB Customized Config Failed, Response is nil."))
}

requestId := *result.Response.RequestId
retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient())
if retryErr != nil {
return tccommon.RetryError(errors.WithStack(retryErr))
}
}

return nil
})

if err != nil {
log.Printf("[CRITAL]%s Disassociate CLB Customized Config Failed, reason:%+v", logId, err)
return err
}

return nil
}
Loading
Loading