@@ -3,12 +3,69 @@ package tencentcloud
33import (
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+
1269func 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 {
241298const testAccClbInstance_basic = `
242299resource "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
266326resource "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
289349resource "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
322382resource "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
345405resource "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
388448resource "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
433493resource "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" {
450510const testAccClbInstance__multi_instance = `
451511resource "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" {
463523const testAccClbInstance__multi_instance_update = `
464524resource "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
0 commit comments