Skip to content

Commit

Permalink
Create models.MustCreateRecord, remove models.NewFromRawA
Browse files Browse the repository at this point in the history
  • Loading branch information
tlimoncelli committed Jan 14, 2025
1 parent 53e0be3 commit e95350d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 47 deletions.
19 changes: 5 additions & 14 deletions integrationTest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,29 +538,20 @@ func makeRec2(typ string) *models.RecordConfig {
}

func a(name string, a string) *models.RecordConfig {
rc, err := models.NewFromRawA([]string{name, a}, nil, "**current-domain**", 300)
rdata, err := models.ParseA([]string{a}, "**current-domain**")
if err != nil {
panic(err)
}
return rc
return models.MustCreateRecord(name, rdata, nil, 300, "**current-domain**")
}

func mx(name string, preference uint16, mx string) *models.RecordConfig {
rc := makeRec2("MX")
spreference := strconv.Itoa(int(preference))
if err := models.FromRaw(rc, "**current-domain**", "MX", []string{name, spreference, mx}, nil); err != nil {
rdata, err := models.ParseMX([]string{spreference, mx}, "**current-domain**")
if err != nil {
panic(err)
}

//fmt.Printf("DEBUG: MX Fields %v\n", rc.Fields)

if rc.Name != strings.ToLower(rc.Name) {
panic("MX short must be lowercase")
}
if rc.NameFQDN != strings.ToLower(rc.NameFQDN) {
panic("MX must be lowercase 2")
}
return rc
return models.MustCreateRecord(name, rdata, nil, 300, "**current-domain**")
}

func srv(name string, priority, weight, port uint16, target string) *models.RecordConfig {
Expand Down
40 changes: 13 additions & 27 deletions models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"maps"
"strconv"
"strings"

"github.com/StackExchange/dnscontrol/v4/pkg/fieldtypes"
)
Expand Down Expand Up @@ -106,6 +107,18 @@ func (rc *RecordConfig) Seal() error {
return nil
}

func MustCreateRecord[T RecordType](label string, rdata T, meta map[string]string, ttl uint32, origin string) *RecordConfig {
rc := &RecordConfig{
Type: strings.Split(fmt.Sprintf("%T", rdata), ".")[1],
TTL: ttl,
}
rc.SetLabel3(label, "", origin) // Label
if err := RecordUpdateFields(rc, rdata, meta); err != nil {
panic(err)
}
return rc
}

//// A

// A is the fields needed to store a DNS record of type A
Expand All @@ -122,15 +135,6 @@ func ParseA(rawfields []string, origin string) (A, error) {
return A{A: a}, nil
}

// NewFromRawA creates a new RecordConfig of type A from rawfields, meta, and origin.
func NewFromRawA(rawfields []string, meta map[string]string, origin string, ttl uint32) (*RecordConfig, error) {
rc := &RecordConfig{TTL: ttl}
if err := PopulateFromRawA(rc, rawfields, meta, origin); err != nil {
return nil, err
}
return rc, nil
}

// PopulateFromRawA updates rc to be an A record with contents from rawfields, meta and origin.
func PopulateFromRawA(rc *RecordConfig, rawfields []string, meta map[string]string, origin string) error {
rc.Type = "A"
Expand Down Expand Up @@ -192,15 +196,6 @@ func ParseMX(rawfields []string, origin string) (MX, error) {
return MX{Preference: preference, Mx: mx}, nil
}

// NewFromRawMX creates a new RecordConfig of type MX from rawfields, meta, and origin.
func NewFromRawMX(rawfields []string, meta map[string]string, origin string, ttl uint32) (*RecordConfig, error) {
rc := &RecordConfig{TTL: ttl}
if err := PopulateFromRawMX(rc, rawfields, meta, origin); err != nil {
return nil, err
}
return rc, nil
}

// PopulateFromRawMX updates rc to be an MX record with contents from rawfields, meta and origin.
func PopulateFromRawMX(rc *RecordConfig, rawfields []string, meta map[string]string, origin string) error {
rc.Type = "MX"
Expand Down Expand Up @@ -272,15 +267,6 @@ func ParseSRV(rawfields []string, origin string) (SRV, error) {
return SRV{Priority: priority, Weight: weight, Port: port, Target: target}, nil
}

// NewFromRawSRV creates a new RecordConfig of type SRV from rawfields, meta, and origin.
func NewFromRawSRV(rawfields []string, meta map[string]string, origin string, ttl uint32) (*RecordConfig, error) {
rc := &RecordConfig{TTL: ttl}
if err := PopulateFromRawSRV(rc, rawfields, meta, origin); err != nil {
return nil, err
}
return rc, nil
}

// PopulateFromRawSRV updates rc to be an SRV record with contents from rawfields, meta and origin.
func PopulateFromRawSRV(rc *RecordConfig, rawfields []string, meta map[string]string, origin string) error {
rc.Type = "SRV"
Expand Down
6 changes: 0 additions & 6 deletions pkg/fieldtypes/fieldtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ func ParseHostnameDot(short, subdomain, origin string) (string, error) {
return "FAIL", fmt.Errorf("short must not be empty")
}
short = strings.ToLower(short)
// if strings.ToLower(short) != short {
// return "", fmt.Errorf("short (%s) must be lowercase", short)
// }
// if short == "." {
// return "", fmt.Errorf("label (%s) must not be just a dot", short)
// }

if lastCharIs(short, '.') {
return short, nil
Expand Down

0 comments on commit e95350d

Please sign in to comment.