11package tencentcloud
22
33import (
4- "context"
5- "fmt"
6- "strings"
74 "testing"
85
96 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10- "github.com/hashicorp/terraform-plugin-sdk/terraform"
117)
128
139var testAPIGatewaythrottlingApiDataSourceName = "data.tencentcloud_api_gateway_throttling_apis"
@@ -16,12 +12,12 @@ func TestAccTencentAPIGatewayThrottlingApisDataSource(t *testing.T) {
1612 resource .Test (t , resource.TestCase {
1713 PreCheck : func () { testAccPreCheck (t ) },
1814 Providers : testAccProviders ,
19- CheckDestroy : testAccCheckThrottlingAPIDestroy ,
15+ CheckDestroy : testAccCheckAPIGatewayAPIDestroy ,
2016 Steps : []resource.TestStep {
2117 {
2218 Config : testAccTestAccTencentAPIGatewayThrottlingApis (),
2319 Check : resource .ComposeAggregateTestCheckFunc (
24- testAccCheckThrottlingAPIExists ( "tencentcloud_api_gateway_throttling_api.service " ),
20+ testAccCheckAPIGatewayAPIExists ( "tencentcloud_api_gateway_api.api " ),
2521 resource .TestCheckResourceAttrSet (testAPIGatewaythrottlingApiDataSourceName + ".id" , "list.#" ),
2622 resource .TestCheckResourceAttrSet (testAPIGatewaythrottlingApiDataSourceName + ".id" , "list.0.service_id" ),
2723 resource .TestCheckResourceAttrSet (testAPIGatewaythrottlingApiDataSourceName + ".id" , "list.0.api_environment_strategies.#" ),
@@ -49,87 +45,6 @@ func TestAccTencentAPIGatewayThrottlingApisDataSource(t *testing.T) {
4945 })
5046}
5147
52- func testAccCheckThrottlingAPIDestroy (s * terraform.State ) error {
53- var (
54- logId = getLogId (contextNil )
55- ctx = context .WithValue (context .TODO (), logIdKey , logId )
56- throttlingService = APIGatewayService {client : testAccProvider .Meta ().(* TencentCloudClient ).apiV3Conn }
57- )
58- for _ , rs := range s .RootModule ().Resources {
59- if rs .Type != "tencentcloud_api_gateway_throttling_api" {
60- continue
61- }
62-
63- serviceId := rs .Primary .Attributes ["service_id" ]
64- environmentName := rs .Primary .Attributes ["environment_name" ]
65- apiIds := rs .Primary .Attributes ["api_ids" ]
66- environmentList , err := throttlingService .DescribeApiEnvironmentStrategyList (ctx , serviceId , []string {environmentName }, "" )
67- if err != nil {
68- return err
69- }
70-
71- for _ , v := range environmentList {
72- if v == nil || ! strings .Contains (apiIds , * v .ApiId ) {
73- continue
74- }
75- environmentSet := v .EnvironmentStrategySet
76- for _ , env := range environmentSet {
77- if env == nil || * env .EnvironmentName != environmentName {
78- continue
79- }
80-
81- if * env .Quota == QUOTA || * env .Quota == QUOTA_MAX {
82- continue
83- }
84- return fmt .Errorf ("throttling API still not restore: %s" , rs .Primary .ID )
85- }
86- }
87- }
88- return nil
89- }
90-
91- func testAccCheckThrottlingAPIExists (n string ) resource.TestCheckFunc {
92- return func (s * terraform.State ) error {
93- var (
94- logId = getLogId (contextNil )
95- ctx = context .WithValue (context .TODO (), logIdKey , logId )
96- throttlingService = APIGatewayService {client : testAccProvider .Meta ().(* TencentCloudClient ).apiV3Conn }
97- )
98-
99- rs , ok := s .RootModule ().Resources [n ]
100- if ! ok {
101- return fmt .Errorf ("API Getway throttling API %s is not found" , n )
102- }
103- if rs .Primary .ID == "" {
104- return fmt .Errorf ("API Getway throttling API id is not set" )
105- }
106- serviceId := rs .Primary .Attributes ["service_id" ]
107- environmentName := rs .Primary .Attributes ["environment_name" ]
108- apiIds := rs .Primary .Attributes ["api_ids" ]
109- environmentList , err := throttlingService .DescribeApiEnvironmentStrategyList (ctx , serviceId , []string {environmentName }, "" )
110- if err != nil {
111- return err
112- }
113-
114- for _ , v := range environmentList {
115- if v == nil || ! strings .Contains (apiIds , * v .ApiId ) {
116- continue
117- }
118- environmentSet := v .EnvironmentStrategySet
119- for _ , env := range environmentSet {
120- if env == nil || * env .EnvironmentName != environmentName {
121- continue
122- }
123-
124- if * env .Quota == QUOTA {
125- return fmt .Errorf ("throttling API still not set value: %s" , rs .Primary .ID )
126- }
127- }
128- }
129- return nil
130- }
131- }
132-
13348func testAccTestAccTencentAPIGatewayThrottlingApis () string {
13449 return `
13550resource "tencentcloud_api_gateway_service" "service" {
@@ -164,29 +79,27 @@ resource "tencentcloud_api_gateway_api" "api" {
16479 service_config_method = "POST"
16580 response_type = "XML"
16681 response_success_example = "<note>success</note>"
167- response_fail_example = "<note>fail</note>"
82+ response_fail_example = "<note>fail</note>"
83+
16884 response_error_codes {
16985 code = 10
17086 msg = "system error"
17187 desc = "system error code"
17288 converted_code = -10
17389 need_convert = true
174- }
175- }
176-
177- resource "tencentcloud_api_gateway_throttling_api" "service" {
178- service_id = tencentcloud_api_gateway_service.service.id
179- strategy = "400"
180- environment_name = "test"
181- api_ids = [tencentcloud_api_gateway_api.api.id]
90+ }
91+
92+ release_limit = 100
93+ pre_limit = 100
94+ test_limit = 100
18295}
18396
18497data "tencentcloud_api_gateway_throttling_apis" "id" {
185- service_id = tencentcloud_api_gateway_throttling_api.service .service_id
98+ service_id = tencentcloud_api_gateway_api.api .service_id
18699}
187100
188101data "tencentcloud_api_gateway_throttling_apis" "foo" {
189- service_id = tencentcloud_api_gateway_throttling_api.service .service_id
102+ service_id = tencentcloud_api_gateway_api.api .service_id
190103 environment_names = ["release", "test"]
191104}
192105`
0 commit comments