Skip to content

Commit 389ef52

Browse files
authored
Merge pull request #1496 from tencentcloudstack/feat/add_apigw_plugin
add apigw plugin
2 parents 871fb45 + 175c6f0 commit 389ef52

28 files changed

+10660
-1892
lines changed

.changelog/1496.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_api_gateway_plugin
3+
```

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/stretchr/testify v1.5.1
2424
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/antiddos v1.0.358
2525
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285
26-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.199
26+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.571
2727
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.0.466
2828
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409
2929
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.520
@@ -35,7 +35,7 @@ require (
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.445
3636
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
3737
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.412
38-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.569
38+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.571
3939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.553
4040
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488
4141
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285 h1:gFmukRG
458458
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/api v1.0.285/go.mod h1:aGlXSWjtSnE6kuqcaRy/NKj1CLiB8NlMSHGsDn+k7Ag=
459459
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.199 h1:e9M5HSIq2xw61Oz9whoaq+QT95rZtowkY/2zhdzx9v4=
460460
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.199/go.mod h1:ynirFavajqWLAcgEp9ZiMd9xn5JnKTJhQisArjMxNtk=
461+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.571 h1:NtCt5TlQoJ9yFSKqTusLeE/3xq82Gs80gxwHK4BMSm8=
462+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway v1.0.571/go.mod h1:G0Uj96Y8P0AUdB2zAwhEJ53FiDr+kZSJTllaaXu4K/k=
461463
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.0.466 h1:JnE62AvMprCOe1/cnLJvjgeBoUOGxhdc0DG2mhs8cjo=
462464
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as v1.0.466/go.mod h1:NBTNnh4SpOTGvr69Ttt4ABQplvwt6eEZXSmO5f9Anf0=
463465
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.409 h1:ToZpNh78SVdKakkeR9YV1a65tjtC4NJl+hrJqTuhO3g=
@@ -536,6 +538,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.564 h1:nAs/
536538
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.564/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
537539
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.569 h1:ucwAQxGji/1kHSsnFf/w/BJ35rU7lFaUAq+uUKzwufE=
538540
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.569/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
541+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.571 h1:V4JQJTavl2X8aRBRBBh4vTOInuzrbqQzHlFitmu782U=
542+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.571/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
539543
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445 h1:Bh7XD0ypNMHYyBOM8hhKsSu+y0VVKUnJVS+YKKhfpGg=
540544
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.445/go.mod h1:jMDD351efCFpT1+KVFbcpu6SbmP4TYmp4qkoCfr63nQ=
541545
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.553 h1:Pl1kYgFhJp0QSoVFSzRsiGk+HfEAkBTQg7+O60tytNA=

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ API GateWay
100100
tencentcloud_api_gateway_api_key
101101
tencentcloud_api_gateway_api_key_attachment
102102
tencentcloud_api_gateway_service_release
103+
tencentcloud_api_gateway_plugin
103104
104105
Cloud Audit(Audit)
105106
Data Source
@@ -1448,6 +1449,7 @@ func Provider() terraform.ResourceProvider {
14481449
"tencentcloud_api_gateway_api_key": resourceTencentCloudAPIGatewayAPIKey(),
14491450
"tencentcloud_api_gateway_api_key_attachment": resourceTencentCloudAPIGatewayAPIKeyAttachment(),
14501451
"tencentcloud_api_gateway_service_release": resourceTencentCloudAPIGatewayServiceRelease(),
1452+
"tencentcloud_api_gateway_plugin": resourceTencentCloudApiGatewayPlugin(),
14511453
"tencentcloud_sqlserver_basic_instance": resourceTencentCloudSqlserverBasicInstance(),
14521454
"tencentcloud_tcr_instance": resourceTencentCloudTcrInstance(),
14531455
"tencentcloud_tcr_namespace": resourceTencentCloudTcrNamespace(),
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
Provides a resource to create a apiGateway plugin
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_api_gateway_plugin" "plugin" {
8+
plugin_name = "terraform-plugin-test"
9+
plugin_type = "IPControl"
10+
plugin_data = jsonencode({
11+
"type" : "white_list",
12+
"blocks" : "1.1.1.1\n2.2.2.2",
13+
})
14+
description = "terraform test"
15+
}
16+
```
17+
18+
Import
19+
20+
apiGateway plugin can be imported using the id, e.g.
21+
22+
```
23+
terraform import tencentcloud_api_gateway_plugin.plugin plugin_id
24+
```
25+
*/
26+
package tencentcloud
27+
28+
import (
29+
"context"
30+
"fmt"
31+
"log"
32+
33+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
34+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
35+
apiGateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808"
36+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
37+
)
38+
39+
func resourceTencentCloudApiGatewayPlugin() *schema.Resource {
40+
return &schema.Resource{
41+
Create: resourceTencentCloudApiGatewayPluginCreate,
42+
Read: resourceTencentCloudApiGatewayPluginRead,
43+
Update: resourceTencentCloudApiGatewayPluginUpdate,
44+
Delete: resourceTencentCloudApiGatewayPluginDelete,
45+
Importer: &schema.ResourceImporter{
46+
State: schema.ImportStatePassthrough,
47+
},
48+
Schema: map[string]*schema.Schema{
49+
"plugin_name": {
50+
Required: true,
51+
Type: schema.TypeString,
52+
Description: "Name of the user define plugin. It must start with a letter and end with letter or number, the rest can contain letters, numbers and dashes(-). The length range is from 2 to 50.",
53+
},
54+
55+
"plugin_type": {
56+
Required: true,
57+
Type: schema.TypeString,
58+
Description: "Type of plugin. Now support IPControl, TrafficControl, Cors, CustomReq, CustomAuth, Routing, TrafficControlByParameter, CircuitBreaker, ProxyCache.",
59+
},
60+
61+
"plugin_data": {
62+
Required: true,
63+
Type: schema.TypeString,
64+
Description: "Statement to define plugin.",
65+
},
66+
67+
"description": {
68+
Optional: true,
69+
Type: schema.TypeString,
70+
Description: "Description of plugin.",
71+
},
72+
},
73+
}
74+
}
75+
76+
func resourceTencentCloudApiGatewayPluginCreate(d *schema.ResourceData, meta interface{}) error {
77+
defer logElapsed("resource.tencentcloud_api_gateway_plugin.create")()
78+
defer inconsistentCheck(d, meta)()
79+
80+
logId := getLogId(contextNil)
81+
82+
var (
83+
request = apiGateway.NewCreatePluginRequest()
84+
response = apiGateway.NewCreatePluginResponse()
85+
pluginId string
86+
)
87+
if v, ok := d.GetOk("plugin_name"); ok {
88+
request.PluginName = helper.String(v.(string))
89+
}
90+
91+
if v, ok := d.GetOk("plugin_type"); ok {
92+
request.PluginType = helper.String(v.(string))
93+
}
94+
95+
if v, ok := d.GetOk("plugin_data"); ok {
96+
request.PluginData = helper.String(v.(string))
97+
}
98+
99+
if v, ok := d.GetOk("description"); ok {
100+
request.Description = helper.String(v.(string))
101+
}
102+
103+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
104+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAPIGatewayClient().CreatePlugin(request)
105+
if e != nil {
106+
return retryError(e)
107+
} else {
108+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
109+
}
110+
response = result
111+
return nil
112+
})
113+
if err != nil {
114+
log.Printf("[CRITAL]%s create apiGateway plugin failed, reason:%+v", logId, err)
115+
return err
116+
}
117+
118+
pluginId = *response.Response.Result.PluginId
119+
d.SetId(pluginId)
120+
121+
return resourceTencentCloudApiGatewayPluginRead(d, meta)
122+
}
123+
124+
func resourceTencentCloudApiGatewayPluginRead(d *schema.ResourceData, meta interface{}) error {
125+
defer logElapsed("resource.tencentcloud_api_gateway_plugin.read")()
126+
defer inconsistentCheck(d, meta)()
127+
128+
logId := getLogId(contextNil)
129+
130+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
131+
132+
service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn}
133+
134+
pluginId := d.Id()
135+
136+
plugin, err := service.DescribeApiGatewayPluginById(ctx, pluginId)
137+
if err != nil {
138+
return err
139+
}
140+
141+
if plugin == nil {
142+
d.SetId("")
143+
return fmt.Errorf("resource `ApiGatewayPlugin` %s does not exist", d.Id())
144+
}
145+
146+
if plugin.PluginName != nil {
147+
_ = d.Set("plugin_name", plugin.PluginName)
148+
}
149+
150+
if plugin.PluginType != nil {
151+
_ = d.Set("plugin_type", plugin.PluginType)
152+
}
153+
154+
if plugin.PluginData != nil {
155+
_ = d.Set("plugin_data", plugin.PluginData)
156+
}
157+
158+
if plugin.Description != nil {
159+
_ = d.Set("description", plugin.Description)
160+
}
161+
162+
return nil
163+
}
164+
165+
func resourceTencentCloudApiGatewayPluginUpdate(d *schema.ResourceData, meta interface{}) error {
166+
defer logElapsed("resource.tencentcloud_api_gateway_plugin.update")()
167+
defer inconsistentCheck(d, meta)()
168+
169+
logId := getLogId(contextNil)
170+
171+
unsupportedUpdateFields := []string{
172+
"plugin_type",
173+
}
174+
for _, field := range unsupportedUpdateFields {
175+
if d.HasChange(field) {
176+
return fmt.Errorf("tencentcloud_api_gateway_plugin update on %s is not support yet", field)
177+
}
178+
}
179+
180+
request := apiGateway.NewModifyPluginRequest()
181+
182+
pluginId := d.Id()
183+
184+
request.PluginId = &pluginId
185+
186+
if d.HasChange("plugin_name") {
187+
if v, ok := d.GetOk("plugin_name"); ok {
188+
request.PluginName = helper.String(v.(string))
189+
}
190+
}
191+
192+
if d.HasChange("plugin_data") {
193+
if v, ok := d.GetOk("plugin_data"); ok {
194+
request.PluginData = helper.String(v.(string))
195+
}
196+
}
197+
198+
if d.HasChange("description") {
199+
if v, ok := d.GetOk("description"); ok {
200+
request.Description = helper.String(v.(string))
201+
}
202+
}
203+
204+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
205+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAPIGatewayClient().ModifyPlugin(request)
206+
if e != nil {
207+
return retryError(e)
208+
} else {
209+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
210+
}
211+
return nil
212+
})
213+
if err != nil {
214+
log.Printf("[CRITAL]%s update apiGateway plugin failed, reason:%+v", logId, err)
215+
return err
216+
}
217+
218+
return resourceTencentCloudApiGatewayPluginRead(d, meta)
219+
}
220+
221+
func resourceTencentCloudApiGatewayPluginDelete(d *schema.ResourceData, meta interface{}) error {
222+
defer logElapsed("resource.tencentcloud_api_gateway_plugin.delete")()
223+
defer inconsistentCheck(d, meta)()
224+
225+
logId := getLogId(contextNil)
226+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
227+
228+
service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn}
229+
pluginId := d.Id()
230+
231+
if err := service.DeleteApiGatewayPluginById(ctx, pluginId); err != nil {
232+
return err
233+
}
234+
235+
return nil
236+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudApiGatewayPluginResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccApiGatewayPlugin,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_api_gateway_plugin.plugin", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_api_gateway_plugin.plugin",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccApiGatewayPlugin = `
31+
32+
resource "tencentcloud_api_gateway_plugin" "plugin" {
33+
plugin_name = "terraform-plugin-test"
34+
plugin_type = "IPControl"
35+
plugin_data = jsonencode({
36+
"type" : "white_list",
37+
"blocks" : "1.1.1.1\n2.2.2.2",
38+
})
39+
description = "terraform test"
40+
}
41+
42+
`

