/
resource_edgecenter_dns_zone_record.go
723 lines (670 loc) · 23.2 KB
/
resource_edgecenter_dns_zone_record.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package edgecenter
import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"strings"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
dnssdk "github.com/Edge-Center/edgecenter-dns-sdk-go"
)
const (
DNSZoneRecordResource = "edgecenter_dns_zone_record"
DNSZoneRecordSchemaZone = "zone"
DNSZoneRecordSchemaDomain = "domain"
DNSZoneRecordSchemaType = "type"
DNSZoneRecordSchemaTTL = "ttl"
DNSZoneRecordSchemaRRSetMeta = "meta"
DNSZoneRecordSchemaFailover = "failover"
DNSZoneRecordSchemaFilter = "filter"
DNSZoneRecordSchemaFailoverProtocol = "protocol"
DNSZoneRecordSchemaFailoverFrequency = "frequency"
DNSZoneRecordSchemaFailoverHost = "host"
DNSZoneRecordSchemaFailoverHTTPStatusCode = "http_status_code"
DNSZoneRecordSchemaFailoverMethod = "method"
DNSZoneRecordSchemaFailoverPort = "port"
DNSZoneRecordSchemaFailoverRegexp = "regexp"
DNSZoneRecordSchemaFailoverTimeout = "timeout"
DNSZoneRecordSchemaFailoverTLS = "tls"
DNSZoneRecordSchemaFailoverURL = "url"
DNSZoneRecordSchemaFailoverVerify = "verify"
DNSZoneRecordSchemaFilterLimit = "limit"
DNSZoneRecordSchemaFilterType = "type"
DNSZoneRecordSchemaFilterStrict = "strict"
DNSZoneRecordSchemaResourceRecord = "resource_record"
DNSZoneRecordSchemaContent = "content"
DNSZoneRecordSchemaEnabled = "enabled"
DNSZoneRecordSchemaMeta = "meta"
DNSZoneRecordSchemaMetaAsn = "asn"
DNSZoneRecordSchemaMetaIP = "ip"
DNSZoneRecordSchemaMetaCountries = "countries"
DNSZoneRecordSchemaMetaContinents = "continents"
DNSZoneRecordSchemaMetaLatLong = "latlong"
DNSZoneRecordSchemaMetaNotes = "notes"
DNSZoneRecordSchemaMetaDefault = "default"
)
func resourceDNSZoneRecord() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
DNSZoneRecordSchemaZone: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
val := i.(string)
if strings.TrimSpace(val) == "" || len(val) > 255 {
return diag.Errorf("dns record zone can't be empty, it also should be less than 256 symbols")
}
return nil
},
Description: "A zone of DNS Zone Record resource.",
},
DNSZoneRecordSchemaDomain: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
val := i.(string)
if strings.TrimSpace(val) == "" || len(val) > 255 {
return diag.Errorf("dns record domain can't be empty, it also should be less than 256 symbols")
}
return nil
},
Description: "A domain of DNS Zone Record resource.",
},
DNSZoneRecordSchemaType: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
val := strings.TrimSpace(i.(string))
types := []string{"A", "AAAA", "MX", "CNAME", "TXT", "CAA", "NS", "SRV"}
for _, t := range types {
if strings.EqualFold(t, val) {
return nil
}
}
return diag.Errorf("dns record type should be one of %v", types)
},
Description: "A type of DNS Zone Record resource.",
},
DNSZoneRecordSchemaTTL: {
Type: schema.TypeInt,
Optional: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
val := i.(int)
if val < 0 {
return diag.Errorf("dns record ttl can't be less than 0")
}
return nil
},
Description: "A ttl of DNS Zone Record resource.",
},
DNSZoneRecordSchemaRRSetMeta: {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Description: "A meta of DNS Zone Record resource.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
DNSZoneRecordSchemaFailover: {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "A failover meta of DNS Zone Record resource.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
DNSZoneRecordSchemaFailoverProtocol: {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
val := strings.TrimSpace(i.(string))
types := []string{"TCP", "UDP", "HTTP", "ICMP"}
for _, t := range types {
if strings.EqualFold(t, val) {
return nil
}
}
return diag.Errorf("dns failover protocol type should be one of %v", types)
},
Description: "A failover protocol of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverFrequency: {
Type: schema.TypeInt,
Required: true,
Description: "A failover frequency of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverHost: {
Type: schema.TypeString,
Optional: true,
Description: "A failover host of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverHTTPStatusCode: {
Type: schema.TypeInt,
Optional: true,
Description: "A failover http status code of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverMethod: {
Type: schema.TypeString,
Optional: true,
Description: "A failover method of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverPort: {
Type: schema.TypeInt,
Optional: true,
Description: "A failover port of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverRegexp: {
Type: schema.TypeString,
Optional: true,
Description: "A failover regexp of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverTimeout: {
Type: schema.TypeInt,
Required: true,
Description: "A failover timeout of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverTLS: {
Type: schema.TypeBool,
Optional: true,
Description: "A failover tls of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverURL: {
Type: schema.TypeString,
Optional: true,
Description: "A failover url of DNS Zone Record resource.",
},
DNSZoneRecordSchemaFailoverVerify: {
Type: schema.TypeBool,
Optional: true,
Description: "A failover verify of DNS Zone Record resource.",
},
},
},
},
},
},
},
DNSZoneRecordSchemaFilter: {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
DNSZoneRecordSchemaFilterLimit: {
Type: schema.TypeInt,
Optional: true,
Description: "A DNS Zone Record filter option that describe how many records will be percolated.",
},
DNSZoneRecordSchemaFilterStrict: {
Type: schema.TypeBool,
Optional: true,
Description: "A DNS Zone Record filter option that describe possibility to return answers if no records were percolated through filter.",
},
DNSZoneRecordSchemaFilterType: {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
names := []string{"geodns", "geodistance", "default", "first_n", "is_healthy"}
name := i.(string)
for _, n := range names {
if n == name {
return nil
}
}
return diag.Errorf("dns record filter type should be one of %v", names)
},
Description: "A DNS Zone Record filter option that describe a name of filter.",
},
},
},
},
DNSZoneRecordSchemaResourceRecord: {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
DNSZoneRecordSchemaContent: {
Type: schema.TypeString,
Required: true,
Description: `A content of DNS Zone Record resource. (TXT: 'anyString', MX: '50 mail.company.io.', CAA: '0 issue "company.org; account=12345"')`,
},
DNSZoneRecordSchemaEnabled: {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Manage of public appearing of DNS Zone Record resource.",
},
DNSZoneRecordSchemaMeta: {
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
DNSZoneRecordSchemaMetaAsn: {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeInt,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
if i.(int) < 0 {
return diag.Errorf("asn cannot be less then 0")
}
return nil
},
},
Optional: true,
Description: "An asn meta (e.g. 12345) of DNS Zone Record resource.",
},
DNSZoneRecordSchemaMetaIP: {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
val := i.(string)
ip := net.ParseIP(val)
if ip == nil {
return diag.Errorf("dns record meta ip has wrong format: %s", val)
}
return nil
},
},
Optional: true,
Description: "An ip meta (e.g. 127.0.0.0) of DNS Zone Record resource.",
},
DNSZoneRecordSchemaMetaLatLong: {
Optional: true,
Type: schema.TypeList,
MaxItems: 2,
MinItems: 2,
Elem: &schema.Schema{
Type: schema.TypeFloat,
},
Description: "A latlong meta (e.g. 27.988056, 86.925278) of DNS Zone Record resource.",
},
DNSZoneRecordSchemaMetaNotes: {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "A notes meta (e.g. Miami DC) of DNS Zone Record resource.",
},
DNSZoneRecordSchemaMetaContinents: {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "Continents meta (e.g. Asia) of DNS Zone Record resource.",
},
DNSZoneRecordSchemaMetaCountries: {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "Countries meta (e.g. USA) of DNS Zone Record resource.",
},
DNSZoneRecordSchemaMetaDefault: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Fallback meta equals true marks records which are used as a default answer (when nothing was selected by specified meta fields).",
},
},
},
},
},
},
Description: "An array of contents with meta of DNS Zone Record resource.",
},
},
CreateContext: checkDNSDependency(resourceDNSZoneRecordCreate),
UpdateContext: checkDNSDependency(resourceDNSZoneRecordUpdate),
ReadContext: checkDNSDependency(resourceDNSZoneRecordRead),
DeleteContext: checkDNSDependency(resourceDNSZoneRecordDelete),
Description: "Represent DNS Zone Record resource. https://dns.edgecenter.ru/zones",
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), ":")
if len(parts) != 3 {
return nil, fmt.Errorf("format must be as zone:domain:type")
}
_ = d.Set(DNSZoneRecordSchemaZone, parts[0])
d.SetId(parts[0])
_ = d.Set(DNSZoneRecordSchemaDomain, parts[1])
_ = d.Set(DNSZoneRecordSchemaType, parts[2])
return []*schema.ResourceData{d}, nil
},
},
}
}
func resourceDNSZoneRecordCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
zone := strings.TrimSpace(d.Get(DNSZoneRecordSchemaZone).(string))
domain := strings.TrimSpace(d.Get(DNSZoneRecordSchemaDomain).(string))
rType := strings.TrimSpace(d.Get(DNSZoneRecordSchemaType).(string))
log.Println("[DEBUG] Start DNS Zone Record Resource creating")
defer log.Printf("[DEBUG] Finish DNS Zone Record Resource creating (id=%s %s %s)\n", zone, domain, rType)
ttl := d.Get(DNSZoneRecordSchemaTTL).(int)
meta := listToFailoverMeta(d.Get(DNSZoneRecordSchemaRRSetMeta).([]interface{}))
if err := verifyFailoverMeta(meta); err != nil {
return diag.FromErr(err)
}
rrSet := dnssdk.RRSet{TTL: ttl, Records: make([]dnssdk.ResourceRecord, 0), Meta: &meta}
err := fillRRSet(d, rType, &rrSet)
if err != nil {
return diag.FromErr(err)
}
config := m.(*Config)
client := config.DNSClient
_, err = client.Zone(ctx, zone)
if err != nil {
return diag.FromErr(fmt.Errorf("find zone: %w", err))
}
err = client.CreateRRSet(ctx, zone, domain, rType, rrSet)
if err != nil {
return diag.FromErr(fmt.Errorf("create zone rrset: %w", err))
}
d.SetId(zone)
return resourceDNSZoneRecordRead(ctx, d, m)
}
func resourceDNSZoneRecordUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if d.Id() == "" {
return diag.Errorf("empty id")
}
zone := strings.TrimSpace(d.Get(DNSZoneRecordSchemaZone).(string))
domain := strings.TrimSpace(d.Get(DNSZoneRecordSchemaDomain).(string))
rType := strings.TrimSpace(d.Get(DNSZoneRecordSchemaType).(string))
log.Println("[DEBUG] Start DNS Zone Record Resource updating")
defer log.Printf("[DEBUG] Finish DNS Zone Record Resource updating (id=%s %s %s)\n", zone, domain, rType)
ttl := d.Get(DNSZoneRecordSchemaTTL).(int)
meta := listToFailoverMeta(d.Get(DNSZoneRecordSchemaRRSetMeta).([]interface{}))
if err := verifyFailoverMeta(meta); err != nil {
return diag.FromErr(err)
}
rrSet := dnssdk.RRSet{TTL: ttl, Records: make([]dnssdk.ResourceRecord, 0), Meta: &meta}
err := fillRRSet(d, rType, &rrSet)
if err != nil {
return diag.FromErr(err)
}
config := m.(*Config)
client := config.DNSClient
err = client.UpdateRRSet(ctx, zone, domain, rType, rrSet)
if err != nil {
return diag.FromErr(fmt.Errorf("update zone rrset: %w", err))
}
d.SetId(zone)
return resourceDNSZoneRecordRead(ctx, d, m)
}
func resourceDNSZoneRecordRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if d.Id() == "" {
return diag.Errorf("empty id")
}
zone := strings.TrimSpace(d.Get(DNSZoneRecordSchemaZone).(string))
domain := strings.TrimSpace(d.Get(DNSZoneRecordSchemaDomain).(string))
rType := strings.TrimSpace(d.Get(DNSZoneRecordSchemaType).(string))
log.Println("[DEBUG] Start DNS Zone Record Resource reading")
defer log.Printf("[DEBUG] Finish DNS Zone Record Resource reading (id=%s %s %s)\n", zone, domain, rType)
config := m.(*Config)
client := config.DNSClient
result, err := client.RRSet(ctx, zone, domain, rType)
if err != nil {
return diag.FromErr(fmt.Errorf("get zone rrset: %w", err))
}
id := struct{ Zone, Domain, Type string }{zone, domain, rType} //nolint: musttag
bs, err := json.Marshal(id)
if err != nil {
return diag.FromErr(err)
}
d.SetId(string(bs))
_ = d.Set(DNSZoneRecordSchemaZone, zone)
_ = d.Set(DNSZoneRecordSchemaDomain, domain)
_ = d.Set(DNSZoneRecordSchemaType, rType)
_ = d.Set(DNSZoneRecordSchemaTTL, result.TTL)
if result.Meta != nil {
rrsetMeta := failoverMetaToList(result.Meta)
if len(rrsetMeta) > 0 {
err = d.Set(DNSZoneRecordSchemaRRSetMeta, rrsetMeta)
if err != nil {
return diag.FromErr(err)
}
}
}
filters := make([]map[string]interface{}, 0)
for _, f := range result.Filters {
filters = append(filters, map[string]interface{}{
DNSZoneRecordSchemaFilterLimit: f.Limit,
DNSZoneRecordSchemaFilterType: f.Type,
DNSZoneRecordSchemaFilterStrict: f.Strict,
})
}
if len(filters) > 0 {
_ = d.Set(DNSZoneRecordSchemaFilter, filters)
}
rr := make([]map[string]interface{}, 0)
for _, rec := range result.Records {
r := map[string]interface{}{}
r[DNSZoneRecordSchemaEnabled] = rec.Enabled
r[DNSZoneRecordSchemaContent] = rec.ContentToString()
meta := map[string]interface{}{}
for key, val := range rec.Meta {
meta[key] = val
}
if len(meta) > 0 {
r[DNSZoneRecordSchemaMeta] = []map[string]interface{}{meta}
}
rr = append(rr, r)
}
if len(rr) > 0 {
_ = d.Set(DNSZoneRecordSchemaResourceRecord, rr)
}
return nil
}
func resourceDNSZoneRecordDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if d.Id() == "" {
return diag.Errorf("empty id")
}
zone := strings.TrimSpace(d.Get(DNSZoneRecordSchemaZone).(string))
domain := strings.TrimSpace(d.Get(DNSZoneRecordSchemaDomain).(string))
rType := strings.TrimSpace(d.Get(DNSZoneRecordSchemaType).(string))
log.Println("[DEBUG] Start DNS Zone Record Resource deleting")
defer log.Printf("[DEBUG] Finish DNS Zone Record Resource deleting (id=%s %s %s)\n", zone, domain, rType)
config := m.(*Config)
client := config.DNSClient
err := client.DeleteRRSet(ctx, zone, domain, rType)
if err != nil {
return diag.FromErr(fmt.Errorf("delete zone rrset: %w", err))
}
d.SetId("")
return nil
}
func fillRRSet(d *schema.ResourceData, rType string, rrSet *dnssdk.RRSet) error {
// set filters
for _, resource := range d.Get(DNSZoneRecordSchemaFilter).(*schema.Set).List() {
filter := dnssdk.RecordFilter{}
filterData := resource.(map[string]interface{})
name := filterData[DNSZoneRecordSchemaFilterType].(string)
filter.Type = name
limit, ok := filterData[DNSZoneRecordSchemaFilterLimit].(int)
if ok {
filter.Limit = uint(limit)
}
strict, ok := filterData[DNSZoneRecordSchemaFilterStrict].(bool)
if ok {
filter.Strict = strict
}
rrSet.AddFilter(filter)
}
// set meta
for _, resource := range d.Get(DNSZoneRecordSchemaResourceRecord).(*schema.Set).List() {
data := resource.(map[string]interface{})
content := data[DNSZoneRecordSchemaContent].(string)
rr := (&dnssdk.ResourceRecord{}).SetContent(rType, content)
enabled := data[DNSZoneRecordSchemaEnabled].(bool)
rr.Enabled = enabled
metaErrs := make([]error, 0)
for _, dataMeta := range data[DNSZoneRecordSchemaMeta].(*schema.Set).List() {
meta := dataMeta.(map[string]interface{})
validWrap := func(rm dnssdk.ResourceMeta) dnssdk.ResourceMeta {
if rm.Valid() != nil {
metaErrs = append(metaErrs, rm.Valid())
}
return rm
}
val := meta[DNSZoneRecordSchemaMetaIP].([]interface{})
ips := make([]string, len(val))
for i, v := range val {
ips[i] = v.(string)
}
if len(ips) > 0 {
rr.AddMeta(dnssdk.NewResourceMetaIP(ips...))
}
val = meta[DNSZoneRecordSchemaMetaCountries].([]interface{})
countries := make([]string, len(val))
for i, v := range val {
countries[i] = v.(string)
}
if len(countries) > 0 {
rr.AddMeta(dnssdk.NewResourceMetaCountries(countries...))
}
val = meta[DNSZoneRecordSchemaMetaContinents].([]interface{})
continents := make([]string, len(val))
for i, v := range val {
continents[i] = v.(string)
}
if len(continents) > 0 {
rr.AddMeta(dnssdk.NewResourceMetaContinents(continents...))
}
val = meta[DNSZoneRecordSchemaMetaNotes].([]interface{})
notes := make([]string, len(val))
for i, v := range val {
notes[i] = v.(string)
}
if len(notes) > 0 {
rr.AddMeta(dnssdk.NewResourceMetaNotes(notes...))
}
latLongVal := meta[DNSZoneRecordSchemaMetaLatLong].([]interface{})
if len(latLongVal) == 2 {
rr.AddMeta(
validWrap(
dnssdk.NewResourceMetaLatLong(
fmt.Sprintf("%f,%f", latLongVal[0].(float64), latLongVal[1].(float64)))))
}
val = meta[DNSZoneRecordSchemaMetaAsn].([]interface{})
asn := make([]uint64, len(val))
for i, v := range val {
asn[i] = uint64(v.(int))
}
if len(asn) > 0 {
rr.AddMeta(dnssdk.NewResourceMetaAsn(asn...))
}
valDefault := meta[DNSZoneRecordSchemaMetaDefault].(bool)
if valDefault {
rr.AddMeta(validWrap(dnssdk.NewResourceMetaDefault()))
}
}
if len(metaErrs) > 0 {
return fmt.Errorf("invalid meta for zone rrset with content %s: %v", content, metaErrs)
}
rrSet.Records = append(rrSet.Records, *rr)
}
return nil
}
func listToFailoverMeta(m []interface{}) dnssdk.Meta {
var meta dnssdk.Meta
if len(m) == 0 {
return meta
}
if m[0] == nil {
return meta
}
fields := m[0].(map[string]interface{})
if props, ok := getOptByName(fields, "failover"); ok {
meta.Failover = &dnssdk.FailoverMeta{
Protocol: props["protocol"].(string),
Port: props["port"].(int),
Frequency: props["frequency"].(int),
Timeout: props["timeout"].(int),
}
if method, ok := props["method"]; ok {
meta.Failover.Method = method.(string)
}
if url, ok := props["url"]; ok {
meta.Failover.Url = url.(string)
}
if tls, ok := props["tls"]; ok {
meta.Failover.Tls = tls.(bool)
}
if regexp, ok := props["regexp"]; ok {
meta.Failover.Regexp = regexp.(string)
}
if httpStatusCode, ok := props["http_status_code"]; ok {
meta.Failover.HTTPStatusCode = httpStatusCode.(int)
}
if host, ok := props["host"]; ok {
meta.Failover.Host = host.(string)
}
if verify, ok := props["verify"]; ok {
meta.Failover.Verify = verify.(bool)
}
}
return meta
}
func failoverMetaToList(meta *dnssdk.Meta) []interface{} {
result := make(map[string][]interface{})
if meta.Failover != nil {
m := make(map[string]interface{})
if meta.Failover.Protocol != "" {
m["protocol"] = meta.Failover.Protocol
}
if meta.Failover.Port != 0 {
m["port"] = meta.Failover.Port
}
if meta.Failover.Frequency != 0 {
m["frequency"] = meta.Failover.Frequency
}
if meta.Failover.Timeout != 0 {
m["timeout"] = meta.Failover.Timeout
}
if meta.Failover.Method != "" {
m["method"] = meta.Failover.Method
}
if meta.Failover.Url != "" {
m["url"] = meta.Failover.Url
}
if meta.Failover.Tls {
m["tls"] = meta.Failover.Tls
m["verify"] = meta.Failover.Verify
}
if meta.Failover.Regexp != "" {
m["regexp"] = meta.Failover.Regexp
}
if meta.Failover.HTTPStatusCode != 0 {
m["http_status_code"] = meta.Failover.HTTPStatusCode
}
if meta.Failover.Host != "" {
m["host"] = meta.Failover.Host
}
result["failover"] = []interface{}{m}
}
return []interface{}{result}
}
func verifyFailoverMeta(meta dnssdk.Meta) error {
if meta.Failover != nil && meta.Failover.Protocol != "HTTP" {
if meta.Failover.Url != "" {
return fmt.Errorf("failover URL can only be set along with HTTP protocol")
}
if meta.Failover.Host != "" {
return fmt.Errorf("failover host can only be set along with HTTP protocol")
}
if meta.Failover.Regexp != "" {
return fmt.Errorf("failover regexp can only be set along with HTTP protocol")
}
if meta.Failover.Method != "" {
return fmt.Errorf("failover method can only be set along with HTTP protocol")
}
}
return nil
}