-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathresource_tc_api_gateway_plugin.go
210 lines (171 loc) · 6.2 KB
/
resource_tc_api_gateway_plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package apigateway
import (
"context"
"fmt"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
apiGateway "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apigateway/v20180808"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudAPIGatewayPlugin() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudAPIGatewayPluginCreate,
Read: resourceTencentCloudAPIGatewayPluginRead,
Update: resourceTencentCloudAPIGatewayPluginUpdate,
Delete: resourceTencentCloudAPIGatewayPluginDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"plugin_name": {
Required: true,
Type: schema.TypeString,
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.",
},
"plugin_type": {
Required: true,
Type: schema.TypeString,
Description: "Type of plugin. Now support IPControl, TrafficControl, Cors, CustomReq, CustomAuth, Routing, TrafficControlByParameter, CircuitBreaker, ProxyCache.",
},
"plugin_data": {
Required: true,
Type: schema.TypeString,
Description: "Statement to define plugin.",
},
"description": {
Optional: true,
Type: schema.TypeString,
Description: "Description of plugin.",
},
},
}
}
func resourceTencentCloudAPIGatewayPluginCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_plugin.create")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = apiGateway.NewCreatePluginRequest()
response = apiGateway.NewCreatePluginResponse()
)
if v, ok := d.GetOk("plugin_name"); ok {
request.PluginName = helper.String(v.(string))
}
if v, ok := d.GetOk("plugin_type"); ok {
request.PluginType = helper.String(v.(string))
}
if v, ok := d.GetOk("plugin_data"); ok {
request.PluginData = helper.String(v.(string))
}
if v, ok := d.GetOk("description"); ok {
request.Description = helper.String(v.(string))
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAPIGatewayClient().CreatePlugin(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create apiGateway plugin failed, reason:%+v", logId, err)
return err
}
d.SetId(*response.Response.Result.PluginId)
return resourceTencentCloudAPIGatewayPluginRead(d, meta)
}
func resourceTencentCloudAPIGatewayPluginRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_plugin.read")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
pluginId = d.Id()
)
plugin, err := service.DescribeApiGatewayPluginById(ctx, pluginId)
if err != nil {
return err
}
if plugin == nil {
d.SetId("")
return fmt.Errorf("resource `ApiGatewayPlugin` %s does not exist", d.Id())
}
if plugin.PluginName != nil {
_ = d.Set("plugin_name", plugin.PluginName)
}
if plugin.PluginType != nil {
_ = d.Set("plugin_type", plugin.PluginType)
}
if plugin.PluginData != nil {
_ = d.Set("plugin_data", plugin.PluginData)
}
if plugin.Description != nil {
_ = d.Set("description", plugin.Description)
}
return nil
}
func resourceTencentCloudAPIGatewayPluginUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_plugin.update")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = apiGateway.NewModifyPluginRequest()
pluginId = d.Id()
)
unsupportedUpdateFields := []string{"plugin_type"}
for _, field := range unsupportedUpdateFields {
if d.HasChange(field) {
return fmt.Errorf("tencentcloud_api_gateway_plugin update on %s is not support yet", field)
}
}
request.PluginId = &pluginId
if d.HasChange("plugin_name") {
if v, ok := d.GetOk("plugin_name"); ok {
request.PluginName = helper.String(v.(string))
}
}
if d.HasChange("plugin_data") {
if v, ok := d.GetOk("plugin_data"); ok {
request.PluginData = helper.String(v.(string))
}
}
if d.HasChange("description") {
if v, ok := d.GetOk("description"); ok {
request.Description = helper.String(v.(string))
}
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAPIGatewayClient().ModifyPlugin(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s update apiGateway plugin failed, reason:%+v", logId, err)
return err
}
return resourceTencentCloudAPIGatewayPluginRead(d, meta)
}
func resourceTencentCloudAPIGatewayPluginDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_plugin.delete")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
pluginId = d.Id()
)
if err := service.DeleteApiGatewayPluginById(ctx, pluginId); err != nil {
return err
}
return nil
}