Skip to content

Commit 8974ade

Browse files
authored
fix: lb - create with tag params (#876)
1 parent 73fdc82 commit 8974ade

File tree

5 files changed

+99
-36
lines changed

5 files changed

+99
-36
lines changed

tencentcloud/resource_tc_clb_instance.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ func resourceTencentCloudClbInstanceCreate(d *schema.ResourceData, meta interfac
418418
if v, ok := d.GetOk("load_balancer_pass_to_target"); ok {
419419
request.LoadBalancerPassToTarget = helper.Bool(v.(bool))
420420
}
421+
422+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
423+
for k, v := range tags {
424+
request.Tags = append(request.Tags, &clb.TagInfo{
425+
TagKey: &k,
426+
TagValue: &v,
427+
})
428+
}
429+
}
430+
421431
clbId := ""
422432
var response *clb.CreateLoadBalancerResponse
423433
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
@@ -539,15 +549,6 @@ func resourceTencentCloudClbInstanceCreate(d *schema.ResourceData, meta interfac
539549
return err
540550
}
541551
}
542-
ctx := context.WithValue(context.TODO(), logIdKey, logId)
543-
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
544-
tcClient := meta.(*TencentCloudClient).apiV3Conn
545-
tagService := &TagService{client: tcClient}
546-
resourceName := BuildTagResourceName("clb", "clb", tcClient.Region, d.Id())
547-
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
548-
return err
549-
}
550-
}
551552

552553
return resourceTencentCloudClbInstanceRead(d, meta)
553554
}

tencentcloud/resource_tc_clb_instance_test.go

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,69 @@ package tencentcloud
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"testing"
78

89
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
910
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1011
)
1112

