nomad: removing old network index lookup methods
This commit is contained in:
parent
373c4ee83a
commit
7586b39a7b
|
@ -3,7 +3,6 @@ package structs
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/go-msgpack/codec"
|
||||
|
@ -551,50 +550,10 @@ func (r *Resources) Copy() *Resources {
|
|||
return newR
|
||||
}
|
||||
|
||||
// NetIndex finds the matching net index either using IP or
|
||||
// CIDR block lookup
|
||||
// NetIndex finds the matching net index using device name
|
||||
func (r *Resources) NetIndex(n *NetworkResource) int {
|
||||
if n.IP != "" {
|
||||
return r.NetIndexByIP(n.IP)
|
||||
}
|
||||
return r.NetIndexByCIDR(n.CIDR)
|
||||
}
|
||||
|
||||
// NetIndexByCIDR scans the list of networks for a matching
|
||||
// CIDR, returning the index. This currently ONLY handles
|
||||
// an exact match and not a subset CIDR.
|
||||
func (r *Resources) NetIndexByCIDR(cidr string) int {
|
||||
for idx, net := range r.Networks {
|
||||
if net.CIDR == cidr {
|
||||
return idx
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// NetIndexByIP scans the list of networks for a matching
|
||||
// CIDR by IP, returning the index.
|
||||
func (r *Resources) NetIndexByIP(ip string) int {
|
||||
// Parse the IP
|
||||
parsed := net.ParseIP(ip)
|
||||
if parsed == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
for idx, n := range r.Networks {
|
||||
// Look for exact IP match
|
||||
if n.IP == ip {
|
||||
return idx
|
||||
}
|
||||
|
||||
// Check for CIDR subset
|
||||
if n.CIDR == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if the CIDR contains the IP
|
||||
_, cidr, _ := net.ParseCIDR(n.CIDR)
|
||||
if cidr != nil && cidr.Contains(parsed) {
|
||||
if net.Device == n.Device {
|
||||
return idx
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,38 +5,21 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestResource_NetIndexByCIDR(t *testing.T) {
|
||||
func TestResource_NetIndex(t *testing.T) {
|
||||
r := &Resources{
|
||||
Networks: []*NetworkResource{
|
||||
&NetworkResource{CIDR: "10.0.0.0/8"},
|
||||
&NetworkResource{CIDR: "127.0.0.0/24"},
|
||||
&NetworkResource{Device: "eth0"},
|
||||
&NetworkResource{Device: "lo0"},
|
||||
&NetworkResource{Device: ""},
|
||||
},
|
||||
}
|
||||
if idx := r.NetIndexByCIDR("10.0.0.0/8"); idx != 0 {
|
||||
if idx := r.NetIndex(&NetworkResource{Device: "eth0"}); idx != 0 {
|
||||
t.Fatalf("Bad: %d", idx)
|
||||
}
|
||||
if idx := r.NetIndexByCIDR("127.0.0.0/24"); idx != 1 {
|
||||
if idx := r.NetIndex(&NetworkResource{Device: "lo0"}); idx != 1 {
|
||||
t.Fatalf("Bad: %d", idx)
|
||||
}
|
||||
if idx := r.NetIndexByCIDR("10.0.0.0/16"); idx != -1 {
|
||||
t.Fatalf("Bad: %d", idx)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResource_NetIndexByIP(t *testing.T) {
|
||||
r := &Resources{
|
||||
Networks: []*NetworkResource{
|
||||
&NetworkResource{CIDR: "10.0.0.0/8"},
|
||||
&NetworkResource{CIDR: "127.0.0.0/24"},
|
||||
},
|
||||
}
|
||||
if idx := r.NetIndexByIP("10.1.2.3"); idx != 0 {
|
||||
t.Fatalf("Bad: %d", idx)
|
||||
}
|
||||
if idx := r.NetIndexByIP("127.0.0.1"); idx != 1 {
|
||||
t.Fatalf("Bad: %d", idx)
|
||||
}
|
||||
if idx := r.NetIndexByIP("11.2.3.4"); idx != -1 {
|
||||
if idx := r.NetIndex(&NetworkResource{Device: "eth1"}); idx != -1 {
|
||||
t.Fatalf("Bad: %d", idx)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue