Skip to content
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
19 changes: 10 additions & 9 deletions tencentcloud/resource_tc_clb_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,16 @@ func resourceTencentCloudClbInstanceCreate(d *schema.ResourceData, meta interfac
if v, ok := d.GetOk("load_balancer_pass_to_target"); ok {
request.LoadBalancerPassToTarget = helper.Bool(v.(bool))
}

if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
for k, v := range tags {
request.Tags = append(request.Tags, &clb.TagInfo{
TagKey: &k,
TagValue: &v,
})
}
}

clbId := ""
var response *clb.CreateLoadBalancerResponse
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
Expand Down Expand Up @@ -539,15 +549,6 @@ func resourceTencentCloudClbInstanceCreate(d *schema.ResourceData, meta interfac
return err
}
}
ctx := context.WithValue(context.TODO(), logIdKey, logId)
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
tcClient := meta.(*TencentCloudClient).apiV3Conn
tagService := &TagService{client: tcClient}
resourceName := BuildTagResourceName("clb", "clb", tcClient.Region, d.Id())
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
return err
}
}

return resourceTencentCloudClbInstanceRead(d, meta)
}
Expand Down
96 changes: 78 additions & 18 deletions tencentcloud/resource_tc_clb_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,69 @@ package tencentcloud
import (
"context"
"fmt"
"log"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

const BasicClbName = "tf-clb-basic"
const InternalClbName = "tf-clb-internal"
const InternalClbNameUpdate = "tf-clb-update-internal"
const MyOpenClbName = "my-open-clb"
const OpenClbName = "tf-clb-open"
const OpenClbNameUpdate = "tf-clb-update-open"

func init() {
// -sweep-run=tencentcloud_clb_instance
resource.AddTestSweepers("tencentcloud_clb_instance", &resource.Sweeper{
Name: "tencentcloud_clb_instance",
F: testSweepClbInstance,
})
}

func testSweepClbInstance(region string) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
cli, err := sharedClientForRegion(region)
if err != nil {
return err
}
client := cli.(*TencentCloudClient).apiV3Conn
service := ClbService{client: client}
testCaseNames := []string{
BasicClbName,
InternalClbName,
InternalClbNameUpdate,
MyOpenClbName,
OpenClbName,
OpenClbNameUpdate,
}

res, err := service.DescribeLoadBalancerByFilter(ctx, map[string]interface{}{})
if err != nil {
return err
}

if len(res) > 0 {
for _, v := range res {
id := *v.LoadBalancerId

name := *v.LoadBalancerName
if !IsContains(testCaseNames, name) {
continue
}
if err := service.DeleteLoadBalancerById(ctx, id); err != nil {
log.Printf("Delete %s error: %s", id, err.Error())
continue
}
}
}

return nil
}

