s/weaveIP/ip

This commit is contained in:
Matthias Radestock committed 2014-11-27 16:31:42 +00:00
1 parent 24702e3a7a
commit 56bfb09656
5 files changed
+47 -47

No files matched your search

+14 -14
View File
@@ -45,22 +45,22 @@ func ListenHttp(domain string, db Zone, port int) {
switch r.Method { switch r.Method {
case "PUT": case "PUT":
ident, weaveIPstr, err := parseUrl(r.URL.Path) ident, ipStr, err := parseUrl(r.URL.Path)
name := r.FormValue("fqdn") name := r.FormValue("fqdn")
if ident == "" || weaveIPstr == "" || name == "" { if ident == "" || ipStr == "" || name == "" {
reqError("Invalid request", "Invalid request: %s, %s", r.URL, r.Form) reqError("Invalid request", "Invalid request: %s, %s", r.URL, r.Form)
return return
} }
weaveIP := net.ParseIP(weaveIPstr) ip := net.ParseIP(ipStr)
if weaveIP == nil { if ip == nil {
reqError("Invalid weave IP", "Invalid weave IP in request: %s", weaveIPstr) reqError("Invalid IP", "Invalid IP in request: %s", ipStr)
return return
} }
if dns.IsSubDomain(domain, name) { if dns.IsSubDomain(domain, name) {
Info.Printf("Adding %s -> %s", name, weaveIPstr) Info.Printf("Adding %s -> %s", name, ipStr)
if err = db.AddRecord(ident, name, weaveIP); err != nil { if err = db.AddRecord(ident, name, ip); err != nil {
if dup, ok := err.(DuplicateError); !ok { if dup, ok := err.(DuplicateError); !ok {
httpErrorAndLog( httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError, Error, w, "Internal error", http.StatusInternalServerError,
@@ -76,19 +76,19 @@ func ListenHttp(domain string, db Zone, port int) {
} }
case "DELETE": case "DELETE":
ident, weaveIPstr, err := parseUrl(r.URL.Path) ident, ipStr, err := parseUrl(r.URL.Path)
if ident == "" || weaveIPstr == "" { if ident == "" || ipStr == "" {
reqError("Invalid Request", "Invalid request: %s, %s", r.URL, r.Form) reqError("Invalid Request", "Invalid request: %s, %s", r.URL, r.Form)
return return
} }
weaveIP := net.ParseIP(weaveIPstr) ip := net.ParseIP(ipStr)
if weaveIP == nil { if ip == nil {
reqError("Invalid IP in request", "Invalid IP in request: %s", weaveIPstr) reqError("Invalid IP in request", "Invalid IP in request: %s", ipStr)
return return
} }
Info.Printf("Deleting %s (%s)", ident, weaveIPstr) Info.Printf("Deleting %s (%s)", ident, ipStr)
if err = db.DeleteRecord(ident, weaveIP); err != nil { if err = db.DeleteRecord(ident, ip); err != nil {
if _, ok := err.(LookupError); !ok { if _, ok := err.(LookupError); !ok {
httpErrorAndLog( httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError, Error, w, "Internal error", http.StatusInternalServerError,
+4 -4
View File
@@ -42,11 +42,11 @@ func TestHttp(t *testing.T) {
assertStatus(t, resp.StatusCode, http.StatusOK, "http response") assertStatus(t, resp.StatusCode, http.StatusOK, "http response")
// Check that the address is now there. // Check that the address is now there.
ip, err := zone.MatchLocal(successTestName) foundIP, err := zone.MatchLocal(successTestName)
assertNoErr(t, err) assertNoErr(t, err)
weaveIP, _, _ := net.ParseCIDR(testAddr1) ip, _, _ := net.ParseCIDR(testAddr1)
if !ip.Equal(weaveIP) { if !foundIP.Equal(ip) {
t.Fatal("Unexpected result for", successTestName, ip) t.Fatal("Unexpected result for", successTestName, foundIP)
} }
// Adding exactly the same address should be OK // Adding exactly the same address should be OK
+3 -3
View File
@@ -32,8 +32,8 @@ func sendQuery(name string, querytype uint16) error {
func TestServerSimpleQuery(t *testing.T) { func TestServerSimpleQuery(t *testing.T) {
log.Println("TestServerSimpleQuery starting") log.Println("TestServerSimpleQuery starting")
var zone = new(ZoneDb) var zone = new(ZoneDb)
weaveIP, _, _ := net.ParseCIDR(testAddr1) ip, _, _ := net.ParseCIDR(testAddr1)
zone.AddRecord(containerID, "test.weave.", weaveIP) zone.AddRecord(containerID, "test.weave.", ip)
mdnsServer, err := NewMDNSServer(zone) mdnsServer, err := NewMDNSServer(zone)
assertNoErr(t, err) assertNoErr(t, err)
@@ -73,7 +73,7 @@ func TestServerSimpleQuery(t *testing.T) {
if receivedCount != 1 { if receivedCount != 1 {
t.Fatal("Unexpected result count for test.weave", receivedCount) t.Fatal("Unexpected result count for test.weave", receivedCount)
} }
if !receivedAddr.Equal(weaveIP) { if !receivedAddr.Equal(ip) {
t.Fatal("Unexpected result for test.weave", receivedAddr) t.Fatal("Unexpected result for test.weave", receivedAddr)
} }
+17 -17
View File
@@ -7,17 +7,17 @@ import (
) )
type Zone interface { type Zone interface {
AddRecord(ident string, name string, weaveIP net.IP) error AddRecord(ident string, name string, ip net.IP) error
DeleteRecord(ident string, weaveIp net.IP) error DeleteRecord(ident string, ip net.IP) error
DeleteRecordsFor(ident string) error DeleteRecordsFor(ident string) error
MatchLocal(name string) (net.IP, error) MatchLocal(name string) (net.IP, error)
MatchLocalIP(ip net.IP) (string, error) MatchLocalIP(ip net.IP) (string, error)
} }
type Record struct { type Record struct {
Ident string Ident string
Name string Name string
WeaveIP net.IP IP net.IP
} }
// Very simple data structure for now, with linear searching. // Very simple data structure for now, with linear searching.
@@ -34,13 +34,13 @@ func (ops LookupError) Error() string {
} }
type DuplicateError struct { type DuplicateError struct {
Name string Name string
WeaveIP net.IP IP net.IP
Ident string Ident string
} }
func (err DuplicateError) Error() string { func (err DuplicateError) Error() string {
return "Duplicate " + err.Name + "," + err.WeaveIP.String() + " for identity " + err.Ident return "Duplicate " + err.Name + "," + err.IP.String() + " for identity " + err.Ident
} }
func (zone *ZoneDb) indexOf(match func(Record) bool) int { func (zone *ZoneDb) indexOf(match func(Record) bool) int {
@@ -57,7 +57,7 @@ func (zone *ZoneDb) MatchLocal(name string) (net.IP, error) {
defer zone.mx.RUnlock() defer zone.mx.RUnlock()
for _, r := range zone.recs { for _, r := range zone.recs {
if r.Name == name { if r.Name == name {
return r.WeaveIP, nil return r.IP, nil
} }
} }
return nil, LookupError(name) return nil, LookupError(name)
@@ -67,30 +67,30 @@ func (zone *ZoneDb) MatchLocalIP(ip net.IP) (string, error) {
zone.mx.RLock() zone.mx.RLock()
defer zone.mx.RUnlock() defer zone.mx.RUnlock()
for _, r := range zone.recs { for _, r := range zone.recs {
if r.WeaveIP.Equal(ip) { if r.IP.Equal(ip) {
return r.Name, nil return r.Name, nil
} }
} }
return "", LookupError(ip.String()) return "", LookupError(ip.String())
} }
func (zone *ZoneDb) AddRecord(ident string, name string, weaveIP net.IP) error { func (zone *ZoneDb) AddRecord(ident string, name string, ip net.IP) error {
zone.mx.Lock() zone.mx.Lock()
defer zone.mx.Unlock() defer zone.mx.Unlock()
fqdn := dns.Fqdn(name) fqdn := dns.Fqdn(name)
if index := zone.indexOf( if index := zone.indexOf(
func(r Record) bool { return r.Name == fqdn && r.WeaveIP.Equal(weaveIP) }); index != -1 { func(r Record) bool { return r.Name == fqdn && r.IP.Equal(ip) }); index != -1 {
return DuplicateError{fqdn, weaveIP, zone.recs[index].Ident} return DuplicateError{fqdn, ip, zone.recs[index].Ident}
} }
zone.recs = append(zone.recs, Record{ident, fqdn, weaveIP}) zone.recs = append(zone.recs, Record{ident, fqdn, ip})
return nil return nil
} }
func (zone *ZoneDb) DeleteRecord(ident string, weaveIP net.IP) error { func (zone *ZoneDb) DeleteRecord(ident string, ip net.IP) error {
zone.mx.Lock() zone.mx.Lock()
defer zone.mx.Unlock() defer zone.mx.Unlock()
if index := zone.indexOf( if index := zone.indexOf(
func(r Record) bool { return r.Ident == ident && r.WeaveIP.Equal(weaveIP) }); index == -1 { func(r Record) bool { return r.Ident == ident && r.IP.Equal(ip) }); index == -1 {
return LookupError(ident) return LookupError(ident)
} else { } else {
zone.recs = append(zone.recs[:index], zone.recs[index+1:]...) zone.recs = append(zone.recs[:index], zone.recs[index+1:]...)
+9 -9
View File
@@ -14,8 +14,8 @@ func TestZone(t *testing.T) {
var zone = new(ZoneDb) var zone = new(ZoneDb)
weaveIP, _, _ := net.ParseCIDR(testAddr1) ip, _, _ := net.ParseCIDR(testAddr1)
err := zone.AddRecord(containerID, successTestName, weaveIP) err := zone.AddRecord(containerID, successTestName, ip)
assertNoErr(t, err) assertNoErr(t, err)
// Add a few more records to make the job harder // Add a few more records to make the job harder
@@ -28,24 +28,24 @@ func TestZone(t *testing.T) {
foundIP, err := zone.MatchLocal(successTestName) foundIP, err := zone.MatchLocal(successTestName)
assertNoErr(t, err) assertNoErr(t, err)
if !foundIP.Equal(weaveIP) { if !foundIP.Equal(ip) {
t.Fatal("Unexpected result for", successTestName, foundIP) t.Fatal("Unexpected result for", successTestName, foundIP)
} }
// See if we can find the address by IP. // See if we can find the address by IP.
foundName, err := zone.MatchLocalIP(weaveIP) foundName, err := zone.MatchLocalIP(ip)
assertNoErr(t, err) assertNoErr(t, err)
if foundName != successTestName { if foundName != successTestName {
t.Fatal("Unexpected result for", weaveIP, foundName) t.Fatal("Unexpected result for", ip, foundName)
} }
// Now try to add the same address again // Now try to add the same address again
err = zone.AddRecord(containerID, successTestName, weaveIP) err = zone.AddRecord(containerID, successTestName, ip)
assertErrorType(t, err, (*DuplicateError)(nil), "duplicate add") assertErrorType(t, err, (*DuplicateError)(nil), "duplicate add")
// Now delete the record // Now delete the record
err = zone.DeleteRecord(containerID, weaveIP) err = zone.DeleteRecord(containerID, ip)
assertNoErr(t, err) assertNoErr(t, err)
// Check that the address is not there now. // Check that the address is not there now.
@@ -66,8 +66,8 @@ func TestDeleteFor(t *testing.T) {
) )
zone := new(ZoneDb) zone := new(ZoneDb)
for _, addr := range []string{addr1, addr2} { for _, addr := range []string{addr1, addr2} {
weaveIP, _, _ := net.ParseCIDR(addr) ip, _, _ := net.ParseCIDR(addr)
err := zone.AddRecord(id, name, weaveIP) err := zone.AddRecord(id, name, ip)
assertNoErr(t, err) assertNoErr(t, err)
} }