traefik/pkg/testhelpers/config.go
Fernandez Ludovic 4c5e7a238d chore: go module
2019-08-12 05:06:04 -07:00

147 lines
4 KiB
Go

package testhelpers
import (
"github.com/containous/traefik/v2/pkg/config/dynamic"
)
// BuildConfiguration is a helper to create a configuration.
func BuildConfiguration(dynamicConfigBuilders ...func(*dynamic.HTTPConfiguration)) *dynamic.HTTPConfiguration {
conf := &dynamic.HTTPConfiguration{}
for _, build := range dynamicConfigBuilders {
build(conf)
}
return conf
}
// WithRouters is a helper to create a configuration.
func WithRouters(opts ...func(*dynamic.Router) string) func(*dynamic.HTTPConfiguration) {
return func(c *dynamic.HTTPConfiguration) {
c.Routers = make(map[string]*dynamic.Router)
for _, opt := range opts {
b := &dynamic.Router{}
name := opt(b)
c.Routers[name] = b
}
}
}
// WithRouter is a helper to create a configuration.
func WithRouter(routerName string, opts ...func(*dynamic.Router)) func(*dynamic.Router) string {
return func(r *dynamic.Router) string {
for _, opt := range opts {
opt(r)
}
return routerName
}
}
// WithRouterMiddlewares is a helper to create a configuration.
func WithRouterMiddlewares(middlewaresName ...string) func(*dynamic.Router) {
return func(r *dynamic.Router) {
r.Middlewares = middlewaresName
}
}
// WithServiceName is a helper to create a configuration.
func WithServiceName(serviceName string) func(*dynamic.Router) {
return func(r *dynamic.Router) {
r.Service = serviceName
}
}
// WithLoadBalancerServices is a helper to create a configuration.
func WithLoadBalancerServices(opts ...func(service *dynamic.LoadBalancerService) string) func(*dynamic.HTTPConfiguration) {
return func(c *dynamic.HTTPConfiguration) {
c.Services = make(map[string]*dynamic.Service)
for _, opt := range opts {
b := &dynamic.LoadBalancerService{}
name := opt(b)
c.Services[name] = &dynamic.Service{
LoadBalancer: b,
}
}
}
}
// WithService is a helper to create a configuration.
func WithService(name string, opts ...func(*dynamic.LoadBalancerService)) func(*dynamic.LoadBalancerService) string {
return func(r *dynamic.LoadBalancerService) string {
for _, opt := range opts {
opt(r)
}
return name
}
}
// WithMiddlewares is a helper to create a configuration.
func WithMiddlewares(opts ...func(*dynamic.Middleware) string) func(*dynamic.HTTPConfiguration) {
return func(c *dynamic.HTTPConfiguration) {
c.Middlewares = make(map[string]*dynamic.Middleware)
for _, opt := range opts {
b := &dynamic.Middleware{}
name := opt(b)
c.Middlewares[name] = b
}
}
}
// WithMiddleware is a helper to create a configuration.
func WithMiddleware(name string, opts ...func(*dynamic.Middleware)) func(*dynamic.Middleware) string {
return func(r *dynamic.Middleware) string {
for _, opt := range opts {
opt(r)
}
return name
}
}
// WithBasicAuth is a helper to create a configuration.
func WithBasicAuth(auth *dynamic.BasicAuth) func(*dynamic.Middleware) {
return func(r *dynamic.Middleware) {
r.BasicAuth = auth
}
}
// WithEntryPoints is a helper to create a configuration.
func WithEntryPoints(eps ...string) func(*dynamic.Router) {
return func(f *dynamic.Router) {
f.EntryPoints = eps
}
}
// WithRule is a helper to create a configuration.
func WithRule(rule string) func(*dynamic.Router) {
return func(f *dynamic.Router) {
f.Rule = rule
}
}
// WithServers is a helper to create a configuration.
func WithServers(opts ...func(*dynamic.Server)) func(*dynamic.LoadBalancerService) {
return func(b *dynamic.LoadBalancerService) {
for _, opt := range opts {
server := dynamic.Server{}
opt(&server)
b.Servers = append(b.Servers, server)
}
}
}
// WithServer is a helper to create a configuration.
func WithServer(url string, opts ...func(*dynamic.Server)) func(*dynamic.Server) {
return func(s *dynamic.Server) {
for _, opt := range opts {
opt(s)
}
s.URL = url
}
}
// WithStickiness is a helper to create a configuration.
func WithStickiness(cookieName string) func(*dynamic.LoadBalancerService) {
return func(b *dynamic.LoadBalancerService) {
b.Stickiness = &dynamic.Stickiness{
CookieName: cookieName,
}
}
}