/
resource_class_list.go
379 lines (361 loc) · 13.2 KB
/
resource_class_list.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package thunder
import (
"context"
edpt "github.com/a10networks/terraform-provider-thunder/thunder/axapi/endpoint"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceClassList() *schema.Resource {
return &schema.Resource{
Description: "`thunder_class_list`: Configure classification list\n\n__PLACEHOLDER__",
CreateContext: resourceClassListCreate,
UpdateContext: resourceClassListUpdate,
ReadContext: resourceClassListRead,
DeleteContext: resourceClassListDelete,
Schema: map[string]*schema.Schema{
"ac_list": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ac_match_type": {
Type: schema.TypeString, Optional: true, Description: "'contains': String contains another string; 'ends-with': String ends with another string; 'equals': String equals another string; 'starts-with': String starts with another string;",
},
"ac_key_string": {
Type: schema.TypeString, Optional: true, Description: "Specify key string",
},
"ac_value": {
Type: schema.TypeString, Optional: true, Description: "Specify value string",
},
"gtp_rate_limit_policy_str": {
Type: schema.TypeString, Optional: true, Description: "GTP Rate Limit Template Name",
},
},
},
},
"dns": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_match_type": {
Type: schema.TypeString, Optional: true, Description: "'contains': Domain contains another string; 'ends-with': Domain ends with another string; 'starts-with': Domain starts-with another string;",
},
"dns_match_string": {
Type: schema.TypeString, Optional: true, Description: "Domain name",
},
"dns_lid": {
Type: schema.TypeInt, Optional: true, Description: "Use Limit ID defined in template (Specify LID index)",
},
"dns_glid": {
Type: schema.TypeString, Optional: true, Description: "Use global Limit ID (Specify global LID index)",
},
"shared_partition_dns_glid": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Reference a glid from shared partition",
},
"dns_glid_shared": {
Type: schema.TypeString, Optional: true, Description: "Use global Limit ID",
},
},
},
},
"file": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Create/Edit a class-list stored as a file",
},
"geo_list": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"geo_location": {
Type: schema.TypeString, Optional: true, Description: "Specify geo-location",
},
"geo_location_ipv6": {
Type: schema.TypeString, Optional: true, Description: "Specify IPv6 geo-location",
},
},
},
},
"ipv4_list": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv4addr": {
Type: schema.TypeString, Optional: true, Description: "Specify IP address",
},
"lid": {
Type: schema.TypeInt, Optional: true, Description: "Use Limit ID defined in template (Specify LID index)",
},
"glid": {
Type: schema.TypeString, Optional: true, Description: "Use global Limit ID (Specify global LID index)",
},
"shared_partition_glid": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Reference a glid from shared partition",
},
"glid_shared": {
Type: schema.TypeString, Optional: true, Description: "Use global Limit ID",
},
"lsn_lid": {
Type: schema.TypeInt, Optional: true, Description: "LSN Limit ID (LID index)",
},
"lsn_radius_profile": {
Type: schema.TypeInt, Optional: true, Description: "LSN RADIUS Profile Index",
},
"gtp_rate_limit_policy_v4": {
Type: schema.TypeString, Optional: true, Description: "GTP Rate Limit Template Name",
},
"age": {
Type: schema.TypeInt, Optional: true, Description: "Specify age in minutes",
},
},
},
},
"ipv6_list": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ipv6_addr": {
Type: schema.TypeString, Optional: true, Description: "Specify IPv6 host or subnet",
},
"v6_lid": {
Type: schema.TypeInt, Optional: true, Description: "Use Limit ID defined in template (Specify LID index)",
},
"v6_glid": {
Type: schema.TypeString, Optional: true, Description: "Use global Limit ID (Specify global LID index)",
},
"shared_partition_v6_glid": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Reference a glid from shared partition",
},
"v6_glid_shared": {
Type: schema.TypeString, Optional: true, Description: "Use global Limit ID",
},
"v6_lsn_lid": {
Type: schema.TypeInt, Optional: true, Description: "LSN Limit ID (LID index)",
},
"v6_lsn_radius_profile": {
Type: schema.TypeInt, Optional: true, Description: "LSN RADIUS Profile Index",
},
"gtp_rate_limit_policy_v6": {
Type: schema.TypeString, Optional: true, Description: "GTP Rate Limit Template Name",
},
"v6_age": {
Type: schema.TypeInt, Optional: true, Description: "Specify age in minutes",
},
},
},
},
"name": {
Type: schema.TypeString, Required: true, Description: "Specify name of the class list",
},
"str_list": {
Type: schema.TypeList, Optional: true, Description: "",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"str": {
Type: schema.TypeString, Optional: true, Description: "Specify key string",
},
"str_lid_dummy": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Use Limit ID defined in template",
},
"str_lid": {
Type: schema.TypeInt, Optional: true, Description: "LID index",
},
"str_glid_dummy": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Use global Limit ID",
},
"str_glid": {
Type: schema.TypeString, Optional: true, Description: "Global LID index",
},
"shared_partition_str_glid": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Reference a glid from shared partition",
},
"str_glid_shared": {
Type: schema.TypeString, Optional: true, Description: "Use global Limit ID",
},
"value_str": {
Type: schema.TypeString, Optional: true, Description: "Specify value string",
},
},
},
},
"type": {
Type: schema.TypeString, Optional: true, Description: "'ac': Make class-list type Aho-Corasick; 'dns': Make class-list type DNS; 'ipv4': Make class-list type IPv4; 'ipv6': Make class-list type IPv6; 'string': Make class-list type String; 'string-case-insensitive': Make class-list type String-case-insensitive. Case insensitive is applied to key string;",
},
"user_tag": {
Type: schema.TypeString, Optional: true, Description: "Customized tag",
},
"uuid": {
Type: schema.TypeString, Optional: true, Computed: true, Description: "uuid of the object",
},
},
}
}
func resourceClassListCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceClassListCreate()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToEndpointClassList(d)
d.SetId(obj.GetId())
err := obj.Post(client.Token, client.Host, logger)
if err != nil {
return diag.FromErr(err)
}
return resourceClassListRead(ctx, d, meta)
}
return diags
}
func resourceClassListUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceClassListUpdate()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToEndpointClassList(d)
err := obj.Put(client.Token, client.Host, logger)
if err != nil {
return diag.FromErr(err)
}
return resourceClassListRead(ctx, d, meta)
}
return diags
}
func resourceClassListDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceClassListDelete()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToEndpointClassList(d)
err := obj.Delete(client.Token, client.Host, d.Id(), logger)
if err != nil {
return diag.FromErr(err)
}
}
return diags
}
func resourceClassListRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceClassListRead()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToEndpointClassList(d)
err := obj.Get(client.Token, client.Host, d.Id(), logger)
if err != nil {
return diag.FromErr(err)
}
}
return diags
}
func getSliceClassListAcList(d []interface{}) []edpt.ClassListAcList {
count1 := len(d)
ret := make([]edpt.ClassListAcList, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.ClassListAcList
oi.AcMatchType = in["ac_match_type"].(string)
oi.AcKeyString = in["ac_key_string"].(string)
oi.AcValue = in["ac_value"].(string)
oi.GtpRateLimitPolicyStr = in["gtp_rate_limit_policy_str"].(string)
ret = append(ret, oi)
}
return ret
}
func getSliceClassListDns(d []interface{}) []edpt.ClassListDns {
count1 := len(d)
ret := make([]edpt.ClassListDns, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.ClassListDns
oi.DnsMatchType = in["dns_match_type"].(string)
oi.DnsMatchString = in["dns_match_string"].(string)
oi.DnsLid = in["dns_lid"].(int)
oi.DnsGlid = in["dns_glid"].(string)
oi.SharedPartitionDnsGlid = in["shared_partition_dns_glid"].(int)
oi.DnsGlidShared = in["dns_glid_shared"].(string)
ret = append(ret, oi)
}
return ret
}
func getSliceClassListGeoList(d []interface{}) []edpt.ClassListGeoList {
count1 := len(d)
ret := make([]edpt.ClassListGeoList, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.ClassListGeoList
oi.GeoLocation = in["geo_location"].(string)
oi.GeoLocationIpv6 = in["geo_location_ipv6"].(string)
ret = append(ret, oi)
}
return ret
}
func getSliceClassListIpv4List(d []interface{}) []edpt.ClassListIpv4List {
count1 := len(d)
ret := make([]edpt.ClassListIpv4List, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.ClassListIpv4List
oi.Ipv4addr = in["ipv4addr"].(string)
oi.Lid = in["lid"].(int)
oi.Glid = in["glid"].(string)
oi.SharedPartitionGlid = in["shared_partition_glid"].(int)
oi.GlidShared = in["glid_shared"].(string)
oi.LsnLid = in["lsn_lid"].(int)
oi.LsnRadiusProfile = in["lsn_radius_profile"].(int)
oi.GtpRateLimitPolicyV4 = in["gtp_rate_limit_policy_v4"].(string)
oi.Age = in["age"].(int)
ret = append(ret, oi)
}
return ret
}
func getSliceClassListIpv6List(d []interface{}) []edpt.ClassListIpv6List {
count1 := len(d)
ret := make([]edpt.ClassListIpv6List, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.ClassListIpv6List
oi.Ipv6Addr = in["ipv6_addr"].(string)
oi.V6Lid = in["v6_lid"].(int)
oi.V6Glid = in["v6_glid"].(string)
oi.SharedPartitionV6Glid = in["shared_partition_v6_glid"].(int)
oi.V6GlidShared = in["v6_glid_shared"].(string)
oi.V6LsnLid = in["v6_lsn_lid"].(int)
oi.V6LsnRadiusProfile = in["v6_lsn_radius_profile"].(int)
oi.GtpRateLimitPolicyV6 = in["gtp_rate_limit_policy_v6"].(string)
oi.V6Age = in["v6_age"].(int)
ret = append(ret, oi)
}
return ret
}
func getSliceClassListStrList(d []interface{}) []edpt.ClassListStrList {
count1 := len(d)
ret := make([]edpt.ClassListStrList, 0, count1)
for _, item := range d {
in := item.(map[string]interface{})
var oi edpt.ClassListStrList
oi.Str = in["str"].(string)
oi.StrLidDummy = in["str_lid_dummy"].(int)
oi.StrLid = in["str_lid"].(int)
oi.StrGlidDummy = in["str_glid_dummy"].(int)
oi.StrGlid = in["str_glid"].(string)
oi.SharedPartitionStrGlid = in["shared_partition_str_glid"].(int)
oi.StrGlidShared = in["str_glid_shared"].(string)
oi.ValueStr = in["value_str"].(string)
ret = append(ret, oi)
}
return ret
}
func dataToEndpointClassList(d *schema.ResourceData) edpt.ClassList {
var ret edpt.ClassList
ret.Inst.AcList = getSliceClassListAcList(d.Get("ac_list").([]interface{}))
ret.Inst.Dns = getSliceClassListDns(d.Get("dns").([]interface{}))
ret.Inst.File = d.Get("file").(int)
ret.Inst.GeoList = getSliceClassListGeoList(d.Get("geo_list").([]interface{}))
ret.Inst.Ipv4List = getSliceClassListIpv4List(d.Get("ipv4_list").([]interface{}))
ret.Inst.Ipv6List = getSliceClassListIpv6List(d.Get("ipv6_list").([]interface{}))
ret.Inst.Name = d.Get("name").(string)
ret.Inst.StrList = getSliceClassListStrList(d.Get("str_list").([]interface{}))
ret.Inst.Type = d.Get("type").(string)
ret.Inst.UserTag = d.Get("user_tag").(string)
//omit uuid
return ret
}