@@ -3,13 +3,64 @@ package tencentcloud
33import (
44 "context"
55 "fmt"
6+ "log"
7+ "strings"
68 "testing"
79
810 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
911 "github.com/hashicorp/terraform-plugin-sdk/terraform"
1012 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1113)
1214
15+ func init () {
16+ resource .AddTestSweepers ("tencentcloud_as_attachment" , & resource.Sweeper {
17+ Name : "tencentcloud_as_attachment" ,
18+ F : testSweepAsAttachment ,
19+ })
20+ }
21+
22+ // go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_as_attachment
23+ func testSweepAsAttachment (r string ) error {
24+ logId := getLogId (contextNil )
25+ ctx := context .WithValue (context .TODO (), logIdKey , logId )
26+ cli , _ := sharedClientForRegion (r )
27+ asService := AsService {client : cli .(* TencentCloudClient ).apiV3Conn }
28+
29+ scalingGroups , err := asService .DescribeAutoScalingGroupByFilter (ctx , "" , "" , "" , nil )
30+ if err != nil {
31+ return fmt .Errorf ("list scaling group error: %s" , err .Error ())
32+ }
33+
34+ for _ , v := range scalingGroups {
35+ scalingGroupId := * v .AutoScalingGroupId
36+ scalingGroupName := * v .AutoScalingGroupName
37+ if ! strings .HasPrefix (scalingGroupName , "tf-as-" ) {
38+ continue
39+ }
40+
41+ var instanceIds []string
42+ err := resource .Retry (readRetryTimeout , func () * resource.RetryError {
43+ result , errRet := asService .DescribeAutoScalingAttachment (ctx , scalingGroupId , true )
44+ if errRet != nil {
45+ return retryError (errRet )
46+ }
47+ instanceIds = result
48+ return nil
49+ })
50+ if err != nil {
51+ return err
52+ }
53+ if len (instanceIds ) == 0 {
54+ continue
55+ }
56+
57+ if err = asService .DetachInstances (ctx , scalingGroupId , instanceIds ); err != nil {
58+ log .Printf ("[ERROR] delete scaling group %s error: %s" , scalingGroupId , err .Error ())
59+ }
60+ }
61+ return nil
62+ }
63+
1364func TestAccTencentCloudAsAttachment (t * testing.T ) {
1465 t .Parallel ()
1566 resource .Test (t , resource.TestCase {
@@ -51,7 +102,7 @@ func testAccCheckAsAttachmentExists(n string) resource.TestCheckFunc {
51102 return fmt .Errorf ("auto scaling attachment id is not set" )
52103 }
53104 asService := AsService {client : testAccProvider .Meta ().(* TencentCloudClient ).apiV3Conn }
54- instances , err := asService .DescribeAutoScalingAttachment (ctx , rs .Primary .ID )
105+ instances , err := asService .DescribeAutoScalingAttachment (ctx , rs .Primary .ID , false )
55106 if err != nil {
56107 return err
57108 }
@@ -74,7 +125,7 @@ func testAccCheckAsAttachmentDestroy(s *terraform.State) error {
74125 continue
75126 }
76127
77- instances , err := asService .DescribeAutoScalingAttachment (ctx , rs .Primary .ID )
128+ instances , err := asService .DescribeAutoScalingAttachment (ctx , rs .Primary .ID , false )
78129 if err != nil {
79130 if sdkErr , ok := err .(* errors.TencentCloudSDKError ); ok {
80131 if sdkErr .Code == AsScalingGroupNotFound {
0 commit comments