plugin/metadata: some cleanups (#1906)

* plugin/metadata: some cleanups

Name to provider.go as that's what being defined right now in the file.
Use request.Request because that's done in variables.go anyway. Name the
main storage M, because there is no further meaning behind.

Remove superfluous methods

Signed-off-by: Miek Gieben <miek@miek.nl>

* Fix test

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben
2018-06-29 15:03:25 +01:00
committed by GitHub
parent e6c00f39f1
commit 2fd31cd3e0
7 changed files with 53 additions and 58 deletions

View File

@@ -7,8 +7,6 @@ import (
"strconv"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
const (
@@ -26,35 +24,32 @@ var All = []string{queryName, queryType, clientIP, clientPort, protocol, serverI
// GetValue calculates and returns the data specified by the variable name.
// Supported varNames are listed in allProvidedVars.
func GetValue(varName string, w dns.ResponseWriter, r *dns.Msg) ([]byte, error) {
req := request.Request{W: w, Req: r}
func GetValue(state request.Request, varName string) ([]byte, error) {
switch varName {
case queryName:
//Query name is written as ascii string
return []byte(req.QName()), nil
return []byte(state.QName()), nil
case queryType:
return uint16ToWire(req.QType()), nil
return uint16ToWire(state.QType()), nil
case clientIP:
return ipToWire(req.Family(), req.IP())
return ipToWire(state.Family(), state.IP())
case clientPort:
return portToWire(req.Port())
return portToWire(state.Port())
case protocol:
// Proto is written as ascii string
return []byte(req.Proto()), nil
return []byte(state.Proto()), nil
case serverIP:
ip, _, err := net.SplitHostPort(w.LocalAddr().String())
ip, _, err := net.SplitHostPort(state.W.LocalAddr().String())
if err != nil {
ip = w.RemoteAddr().String()
ip = state.W.RemoteAddr().String()
}
return ipToWire(family(w.RemoteAddr()), ip)
return ipToWire(state.Family(), ip)
case serverPort:
_, port, err := net.SplitHostPort(w.LocalAddr().String())
_, port, err := net.SplitHostPort(state.W.LocalAddr().String())
if err != nil {
port = "0"
}

View File

@@ -5,6 +5,8 @@ import (
"testing"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
@@ -63,8 +65,9 @@ func TestGetValue(t *testing.T) {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)
m.Question[0].Qclass = dns.ClassINET
state := request.Request{W: &test.ResponseWriter{}, Req: m}
value, err := GetValue(tc.varName, &test.ResponseWriter{}, m)
value, err := GetValue(state, tc.varName)
if tc.shouldErr && err == nil {
t.Errorf("Test %d: Expected error, but didn't recieve", i)