Skip to content

Commit 4018e2e

Browse files
committed
feat/apigw
1 parent 897fbf8 commit 4018e2e

12 files changed

+1194
-0
lines changed

.changelog/2064.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ tencentcloud_api_gateway_api_app_attachment
66
tencentcloud_api_gateway_upstream
77
```
88

9+
```release-note:new-data-source
10+
tencentcloud_api_gateway_plugins
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_api_gateway_upstreams
15+
```
16+
917
```release-note:enhancement
1018
resource/tencentcloud_api_gateway_api_app: Support `tag`
1119
```
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/*
2+
Use this data source to query detailed information of apigateway plugin
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_api_gateway_plugins" "example" {
8+
service_id = tencentcloud_api_gateway_service_release.example.service_id
9+
plugin_id = tencentcloud_api_gateway_plugin.example.id
10+
environment_name = "release"
11+
}
12+
13+
resource "tencentcloud_api_gateway_service" "example" {
14+
service_name = "tf_example"
15+
protocol = "http&https"
16+
service_desc = "desc."
17+
net_type = ["INNER", "OUTER"]
18+
ip_version = "IPv4"
19+
tags = {
20+
testKey = "testValue"
21+
}
22+
release_limit = 500
23+
pre_limit = 500
24+
test_limit = 500
25+
}
26+
27+
resource "tencentcloud_api_gateway_api" "example" {
28+
service_id = tencentcloud_api_gateway_service.example.id
29+
api_name = "hello"
30+
api_desc = "my hello api"
31+
auth_type = "NONE"
32+
protocol = "HTTP"
33+
enable_cors = true
34+
request_config_path = "/user/info"
35+
request_config_method = "GET"
36+
37+
request_parameters {
38+
name = "name"
39+
position = "QUERY"
40+
type = "string"
41+
desc = "who are you?"
42+
default_value = "tom"
43+
required = true
44+
}
45+
service_config_type = "HTTP"
46+
service_config_timeout = 15
47+
service_config_url = "http://www.qq.com"
48+
service_config_path = "/user"
49+
service_config_method = "GET"
50+
response_type = "HTML"
51+
response_success_example = "success"
52+
response_fail_example = "fail"
53+
response_error_codes {
54+
code = 500
55+
msg = "system error"
56+
desc = "system error code"
57+
converted_code = 5000
58+
need_convert = true
59+
}
60+
}
61+
62+
resource "tencentcloud_api_gateway_service_release" "example" {
63+
service_id = tencentcloud_api_gateway_api.example.service_id
64+
environment_name = "release"
65+
release_desc = "desc."
66+
}
67+
68+
resource "tencentcloud_api_gateway_plugin" "example" {
69+
plugin_name = "tf-example"
70+
plugin_type = "IPControl"
71+
plugin_data = jsonencode({
72+
"type" : "white_list",
73+
"blocks" : "1.1.1.1",
74+
})
75+
description = "desc."
76+
}
77+
```
78+
*/
79+
package tencentcloud
80+
81+
import (
82+
"context"
83+
84+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
85+
86+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
87+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88+
apigateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808"
89+
)
90+
91+
func dataSourceTencentCloudAPIGatewayPlugins() *schema.Resource {
92+
return &schema.Resource{
93+
Read: dataSourceTencentCloudAPIGatewayPluginRead,
94+
Schema: map[string]*schema.Schema{
95+
"service_id": {
96+
Required: true,
97+
Type: schema.TypeString,
98+
Description: "The service ID to query.",
99+
},
100+
"plugin_id": {
101+
Required: true,
102+
Type: schema.TypeString,
103+
Description: "The plugin ID to query.",
104+
},
105+
"environment_name": {
106+
Required: true,
107+
Type: schema.TypeString,
108+
Description: "Environmental information.",
109+
},
110+
"result": {
111+
Computed: true,
112+
Type: schema.TypeList,
113+
Description: "List of plugin related APIs.",
114+
Elem: &schema.Resource{
115+
Schema: map[string]*schema.Schema{
116+
"api_id": {
117+
Type: schema.TypeString,
118+
Computed: true,
119+
Description: "API ID.",
120+
},
121+
"api_name": {
122+
Type: schema.TypeString,
123+
Computed: true,
124+
Description: "API name.",
125+
},
126+
"api_type": {
127+
Type: schema.TypeString,
128+
Computed: true,
129+
Description: "API type.",
130+
},
131+
"path": {
132+
Type: schema.TypeString,
133+
Computed: true,
134+
Description: "API path.",
135+
},
136+
"method": {
137+
Type: schema.TypeString,
138+
Computed: true,
139+
Description: "API method.",
140+
},
141+
"attached_other_plugin": {
142+
Type: schema.TypeBool,
143+
Computed: true,
144+
Description: "Whether the API is bound to other plugins.Note: This field may return null, indicating that a valid value cannot be obtained.",
145+
},
146+
"is_attached": {
147+
Type: schema.TypeBool,
148+
Computed: true,
149+
Description: "Whether the API is bound to the current plugin.Note: This field may return null, indicating that a valid value cannot be obtained.",
150+
},
151+
},
152+
},
153+
},
154+
"result_output_file": {
155+
Type: schema.TypeString,
156+
Optional: true,
157+
Description: "Used to save results.",
158+
},
159+
},
160+
}
161+
}
162+
163+
func dataSourceTencentCloudAPIGatewayPluginRead(d *schema.ResourceData, meta interface{}) error {
164+
defer logElapsed("data_source.tencentcloud_api_gateway_plugins.read")()
165+
defer inconsistentCheck(d, meta)()
166+
167+
var (
168+
logId = getLogId(contextNil)
169+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
170+
service = APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn}
171+
infos []*apigateway.AvailableApiInfo
172+
)
173+
174+
paramMap := make(map[string]interface{})
175+
if v, ok := d.GetOk("service_id"); ok {
176+
paramMap["ServiceId"] = helper.String(v.(string))
177+
}
178+
179+
if v, ok := d.GetOk("plugin_id"); ok {
180+
paramMap["PluginId"] = helper.String(v.(string))
181+
}
182+
183+
if v, ok := d.GetOk("environment_name"); ok {
184+
paramMap["EnvironmentName"] = helper.String(v.(string))
185+
}
186+
187+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
188+
result, e := service.DescribeAPIGatewayPluginByFilter(ctx, paramMap)
189+
if e != nil {
190+
return retryError(e)
191+
}
192+
193+
infos = result
194+
return nil
195+
})
196+
197+
if err != nil {
198+
return err
199+
}
200+
201+
ids := make([]string, 0, len(infos))
202+
if infos != nil {
203+
apiSetList := []interface{}{}
204+
for _, apiSet := range infos {
205+
apiSetMap := map[string]interface{}{}
206+
if apiSet.ApiId != nil {
207+
apiSetMap["api_id"] = apiSet.ApiId
208+
}
209+
210+
if apiSet.ApiName != nil {
211+
apiSetMap["api_name"] = apiSet.ApiName
212+
}
213+
214+
if apiSet.ApiType != nil {
215+
apiSetMap["api_type"] = apiSet.ApiType
216+
}
217+
218+
if apiSet.Path != nil {
219+
apiSetMap["path"] = apiSet.Path
220+
}
221+
222+
if apiSet.Method != nil {
223+
apiSetMap["method"] = apiSet.Method
224+
}
225+
226+
if apiSet.AttachedOtherPlugin != nil {
227+
apiSetMap["attached_other_plugin"] = apiSet.AttachedOtherPlugin
228+
}
229+
230+
if apiSet.IsAttached != nil {
231+
apiSetMap["is_attached"] = apiSet.IsAttached
232+
}
233+
234+
apiSetList = append(apiSetList, apiSetMap)
235+
ids = append(ids, *apiSet.ApiId)
236+
}
237+
238+
_ = d.Set("result", apiSetList)
239+
}
240+
241+
d.SetId(helper.DataResourceIdsHash(ids))
242+
output, ok := d.GetOk("result_output_file")
243+
if ok && output.(string) != "" {
244+
if e := writeToFile(output.(string), d); e != nil {
245+
return e
246+
}
247+
}
248+
249+
return nil
250+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudApigatewayPluginsDataSource_basic -v
10+
func TestAccTencentCloudApigatewayPluginsDataSource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccApigatewayPluginDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_api_gateway_plugins.example"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const testAccApigatewayPluginDataSource = `
29+
data "tencentcloud_api_gateway_plugins" "example" {
30+
service_id = tencentcloud_api_gateway_service_release.example.service_id
31+
plugin_id = tencentcloud_api_gateway_plugin.example.id
32+
environment_name = "release"
33+
}
34+
35+
resource "tencentcloud_api_gateway_service" "example" {
36+
service_name = "tf_example"
37+
protocol = "http&https"
38+
service_desc = "desc."
39+
net_type = ["INNER", "OUTER"]
40+
ip_version = "IPv4"
41+
tags = {
42+
testKey = "testValue"
43+
}
44+
release_limit = 500
45+
pre_limit = 500
46+
test_limit = 500
47+
}
48+
49+
resource "tencentcloud_api_gateway_api" "example" {
50+
service_id = tencentcloud_api_gateway_service.example.id
51+
api_name = "hello"
52+
api_desc = "my hello api"
53+
auth_type = "NONE"
54+
protocol = "HTTP"
55+
enable_cors = true
56+
request_config_path = "/user/info"
57+
request_config_method = "GET"
58+
59+
request_parameters {
60+
name = "name"
61+
position = "QUERY"
62+
type = "string"
63+
desc = "who are you?"
64+
default_value = "tom"
65+
required = true
66+
}
67+
service_config_type = "HTTP"
68+
service_config_timeout = 15
69+
service_config_url = "http://www.qq.com"
70+
service_config_path = "/user"
71+
service_config_method = "GET"
72+
response_type = "HTML"
73+
response_success_example = "success"
74+
response_fail_example = "fail"
75+
response_error_codes {
76+
code = 500
77+
msg = "system error"
78+
desc = "system error code"
79+
converted_code = 5000
80+
need_convert = true
81+
}
82+
}
83+
84+
resource "tencentcloud_api_gateway_service_release" "example" {
85+
service_id = tencentcloud_api_gateway_api.example.service_id
86+
environment_name = "release"
87+
release_desc = "desc."
88+
}
89+
90+
resource "tencentcloud_api_gateway_plugin" "example" {
91+
plugin_name = "tf-example"
92+
plugin_type = "IPControl"
93+
plugin_data = jsonencode({
94+
"type" : "white_list",
95+
"blocks" : "1.1.1.1",
96+
})
97+
description = "desc."
98+
}
99+
`

0 commit comments

Comments
 (0)