Skip to content

Commit

Permalink
Refactor if ... to switch case
Browse files Browse the repository at this point in the history
Also, slightly improve ready_test
  • Loading branch information
oz123 committed Mar 6, 2023
1 parent 289411e commit e838aee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
21 changes: 9 additions & 12 deletions netbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions ready_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

0 comments on commit e838aee

Please sign in to comment.