Skip to content

Commit b4d7959

Browse files
authored
add apigw plugin attach (#1498)
* add apigw plugin attach * add changelog * remove unuse variable
1 parent 389ef52 commit b4d7959

File tree

7 files changed

+354
-0
lines changed

7 files changed

+354
-0
lines changed

.changelog/1498.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_attachment
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ API GateWay
101101
tencentcloud_api_gateway_api_key_attachment
102102
tencentcloud_api_gateway_service_release
103103
tencentcloud_api_gateway_plugin
104+
tencentcloud_api_gateway_plugin_attachment
104105
105106
Cloud Audit(Audit)
106107
Data Source
@@ -1450,6 +1451,7 @@ func Provider() terraform.ResourceProvider {
14501451
"tencentcloud_api_gateway_api_key_attachment": resourceTencentCloudAPIGatewayAPIKeyAttachment(),
14511452
"tencentcloud_api_gateway_service_release": resourceTencentCloudAPIGatewayServiceRelease(),
14521453
"tencentcloud_api_gateway_plugin": resourceTencentCloudApiGatewayPlugin(),
1454+
"tencentcloud_api_gateway_plugin_attachment": resourceTencentCloudApiGatewayPluginAttachment(),
14531455
"tencentcloud_sqlserver_basic_instance": resourceTencentCloudSqlserverBasicInstance(),
14541456
"tencentcloud_tcr_instance": resourceTencentCloudTcrInstance(),
14551457
"tencentcloud_tcr_namespace": resourceTencentCloudTcrNamespace(),
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
Provides a resource to create a apiGateway plugin_attachment
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_api_gateway_plugin_attachment" "plugin_attachment" {
8+
plugin_id = "plugin-ny74siyz"
9+
service_id = "service-n1mgl0sq"
10+
environment_name = "test"
11+
api_id = "api-6tfrdysk"
12+
}
13+
```
14+
15+
Import
16+
17+
apiGateway plugin_attachment can be imported using the id, e.g.
18+
19+
```
20+
terraform import tencentcloud_api_gateway_plugin_attachment.plugin_attachment pluginId#serviceId#environmentName#apiId
21+
```
22+
*/
23+
package tencentcloud
24+
25+
import (
26+
"context"
27+
"fmt"
28+
"log"
29+
"strings"
30+
31+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
32+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
33+
apiGateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808"
34+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
35+
)
36+
37+
func resourceTencentCloudApiGatewayPluginAttachment() *schema.Resource {
38+
return &schema.Resource{
39+
Create: resourceTencentCloudApiGatewayPluginAttachmentCreate,
40+
Read: resourceTencentCloudApiGatewayPluginAttachmentRead,
41+
Delete: resourceTencentCloudApiGatewayPluginAttachmentDelete,
42+
Importer: &schema.ResourceImporter{
43+
State: schema.ImportStatePassthrough,
44+
},
45+
Schema: map[string]*schema.Schema{
46+
"plugin_id": {
47+
Required: true,
48+
ForceNew: true,
49+
Type: schema.TypeString,
50+
Description: "Id of Plugin.",
51+
},
52+
53+
"service_id": {
54+
Required: true,
55+
ForceNew: true,
56+
Type: schema.TypeString,
57+
Description: "Id of Service.",
58+
},
59+
60+
"environment_name": {
61+
Required: true,
62+
ForceNew: true,
63+
Type: schema.TypeString,
64+
Description: "Name of Environment.",
65+
},
66+
67+
"api_id": {
68+
Required: true,
69+
ForceNew: true,
70+
Type: schema.TypeString,
71+
Description: "Id of API.",
72+
},
73+
},
74+
}
75+
}
76+
77+
func resourceTencentCloudApiGatewayPluginAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
78+
defer logElapsed("resource.tencentcloud_api_gateway_plugin_attachment.create")()
79+
defer inconsistentCheck(d, meta)()
80+
81+
logId := getLogId(contextNil)
82+
83+
var (
84+
request = apiGateway.NewAttachPluginRequest()
85+
pluginId string
86+
serviceId string
87+
environmentName string
88+
apiId string
89+
)
90+
if v, ok := d.GetOk("plugin_id"); ok {
91+
pluginId = v.(string)
92+
request.PluginId = helper.String(v.(string))
93+
}
94+
95+
if v, ok := d.GetOk("service_id"); ok {
96+
serviceId = v.(string)
97+
request.ServiceId = helper.String(v.(string))
98+
}
99+
100+
if v, ok := d.GetOk("environment_name"); ok {
101+
environmentName = v.(string)
102+
request.EnvironmentName = helper.String(v.(string))
103+
}
104+
105+
if v, ok := d.GetOk("api_id"); ok {
106+
apiId = v.(string)
107+
request.ApiIds = []*string{helper.String(v.(string))}
108+
}
109+
110+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
111+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseAPIGatewayClient().AttachPlugin(request)
112+
if e != nil {
113+
return retryError(e)
114+
} else {
115+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
116+
}
117+
return nil
118+
})
119+
if err != nil {
120+
log.Printf("[CRITAL]%s create apiGateway pluginAttachment failed, reason:%+v", logId, err)
121+
return err
122+
}
123+
124+
d.SetId(pluginId + FILED_SP + serviceId + FILED_SP + environmentName + FILED_SP + apiId)
125+
126+
return resourceTencentCloudApiGatewayPluginAttachmentRead(d, meta)
127+
}
128+
129+
func resourceTencentCloudApiGatewayPluginAttachmentRead(d *schema.ResourceData, meta interface{}) error {
130+
defer logElapsed("resource.tencentcloud_api_gateway_plugin_attachment.read")()
131+
defer inconsistentCheck(d, meta)()
132+
133+
logId := getLogId(contextNil)
134+
135+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
136+
137+
service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn}
138+
139+
idSplit := strings.Split(d.Id(), FILED_SP)
140+
if len(idSplit) != 4 {
141+
return fmt.Errorf("id is broken,%s", d.Id())
142+
}
143+
pluginId := idSplit[0]
144+
serviceId := idSplit[1]
145+
environmentName := idSplit[2]
146+
apiId := idSplit[3]
147+
148+
pluginAttachment, err := service.DescribeApiGatewayPluginAttachmentById(ctx, pluginId, serviceId, environmentName, apiId)
149+
if err != nil {
150+
return err
151+
}
152+
153+
if pluginAttachment == nil {
154+
d.SetId("")
155+
return fmt.Errorf("resource `ApiGatewayPluginAttachment` %s does not exist", d.Id())
156+
}
157+
158+
_ = d.Set("plugin_id", pluginId)
159+
160+
if pluginAttachment.ServiceId != nil {
161+
_ = d.Set("service_id", pluginAttachment.ServiceId)
162+
}
163+
164+
if pluginAttachment.Environment != nil {
165+
_ = d.Set("environment_name", pluginAttachment.Environment)
166+
}
167+
168+
if pluginAttachment.ApiId != nil {
169+
_ = d.Set("api_id", pluginAttachment.ApiId)
170+
}
171+
172+
return nil
173+
}
174+
175+
func resourceTencentCloudApiGatewayPluginAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
176+
defer logElapsed("resource.tencentcloud_api_gateway_plugin_attachment.delete")()
177+
defer inconsistentCheck(d, meta)()
178+
179+
logId := getLogId(contextNil)
180+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
181+
182+
service := APIGatewayService{client: meta.(*TencentCloudClient).apiV3Conn}
183+
idSplit := strings.Split(d.Id(), FILED_SP)
184+
if len(idSplit) != 4 {
185+
return fmt.Errorf("id is broken,%s", d.Id())
186+
}
187+
pluginId := idSplit[0]
188+
serviceId := idSplit[1]
189+
environmentName := idSplit[2]
190+
apiId := idSplit[3]
191+
192+
if err := service.DeleteApiGatewayPluginAttachmentById(ctx, pluginId, serviceId, environmentName, apiId); err != nil {
193+
return err
194+
}
195+
196+
return nil
197+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudNeedFixApiGatewayPluginAttachmentResource_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: testAccApiGatewayPluginAttachment,
19+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_api_gateway_plugin_attachment.plugin_attachment", "id")),
20+
},
21+
{
22+
ResourceName: "tencentcloud_api_gateway_plugin_attachment.plugin_attachment",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccApiGatewayPluginAttachment = `
31+
32+
resource "tencentcloud_api_gateway_plugin_attachment" "plugin_attachment" {
33+
plugin_id = "plugin-ny74siyz"
34+
service_id = "service-n1mgl0sq"
35+
environment_name = "test"
36+
api_id = "api-6tfrdysk"
37+
}
38+
39+
`

tencentcloud/service_tencentcloud_api_gateway.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,3 +1639,64 @@ func (me *APIGatewayService) DeleteApiGatewayPluginById(ctx context.Context, plu
16391639

16401640
return
16411641
}
1642+
1643+
func (me *APIGatewayService) DescribeApiGatewayPluginAttachmentById(ctx context.Context, pluginId string, serviceId string, environmentName string, apiId string) (pluginAttachment *apigateway.AttachedApiInfo, errRet error) {
1644+
logId := getLogId(ctx)
1645+
1646+
request := apigateway.NewDescribePluginApisRequest()
1647+
request.PluginId = &pluginId
1648+
1649+
defer func() {
1650+
if errRet != nil {
1651+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1652+
}
1653+
}()
1654+
1655+
ratelimit.Check(request.GetAction())
1656+
1657+
response, err := me.client.UseAPIGatewayClient().DescribePluginApis(request)
1658+
if err != nil {
1659+
errRet = err
1660+
return
1661+
}
1662+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1663+
1664+
if len(response.Response.Result.AttachedApis) < 1 {
1665+
return
1666+
}
1667+
1668+
for _, api := range response.Response.Result.AttachedApis {
1669+
if *api.ServiceId == serviceId && *api.Environment == environmentName && *api.ApiId == apiId {
1670+
pluginAttachment = api
1671+
return
1672+
}
1673+
}
1674+
return
1675+
}
1676+
1677+
func (me *APIGatewayService) DeleteApiGatewayPluginAttachmentById(ctx context.Context, pluginId string, serviceId string, environmentName string, apiId string) (errRet error) {
1678+
logId := getLogId(ctx)
1679+
1680+
request := apigateway.NewDetachPluginRequest()
1681+
request.PluginId = &pluginId
1682+
request.ServiceId = &serviceId
1683+
request.EnvironmentName = &environmentName
1684+
request.ApiId = &apiId
1685+
1686+
defer func() {
1687+
if errRet != nil {
1688+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1689+
}
1690+
}()
1691+
1692+
ratelimit.Check(request.GetAction())
1693+
1694+
response, err := me.client.UseAPIGatewayClient().DetachPlugin(request)
1695+
if err != nil {
1696+
errRet = err
1697+
return
1698+
}
1699+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1700+
1701+
return
1702+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
subcategory: "API GateWay"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_api_gateway_plugin_attachment"
5+
sidebar_current: "docs-tencentcloud-resource-api_gateway_plugin_attachment"
6+
description: |-
7+
Provides a resource to create a apiGateway plugin_attachment
8+
---
9+
10+
# tencentcloud_api_gateway_plugin_attachment
11+
12+
Provides a resource to create a apiGateway plugin_attachment
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_api_gateway_plugin_attachment" "plugin_attachment" {
18+
plugin_id = "plugin-ny74siyz"
19+
service_id = "service-n1mgl0sq"
20+
environment_name = "test"
21+
api_id = "api-6tfrdysk"
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
The following arguments are supported:
28+
29+
* `api_id` - (Required, String, ForceNew) Id of API.
30+
* `environment_name` - (Required, String, ForceNew) Name of Environment.
31+
* `plugin_id` - (Required, String, ForceNew) Id of Plugin.
32+
* `service_id` - (Required, String, ForceNew) Id of Service.
33+
34+
## Attributes Reference
35+
36+
In addition to all arguments above, the following attributes are exported:
37+
38+
* `id` - ID of the resource.
39+
40+
41+
42+
## Import
43+
44+
apiGateway plugin_attachment can be imported using the id, e.g.
45+
46+
```
47+
terraform import tencentcloud_api_gateway_plugin_attachment.plugin_attachment pluginId#serviceId#environmentName#apiId
48+
```
49+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
<li>
8181
<a href="/docs/providers/tencentcloud/r/api_gateway_plugin.html">tencentcloud_api_gateway_plugin</a>
8282
</li>
83+
<li>
84+
<a href="/docs/providers/tencentcloud/r/api_gateway_plugin_attachment.html">tencentcloud_api_gateway_plugin_attachment</a>
85+
</li>
8386
<li>
8487
<a href="/docs/providers/tencentcloud/r/api_gateway_service.html">tencentcloud_api_gateway_service</a>
8588
</li>

0 commit comments

Comments
 (0)