pdns-auth-proxy/pdns_search.go

91 lines
2.4 KiB
Go

package main
type (
// DNSSearch is the json structure of a search query response
DNSSearch []*DNSSearchEntry
// DNSSearchEntry is an element of a DNSSearch
DNSSearchEntry struct {
Content string `json:"content"`
Disabled bool `json:"disabled"`
Name string `json:"name"`
ObjectType string `json:"object_type"`
TTL int `json:"ttl"`
Type string `json:"type"`
Zone string `json:"zone"`
ZoneID string `json:"zone_id"`
}
)
// IsRecord tells if d is a record or not
func (d DNSSearchEntry) IsRecord() bool {
return d.ObjectType == "record"
}
// IsComment tells if d is a comment or not
func (d DNSSearchEntry) IsComment() bool {
return d.ObjectType == "comment"
}
// DNSQuery converts a DNSSearch to a DNSQuery
func (d DNSSearch) DNSQuery() *DNSQuery {
tempRet := map[string]map[string]*DNSRRSet{}
tempContent := map[string]map[string][]*DNSRecord{}
comments := map[string][]*DNSComments{}
for _, record := range d {
switch {
// ignore disabled records
case record.Disabled:
continue
// store the comments
case record.IsComment():
comments[record.Name] = []*DNSComments{{Content: record.Content}}
continue
// ignore non records
case !record.IsRecord():
continue
}
// check if we already encounter this record
if _, ok := tempRet[record.Name]; !ok {
tempRet[record.Name] = map[string]*DNSRRSet{}
tempContent[record.Name] = map[string][]*DNSRecord{}
}
// store the entry
if _, ok := tempRet[record.Name][record.Type]; !ok {
tempRet[record.Name][record.Type] = &DNSRRSet{
Name: record.Name,
Type: record.Type,
ChangeType: "REPLACE",
TTL: record.TTL,
}
tempContent[record.Name][record.Type] = []*DNSRecord{}
}
// and store the content
tempContent[record.Name][record.Type] = append(tempContent[record.Name][record.Type], &DNSRecord{
Content: record.Content,
Disabled: false,
SetPTR: false,
})
}
// stitch everything together
retRRSet := []*DNSRRSet{}
for rName, recordsByName := range tempRet {
for rType, record := range recordsByName {
if c, ok := comments[rName]; ok {
record.Comment = c
}
if content, ok := tempContent[rName][rType]; ok {
record.Records = content
}
retRRSet = append(retRRSet, record)
}
}
return &DNSQuery{RRSets: retRRSet}
}
func (d *DNSSearch) String() string {
return string(printJSON(d))
}