traefik/testhelpers/config.go

155 lines
4.1 KiB
Go
Raw Normal View History

2018-06-05 10:32:03 +00:00
package testhelpers
import (
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/config"
2018-06-05 10:32:03 +00:00
)
// BuildConfiguration is a helper to create a configuration.
func BuildConfiguration(dynamicConfigBuilders ...func(*config.HTTPConfiguration)) *config.HTTPConfiguration {
conf := &config.HTTPConfiguration{}
2018-06-05 10:32:03 +00:00
for _, build := range dynamicConfigBuilders {
2018-11-14 09:18:03 +00:00
build(conf)
2018-06-05 10:32:03 +00:00
}
2018-11-14 09:18:03 +00:00
return conf
2018-06-05 10:32:03 +00:00
}
2018-11-14 09:18:03 +00:00
// WithRouters is a helper to create a configuration.
func WithRouters(opts ...func(*config.Router) string) func(*config.HTTPConfiguration) {
return func(c *config.HTTPConfiguration) {
2018-11-14 09:18:03 +00:00
c.Routers = make(map[string]*config.Router)
2018-06-05 10:32:03 +00:00
for _, opt := range opts {
2018-11-14 09:18:03 +00:00
b := &config.Router{}
2018-06-05 10:32:03 +00:00
name := opt(b)
2018-11-14 09:18:03 +00:00
c.Routers[name] = b
2018-06-05 10:32:03 +00:00
}
}
}
2018-11-14 09:18:03 +00:00
// WithRouter is a helper to create a configuration.
func WithRouter(routerName string, opts ...func(*config.Router)) func(*config.Router) string {
return func(r *config.Router) string {
2018-06-05 10:32:03 +00:00
for _, opt := range opts {
2018-11-14 09:18:03 +00:00
opt(r)
2018-06-05 10:32:03 +00:00
}
2018-11-14 09:18:03 +00:00
return routerName
2018-06-05 10:32:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
// WithRouterMiddlewares is a helper to create a configuration.
func WithRouterMiddlewares(middlewaresName ...string) func(*config.Router) {
return func(r *config.Router) {
r.Middlewares = middlewaresName
2018-06-05 10:32:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
// WithServiceName is a helper to create a configuration.
func WithServiceName(serviceName string) func(*config.Router) {
return func(r *config.Router) {
r.Service = serviceName
2018-06-05 10:32:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
// WithLoadBalancerServices is a helper to create a configuration.
func WithLoadBalancerServices(opts ...func(service *config.LoadBalancerService) string) func(*config.HTTPConfiguration) {
return func(c *config.HTTPConfiguration) {
2018-11-14 09:18:03 +00:00
c.Services = make(map[string]*config.Service)
for _, opt := range opts {
b := &config.LoadBalancerService{}
name := opt(b)
c.Services[name] = &config.Service{
LoadBalancer: b,
}
2018-06-05 10:32:03 +00:00
}
}
}
2018-11-14 09:18:03 +00:00
// WithService is a helper to create a configuration.
func WithService(name string, opts ...func(*config.LoadBalancerService)) func(*config.LoadBalancerService) string {
return func(r *config.LoadBalancerService) string {
2018-06-05 10:32:03 +00:00
for _, opt := range opts {
2018-11-14 09:18:03 +00:00
opt(r)
2018-06-05 10:32:03 +00:00
}
2018-11-14 09:18:03 +00:00
return name
2018-06-05 10:32:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
// WithMiddlewares is a helper to create a configuration.
func WithMiddlewares(opts ...func(*config.Middleware) string) func(*config.HTTPConfiguration) {
return func(c *config.HTTPConfiguration) {
2018-11-14 09:18:03 +00:00
c.Middlewares = make(map[string]*config.Middleware)
2018-06-05 10:32:03 +00:00
for _, opt := range opts {
2018-11-14 09:18:03 +00:00
b := &config.Middleware{}
name := opt(b)
c.Middlewares[name] = b
2018-06-05 10:32:03 +00:00
}
2018-11-14 09:18:03 +00:00
}
}
2018-06-06 13:20:03 +00:00
2018-11-14 09:18:03 +00:00
// WithMiddleware is a helper to create a configuration.
func WithMiddleware(name string, opts ...func(*config.Middleware)) func(*config.Middleware) string {
return func(r *config.Middleware) string {
for _, opt := range opts {
opt(r)
2018-06-06 13:20:03 +00:00
}
2018-11-14 09:18:03 +00:00
return name
2018-06-05 10:32:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
// WithBasicAuth is a helper to create a configuration.
func WithBasicAuth(auth *config.BasicAuth) func(*config.Middleware) {
return func(r *config.Middleware) {
r.BasicAuth = auth
2018-06-06 13:20:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
// WithEntryPoints is a helper to create a configuration.
func WithEntryPoints(eps ...string) func(*config.Router) {
return func(f *config.Router) {
2018-06-05 10:32:03 +00:00
f.EntryPoints = eps
}
}
2018-11-14 09:18:03 +00:00
// WithRule is a helper to create a configuration.
func WithRule(rule string) func(*config.Router) {
return func(f *config.Router) {
f.Rule = rule
}
}
// WithServers is a helper to create a configuration.
func WithServers(opts ...func(*config.Server)) func(*config.LoadBalancerService) {
return func(b *config.LoadBalancerService) {
2018-06-05 10:32:03 +00:00
for _, opt := range opts {
2018-11-14 09:18:03 +00:00
server := config.Server{Weight: 1}
opt(&server)
b.Servers = append(b.Servers, server)
2018-06-05 10:32:03 +00:00
}
}
}
2018-11-14 09:18:03 +00:00
// WithServer is a helper to create a configuration.
func WithServer(url string, opts ...func(*config.Server)) func(*config.Server) {
return func(s *config.Server) {
for _, opt := range opts {
opt(s)
}
s.URL = url
2018-06-05 10:32:03 +00:00
}
}
2018-11-14 09:18:03 +00:00
// WithLBMethod is a helper to create a configuration.
func WithLBMethod(method string) func(*config.LoadBalancerService) {
return func(b *config.LoadBalancerService) {
b.Method = method
}
}
2018-11-14 09:18:03 +00:00
// WithStickiness is a helper to create a configuration.
func WithStickiness(cookieName string) func(*config.LoadBalancerService) {
return func(b *config.LoadBalancerService) {
b.Stickiness = &config.Stickiness{
CookieName: cookieName,
2018-06-05 10:32:03 +00:00
}
}
}