tencentcloud/resource_tc_cfs_auto_snapshot_policy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ package tencentcloud
2525
import (
2626
"context"
2727
"fmt"
28+
"log"
29+
2830
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2931
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
3032
cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719"
3133
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
32-
"log"
3334
)
3435

3536
func resourceTencentCloudCfsAutoSnapshotPolicy() *schema.Resource {

tencentcloud/resource_tc_cfs_auto_snapshot_policy_attachment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ package tencentcloud
2323
import (
2424
"context"
2525
"fmt"
26+
"log"
27+
"strings"
28+
2629
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2730
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2831
cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719"
2932
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
30-
"log"
31-
"strings"
3233
)
3334

3435
func resourceTencentCloudCfsAutoSnapshotPolicyAttachment() *schema.Resource {

tencentcloud/resource_tc_cfs_auto_snapshot_policy_attachment_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package tencentcloud
22

33
import (
4-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
54
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
67
)
78

89
func TestAccTencentCloudCfsAutoSnapshotPolicyAttachmentResource_basic(t *testing.T) {

tencentcloud/resource_tc_cfs_auto_snapshot_policy_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package tencentcloud
22

33
import (
4-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
54
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
67
)
78

89
func TestAccTencentCloudCfsAutoSnapshotPolicyResource_basic(t *testing.T) {

0 commit comments

Comments
 (0)