Skip to content

[WIP]fix(clb): [122612572] resource update #3260

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs v1.0.600
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam v1.0.695
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.1073
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1107
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1132
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.1033
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.1078
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1130
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1132
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.1128
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.1111
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.1073 h1:+nT
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.1073/go.mod h1:D9xdyB3utAtgGwTExSGxHSVQMfVHEUo/bfaBTxjHkao=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1107 h1:sUiyGfYHzNpHKJx6DlRu1K8xBEGaWlOQT+8W5Ws0v4U=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1107/go.mod h1:L1r6mc7XjFlyLlOVqRvPEhjXneHywnyjyv9EoiGBRvM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1132 h1:dXKq4d4zTZYV0q1uyf7AJM+u00dd5ndL7lqGfjkmQW0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.1132/go.mod h1:nwCB78ZVNC6VC+rd31yxhUeff/Ve7z4+yrL3A2B+/dg=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.1033 h1:dIr+MVsZeUBiKZELfJh5HRJdI+BI6lCp5pv/2oXekuk=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.1033/go.mod h1:7oFlNimGSTHFy6JV7W/IZKuJWr+NUjCnGLTvb9MWNrY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.1078 h1:6yf63sgR2q1ikVedWTXldtrArDHXG01yIdcWSSI5e5Y=
Expand Down Expand Up @@ -933,6 +935,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1128/go.mod
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1129/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1130 h1:pjtrqJUaidq1fFWwPvBorq7FScq0LXiQ7pZjJZx3Jsc=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1130/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1132 h1:ecdDh9up1kMKSZtEpvRHGF6BsIlfBtdCSBZm3SK5/Xk=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1132/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.0.993 h1:WlPgXldQCxt7qi5Xrc6j6zTrsXWzN5BcOGs7Irq7fwQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.0.993/go.mod h1:Z9U8zNtyuyKhjS0698wqsrG/kLx1TQ5CEixXBwVe7xY=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.860 h1:F3esKBIT3HW9+7Gt8cVgf8X06VdGIczpgLBUECzSEzU=
Expand Down
15 changes: 15 additions & 0 deletions tencentcloud/services/clb/resource_tc_clb_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ func ResourceTencentCloudClbListener() *schema.Resource {
Optional: true,
Description: "Whether to enable SNAT.",
},
"full_end_ports": {
Type: schema.TypeList,
Optional: true,
Description: "End port of the full port segment listener.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
//computed
"listener_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -411,6 +417,15 @@ func resourceTencentCloudClbListenerCreate(d *schema.ResourceData, meta interfac
request.SnatEnable = helper.Bool(v.(bool))
}

if v, ok := d.GetOk("full_end_ports"); ok {
fullEndPorts := v.([]interface{})
request.FullEndPorts = make([]*int64, 0, len(fullEndPorts))
for i := range fullEndPorts {
item := fullEndPorts[i].(int)
request.FullEndPorts = append(request.FullEndPorts, helper.IntInt64(item))
}
}

var response *clb.CreateListenerResponse
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().CreateListener(request)
Expand Down
242 changes: 196 additions & 46 deletions tencentcloud/services/clb/resource_tc_clb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package clb

