-
Notifications
You must be signed in to change notification settings - Fork 133
/
resource_tc_api_gateway_usage_plan.go
313 lines (281 loc) · 10.2 KB
/
resource_tc_api_gateway_usage_plan.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package apigateway
import (
"context"
"fmt"
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 ResourceTencentCloudAPIGatewayUsagePlan() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudAPIGatewayUsagePlanCreate,
Read: resourceTencentCloudAPIGatewayUsagePlanRead,
Update: resourceTencentCloudAPIGatewayUsagePlanUpdate,
Delete: resourceTencentCloudAPIGatewayUsagePlanDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"usage_plan_name": {
Type: schema.TypeString,
Required: true,
Description: "Custom usage plan name.",
},
"usage_plan_desc": {
Type: schema.TypeString,
Optional: true,
Description: "Custom usage plan description.",
},
"max_request_num": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
ValidateFunc: func(i interface{}, s string) (strings []string, errors []error) {
if value := int64(i.(int)); value == -1 {
return
}
return tccommon.ValidateIntegerInRange(1, 99999999)(i, s)
},
Description: "Total number of requests allowed. Valid values: -1, [1,99999999]. The default value is -1, which indicates no limit.",
},
"max_request_num_pre_sec": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
ValidateFunc: func(i interface{}, s string) (strings []string, errors []error) {
if value := int64(i.(int)); value == -1 {
return
}
return tccommon.ValidateIntegerInRange(1, 2000)(i, s)
},
Description: "Limit of requests per second. Valid values: -1, [1,2000]. The default value is -1, which indicates no limit.",
},
// Computed values.
"modify_time": {
Type: schema.TypeString,
Computed: true,
Description: "Last modified time in the format of YYYY-MM-DDThh:mm:ssZ according to ISO 8601 standard. UTC time is used.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time in the format of YYYY-MM-DDThh:mm:ssZ according to ISO 8601 standard. UTC time is used.",
},
"attach_api_keys": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "Attach API keys list.",
},
"attach_list": {
Type: schema.TypeList,
Computed: true,
Description: "Attach service and API list.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_id": {
Type: schema.TypeString,
Computed: true,
Description: "The service ID.",
},
"service_name": {
Type: schema.TypeString,
Computed: true,
Description: "The service name.",
},
"api_id": {
Type: schema.TypeString,
Computed: true,
Description: "The API ID, this value is empty if attach service.",
},
"api_name": {
Type: schema.TypeString,
Computed: true,
Description: "The API name, this value is empty if attach service.",
},
"path": {
Type: schema.TypeString,
Computed: true,
Description: "The API path, this value is empty if attach service.",
},
"method": {
Type: schema.TypeString,
Computed: true,
Description: "The API method, this value is empty if attach service.",
},
"environment": {
Type: schema.TypeString,
Computed: true,
Description: "The environment name.",
},
"modify_time": {
Type: schema.TypeString,
Computed: true,
Description: "Last modified time in the format of YYYY-MM-DDThh:mm:ssZ according to ISO 8601 standard. UTC time is used.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time in the format of YYYY-MM-DDThh:mm:ssZ according to ISO 8601 standard. UTC time is used.",
},
},
},
},
},
}
}
func resourceTencentCloudAPIGatewayUsagePlanCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_usage_plan.create")()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
apiGatewayService = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
usagePlanName = d.Get("usage_plan_name").(string)
maxRequestNum = int64(d.Get("max_request_num").(int))
maxRequestNumPreSec = int64(d.Get("max_request_num_pre_sec").(int))
usagePlanDesc *string
)
if v, has := d.GetOk("usage_plan_desc"); has {
usagePlanDesc = helper.String(v.(string))
}
usagePlanId, err := apiGatewayService.CreateUsagePlan(ctx, usagePlanName, usagePlanDesc, maxRequestNum, maxRequestNumPreSec)
if err != nil {
return err
}
d.SetId(usagePlanId)
//wait usage plan create ok
if outErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
_, has, inErr := apiGatewayService.DescribeUsagePlan(ctx, usagePlanId)
if inErr != nil {
return tccommon.RetryError(inErr, tccommon.InternalError)
}
if has {
return nil
}
return resource.RetryableError(fmt.Errorf(" usage plan %s not found on server", usagePlanId))
}); outErr != nil {
return outErr
}
return resourceTencentCloudAPIGatewayUsagePlanRead(d, meta)
}
func resourceTencentCloudAPIGatewayUsagePlanRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_usage_plan.read")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
apiGatewayService = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
usagePlanId = d.Id()
info apigateway.UsagePlanInfo
attachList []*apigateway.UsagePlanEnvironment
err error
has bool
)
if err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
info, has, err = apiGatewayService.DescribeUsagePlan(ctx, usagePlanId)
if err != nil {
return tccommon.RetryError(err, tccommon.InternalError)
}
return nil
}); err != nil {
return err
}
if !has {
d.SetId("")
return nil
}
//service attach and API
for _, bindType := range API_GATEWAY_TYPES {
if err = resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
list, inErr := apiGatewayService.DescribeUsagePlanEnvironments(ctx, usagePlanId, bindType)
if inErr != nil {
return tccommon.RetryError(inErr, tccommon.InternalError)
}
attachList = append(attachList, list...)
return nil
}); err != nil {
return err
}
}
infoAttachList := make([]map[string]interface{}, 0, len(attachList))
for _, v := range attachList {
infoAttachList = append(infoAttachList, map[string]interface{}{
"service_id": v.ServiceId,
"service_name": v.ServiceName,
"api_id": v.ApiId,
"api_name": v.ApiName,
"path": v.Path,
"method": v.Method,
"environment": v.Environment,
"modify_time": v.ModifiedTime,
"create_time": v.CreatedTime,
})
}
_ = d.Set("usage_plan_name", info.UsagePlanName)
_ = d.Set("usage_plan_desc", info.UsagePlanDesc)
_ = d.Set("max_request_num", info.MaxRequestNum)
_ = d.Set("max_request_num_pre_sec", info.MaxRequestNumPreSec)
_ = d.Set("modify_time", info.ModifiedTime)
_ = d.Set("create_time", info.CreatedTime)
_ = d.Set("attach_list", infoAttachList)
attachApiKeys := make([]string, 0, len(info.BindSecretIds))
for _, v := range info.BindSecretIds {
attachApiKeys = append(attachApiKeys, *v)
}
_ = d.Set("attach_api_keys", attachApiKeys)
return nil
}
func resourceTencentCloudAPIGatewayUsagePlanUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_usage_plan.update")()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
usagePlanId = d.Id()
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
apiGatewayService = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
usagePlanName = d.Get("usage_plan_name").(string)
maxRequestNum = int64(d.Get("max_request_num").(int))
maxRequestNumPreSec = int64(d.Get("max_request_num_pre_sec").(int))
err error
usagePlanDesc *string
)
if v, has := d.GetOk("usage_plan_desc"); has {
usagePlanDesc = helper.String(v.(string))
}
if d.HasChange("usage_plan_name") || d.HasChange("usage_plan_desc") ||
d.HasChange("max_request_num") || d.HasChange("max_request_num_pre_sec") {
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
err = apiGatewayService.ModifyUsagePlan(ctx,
usagePlanId,
usagePlanName,
usagePlanDesc,
maxRequestNum,
maxRequestNumPreSec)
if nil != err {
return tccommon.RetryError(err)
}
return nil
})
if err != nil {
return err
}
}
return resourceTencentCloudAPIGatewayUsagePlanRead(d, meta)
}
func resourceTencentCloudAPIGatewayUsagePlanDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_api_gateway_usage_plan.delete")()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
apiGatewayService = APIGatewayService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
usagePlanId = d.Id()
)
return resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
inErr := apiGatewayService.DeleteUsagePlan(ctx, usagePlanId)
if inErr != nil {
return tccommon.RetryError(inErr)
}
return nil
})
}