6
6
"log"
7
7
"strings"
8
8
"testing"
9
-
10
- vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
11
- "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
9
+ "time"
12
10
13
11
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
14
12
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -35,10 +33,7 @@ func testSweepSecurityGroups(region string) error {
35
33
client : client .apiV3Conn ,
36
34
}
37
35
38
- sgs , err := service .DescribeSecurityGroups (ctx , nil , helper .String (NamePrefix ), nil , nil )
39
- var sgIds []* string
40
- var candidates []string
41
- var referredSgs = make (map [string ][]* string , 0 )
36
+ sgs , err := service .DescribeSecurityGroups (ctx , nil , nil , nil , nil )
42
37
43
38
if err != nil {
44
39
return fmt .Errorf ("DescribeSecurityGroups error: %s" , err .Error ())
@@ -47,43 +42,25 @@ func testSweepSecurityGroups(region string) error {
47
42
for _ , v := range sgs {
48
43
name := * v .SecurityGroupName
49
44
id := * v .SecurityGroupId
50
- sgIds = append (sgIds , v .SecurityGroupId )
51
- if ! strings .HasPrefix (name , NamePrefix ) {
52
- continue
53
- }
54
- candidates = append (candidates , id )
55
-
56
- }
57
45
58
- refReq := vpc .NewDescribeSecurityGroupReferencesRequest ()
59
- refReq .SecurityGroupIds = sgIds
46
+ now := time .Now ()
47
+ createTime := stringTotime (* v .CreatedTime )
48
+ interval := now .Sub (createTime ).Minutes ()
60
49
61
- refRes , err := client .apiV3Conn .UseVpcClient ().DescribeSecurityGroupReferences (refReq )
62
- if err != nil {
63
- return fmt .Errorf ("DescribeSecurityGroupReferences error: %s" , err .Error ())
64
- }
65
- for _ , v := range refRes .Response .ReferredSecurityGroupSet {
66
- if len (v .ReferredSecurityGroupIds ) > 0 {
67
- referredSgs [* v .SecurityGroupId ] = v .ReferredSecurityGroupIds
50
+ if strings .HasPrefix (name , keepResource ) || strings .HasPrefix (name , defaultResource ) {
51
+ continue
68
52
}
69
- }
70
-
71
- res , err := service .DescribeSecurityGroupsAssociate (ctx , candidates )
72
- if err != nil {
73
- return fmt .Errorf ("DescribeSecurityGroupsAssociate error: %s" , err .Error ())
74
- }
75
53
76
- for _ , v := range res {
77
- id := * v .SecurityGroupId
78
-
79
- if * v .TotalCount > 0 || len (referredSgs [id ]) > 0 {
54
+ // less than 30 minute, not delete
55
+ if needProtect == 1 && int64 (interval ) < 30 {
80
56
continue
81
57
}
82
58
83
59
if err := service .DeleteSecurityGroup (ctx , id ); err != nil {
84
60
log .Printf ("[ERROR] sweep security group %s error: %s" , id , err .Error ())
85
61
}
86
62
}
63
+
87
64
return nil
88
65
}
89
66
0 commit comments