-
Notifications
You must be signed in to change notification settings - Fork 2
/
set.go
70 lines (57 loc) · 1.25 KB
/
set.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
package newdns
import (
"fmt"
"time"
)
// Set is a set of records.
type Set struct {
// The FQDN of the set.
Name string
// The type of the record.
Type Type
// The records in the set.
Records []Record
// The TTL of the set.
//
// Default: 5m.
TTL time.Duration
}
// Validate will validate the set and ensure defaults.
func (s *Set) Validate() error {
// check name
if !IsDomain(s.Name, true) {
return fmt.Errorf("invalid name: %s", s.Name)
}
// check type
if !s.Type.supported() {
return fmt.Errorf("unsupported type: %d", s.Type)
}
// check records
if len(s.Records) == 0 {
return fmt.Errorf("missing records")
}
// check CNAME records
if s.Type == CNAME && len(s.Records) > 1 {
return fmt.Errorf("multiple CNAME records")
}
// validate records
for _, record := range s.Records {
err := record.Validate(s.Type)
if err != nil {
return fmt.Errorf("invalid record: %w", err)
}
}
// check for duplicate addresses if not TXT
if len(s.Records) > 1 && s.Type != TXT {
for i := 0; i < len(s.Records)-1; i++ {
if s.Records[i].Address == s.Records[i+1].Address {
return fmt.Errorf("duplicate address: %s", s.Records[i].Address)
}
}
}
// set default ttl
if s.TTL == 0 {
s.TTL = 5 * time.Minute
}
return nil
}