-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
133 lines (108 loc) · 2.27 KB
/
dns.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
package config
import (
"encoding/json"
"github.com/apex/up/internal/validate"
"github.com/pkg/errors"
)
// recordTypes is a list of valid record types.
var recordTypes = []string{
"ALIAS",
"A",
"AAAA",
"CNAME",
"MX",
"NAPTR",
"NS",
"PTR",
"SOA",
"SPF",
"SRV",
"TXT",
}
// DNS config.
type DNS struct {
Zones []*Zone `json:"zones"`
}
// UnmarshalJSON implementation.
func (d *DNS) UnmarshalJSON(b []byte) error {
var zones map[string][]*Record
if err := json.Unmarshal(b, &zones); err != nil {
return err
}
for name, records := range zones {
zone := &Zone{Name: name, Records: records}
d.Zones = append(d.Zones, zone)
}
return nil
}
// Default implementation.
func (d *DNS) Default() error {
for _, z := range d.Zones {
if err := z.Default(); err != nil {
return errors.Wrapf(err, "zone %s", z.Name)
}
}
return nil
}
// Validate implementation.
func (d *DNS) Validate() error {
for _, z := range d.Zones {
if err := z.Validate(); err != nil {
return errors.Wrapf(err, "zone %s", z.Name)
}
}
return nil
}
// Zone is a DNS zone.
type Zone struct {
Name string `json:"name"`
Records []*Record `json:"records"`
}
// Default implementation.
func (z *Zone) Default() error {
for i, r := range z.Records {
if err := r.Default(); err != nil {
return errors.Wrapf(err, "record %d", i)
}
}
return nil
}
// Validate implementation.
func (z *Zone) Validate() error {
for i, r := range z.Records {
if err := r.Validate(); err != nil {
return errors.Wrapf(err, "record %d", i)
}
}
return nil
}
// Record is a DNS record.
type Record struct {
Name string `json:"name"`
Type string `json:"type"`
TTL int `json:"ttl"`
Value []string `json:"value"`
}
// Validate implementation.
func (r *Record) Validate() error {
if err := validate.List(r.Type, recordTypes); err != nil {
return errors.Wrap(err, ".type")
}
if err := validate.RequiredString(r.Name); err != nil {
return errors.Wrap(err, ".name")
}
if err := validate.RequiredStrings(r.Value); err != nil {
return errors.Wrap(err, ".value")
}
if err := validate.MinStrings(r.Value, 1); err != nil {
return errors.Wrap(err, ".value")
}
return nil
}
// Default implementation.
func (r *Record) Default() error {
if r.TTL == 0 {
r.TTL = 300
}
return nil
}