13+
const BasicClbName = "tf-clb-basic"
14+
const InternalClbName = "tf-clb-internal"
15+
const InternalClbNameUpdate = "tf-clb-update-internal"
16+
const MyOpenClbName = "my-open-clb"
17+
const OpenClbName = "tf-clb-open"
18+
const OpenClbNameUpdate = "tf-clb-update-open"
19+
20+
func init() {
21+
// -sweep-run=tencentcloud_clb_instance
22+
resource.AddTestSweepers("tencentcloud_clb_instance", &resource.Sweeper{
23+
Name: "tencentcloud_clb_instance",
24+
F: testSweepClbInstance,
25+
})
26+
}
27+
28+
func testSweepClbInstance(region string) error {
29+
logId := getLogId(contextNil)
30+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
31+
cli, err := sharedClientForRegion(region)
32+
if err != nil {
33+
return err
34+
}
35+
client := cli.(*TencentCloudClient).apiV3Conn
36+
service := ClbService{client: client}
37+
testCaseNames := []string{
38+
BasicClbName,
39+
InternalClbName,
40+
InternalClbNameUpdate,
41+
MyOpenClbName,
42+
OpenClbName,
43+
OpenClbNameUpdate,
44+
}
45+
46+
res, err := service.DescribeLoadBalancerByFilter(ctx, map[string]interface{}{})
47+
if err != nil {
48+
return err
49+
}
50+
51+
if len(res) > 0 {
52+
for _, v := range res {
53+
id := *v.LoadBalancerId
54+
55+
name := *v.LoadBalancerName
56+
if !IsContains(testCaseNames, name) {
57+
continue
58+
}
59+
if err := service.DeleteLoadBalancerById(ctx, id); err != nil {
60+
log.Printf("Delete %s error: %s", id, err.Error())
61+
continue
62+
}
63+
}
64+
}
65+
66+
return nil
67+
}
68+
1269
func TestAccTencentCloudClbInstance_basic(t *testing.T) {
1370
t.Parallel()
1471

@@ -22,7 +79,7 @@ func TestAccTencentCloudClbInstance_basic(t *testing.T) {
2279
Check: resource.ComposeTestCheckFunc(
2380
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_basic"),
2481
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_basic", "network_type", "OPEN"),
25-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_basic", "clb_name", "tf-clb-basic"),
82+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_basic", "clb_name", BasicClbName),
2683
),
2784
},
2885
{
@@ -47,7 +104,7 @@ func TestAccTencentCloudClbInstance_open(t *testing.T) {
47104
Check: resource.ComposeTestCheckFunc(
48105
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_open"),
49106
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "network_type", "OPEN"),
50-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", "tf-clb-open"),
107+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", OpenClbName),
51108
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "project_id", "0"),
52109
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "security_groups.#", "1"),
53110
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_open", "security_groups.0"),
@@ -60,7 +117,7 @@ func TestAccTencentCloudClbInstance_open(t *testing.T) {
60117
Config: testAccClbInstance_update_open,
61118
Check: resource.ComposeTestCheckFunc(
62119
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_open"),
63-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", "tf-clb-update-open"),
120+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "clb_name", OpenClbNameUpdate),
64121
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "network_type", "OPEN"),
65122
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_open", "project_id", "0"),
66123
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_open", "vpc_id"),
@@ -87,7 +144,7 @@ func TestAccTencentCloudClbInstance_internal(t *testing.T) {
87144
Config: testAccClbInstance_internal,
88145
Check: resource.ComposeTestCheckFunc(
89146
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_internal"),
90-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", "tf-clb-internal"),
147+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", InternalClbName),
91148
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "network_type", "INTERNAL"),
92149
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "project_id", "0"),
93150
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_internal", "vpc_id"),
@@ -99,7 +156,7 @@ func TestAccTencentCloudClbInstance_internal(t *testing.T) {
99156
Config: testAccClbInstance_update,
100157
Check: resource.ComposeTestCheckFunc(
101158
testAccCheckClbInstanceExists("tencentcloud_clb_instance.clb_internal"),
102-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", "tf-clb-update-internal"),
159+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "clb_name", InternalClbNameUpdate),
103160
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "network_type", "INTERNAL"),
104161
resource.TestCheckResourceAttr("tencentcloud_clb_instance.clb_internal", "project_id", "0"),
105162
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.clb_internal", "vpc_id"),
@@ -129,7 +186,7 @@ func TestAccTencentCloudClbInstance_default_enable(t *testing.T) {
129186
Check: resource.ComposeTestCheckFunc(
130187
testAccCheckClbInstanceExists("tencentcloud_clb_instance.default_enable"),
131188
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "network_type", "OPEN"),
132-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", "my-open-clb"),
189+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", MyOpenClbName),
133190
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "project_id", "0"),
134191
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.default_enable", "vpc_id"),
135192
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "load_balancer_pass_to_target", "true"),
@@ -144,7 +201,7 @@ func TestAccTencentCloudClbInstance_default_enable(t *testing.T) {
144201
Check: resource.ComposeTestCheckFunc(
145202
testAccCheckClbInstanceExists("tencentcloud_clb_instance.default_enable"),
146203
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "network_type", "OPEN"),
147-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", "my-open-clb"),
204+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "clb_name", MyOpenClbName),
148205
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "project_id", "0"),
149206
resource.TestCheckResourceAttrSet("tencentcloud_clb_instance.default_enable", "vpc_id"),
150207
resource.TestCheckResourceAttr("tencentcloud_clb_instance.default_enable", "load_balancer_pass_to_target", "true"),
@@ -171,7 +228,7 @@ func TestAccTencentCloudClbInstance_multiple_instance(t *testing.T) {
171228
Check: resource.ComposeTestCheckFunc(
172229
testAccCheckClbInstanceExists("tencentcloud_clb_instance.multiple_instance"),
173230
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "network_type", "OPEN"),
174-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", "my-open-clb"),
231+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", MyOpenClbName),
175232
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "master_zone_id", "100003"),
176233
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "slave_zone_id", "100004"),
177234
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "tags.test", "mytest"),
@@ -182,7 +239,7 @@ func TestAccTencentCloudClbInstance_multiple_instance(t *testing.T) {
182239
Check: resource.ComposeTestCheckFunc(
183240
testAccCheckClbInstanceExists("tencentcloud_clb_instance.multiple_instance"),
184241
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "network_type", "OPEN"),
185-
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", "my-open-clb"),
242+
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "clb_name", MyOpenClbName),
186243
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "master_zone_id", "100003"),
187244
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "slave_zone_id", "100004"),
188245
resource.TestCheckResourceAttr("tencentcloud_clb_instance.multiple_instance", "tags.test", "open"),
@@ -241,7 +298,10 @@ func testAccCheckClbInstanceExists(n string) resource.TestCheckFunc {
241298
const testAccClbInstance_basic = `
242299
resource "tencentcloud_clb_instance" "clb_basic" {
243300
network_type = "OPEN"
244-
clb_name = "tf-clb-basic"
301+
clb_name = "` + BasicClbName + `"
302+
tags = {
303+
test = "tf"
304+
}
245305
}
246306
`
247307

@@ -265,7 +325,7 @@ resource "tencentcloud_subnet" "subnet" {
265325
266326
resource "tencentcloud_clb_instance" "clb_internal" {
267327
network_type = "INTERNAL"
268-
clb_name = "tf-clb-internal"
328+
clb_name = "` + InternalClbName + `"
269329
vpc_id = tencentcloud_vpc.foo.id
270330
subnet_id = tencentcloud_subnet.subnet.id
271331
project_id = 0
@@ -288,7 +348,7 @@ resource "tencentcloud_vpc" "foo" {
288348
289349
resource "tencentcloud_clb_instance" "clb_open" {
290350
network_type = "OPEN"
291-
clb_name = "tf-clb-open"
351+
clb_name = "` + OpenClbName + `"
292352
project_id = 0
293353
vpc_id = tencentcloud_vpc.foo.id
294354
target_region_info_region = "ap-guangzhou"
@@ -321,7 +381,7 @@ resource "tencentcloud_subnet" "subnet" {
321381
322382
resource "tencentcloud_clb_instance" "clb_internal" {
323383
network_type = "INTERNAL"
324-
clb_name = "tf-clb-update-internal"
384+
clb_name = "` + InternalClbNameUpdate + `"
325385
vpc_id = tencentcloud_vpc.foo.id
326386
subnet_id = tencentcloud_subnet.subnet.id
327387
project_id = 0
@@ -344,7 +404,7 @@ resource "tencentcloud_vpc" "foo" {
344404
345405
resource "tencentcloud_clb_instance" "clb_open" {
346406
network_type = "OPEN"
347-
clb_name = "tf-clb-update-open"
407+
clb_name = "` + OpenClbNameUpdate + `"
348408
vpc_id = tencentcloud_vpc.foo.id
349409
project_id = 0
350410
target_region_info_region = "ap-guangzhou"
@@ -387,7 +447,7 @@ resource "tencentcloud_vpc" "foo" {
387447
388448
resource "tencentcloud_clb_instance" "default_enable" {
389449
network_type = "OPEN"
390-
clb_name = "my-open-clb"
450+
clb_name = "` + MyOpenClbName + `"
391451
project_id = 0
392452
vpc_id = tencentcloud_vpc.foo.id
393453
load_balancer_pass_to_target = true
@@ -432,7 +492,7 @@ resource "tencentcloud_vpc" "foo" {
432492
433493
resource "tencentcloud_clb_instance" "default_enable" {
434494
network_type = "OPEN"
435-
clb_name = "my-open-clb"
495+
clb_name = "` + MyOpenClbName + `"
436496
project_id = 0
437497
vpc_id = tencentcloud_vpc.foo.id
438498
load_balancer_pass_to_target = true
@@ -450,7 +510,7 @@ resource "tencentcloud_clb_instance" "default_enable" {
450510
const testAccClbInstance__multi_instance = `
451511
resource "tencentcloud_clb_instance" "multiple_instance" {
452512
network_type = "OPEN"
453-
clb_name = "my-open-clb"
513+
clb_name = "` + MyOpenClbName + `"
454514
master_zone_id = "100003"
455515
slave_zone_id = "100004"
456516
@@ -463,7 +523,7 @@ resource "tencentcloud_clb_instance" "multiple_instance" {
463523
const testAccClbInstance__multi_instance_update = `
464524
resource "tencentcloud_clb_instance" "multiple_instance" {
465525
network_type = "OPEN"
466-
clb_name = "my-open-clb"
526+
clb_name = "` + MyOpenClbName + `"
467527
master_zone_id = "100003"
468528
slave_zone_id = "100004"
469529

tencentcloud/resource_tc_clb_log_topic_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ func TestAccTencentCloudClbInstanceTopic(t *testing.T) {
2020
{
2121
Config: testAccClbInstanceTopic,
2222
Check: resource.ComposeTestCheckFunc(
23-
testAccCheckClbInstanceTopicExists("tencentcloud_clb_instances_topic.topic"),
24-
resource.TestCheckResourceAttr("tencentcloud_clb_instances_topic.topic", "topic_name", "clb-topic-test"),
25-
resource.TestCheckResourceAttr("tencentcloud_clb_instances_topic.topic", "partition_count", "3"),
23+
testAccCheckClbInstanceTopicExists("tencentcloud_clb_log_topic.topic"),
24+
resource.TestCheckResourceAttr("tencentcloud_clb_log_topic.topic", "topic_name", "clb-topic-test"),
2625
),
2726
},
2827
},
@@ -44,8 +43,7 @@ func testAccCheckClbInstanceTopicExists(n string) resource.TestCheckFunc {
4443
clsService := ClsService{
4544
client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn,
4645
}
47-
topicName := rs.Primary.Attributes["topic_name"]
48-
instance, err := clsService.DescribeTopicsById(ctx, topicName)
46+
instance, err := clsService.DescribeTopicsById(ctx, rs.Primary.ID)
4947
if err != nil {
5048
return err
5149
}
@@ -58,8 +56,12 @@ func testAccCheckClbInstanceTopicExists(n string) resource.TestCheckFunc {
5856
}
5957

6058
const testAccClbInstanceTopic = `
59+
resource "tencentcloud_clb_log_set" "set1" {
60+
period = 7
61+
}
62+
6163
resource "tencentcloud_clb_log_topic" "topic" {
64+
log_set_id = tencentcloud_clb_log_set.set1.id
6265
topic_name="clb-topic-test"
63-
partition_count=3
6466
}
6567
`

tencentcloud/resource_tc_clb_target_group_attachment_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ resource "tencentcloud_vpc" "foo" {
145145
146146
resource "tencentcloud_clb_instance" "clb_basic" {
147147
network_type = "OPEN"
148-
clb_name = "tf-clb-basic"
148+
clb_name = "tf-clb-attach-basic"
149149
vpc_id = tencentcloud_vpc.foo.id
150150
}
151151

tencentcloud/resource_tc_clb_target_group_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ resource "tencentcloud_clb_target_group" "test"{
116116
`
117117

118118
const testAccClbInstanceTargetGroup = `
119-
resource "tencentcloud_clb_instance" "target_group" {
119+
resource "tencentcloud_clb_target_group" "target_group" {
120120
target_group_name = "tgt_grp_test"
121121
port = 33
122122
target_group_instances {
@@ -127,7 +127,7 @@ resource "tencentcloud_clb_instance" "target_group" {
127127
`
128128

129129
const testAccClbInstanceTargetGroupUpdate = `
130-
resource "tencentcloud_clb_instance" "target_group" {
130+
resource "tencentcloud_clb_target_group" "target_group" {
131131
target_group_name = "tgt_grp_test"
132132
port = 44
133133
target_group_instances {

0 commit comments

Comments
 (0)