Look up by IP

This commit is contained in:
Bryan Boreham committed 2014-10-24 14:47:49 +01:00
1 parent 5ee60a0003
commit d868d27263
2 files changed
+20

No files matched your search

+12
View File
@@ -11,6 +11,7 @@ type Zone interface {
DeleteRecord(ident string, weaveIp net.IP) error
DeleteRecordsFor(ident string) error
MatchLocal(name string) (net.IP, error)
MatchLocalIP(ip net.IP) (string, error)
}
type Record struct {
@@ -63,6 +64,17 @@ func (zone *ZoneDb) MatchLocal(name string) (net.IP, error) {
return nil, LookupError(name)
}
func (zone *ZoneDb) MatchLocalIP(ip net.IP) (string, error) {
zone.mx.RLock()
defer zone.mx.RUnlock()
for _, r := range zone.recs {
if r.WeaveIP.Equal(ip) {
return r.Name, nil
}
}
return "", LookupError(ip.String())
}
func (zone *ZoneDb) AddRecord(ident string, name string, localIP net.IP, weaveIP net.IP) error {
zone.mx.Lock()
defer zone.mx.Unlock()
+8
View File
@@ -34,6 +34,14 @@ func TestZone(t *testing.T) {
t.Fatal("Unexpected result for", successTestName, foundIP)
}
// See if we can find the address by IP.
foundName, err := zone.MatchLocalIP(weaveIP)
assertNoErr(t, err)
if foundName != successTestName {
t.Fatal("Unexpected result for", weaveIP, foundName)
}
// Now try to add the same address again
err = zone.AddRecord(containerID, successTestName, ip, weaveIP)
assertErrorType(t, err, (*DuplicateError)(nil), "duplicate add")