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