import (
"context"
"fmt"
"log"

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"
)
Expand Down Expand Up @@ -36,7 +40,7 @@ func ResourceTencentCloudClbTargetGroup() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: tccommon.ValidatePort,
Description: "The default port of target group, add server after can use it.",
Description: "The default port of target group, add server after can use it. If `full_listen_switch` is true, setting this parameter is not supported.",
},
"target_group_instances": {
Type: schema.TypeList,
Expand Down Expand Up @@ -72,6 +76,49 @@ func ResourceTencentCloudClbTargetGroup() *schema.Resource {
},
},
},
"type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{"v1", "v2"}),
Description: "Target group type, currently supports v1 (old version target group), v2 (new version target group), defaults to v1 (old version target group).",
},
"protocol": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: tccommon.ValidateAllowedStringValue([]string{"TCP", "UDP"}),
Description: "Target group backend forwarding protocol. This item is required for the v2 new version target group. Currently supports `TCP`, `UDP`.",
},
"tags": {
Optional: true,
Type: schema.TypeList,
Description: "Label.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tag_key": {
Type: schema.TypeString,
Required: true,
Description: "Tag key.",
},
"tag_value": {
Type: schema.TypeString,
Required: true,
Description: "Tag value.",
},
},
},
},
"weight": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: tccommon.ValidateIntegerInRange(0, 100),
Description: "Default weights for backend services. Value range [0, 100]. After setting this value, when adding backend services to the target group, if the backend services do not have separate weights set, the default weights here will be used.",
},
"full_listen_switch": {
Type: schema.TypeBool,
Optional: true,
Description: "Full listening target group identifier, true indicates full listening target group, false indicates not full listening target group.",
},
},
}
}
Expand All @@ -80,40 +127,81 @@ func resourceTencentCloudClbTargetCreate(d *schema.ResourceData, meta interface{
defer tccommon.LogElapsed("resource.tencentcloud_clb_target_group.create")()

var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
clbService = ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
vpcId = d.Get("vpc_id").(string)
targetGroupName = d.Get("target_group_name").(string)
port = uint64(d.Get("port").(int))
insAttachments = make([]*clb.TargetGroupInstance, 0)
targetGroupId string
err error
logId = tccommon.GetLogId(tccommon.ContextNil)
request = clb.NewCreateTargetGroupRequest()
response = clb.NewCreateTargetGroupResponse()
)

if v, ok := d.GetOk("target_group_instances"); ok {
targetGroupInstances := v.([]interface{})
for _, v1 := range targetGroupInstances {
value := v1.(map[string]interface{})
bindIP := value["bind_ip"].(string)
port := uint64(value["port"].(int))
weight := uint64(value["weight"].(int))
newPort := uint64(value["new_port"].(int))
tgtGrp := &clb.TargetGroupInstance{
BindIP: &bindIP,
Port: &port,
Weight: &weight,
NewPort: &newPort,
if v, ok := d.GetOk("target_group_name"); ok {
request.TargetGroupName = helper.String(v.(string))
}

if v, ok := d.GetOk("vpc_id"); ok {
request.VpcId = helper.String(v.(string))
}

if v, ok := d.GetOkExists("port"); ok {
request.Port = helper.IntUint64(v.(int))
}

if v, ok := d.GetOk("type"); ok {
request.Type = helper.String(v.(string))
}

if v, ok := d.GetOk("protocol"); ok {
request.Protocol = helper.String(v.(string))
}

if v, ok := d.GetOk("tags"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
clbTags := clb.TagInfo{}
if v, ok := dMap["tag_key"]; ok {
clbTags.TagKey = helper.String(v.(string))
}

if v, ok := dMap["tag_value"]; ok {
clbTags.TagValue = helper.String(v.(string))
}
insAttachments = append(insAttachments, tgtGrp)

request.Tags = append(request.Tags, &clbTags)
}
}

targetGroupId, err = clbService.CreateTargetGroup(ctx, targetGroupName, vpcId, port, insAttachments)
if v, ok := d.GetOkExists("weight"); ok {
request.Weight = helper.IntUint64(v.(int))
}

if v, ok := d.GetOkExists("full_listen_switch"); ok {
request.FullListenSwitch = helper.Bool(v.(bool))
}

err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().CreateTargetGroup(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 {
return resource.NonRetryableError(fmt.Errorf("Create target group failed, Response is nil."))
}

response = result
return nil
})

if err != nil {
log.Printf("[CRITAL]%s create target group failed, reason:%+v", logId, err)
return err
}
d.SetId(targetGroupId)

if response.Response.TargetGroupId == nil {
return fmt.Errorf("TargetGroupId is nil.")
}

d.SetId(*response.Response.TargetGroupId)

return resourceTencentCloudClbTargetRead(d, meta)

Expand All @@ -124,23 +212,64 @@ func resourceTencentCloudClbTargetRead(d *schema.ResourceData, meta interface{})
defer tccommon.InconsistentCheck(d, meta)()

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

filters := make(map[string]string)
targetGroupInfos, err := clbService.DescribeTargetGroups(ctx, id, filters)
targetGroupInfos, err := clbService.DescribeTargetGroups(ctx, targetGroupId, filters)
if err != nil {
return err
}

if len(targetGroupInfos) < 1 {
d.SetId("")
return nil
}
_ = d.Set("target_group_name", targetGroupInfos[0].TargetGroupName)
_ = d.Set("vpc_id", targetGroupInfos[0].VpcId)
_ = d.Set("port", targetGroupInfos[0].Port)

if targetGroupInfos[0].TargetGroupName != nil {
_ = d.Set("target_group_name", targetGroupInfos[0].TargetGroupName)
}

if targetGroupInfos[0].VpcId != nil {
_ = d.Set("vpc_id", targetGroupInfos[0].VpcId)
}

if targetGroupInfos[0].Port != nil {
_ = d.Set("port", targetGroupInfos[0].Port)
}

if targetGroupInfos[0].TargetGroupType != nil {
_ = d.Set("type", targetGroupInfos[0].TargetGroupType)
}

if targetGroupInfos[0].Tag != nil {
tagsList := make([]interface{}, 0, len(targetGroupInfos[0].Tag))
for _, tags := range targetGroupInfos[0].Tag {
tagsMap := map[string]interface{}{}
if tags.TagKey != nil {
tagsMap["tag_key"] = tags.TagKey
}

if tags.TagValue != nil {
tagsMap["tag_value"] = tags.TagValue
}

tagsList = append(tagsList, tagsMap)
}

_ = d.Set("tags", tagsList)
}

if targetGroupInfos[0].Weight != nil {
_ = d.Set("weight", targetGroupInfos[0].Weight)
}

if targetGroupInfos[0].FullListenSwitch != nil {
_ = d.Set("full_listen_switch", targetGroupInfos[0].FullListenSwitch)
}

return nil
}
Expand All @@ -150,23 +279,44 @@ func resourceTencentCloudClbTargetUpdate(d *schema.ResourceData, meta interface{

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

isChanged := false
if d.HasChange("port") || d.HasChange("target_group_name") {
isChanged = true
port = uint64(d.Get("port").(int))
tgtGroupName = d.Get("target_group_name").(string)
immutableArgs := []string{"type", "protocol", "tags", "full_listen_switch"}
for _, v := range immutableArgs {
if d.HasChange(v) {
return fmt.Errorf("argument `%s` cannot be changed", v)
}
}

if isChanged {
err := clbService.ModifyTargetGroup(ctx, targetGroupId, tgtGroupName, port)
if d.HasChange("target_group_name") || d.HasChange("port") || d.HasChange("weight") {
request := clb.NewModifyTargetGroupAttributeRequest()
if v, ok := d.GetOk("target_group_name"); ok {
request.TargetGroupName = helper.String(v.(string))
}

if v, ok := d.GetOkExists("port"); ok {
request.Port = helper.IntUint64(v.(int))
}

if v, ok := d.GetOkExists("weight"); ok {
request.Weight = helper.IntUint64(v.(int))
}

request.TargetGroupId = &targetGroupId
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().ModifyTargetGroupAttribute(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())
}

return nil
})

if err != nil {
log.Printf("[CRITAL]%s modify target group failed, reason:%+v", logId, err)
return err
}
}
Expand All @@ -185,9 +335,9 @@ func resourceTencentCloudClbTargetDelete(d *schema.ResourceData, meta interface{
)

err := clbService.DeleteTarget(ctx, targetGroupId)

if err != nil {
return err
}

return nil
}
Loading
Loading