Add endpoint filter
This commit is contained in:
parent
febe87dfb8
commit
ed764ca0b2
@ -4,6 +4,25 @@ import (
|
|||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FilterEndpoint is an endpoint based Select Filter which will
|
||||||
|
// only return services with the endpoint specified.
|
||||||
|
func FilterEndpoint(name string) Filter {
|
||||||
|
return func(old []*registry.Service) []*registry.Service {
|
||||||
|
var services []*registry.Service
|
||||||
|
|
||||||
|
for _, service := range old {
|
||||||
|
for _, ep := range service.Endpoints {
|
||||||
|
if ep.Name == name {
|
||||||
|
services = append(services, service)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return services
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FilterLabel is a label based Select Filter which will
|
// FilterLabel is a label based Select Filter which will
|
||||||
// only return services with the label specified.
|
// only return services with the label specified.
|
||||||
func FilterLabel(key, val string) Filter {
|
func FilterLabel(key, val string) Filter {
|
||||||
|
@ -6,6 +6,87 @@ import (
|
|||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestFilterEndpoint(t *testing.T) {
|
||||||
|
testData := []struct {
|
||||||
|
services []*registry.Service
|
||||||
|
endpoint string
|
||||||
|
count int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
services: []*registry.Service{
|
||||||
|
®istry.Service{
|
||||||
|
Name: "test",
|
||||||
|
Version: "1.0.0",
|
||||||
|
Endpoints: []*registry.Endpoint{
|
||||||
|
®istry.Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
®istry.Service{
|
||||||
|
Name: "test",
|
||||||
|
Version: "1.1.0",
|
||||||
|
Endpoints: []*registry.Endpoint{
|
||||||
|
®istry.Endpoint{
|
||||||
|
Name: "Baz.Bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
endpoint: "Foo.Bar",
|
||||||
|
count: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
services: []*registry.Service{
|
||||||
|
®istry.Service{
|
||||||
|
Name: "test",
|
||||||
|
Version: "1.0.0",
|
||||||
|
Endpoints: []*registry.Endpoint{
|
||||||
|
®istry.Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
®istry.Service{
|
||||||
|
Name: "test",
|
||||||
|
Version: "1.1.0",
|
||||||
|
Endpoints: []*registry.Endpoint{
|
||||||
|
®istry.Endpoint{
|
||||||
|
Name: "Foo.Bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
endpoint: "Bar.Baz",
|
||||||
|
count: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range testData {
|
||||||
|
filter := FilterEndpoint(data.endpoint)
|
||||||
|
services := filter(data.services)
|
||||||
|
|
||||||
|
if len(services) != data.count {
|
||||||
|
t.Fatalf("Expected %d services, got %d", data.count, len(services))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, service := range services {
|
||||||
|
var seen bool
|
||||||
|
|
||||||
|
for _, ep := range service.Endpoints {
|
||||||
|
if ep.Name == data.endpoint {
|
||||||
|
seen = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if seen == false && data.count > 0 {
|
||||||
|
t.Fatalf("Expected %d services but seen is %t; result %+v", data.count, seen, services)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFilterLabel(t *testing.T) {
|
func TestFilterLabel(t *testing.T) {
|
||||||
testData := []struct {
|
testData := []struct {
|
||||||
services []*registry.Service
|
services []*registry.Service
|
||||||
|
Loading…
x
Reference in New Issue
Block a user