2018-06-05 12:32:03 +02:00
package testhelpers
import (
2020-09-16 15:46:04 +02:00
"github.com/traefik/traefik/v2/pkg/config/dynamic"
2018-06-05 12:32:03 +02:00
)
// BuildConfiguration is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func BuildConfiguration ( dynamicConfigBuilders ... func ( * dynamic . HTTPConfiguration ) ) * dynamic . HTTPConfiguration {
2020-03-05 12:46:05 +01:00
conf := & dynamic . HTTPConfiguration {
2020-09-11 15:40:03 +02:00
Models : map [ string ] * dynamic . Model { } ,
ServersTransports : map [ string ] * dynamic . ServersTransport { } ,
2020-03-05 12:46:05 +01:00
}
2018-06-05 12:32:03 +02:00
for _ , build := range dynamicConfigBuilders {
2018-11-14 10:18:03 +01:00
build ( conf )
2018-06-05 12:32:03 +02:00
}
2018-11-14 10:18:03 +01:00
return conf
2018-06-05 12:32:03 +02:00
}
2018-11-14 10:18:03 +01:00
// WithRouters is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithRouters ( opts ... func ( * dynamic . Router ) string ) func ( * dynamic . HTTPConfiguration ) {
return func ( c * dynamic . HTTPConfiguration ) {
c . Routers = make ( map [ string ] * dynamic . Router )
2018-06-05 12:32:03 +02:00
for _ , opt := range opts {
2019-07-10 09:26:04 +02:00
b := & dynamic . Router { }
2018-06-05 12:32:03 +02:00
name := opt ( b )
2018-11-14 10:18:03 +01:00
c . Routers [ name ] = b
2018-06-05 12:32:03 +02:00
}
}
}
2018-11-14 10:18:03 +01:00
// WithRouter is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithRouter ( routerName string , opts ... func ( * dynamic . Router ) ) func ( * dynamic . Router ) string {
return func ( r * dynamic . Router ) string {
2018-06-05 12:32:03 +02:00
for _ , opt := range opts {
2018-11-14 10:18:03 +01:00
opt ( r )
2018-06-05 12:32:03 +02:00
}
2018-11-14 10:18:03 +01:00
return routerName
2018-06-05 12:32:03 +02:00
}
}
2018-11-14 10:18:03 +01:00
// WithRouterMiddlewares is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithRouterMiddlewares ( middlewaresName ... string ) func ( * dynamic . Router ) {
return func ( r * dynamic . Router ) {
2018-11-14 10:18:03 +01:00
r . Middlewares = middlewaresName
2018-06-05 12:32:03 +02:00
}
}
2018-11-14 10:18:03 +01:00
// WithServiceName is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithServiceName ( serviceName string ) func ( * dynamic . Router ) {
return func ( r * dynamic . Router ) {
2018-11-14 10:18:03 +01:00
r . Service = serviceName
2018-06-05 12:32:03 +02:00
}
}
2018-11-14 10:18:03 +01:00
// WithLoadBalancerServices is a helper to create a configuration.
2019-08-26 10:30:05 +02:00
func WithLoadBalancerServices ( opts ... func ( service * dynamic . ServersLoadBalancer ) string ) func ( * dynamic . HTTPConfiguration ) {
2019-07-10 09:26:04 +02:00
return func ( c * dynamic . HTTPConfiguration ) {
c . Services = make ( map [ string ] * dynamic . Service )
2018-11-14 10:18:03 +01:00
for _ , opt := range opts {
2019-08-26 10:30:05 +02:00
b := & dynamic . ServersLoadBalancer { }
2018-11-14 10:18:03 +01:00
name := opt ( b )
2019-07-10 09:26:04 +02:00
c . Services [ name ] = & dynamic . Service {
2018-11-14 10:18:03 +01:00
LoadBalancer : b ,
}
2018-06-05 12:32:03 +02:00
}
}
}
2018-11-14 10:18:03 +01:00
// WithService is a helper to create a configuration.
2019-08-26 10:30:05 +02:00
func WithService ( name string , opts ... func ( * dynamic . ServersLoadBalancer ) ) func ( * dynamic . ServersLoadBalancer ) string {
return func ( r * dynamic . ServersLoadBalancer ) string {
2018-06-05 12:32:03 +02:00
for _ , opt := range opts {
2018-11-14 10:18:03 +01:00
opt ( r )
2018-06-05 12:32:03 +02:00
}
2018-11-14 10:18:03 +01:00
return name
2018-06-05 12:32:03 +02:00
}
}
2018-11-14 10:18:03 +01:00
// WithMiddlewares is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithMiddlewares ( opts ... func ( * dynamic . Middleware ) string ) func ( * dynamic . HTTPConfiguration ) {
return func ( c * dynamic . HTTPConfiguration ) {
c . Middlewares = make ( map [ string ] * dynamic . Middleware )
2018-06-05 12:32:03 +02:00
for _ , opt := range opts {
2019-07-10 09:26:04 +02:00
b := & dynamic . Middleware { }
2018-11-14 10:18:03 +01:00
name := opt ( b )
c . Middlewares [ name ] = b
2018-06-05 12:32:03 +02:00
}
2018-11-14 10:18:03 +01:00
}
}
2018-06-06 15:20:03 +02:00
2018-11-14 10:18:03 +01:00
// WithMiddleware is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithMiddleware ( name string , opts ... func ( * dynamic . Middleware ) ) func ( * dynamic . Middleware ) string {
return func ( r * dynamic . Middleware ) string {
2018-11-14 10:18:03 +01:00
for _ , opt := range opts {
opt ( r )
2018-06-06 15:20:03 +02:00
}
2018-11-14 10:18:03 +01:00
return name
2018-06-05 12:32:03 +02:00
}
}
2018-11-14 10:18:03 +01:00
// WithBasicAuth is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithBasicAuth ( auth * dynamic . BasicAuth ) func ( * dynamic . Middleware ) {
return func ( r * dynamic . Middleware ) {
2018-11-14 10:18:03 +01:00
r . BasicAuth = auth
2018-06-06 15:20:03 +02:00
}
}
2018-11-14 10:18:03 +01:00
// WithEntryPoints is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithEntryPoints ( eps ... string ) func ( * dynamic . Router ) {
return func ( f * dynamic . Router ) {
2018-06-05 12:32:03 +02:00
f . EntryPoints = eps
}
}
2018-11-14 10:18:03 +01:00
// WithRule is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithRule ( rule string ) func ( * dynamic . Router ) {
return func ( f * dynamic . Router ) {
2018-11-14 10:18:03 +01:00
f . Rule = rule
}
}
// WithServers is a helper to create a configuration.
2019-08-26 10:30:05 +02:00
func WithServers ( opts ... func ( * dynamic . Server ) ) func ( * dynamic . ServersLoadBalancer ) {
return func ( b * dynamic . ServersLoadBalancer ) {
2018-06-05 12:32:03 +02:00
for _ , opt := range opts {
2019-07-10 09:26:04 +02:00
server := dynamic . Server { }
2018-11-14 10:18:03 +01:00
opt ( & server )
b . Servers = append ( b . Servers , server )
2018-06-05 12:32:03 +02:00
}
}
}
2018-11-14 10:18:03 +01:00
// WithServer is a helper to create a configuration.
2019-07-10 09:26:04 +02:00
func WithServer ( url string , opts ... func ( * dynamic . Server ) ) func ( * dynamic . Server ) {
return func ( s * dynamic . Server ) {
2018-11-14 10:18:03 +01:00
for _ , opt := range opts {
opt ( s )
}
s . URL = url
2018-06-05 12:32:03 +02:00
}
}
2019-08-26 10:30:05 +02:00
// WithSticky is a helper to create a configuration.
func WithSticky ( cookieName string ) func ( * dynamic . ServersLoadBalancer ) {
return func ( b * dynamic . ServersLoadBalancer ) {
b . Sticky = & dynamic . Sticky {
Cookie : & dynamic . Cookie { Name : cookieName } ,
2018-06-05 12:32:03 +02:00
}
}
}