From b6fb969ab9338dbeab1c79e54f5c70b1aeba953b Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Fri, 26 Jul 2019 12:43:17 +0100 Subject: [PATCH] Add List and Lookup implementation. Default error for not implement. --- network/router/proto/router.pb.go | 97 ++++++++++++++++++------------- network/router/proto/router.proto | 4 ++ network/router/service/service.go | 60 +++++++++++++++++-- 3 files changed, 116 insertions(+), 45 deletions(-) diff --git a/network/router/proto/router.pb.go b/network/router/proto/router.pb.go index ef9fc698..ae0b9a5c 100644 --- a/network/router/proto/router.pb.go +++ b/network/router/proto/router.pb.go @@ -5,8 +5,9 @@ package go_micro_router import ( fmt "fmt" - proto "github.com/golang/protobuf/proto" math "math" + + proto "github.com/golang/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. @@ -558,7 +559,11 @@ func (m *Event) GetRoute() *Route { // Query is passed in a LookupRequest type Query struct { // service to lookup - Service string `protobuf:"bytes,1,opt,name=service,proto3" json:"service,omitempty"` + Service string `protobuf:"bytes,1,opt,name=service,proto3" json:"service,omitempty"` + // gateway to lookup + Gateway string `protobuf:"bytes,2,opt,name=gateway,proto3" json:"gateway,omitempty"` + // network to lookup + Network string `protobuf:"bytes,3,opt,name=network,proto3" json:"network,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -596,6 +601,20 @@ func (m *Query) GetService() string { return "" } +func (m *Query) GetGateway() string { + if m != nil { + return m.Gateway + } + return "" +} + +func (m *Query) GetNetwork() string { + if m != nil { + return m.Network + } + return "" +} + // Route is a service route type Route struct { // service for the route @@ -704,42 +723,40 @@ func init() { func init() { proto.RegisterFile("router.proto", fileDescriptor_367072455c71aedc) } var fileDescriptor_367072455c71aedc = []byte{ - // 584 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x54, 0xc1, 0x6e, 0xd3, 0x40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 0x75, 0x37, 0x3b, 0xea, 0x65, 0x29, 0x5e, 0xc0, 0x40, - 0x2e, 0x4b, 0xf2, 0x7a, 0x23, 0x76, 0xea, 0x8c, 0x9f, 0x6d, 0x7d, 0x4c, 0xb7, 0x7d, 0x59, 0x96, - 0x14, 0x29, 0x20, 0x1e, 0x83, 0x2d, 0xb3, 0x19, 0x09, 0x19, 0xcf, 0x4a, 0xaf, 0x3f, 0x62, 0xa7, - 0xfd, 0x68, 0x9d, 0x40, 0x17, 0xfa, 0x52, 0x4e, 0xbd, 0x81, 0xca, 0x57, 0xc7, 0x8a, 0x0f, 0x2d, - 0x28, 0x97, 0xc2, 0x33, 0x3b, 0xf8, 0x5c, 0x57, 0xe5, 0xa8, 0x46, 0x05, 0x4f, 0xe0, 0xf0, 0x13, - 0x2f, 0x12, 0x12, 0xa2, 0x59, 0x49, 0xe0, 0x82, 0x73, 0xc5, 0x29, 0x96, 0xb4, 0x99, 0xf9, 0x40, - 0x53, 0xfa, 0x37, 0xf3, 0xb5, 0x4c, 0x37, 0x31, 0x3f, 0x19, 0x98, 0x6a, 0x34, 0x86, 0x35, 0x47, - 0xa6, 0x38, 0xfa, 0xbb, 0x2f, 0xd0, 0x45, 0xb1, 0xd7, 0xa6, 0x78, 0x0e, 0xa6, 0xea, 0x53, 0xe4, - 0xbb, 0xf5, 0xd1, 0xa0, 0xe0, 0x04, 0x4c, 0x25, 0x38, 0x7a, 0xb0, 0x27, 0x88, 0x2f, 0xb2, 0x84, - 0xea, 0xed, 0x37, 0x61, 0xf0, 0x8b, 0x81, 0xa9, 0x7a, 0xba, 0x31, 0x55, 0x25, 0x4e, 0x53, 0x4e, - 0x42, 0xa8, 0x0b, 0xd9, 0x51, 0x13, 0x56, 0x95, 0xbb, 0x58, 0xd2, 0x43, 0xbc, 0x54, 0x17, 0xb2, - 0xa3, 0x26, 0xac, 0x2a, 0x39, 0xc9, 0x87, 0x82, 0x4f, 0x94, 0x1e, 0x76, 0xd4, 0x84, 0x88, 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, 0xbb, 0xa5, 0xfe, 0xf3, 0x5f, 0xff, 0x0d, 0x00, 0x00, 0xff, 0xff, - 0xde, 0x0b, 0x1c, 0x4a, 0x03, 0x06, 0x00, 0x00, + // 555 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x54, 0xdd, 0x6e, 0x94, 0x40, + 0x14, 0x06, 0x76, 0xa1, 0xe1, 0x74, 0x97, 0xd2, 0x73, 0x61, 0x08, 0x69, 0xea, 0x66, 0x8c, 0xa6, + 0x69, 0xe3, 0xd8, 0xac, 0x4f, 0xd0, 0xa8, 0x77, 0xbd, 0x50, 0x62, 0xe3, 0x35, 0xc2, 0x89, 0x92, + 0xee, 0x02, 0x9d, 0x99, 0xdd, 0x66, 0x1f, 0xc1, 0x67, 0xf0, 0x75, 0x7c, 0x30, 0xc3, 0xcc, 0xd0, + 0x65, 0x49, 0x6a, 0xbc, 0x9b, 0xef, 0x9b, 0x8f, 0x73, 0xe6, 0xfc, 0x7c, 0xc0, 0x4c, 0x34, 0x1b, + 0x45, 0x82, 0xb7, 0xa2, 0x51, 0x0d, 0x9b, 0xc3, 0xf1, 0x6d, 0x25, 0x55, 0x46, 0x0f, 0x1b, 0x92, + 0x8a, 0x71, 0x98, 0x19, 0x28, 0xdb, 0xa6, 0x96, 0x84, 0xe7, 0x10, 0x68, 0xb9, 0x4c, 0xdc, 0xc5, + 0xe4, 0xe2, 0x78, 0x19, 0xf0, 0xac, 0x83, 0x99, 0x65, 0xd9, 0x5b, 0x98, 0xdf, 0x36, 0xcd, 0xfd, + 0xa6, 0xb5, 0x01, 0xf0, 0x0c, 0xfc, 0x87, 0x0d, 0x89, 0x5d, 0xe2, 0x2e, 0x5c, 0xad, 0xff, 0xd2, + 0xa1, 0xcc, 0x90, 0xec, 0x1a, 0xa2, 0x5e, 0xfe, 0x9f, 0x09, 0x22, 0x98, 0x7d, 0xcb, 0x55, 0xf1, +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x0b, 0xaf, + 0x45, 0xa6, 0x5b, 0x03, 0xd1, 0xc8, 0x03, 0xce, 0xf7, 0x40, 0xff, 0xa5, 0xde, 0xff, 0x0d, 0x00, + 0x00, 0xff, 0xff, 0x20, 0xdd, 0x0c, 0xbd, 0xb5, 0x04, 0x00, 0x00, } diff --git a/network/router/proto/router.proto b/network/router/proto/router.proto index 77bfb58b..921e3e7e 100644 --- a/network/router/proto/router.proto +++ b/network/router/proto/router.proto @@ -91,6 +91,10 @@ message Event { message Query { // service to lookup string service = 1; + // gateway to lookup + string gateway = 2; + // network to lookup + string network = 3; } // Route is a service route diff --git a/network/router/service/service.go b/network/router/service/service.go index ed2d0bf3..cc79d5ce 100644 --- a/network/router/service/service.go +++ b/network/router/service/service.go @@ -1,6 +1,8 @@ package service import ( + "context" + "errors" "sync" "github.com/micro/go-micro/client" @@ -8,6 +10,11 @@ import ( pb "github.com/micro/go-micro/network/router/proto" ) +var ( + // ErrNotImplemented means the functionality has not been implemented + ErrNotImplemented = errors.New("not implemented") +) + type svc struct { router pb.RouterService opts router.Options @@ -63,27 +70,70 @@ func (s *svc) Process(a *router.Advert) error { // Create new route in the routing table func (s *svc) Create(r router.Route) error { - return nil + return ErrNotImplemented } // Delete deletes existing route from the routing table func (s *svc) Delete(r router.Route) error { - return nil + return ErrNotImplemented } // Update updates route in the routing table func (s *svc) Update(r router.Route) error { - return nil + return ErrNotImplemented } // List returns the list of all routes in the table func (s *svc) List() ([]router.Route, error) { - return nil, nil + resp, err := s.router.List(context.Background(), &pb.ListRequest{}) + 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, + Metric: int(route.Metric), + } + } + + return routes, nil } // Lookup looks up routes in the routing table and returns them func (s *svc) Lookup(q router.Query) ([]router.Route, error) { - return nil, nil + // call the router + resp, err := s.router.Lookup(context.Background(), &pb.LookupRequest{ + Query: &pb.Query{ + Service: q.Options().Service, + Gateway: q.Options().Gateway, + Network: q.Options().Network, + }, + }) + + // 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, + Metric: int(route.Metric), + } + } + + return routes, nil } // Watch returns a watcher which allows to track updates to the routing table