2020-01-27 09:40:05 +00:00
|
|
|
package provider
|
2019-01-15 13:28:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
2019-01-15 13:28:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey int
|
|
|
|
|
|
|
|
const (
|
2020-01-27 09:40:05 +00:00
|
|
|
key contextKey = iota
|
2019-01-15 13:28:04 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// AddInContext Adds the provider name in the context.
|
2020-01-27 09:40:05 +00:00
|
|
|
func AddInContext(ctx context.Context, elementName string) context.Context {
|
2019-06-19 22:40:05 +00:00
|
|
|
parts := strings.Split(elementName, "@")
|
2019-01-15 13:28:04 +00:00
|
|
|
if len(parts) == 1 {
|
|
|
|
log.FromContext(ctx).Debugf("Could not find a provider for %s.", elementName)
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-01-27 09:40:05 +00:00
|
|
|
if name, ok := ctx.Value(key).(string); ok && name == parts[1] {
|
2019-01-15 13:28:04 +00:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-01-27 09:40:05 +00:00
|
|
|
return context.WithValue(ctx, key, parts[1])
|
2019-01-15 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetQualifiedName Gets the fully qualified name.
|
|
|
|
func GetQualifiedName(ctx context.Context, elementName string) string {
|
2019-06-19 22:40:05 +00:00
|
|
|
parts := strings.Split(elementName, "@")
|
2019-01-15 13:28:04 +00:00
|
|
|
if len(parts) == 1 {
|
2020-01-27 09:40:05 +00:00
|
|
|
if providerName, ok := ctx.Value(key).(string); ok {
|
2019-01-15 13:28:04 +00:00
|
|
|
return MakeQualifiedName(providerName, parts[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elementName
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// MakeQualifiedName Creates a qualified name for an element.
|
2020-07-07 12:42:03 +00:00
|
|
|
func MakeQualifiedName(providerName, elementName string) string {
|
2019-06-21 07:54:04 +00:00
|
|
|
return elementName + "@" + providerName
|
2019-01-15 13:28:04 +00:00
|
|
|
}
|