@@ -4,12 +4,106 @@ import (
44 "context"
55 "fmt"
66 "log"
7+ "strings"
78 "testing"
9+ "time"
810
911 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012 "github.com/hashicorp/terraform-plugin-sdk/terraform"
13+ "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
14+ emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103"
1115)
1216
17+ func init () {
18+ // go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_emr
19+ resource .AddTestSweepers ("tencentcloud_emr" , & resource.Sweeper {
20+ Name : "tencentcloud_emr" ,
21+ F : func (r string ) error {
22+ logId := getLogId (contextNil )
23+ ctx := context .WithValue (context .TODO (), logIdKey , logId )
24+ sharedClient , err := sharedClientForRegion (r )
25+ if err != nil {
26+ return fmt .Errorf ("getting tencentcloud client error: %s" , err .Error ())
27+ }
28+ client := sharedClient .(* TencentCloudClient )
29+
30+ emrService := EMRService {client : client .apiV3Conn }
31+ filters := make (map [string ]interface {})
32+ filters ["display_strategy" ] = DisplayStrategyIsclusterList
33+ clusters , err := emrService .DescribeInstances (ctx , filters )
34+ if err != nil {
35+ return nil
36+ }
37+ for _ , cluster := range clusters {
38+ clusterName := * cluster .ClusterName
39+ if strings .HasPrefix (clusterName , keepResource ) || strings .HasPrefix (clusterName , defaultResource ) {
40+ continue
41+ }
42+ now := time .Now ()
43+ createTime := stringTotime (* cluster .AddTime )
44+ interval := now .Sub (createTime ).Minutes ()
45+ // less than 30 minute, not delete
46+ if needProtect == 1 && int64 (interval ) < 30 {
47+ continue
48+ }
49+ metaDB := cluster .MetaDb
50+ instanceId := * cluster .ClusterId
51+ request := emr .NewTerminateInstanceRequest ()
52+ request .InstanceId = & instanceId
53+ if _ , err = emrService .client .UseEmrClient ().TerminateInstance (request ); err != nil {
54+ return nil
55+ }
56+ err = resource .Retry (10 * readRetryTimeout , func () * resource.RetryError {
57+ clusters , err := emrService .DescribeInstancesById (ctx , instanceId , DisplayStrategyIsclusterList )
58+
59+ if e , ok := err .(* errors.TencentCloudSDKError ); ok {
60+ if e .GetCode () == "InternalError.ClusterNotFound" {
61+ return nil
62+ }
63+ if e .GetCode () == "UnauthorizedOperation" {
64+ return nil
65+ }
66+ }
67+
68+ if len (clusters ) > 0 {
69+ status := * (clusters [0 ].Status )
70+ if status != EmrInternetStatusDeleted {
71+ return resource .RetryableError (
72+ fmt .Errorf ("%v create cluster endpoint status still is %v" , instanceId , status ))
73+ }
74+ }
75+
76+ if err != nil {
77+ return resource .RetryableError (err )
78+ }
79+ return nil
80+ })
81+ if err != nil {
82+ return nil
83+ }
84+
85+ if metaDB != nil && * metaDB != "" {
86+ // remove metadb
87+ mysqlService := MysqlService {client : client .apiV3Conn }
88+
89+ err = resource .Retry (writeRetryTimeout , func () * resource.RetryError {
90+ err := mysqlService .OfflineIsolatedInstances (ctx , * metaDB )
91+ if err != nil {
92+ return retryError (err , InternalError )
93+ }
94+ return nil
95+ })
96+
97+ if err != nil {
98+ return nil
99+ }
100+ }
101+ }
102+ return nil
103+ },
104+ })
105+ }
106+
13107var testEmrClusterResourceKey = "tencentcloud_emr_cluster.emrrrr"
14108
15109func TestAccTencentCloudEmrClusterResource (t * testing.T ) {
@@ -19,13 +113,13 @@ func TestAccTencentCloudEmrClusterResource(t *testing.T) {
19113 Providers : testAccProviders ,
20114 Steps : []resource.TestStep {
21115 {
22- Config : testEmrBasic () ,
116+ Config : testEmrBasic ,
23117 Check : resource .ComposeTestCheckFunc (
24118 testAccCheckEmrExists (testEmrClusterResourceKey ),
25119 resource .TestCheckResourceAttr (testEmrClusterResourceKey , "product_id" , "4" ),
26120 resource .TestCheckResourceAttr (testEmrClusterResourceKey , "display_strategy" , "clusterList" ),
27- resource .TestCheckResourceAttr (testEmrClusterResourceKey , "vpc_settings.vpc_id" , defaultVpcId ),
28- resource .TestCheckResourceAttr (testEmrClusterResourceKey , "vpc_settings.subnet_id" , defaultSubnetId ),
121+ resource .TestCheckResourceAttr (testEmrClusterResourceKey , "vpc_settings.vpc_id" , defaultEMRVpcId ),
122+ resource .TestCheckResourceAttr (testEmrClusterResourceKey , "vpc_settings.subnet_id" , defaultEMRSubnetId ),
29123 resource .TestCheckResourceAttr (testEmrClusterResourceKey , "softwares.0" , "zookeeper-3.6.1" ),
30124 resource .TestCheckResourceAttr (testEmrClusterResourceKey , "support_ha" , "0" ),
31125 resource .TestCheckResourceAttr (testEmrClusterResourceKey , "instance_name" , "emr-test-demo" ),
@@ -88,14 +182,13 @@ func testAccCheckEmrExists(n string) resource.TestCheckFunc {
88182 }
89183}
90184
91- func testEmrBasic () string {
92- return fmt .Sprintf (`
185+ const testEmrBasic = defaultEMRVariable + `
93186resource "tencentcloud_emr_cluster" "emrrrr" {
94187 product_id=4
95188 display_strategy="clusterList"
96189 vpc_settings={
97- vpc_id="%s"
98- subnet_id:"%s"
190+ vpc_id=var.vpc_id
191+ subnet_id=var.subnet_id
99192 }
100193 softwares=[
101194 "zookeeper-3.6.1",
@@ -134,7 +227,6 @@ resource "tencentcloud_emr_cluster" "emrrrr" {
134227 zone="ap-guangzhou-3"
135228 project_id=0
136229 }
137- sg_id="%s"
230+ sg_id=var.sg_id
138231 }
139- ` , defaultVpcId , defaultSubnetId , defaultEMRSgId )
140- }
232+ `
0 commit comments