1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

IGNORE() now supports glob pattern/wildcards (#463)

This commit is contained in:
Koen Vlaswinkel
2019-05-27 16:14:29 +02:00
committed by Tom Limoncelli
parent b3e35b56a2
commit 5538de6682
40 changed files with 3102 additions and 2 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"sort"
"github.com/gobwas/glob"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/pkg/printer"
)
@@ -32,12 +34,17 @@ func New(dc *models.DomainConfig, extraValues ...func(*models.RecordConfig) map[
return &differ{
dc: dc,
extraValues: extraValues,
// compile IGNORE glob patterns
compiledIgnoredLabels: compileIgnoredLabels(dc.IgnoredLabels),
}
}
type differ struct {
dc *models.DomainConfig
extraValues []func(*models.RecordConfig) map[string]string
compiledIgnoredLabels []glob.Glob
}
// get normalized content for record. target, ttl, mxprio, and specified metadata
@@ -222,9 +229,24 @@ func sortedKeys(m map[string]*models.RecordConfig) []string {
return s
}
func compileIgnoredLabels(ignoredLabels []string) []glob.Glob {
result := make([]glob.Glob, 0, len(ignoredLabels))
for _, tst := range ignoredLabels {
g, err := glob.Compile(tst, '.')
if err != nil {
panic(fmt.Sprintf("Failed to compile IGNORE pattern %q: %v", tst, err))
}
result = append(result, g)
}
return result
}
func (d *differ) matchIgnored(name string) bool {
for _, tst := range d.dc.IgnoredLabels {
if name == tst {
for _, tst := range d.compiledIgnoredLabels {
if tst.Match(name) {
return true
}
}

View File

@@ -230,3 +230,35 @@ func TestModifyingIgnoredRecords(t *testing.T) {
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "www2"})
}
func TestGlobIgnoredRecords(t *testing.T) {
existing := []*models.RecordConfig{
myRecord("www1 MX 1 1.1.1.1"),
myRecord("foo.www2 MX 1 1.1.1.1"),
myRecord("foo.bar.www3 MX 1 1.1.1.1"),
myRecord("www4 MX 1 1.1.1.1"),
}
desired := []*models.RecordConfig{
myRecord("www4 MX 1 2.2.2.2"),
}
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "*.www2", "**.www3"})
}
func TestInvalidGlobIgnoredRecord(t *testing.T) {
existing := []*models.RecordConfig{
myRecord("www1 MX 1 1.1.1.1"),
myRecord("www2 MX 1 1.1.1.1"),
myRecord("www3 MX 1 1.1.1.1"),
}
desired := []*models.RecordConfig{
myRecord("www4 MX 1 2.2.2.2"),
}
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic: invalid glob pattern for IGNORE")
}
}()
checkLengthsFull(t, existing, desired, 0, 1, 0, 0, false, []string{"www1", "www2", "[.www3"})
}