func TestAccTencentCloudClbInstance_basic(t *testing.T) {
t.Parallel()

Expand All @@ -22,7 +79,7 @@ func TestAccTencentCloudClbInstance_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_basic"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_basic", "network_type", "OPEN"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_basic", "clb_name", "tf-clb-basic"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_basic", "clb_name", BasicClbName),
),
},
{
Expand All @@ -47,7 +104,7 @@ func TestAccTencentCloudClbInstance_open(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_open"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "network_type", "OPEN"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", "tf-clb-open"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", OpenClbName),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "project_id", "0"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "security_groups.#", "1"),
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_open", "security_groups.0"),
Expand All @@ -60,7 +117,7 @@ func TestAccTencentCloudClbInstance_open(t *testing.T) {
Config: testAccClbInstance_update_open,
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_open"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", "tf-clb-update-open"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", OpenClbNameUpdate),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "network_type", "OPEN"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "project_id", "0"),
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_open", "vpc_id"),
Expand All @@ -87,7 +144,7 @@ func TestAccTencentCloudClbInstance_internal(t *testing.T) {
Config: testAccClbInstance_internal,
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_internal"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", "tf-clb-internal"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", InternalClbName),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "network_type", "INTERNAL"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "project_id", "0"),
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_internal", "vpc_id"),
Expand All @@ -99,7 +156,7 @@ func TestAccTencentCloudClbInstance_internal(t *testing.T) {
Config: testAccClbInstance_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_internal"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", "tf-clb-update-internal"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", InternalClbNameUpdate),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "network_type", "INTERNAL"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "project_id", "0"),
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_internal", "vpc_id"),
Expand Down Expand Up @@ -129,7 +186,7 @@ func TestAccTencentCloudClbInstance_default_enable(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.default_enable"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "network_type", "OPEN"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", "my-open-clb"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", MyOpenClbName),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "project_id", "0"),
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.default_enable", "vpc_id"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "load_balancer_pass_to_target", "true"),
Expand All @@ -144,7 +201,7 @@ func TestAccTencentCloudClbInstance_default_enable(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.default_enable"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "network_type", "OPEN"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", "my-open-clb"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", MyOpenClbName),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "project_id", "0"),
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.default_enable", "vpc_id"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "load_balancer_pass_to_target", "true"),
Expand All @@ -171,7 +228,7 @@ func TestAccTencentCloudClbInstance_multiple_instance(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.multiple_instance"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "network_type", "OPEN"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", "my-open-clb"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", MyOpenClbName),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "master_zone_id", "100003"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "slave_zone_id", "100004"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "tags.test", "mytest"),
Expand All @@ -182,7 +239,7 @@ func TestAccTencentCloudClbInstance_multiple_instance(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceExists("tencentcloud_clb_instance.multiple_instance"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "network_type", "OPEN"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", "my-open-clb"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", MyOpenClbName),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "master_zone_id", "100003"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "slave_zone_id", "100004"),
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "tags.test", "open"),
Expand Down Expand Up @@ -241,7 +298,10 @@ func testAccCheckClbInstanceExists(n string) resource.TestCheckFunc {
const testAccClbInstance_basic = `
resource "tencentcloud_clb_instance" "clb_basic" {
network_type = "OPEN"
clb_name = "tf-clb-basic"
clb_name = "` + BasicClbName + `"
tags = {
test = "tf"
}
}
`

Expand All @@ -265,7 +325,7 @@ resource "tencentcloud_subnet" "subnet" {

resource "tencentcloud_clb_instance" "clb_internal" {
network_type = "INTERNAL"
clb_name = "tf-clb-internal"
clb_name = "` + InternalClbName + `"
vpc_id = tencentcloud_vpc.foo.id
subnet_id = tencentcloud_subnet.subnet.id
project_id = 0
Expand All @@ -288,7 +348,7 @@ resource "tencentcloud_vpc" "foo" {

resource "tencentcloud_clb_instance" "clb_open" {
network_type = "OPEN"
clb_name = "tf-clb-open"
clb_name = "` + OpenClbName + `"
project_id = 0
vpc_id = tencentcloud_vpc.foo.id
target_region_info_region = "ap-guangzhou"
Expand Down Expand Up @@ -321,7 +381,7 @@ resource "tencentcloud_subnet" "subnet" {

resource "tencentcloud_clb_instance" "clb_internal" {
network_type = "INTERNAL"
clb_name = "tf-clb-update-internal"
clb_name = "` + InternalClbNameUpdate + `"
vpc_id = tencentcloud_vpc.foo.id
subnet_id = tencentcloud_subnet.subnet.id
project_id = 0
Expand All @@ -344,7 +404,7 @@ resource "tencentcloud_vpc" "foo" {

resource "tencentcloud_clb_instance" "clb_open" {
network_type = "OPEN"
clb_name = "tf-clb-update-open"
clb_name = "` + OpenClbNameUpdate + `"
vpc_id = tencentcloud_vpc.foo.id
project_id = 0
target_region_info_region = "ap-guangzhou"
Expand Down Expand Up @@ -387,7 +447,7 @@ resource "tencentcloud_vpc" "foo" {

resource "tencentcloud_clb_instance" "default_enable" {
network_type = "OPEN"
clb_name = "my-open-clb"
clb_name = "` + MyOpenClbName + `"
project_id = 0
vpc_id = tencentcloud_vpc.foo.id
load_balancer_pass_to_target = true
Expand Down Expand Up @@ -432,7 +492,7 @@ resource "tencentcloud_vpc" "foo" {

resource "tencentcloud_clb_instance" "default_enable" {
network_type = "OPEN"
clb_name = "my-open-clb"
clb_name = "` + MyOpenClbName + `"
project_id = 0
vpc_id = tencentcloud_vpc.foo.id
load_balancer_pass_to_target = true
Expand All @@ -450,7 +510,7 @@ resource "tencentcloud_clb_instance" "default_enable" {
const testAccClbInstance__multi_instance = `
resource "tencentcloud_clb_instance" "multiple_instance" {
network_type = "OPEN"
clb_name = "my-open-clb"
clb_name = "` + MyOpenClbName + `"
master_zone_id = "100003"
slave_zone_id = "100004"

Expand All @@ -463,7 +523,7 @@ resource "tencentcloud_clb_instance" "multiple_instance" {
const testAccClbInstance__multi_instance_update = `
resource "tencentcloud_clb_instance" "multiple_instance" {
network_type = "OPEN"
clb_name = "my-open-clb"
clb_name = "` + MyOpenClbName + `"
master_zone_id = "100003"
slave_zone_id = "100004"

Expand Down
14 changes: 8 additions & 6 deletions tencentcloud/resource_tc_clb_log_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ func TestAccTencentCloudClbInstanceTopic(t *testing.T) {
{
Config: testAccClbInstanceTopic,
Check: resource.ComposeTestCheckFunc(
testAccCheckClbInstanceTopicExists("tencentcloud_clb_instances_topic.topic"),
resource.TestCheckResourceAttr("tencentcloud_clb_instances_topic.topic", "topic_name", "clb-topic-test"),
resource.TestCheckResourceAttr("tencentcloud_clb_instances_topic.topic", "partition_count", "3"),
testAccCheckClbInstanceTopicExists("tencentcloud_clb_log_topic.topic"),
resource.TestCheckResourceAttr("tencentcloud_clb_log_topic.topic", "topic_name", "clb-topic-test"),
),
},
},
Expand All @@ -44,8 +43,7 @@ func testAccCheckClbInstanceTopicExists(n string) resource.TestCheckFunc {
clsService := ClsService{
client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn,
}
topicName := rs.Primary.Attributes["topic_name"]
instance, err := clsService.DescribeTopicsById(ctx, topicName)
instance, err := clsService.DescribeTopicsById(ctx, rs.Primary.ID)
if err != nil {
return err
}
Expand All @@ -58,8 +56,12 @@ func testAccCheckClbInstanceTopicExists(n string) resource.TestCheckFunc {
}

const testAccClbInstanceTopic = `
resource "tencentcloud_clb_log_set" "set1" {
period = 7
}

resource "tencentcloud_clb_log_topic" "topic" {
log_set_id = tencentcloud_clb_log_set.set1.id
topic_name="clb-topic-test"
partition_count=3
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ resource "tencentcloud_vpc" "foo" {

resource "tencentcloud_clb_instance" "clb_basic" {
network_type = "OPEN"
clb_name = "tf-clb-basic"
clb_name = "tf-clb-attach-basic"
vpc_id = tencentcloud_vpc.foo.id
}

Expand Down
4 changes: 2 additions & 2 deletions tencentcloud/resource_tc_clb_target_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ resource "tencentcloud_clb_target_group" "test"{
`

const testAccClbInstanceTargetGroup = `
resource "tencentcloud_clb_instance" "target_group" {
resource "tencentcloud_clb_target_group" "target_group" {
target_group_name = "tgt_grp_test"
port = 33
target_group_instances {
Expand All @@ -127,7 +127,7 @@ resource "tencentcloud_clb_instance" "target_group" {
`

const testAccClbInstanceTargetGroupUpdate = `
resource "tencentcloud_clb_instance" "target_group" {
resource "tencentcloud_clb_target_group" "target_group" {
target_group_name = "tgt_grp_test"
port = 44
target_group_instances {
Expand Down