2019-07-26 00:19:05 +02:00
|
|
|
package router
|
2019-06-18 19:33:05 +02:00
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// QueryOption sets routing table query options
|
2019-06-13 13:09:49 +02:00
|
|
|
type QueryOption func(*QueryOptions)
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// QueryOptions are routing table query options
|
2019-06-07 00:29:24 +02:00
|
|
|
type QueryOptions struct {
|
2019-07-09 16:45:42 +02:00
|
|
|
// Service is destination service name
|
|
|
|
Service string
|
|
|
|
// Gateway is route gateway
|
|
|
|
Gateway string
|
2019-06-19 22:22:14 +02:00
|
|
|
// Network is network address
|
2019-06-12 23:30:42 +02:00
|
|
|
Network string
|
2019-08-28 00:08:35 +02:00
|
|
|
// Router is router id
|
|
|
|
Router string
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 00:08:35 +02:00
|
|
|
// QueryService sets service to query
|
2019-07-09 16:45:42 +02:00
|
|
|
func QueryService(s string) QueryOption {
|
2019-06-07 00:29:24 +02:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 16:45:42 +02:00
|
|
|
o.Service = s
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 00:08:35 +02:00
|
|
|
// QueryGateway sets gateway address to query
|
2019-07-09 16:45:42 +02:00
|
|
|
func QueryGateway(g string) QueryOption {
|
2019-06-07 00:29:24 +02:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 16:45:42 +02:00
|
|
|
o.Gateway = g
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 00:08:35 +02:00
|
|
|
// QueryNetwork sets network name to query
|
2019-07-09 16:45:42 +02:00
|
|
|
func QueryNetwork(n string) QueryOption {
|
2019-06-17 00:09:59 +02:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 16:45:42 +02:00
|
|
|
o.Network = n
|
2019-06-19 22:22:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 00:08:35 +02:00
|
|
|
// QueryRouter sets router id to query
|
|
|
|
func QueryRouter(r string) QueryOption {
|
|
|
|
return func(o *QueryOptions) {
|
|
|
|
o.Router = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// Query is routing table query
|
2019-06-07 00:29:24 +02:00
|
|
|
type Query interface {
|
|
|
|
// Options returns query options
|
|
|
|
Options() QueryOptions
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// query is a basic implementation of Query
|
2019-06-07 00:29:24 +02:00
|
|
|
type query struct {
|
|
|
|
opts QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQuery creates new query and returns it
|
|
|
|
func NewQuery(opts ...QueryOption) Query {
|
2019-06-12 23:30:42 +02:00
|
|
|
// default options
|
|
|
|
qopts := QueryOptions{
|
2019-07-09 16:45:42 +02:00
|
|
|
Service: "*",
|
|
|
|
Gateway: "*",
|
|
|
|
Network: "*",
|
2019-08-28 00:08:35 +02:00
|
|
|
Router: "*",
|
2019-06-12 23:30:42 +02:00
|
|
|
}
|
2019-06-07 00:29:24 +02:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&qopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &query{
|
|
|
|
opts: qopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns query options
|
|
|
|
func (q *query) Options() QueryOptions {
|
|
|
|
return q.opts
|
|
|
|
}
|