11package tencentcloud
22
33import (
4+ "context"
5+ "fmt"
6+ "strings"
47 "testing"
58
69 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+ "github.com/hashicorp/terraform-plugin-sdk/terraform"
11+ "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
712)
813
9- func TestAccTencentCloudNeedFixTemAppConfig_basic (t * testing.T ) {
14+ // go test -i; go test -test.run TestAccTencentCloudNeedFixTemAppConfigResource_basic -v
15+ func TestAccTencentCloudNeedFixTemAppConfigResource_basic (t * testing.T ) {
1016 t .Parallel ()
11-
1217 resource .Test (t , resource.TestCase {
13- PreCheck : func () { testAccPreCheck (t ) },
14- Providers : testAccProviders ,
18+ PreCheck : func () {
19+ testAccPreCheck (t )
20+ },
21+ Providers : testAccProviders ,
22+ CheckDestroy : testAccCheckTemAppConfigDestroy ,
1523 Steps : []resource.TestStep {
1624 {
1725 Config : testAccTemAppConfig ,
1826 Check : resource .ComposeTestCheckFunc (
27+ testAccCheckTemAppConfigExists ("tencentcloud_tem_app_config.appConfig" ),
1928 resource .TestCheckResourceAttrSet ("tencentcloud_tem_app_config.appConfig" , "id" ),
29+ resource .TestCheckResourceAttr ("tencentcloud_tem_app_config.appConfig" , "environment_id" , defaultEnvironmentId ),
30+ resource .TestCheckResourceAttr ("tencentcloud_tem_app_config.appConfig" , "name" , "demo" ),
31+ resource .TestCheckResourceAttr ("tencentcloud_tem_app_config.appConfig" , "config_data.#" , "1" ),
32+ resource .TestCheckResourceAttr ("tencentcloud_tem_app_config.appConfig" , "config_data.0.key" , "key" ),
33+ resource .TestCheckResourceAttr ("tencentcloud_tem_app_config.appConfig" , "config_data.0.value" , "value" ),
2034 ),
2135 },
2236 {
@@ -28,19 +42,84 @@ func TestAccTencentCloudNeedFixTemAppConfig_basic(t *testing.T) {
2842 })
2943}
3044
31- const testAccTemAppConfig = `
45+ func testAccCheckTemAppConfigDestroy (s * terraform.State ) error {
46+ logId := getLogId (contextNil )
47+ ctx := context .WithValue (context .TODO (), logIdKey , logId )
48+ service := TemService {client : testAccProvider .Meta ().(* TencentCloudClient ).apiV3Conn }
49+ for _ , rs := range s .RootModule ().Resources {
50+ if rs .Type != "tencentcloud_tem_app_config" {
51+ continue
52+ }
53+ idSplit := strings .Split (rs .Primary .ID , FILED_SP )
54+ if len (idSplit ) != 2 {
55+ return fmt .Errorf ("id is broken,%s" , rs .Primary .ID )
56+ }
57+ environmentId := idSplit [0 ]
58+ name := idSplit [1 ]
59+
60+ res , err := service .DescribeTemAppConfig (ctx , environmentId , name )
61+ if err != nil {
62+ if sdkErr , ok := err .(* errors.TencentCloudSDKError ); ok {
63+ if sdkErr .Code == "InternalError.DescribeConfigDataError" {
64+ return nil
65+ }
66+ }
67+ return err
68+ }
69+
70+ if res != nil {
71+ return fmt .Errorf ("tem app config %s still exists" , rs .Primary .ID )
72+ }
73+ }
74+ return nil
75+ }
76+
77+ func testAccCheckTemAppConfigExists (r string ) resource.TestCheckFunc {
78+ return func (s * terraform.State ) error {
79+ logId := getLogId (contextNil )
80+ ctx := context .WithValue (context .TODO (), logIdKey , logId )
81+
82+ rs , ok := s .RootModule ().Resources [r ]
83+ if ! ok {
84+ return fmt .Errorf ("resource %s is not found" , r )
85+ }
86+
87+ idSplit := strings .Split (rs .Primary .ID , FILED_SP )
88+ if len (idSplit ) != 2 {
89+ return fmt .Errorf ("id is broken,%s" , rs .Primary .ID )
90+ }
91+ environmentId := idSplit [0 ]
92+ name := idSplit [1 ]
93+
94+ service := TemService {client : testAccProvider .Meta ().(* TencentCloudClient ).apiV3Conn }
95+ res , err := service .DescribeTemAppConfig (ctx , environmentId , name )
96+ if err != nil {
97+ return err
98+ }
99+
100+ if res == nil {
101+ return fmt .Errorf ("tem app config %s is not found" , rs .Primary .ID )
102+ }
103+
104+ return nil
105+ }
106+ }
107+
108+ const testAccTemAppConfigVar = `
109+ variable "environment_id" {
110+ default = "` + defaultEnvironmentId + `"
111+ }
112+ `
113+
114+ const testAccTemAppConfig = testAccTemAppConfigVar + `
32115
33116resource "tencentcloud_tem_app_config" "appConfig" {
34- environment_id = "en-o5edaepv"
117+ environment_id = var.environment_id
35118 name = "demo"
36119 config_data {
37120 key = "key"
38121 value = "value"
39122 }
40- config_data {
41- key = "key1"
42- value = "value1"
43- }
44123}
45124
46125`
0 commit comments