Skip to content

Commit 6a509e9

Browse files
authored
fix as testing failed case (#1339)
1 parent e231186 commit 6a509e9

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

tencentcloud/resource_tc_as_attachment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func resourceTencentCloudAsAttachmentRead(d *schema.ResourceData, meta interface
7878
}
7979
var instanceIds []string
8080
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
81-
result, errRet := asService.DescribeAutoScalingAttachment(ctx, scalingGroupId)
81+
result, errRet := asService.DescribeAutoScalingAttachment(ctx, scalingGroupId, false)
8282
if errRet != nil {
8383
return retryError(errRet)
8484
}
@@ -142,7 +142,7 @@ func resourceTencentCloudAsAttachmentDelete(d *schema.ResourceData, meta interfa
142142
}
143143
var instanceIds []string
144144
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
145-
result, errRet := asService.DescribeAutoScalingAttachment(ctx, scalingGroupId)
145+
result, errRet := asService.DescribeAutoScalingAttachment(ctx, scalingGroupId, false)
146146
if errRet != nil {
147147
return retryError(errRet)
148148
}

tencentcloud/resource_tc_as_attachment_test.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,64 @@ package tencentcloud
33
import (
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+
1364
func 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 {

tencentcloud/service_tencentcloud_as.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (me *AsService) DetachInstances(ctx context.Context, scalingGroupId string,
301301
return nil
302302
}
303303

304-
func (me *AsService) DescribeAutoScalingAttachment(ctx context.Context, scalingGroupId string) (instanceIds []string, errRet error) {
304+
func (me *AsService) DescribeAutoScalingAttachment(ctx context.Context, scalingGroupId string, fully bool) (instanceIds []string, errRet error) {
305305
logId := getLogId(ctx)
306306
request := as.NewDescribeAutoScalingInstancesRequest()
307307
request.Filters = []*as.Filter{
@@ -321,7 +321,7 @@ func (me *AsService) DescribeAutoScalingAttachment(ctx context.Context, scalingG
321321

322322
instanceIds = make([]string, 0)
323323
for _, instance := range response.Response.AutoScalingInstanceSet {
324-
if *instance.CreationType == "MANUAL_ATTACHING" {
324+
if *instance.CreationType == "MANUAL_ATTACHING" || fully {
325325
instanceIds = append(instanceIds, *instance.InstanceId)
326326
}
327327
}

0 commit comments

Comments
 (0)