-
Notifications
You must be signed in to change notification settings - Fork 67
/
records.go
196 lines (169 loc) · 4.08 KB
/
records.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package dns
import (
"fmt"
"strings"
)
const (
RS_META = "META"
RS_ALIAS_A = "ALIAS" // provider specific alias for CNAME record (AWS alias target A)
RS_ALIAS_AAAA = "ALIAS_AAAA" // provider specific alias for CNAME record (AWS alias target AAAA)
)
const (
RS_TXT = "TXT"
RS_CNAME = "CNAME"
RS_A = "A"
RS_AAAA = "AAAA"
)
const RS_NS = "NS"
////////////////////////////////////////////////////////////////////////////////
// Record Sets
////////////////////////////////////////////////////////////////////////////////
type (
RecordSets map[string]*RecordSet
Records []*Record
)
func (this RecordSets) Clone() RecordSets {
clone := RecordSets{}
for rk, rv := range this {
clone[rk] = rv.Clone()
}
return clone
}
type Record struct {
Value string
}
func (this *Record) Clone() *Record {
return &Record{this.Value}
}
type RecordSet struct {
Type string
TTL int64
IgnoreTTL bool
Records Records
}
func NewRecordSet(rtype string, ttl int64, records []*Record) *RecordSet {
return &RecordSet{Type: rtype, TTL: ttl, Records: records}
}
func (this *RecordSet) Clone() *RecordSet {
set := &RecordSet{Type: this.Type, TTL: this.TTL, IgnoreTTL: this.IgnoreTTL}
for _, r := range this.Records {
set.Records = append(set.Records, r.Clone())
}
return set
}
func (this *RecordSet) Length() int {
if this == nil {
return 0
}
return len(this.Records)
}
func (this *RecordSet) Add(records ...*Record) *RecordSet {
for _, r := range records {
this.Records = append(this.Records, r)
}
return this
}
func (this *RecordSet) RecordString() string {
line := ""
sep := ""
for _, r := range this.Records {
line = fmt.Sprintf("%s%s%s", line, sep, r.Value)
sep = ", "
}
if line == "" {
return "no records"
}
return "[" + line + "]"
}
func (this *RecordSet) Match(set *RecordSet) bool {
if len(this.Records) != len(set.Records) {
return false
}
if !this.IgnoreTTL && !set.IgnoreTTL && this.TTL != set.TTL {
return false
}
for _, r := range this.Records {
found := false
for _, t := range set.Records {
if t.Value == r.Value {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func (this *RecordSet) GetAttr(name string) string {
if this.Type == RS_TXT || this.Type == RS_META {
prefix := newAttrKeyPrefix(name)
for _, r := range this.Records {
if strings.HasPrefix(r.Value, prefix) {
return r.Value[len(prefix) : len(r.Value)-1]
}
}
}
return ""
}
func (this *RecordSet) SetAttr(name string, value string) {
prefix := newAttrKeyPrefix(name)
for _, r := range this.Records {
if strings.HasPrefix(r.Value, prefix) {
r.Value = newAttrValue(name, value)
return
}
}
r := newAttrRecord(name, value)
this.Records = append(this.Records, r)
}
func (this *RecordSet) DeleteAttr(name string) {
prefix := newAttrKeyPrefix(name)
for i, r := range this.Records {
if strings.HasPrefix(r.Value, prefix) {
this.Records = append(this.Records[:i], this.Records[i+1:]...)
return
}
}
}
func (this *RecordSet) DiffTo(set *RecordSet) (new Records, update Records, delete Records) {
nextOwn:
for _, r := range this.Records {
for _, d := range set.Records {
if d.Value == r.Value {
if this.TTL != set.TTL {
update = append(update, r)
}
continue nextOwn
}
}
new = append(new, r)
}
nextForeign:
for _, d := range set.Records {
for _, r := range this.Records {
if d.Value == r.Value {
continue nextForeign
}
}
delete = append(delete, d)
}
return
}
func newAttrKeyPrefix(name string) string {
return fmt.Sprintf("\"%s=", name)
}
func newAttrValue(name, value string) string {
return fmt.Sprintf("%s%s\"", newAttrKeyPrefix(name), value)
}
func newAttrRecord(name, value string) *Record {
return &Record{Value: newAttrValue(name, value)}
}
func newAttrRecordSet(ty string, name, value string) *RecordSet {
records := []*Record{newAttrRecord(name, value)}
return &RecordSet{Type: ty, TTL: 600, IgnoreTTL: false, Records: records}
}