From 2de40ed22027babf9c21034343a1845ed6c92e61 Mon Sep 17 00:00:00 2001 From: KGSN Date: Fri, 25 Feb 2022 14:44:46 +0800 Subject: [PATCH] fix: lb - create with tag params --- tencentcloud/resource_tc_clb_instance.go | 19 ++-- tencentcloud/resource_tc_clb_instance_test.go | 96 +++++++++++++++---- .../resource_tc_clb_log_topic_test.go | 14 +-- ...rce_tc_clb_target_group_attachment_test.go | 2 +- .../resource_tc_clb_target_group_test.go | 4 +- 5 files changed, 99 insertions(+), 36 deletions(-) diff --git a/tencentcloud/resource_tc_clb_instance.go b/tencentcloud/resource_tc_clb_instance.go index 14eafb9cbf..7a00516bfd 100644 --- a/tencentcloud/resource_tc_clb_instance.go +++ b/tencentcloud/resource_tc_clb_instance.go @@ -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 { @@ -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) } diff --git a/tencentcloud/resource_tc_clb_instance_test.go b/tencentcloud/resource_tc_clb_instance_test.go index 4f038454ff..dc7ca12ae8 100644 --- a/tencentcloud/resource_tc_clb_instance_test.go +++ b/tencentcloud/resource_tc_clb_instance_test.go @@ -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() @@ -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), ), }, { @@ -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"), @@ -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"), @@ -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"), @@ -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"), @@ -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"), @@ -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"), @@ -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"), @@ -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"), @@ -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" + } } ` @@ -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 @@ -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" @@ -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 @@ -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" @@ -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 @@ -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 @@ -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" @@ -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" diff --git a/tencentcloud/resource_tc_clb_log_topic_test.go b/tencentcloud/resource_tc_clb_log_topic_test.go index 81c010e3c9..99d71bbcc5 100644 --- a/tencentcloud/resource_tc_clb_log_topic_test.go +++ b/tencentcloud/resource_tc_clb_log_topic_test.go @@ -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"), ), }, }, @@ -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 } @@ -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 } ` diff --git a/tencentcloud/resource_tc_clb_target_group_attachment_test.go b/tencentcloud/resource_tc_clb_target_group_attachment_test.go index cc9c84dd11..4b15cc52e2 100644 --- a/tencentcloud/resource_tc_clb_target_group_attachment_test.go +++ b/tencentcloud/resource_tc_clb_target_group_attachment_test.go @@ -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 } diff --git a/tencentcloud/resource_tc_clb_target_group_test.go b/tencentcloud/resource_tc_clb_target_group_test.go index 239a4d237c..6076a9b787 100644 --- a/tencentcloud/resource_tc_clb_target_group_test.go +++ b/tencentcloud/resource_tc_clb_target_group_test.go @@ -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 { @@ -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 {