2019-07-29 20:57:40 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/client"
|
2019-08-05 19:44:33 +03:00
|
|
|
"github.com/micro/go-micro/router"
|
2019-12-10 14:46:13 +03:00
|
|
|
pb "github.com/micro/go-micro/router/service/proto"
|
2019-07-29 20:57:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type table struct {
|
|
|
|
table pb.TableService
|
|
|
|
callOpts []client.CallOption
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new route in the routing table
|
|
|
|
func (t *table) Create(r router.Route) error {
|
|
|
|
route := &pb.Route{
|
|
|
|
Service: r.Service,
|
|
|
|
Address: r.Address,
|
|
|
|
Gateway: r.Gateway,
|
|
|
|
Network: r.Network,
|
|
|
|
Link: r.Link,
|
2019-10-23 14:53:36 +03:00
|
|
|
Metric: r.Metric,
|
2019-07-29 20:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := t.table.Create(context.Background(), route, t.callOpts...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes existing route from the routing table
|
|
|
|
func (t *table) Delete(r router.Route) error {
|
|
|
|
route := &pb.Route{
|
|
|
|
Service: r.Service,
|
|
|
|
Address: r.Address,
|
|
|
|
Gateway: r.Gateway,
|
|
|
|
Network: r.Network,
|
|
|
|
Link: r.Link,
|
2019-10-23 14:53:36 +03:00
|
|
|
Metric: r.Metric,
|
2019-07-29 20:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := t.table.Delete(context.Background(), route, t.callOpts...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates route in the routing table
|
|
|
|
func (t *table) Update(r router.Route) error {
|
|
|
|
route := &pb.Route{
|
|
|
|
Service: r.Service,
|
|
|
|
Address: r.Address,
|
|
|
|
Gateway: r.Gateway,
|
|
|
|
Network: r.Network,
|
|
|
|
Link: r.Link,
|
2019-10-23 14:53:36 +03:00
|
|
|
Metric: r.Metric,
|
2019-07-29 20:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := t.table.Update(context.Background(), route, t.callOpts...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns the list of all routes in the table
|
|
|
|
func (t *table) List() ([]router.Route, error) {
|
|
|
|
resp, err := t.table.List(context.Background(), &pb.Request{}, t.callOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
routes := make([]router.Route, len(resp.Routes))
|
|
|
|
for i, route := range resp.Routes {
|
|
|
|
routes[i] = router.Route{
|
|
|
|
Service: route.Service,
|
|
|
|
Address: route.Address,
|
|
|
|
Gateway: route.Gateway,
|
|
|
|
Network: route.Network,
|
|
|
|
Link: route.Link,
|
2019-10-23 14:53:36 +03:00
|
|
|
Metric: route.Metric,
|
2019-07-29 20:57:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup looks up routes in the routing table and returns them
|
2019-10-09 19:13:52 +03:00
|
|
|
func (t *table) Query(q ...router.QueryOption) ([]router.Route, error) {
|
|
|
|
query := router.NewQuery(q...)
|
|
|
|
|
2019-07-29 20:57:40 +03:00
|
|
|
// call the router
|
|
|
|
resp, err := t.table.Query(context.Background(), &pb.QueryRequest{
|
|
|
|
Query: &pb.Query{
|
2019-10-09 19:13:52 +03:00
|
|
|
Service: query.Service,
|
|
|
|
Gateway: query.Gateway,
|
|
|
|
Network: query.Network,
|
2019-07-29 20:57:40 +03:00
|
|
|
},
|
|
|
|
}, t.callOpts...)
|
|
|
|
|
|
|
|
// errored out
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
routes := make([]router.Route, len(resp.Routes))
|
|
|
|
for i, route := range resp.Routes {
|
|
|
|
routes[i] = router.Route{
|
|
|
|
Service: route.Service,
|
|
|
|
Address: route.Address,
|
|
|
|
Gateway: route.Gateway,
|
|
|
|
Network: route.Network,
|
|
|
|
Link: route.Link,
|
2019-10-23 14:53:36 +03:00
|
|
|
Metric: route.Metric,
|
2019-07-29 20:57:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|