From e838aeeb098acc9b9cc9f85842f28c945dff9d49 Mon Sep 17 00:00:00 2001 From: Oz Tiram Date: Mon, 6 Mar 2023 13:21:10 +0100 Subject: [PATCH] Refactor if ... to switch case Also, slightly improve ready_test --- netbox.go | 21 +++++++++------------ ready_test.go | 4 ++-- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/netbox.go b/netbox.go index 88c05ab..35bcd20 100644 --- a/netbox.go +++ b/netbox.go @@ -67,24 +67,13 @@ func (n *Netbox) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) qname := state.Name() - // check record type here and bail out if not A, AAAA or PTR - if state.QType() != dns.TypeA && state.QType() != dns.TypeAAAA && state.QType() != dns.TypePTR { - // always fallthrough if configured - if n.Fall.Through(qname) { - return plugin.NextOrFailure(n.Name(), n.Next, ctx, w, r) - } - - // otherwise return SERVFAIL here without fallthrough - return dnserror(dns.RcodeServerFailure, state, err) - } - // Export metric with the server label set to the current // server handling the request. requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc() answers := []dns.RR{} - // handle A and AAAA records only + // check record type here and bail out if not A, AAAA or PTR switch state.QType() { case dns.TypeA: ips, err = n.query(strings.TrimRight(qname, "."), familyIP4) @@ -95,6 +84,14 @@ func (n *Netbox) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) case dns.TypePTR: domains, err = n.queryreverse(qname) answers = ptr(qname, uint32(n.TTL), domains) + default: + // always fallthrough if configured + if n.Fall.Through(qname) { + return plugin.NextOrFailure(n.Name(), n.Next, ctx, w, r) + } + + // otherwise return SERVFAIL here without fallthrough + return dnserror(dns.RcodeServerFailure, state, err) } if len(answers) == 0 { diff --git a/ready_test.go b/ready_test.go index fe001ec..a1924df 100644 --- a/ready_test.go +++ b/ready_test.go @@ -15,7 +15,7 @@ func TestNetboxReady(t *testing.T) { nb := Netbox{Url: "https://example.org/api/ipam/ip-addresses", Token: "s3kr3tt0ken", Client: &http.Client{}} ready := nb.Ready() if !ready { - t.Errorf("Expected ready %v, got %v", true, ready) + t.Errorf("Expected ready be %v, got %v", true, ready) } } @@ -27,6 +27,6 @@ func TestNetboxNotReady(t *testing.T) { nb := Netbox{Url: "https://example.org/api/ipam/ip-addresses", Token: "s3kr3tt0ken", Client: &http.Client{}} not_ready := nb.Ready() if not_ready { - t.Errorf("Expected ready %v, got %v", false, not_ready) + t.Errorf("Expected ready to be %v, got %v", false, not_ready) } }