github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/tenant/tenant.go (about) 1 package tenant 2 3 import ( 4 "context" 5 "strings" 6 7 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 8 ) 9 10 type tntCtxKey int 11 type localTntIDCtxKey int 12 13 // TenantContextKey is the key under which the TenantCtx is saved in a given context.Context. 14 const TenantContextKey tntCtxKey = iota 15 16 // LocalTenantIDContextKey is the key under which the local tenant id is saved in a given context.Context. 17 const LocalTenantIDContextKey localTntIDCtxKey = iota 18 19 // TenantCtx is the structure can be saved in a request context. It is used to determine the tenant context in which the request is being executed. 20 type TenantCtx struct { 21 InternalID string 22 ExternalID string 23 } 24 25 // LoadFromContext retrieves the internal tenant ID from the provided context. It returns error if such ID cannot be found. 26 func LoadFromContext(ctx context.Context) (string, error) { 27 tenant, ok := ctx.Value(TenantContextKey).(TenantCtx) 28 29 if !ok { 30 return "", apperrors.NewCannotReadTenantError() 31 } 32 33 if tenant.InternalID == "" { 34 return "", apperrors.NewTenantRequiredError() 35 } 36 37 return tenant.InternalID, nil 38 } 39 40 // LoadTenantPairFromContext retrieves the whole tenant context from the provided request context. It returns error if such ID cannot be found. 41 func LoadTenantPairFromContext(ctx context.Context) (TenantCtx, error) { 42 tenant, ok := ctx.Value(TenantContextKey).(TenantCtx) 43 44 if !ok { 45 return TenantCtx{}, apperrors.NewCannotReadTenantError() 46 } 47 48 if tenant.InternalID == "" { 49 return TenantCtx{}, apperrors.NewTenantRequiredError() 50 } 51 52 return tenant, nil 53 } 54 55 // LoadTenantPairFromContextNoChecks retrieves the whole tenant context from the provided request context. Checks for the values of the InternalID/ExternalID are not included and should be done outside the func case by case. 56 func LoadTenantPairFromContextNoChecks(ctx context.Context) (TenantCtx, error) { 57 tenant, ok := ctx.Value(TenantContextKey).(TenantCtx) 58 59 if !ok { 60 return TenantCtx{}, apperrors.NewCannotReadTenantError() 61 } 62 63 return tenant, nil 64 } 65 66 // SaveToContext returns a child context of the provided context, including the provided tenant information. 67 // The internal tenant ID can be later retrieved from the context by calling LoadFromContext. 68 func SaveToContext(ctx context.Context, internalID, externalID string) context.Context { 69 tenantCtx := TenantCtx{InternalID: internalID, ExternalID: externalID} 70 return context.WithValue(ctx, TenantContextKey, tenantCtx) 71 } 72 73 // LoadLocalTenantIDFromContext retrieves the local tenant ID from the provided context. It returns error if such ID cannot be found. 74 func LoadLocalTenantIDFromContext(ctx context.Context) (string, error) { 75 localTenantID, ok := ctx.Value(LocalTenantIDContextKey).(string) 76 77 if !ok { 78 return "", apperrors.NewInternalError("cannot read local tenant id from context") 79 } 80 81 localTenantID = strings.TrimSpace(localTenantID) 82 if localTenantID == "" { 83 return "", apperrors.NewInternalError("local tenant id is required") 84 } 85 86 return localTenantID, nil 87 } 88 89 // SaveLocalTenantIDToContext returns a child context of the provided context, including the provided local tenant id. 90 func SaveLocalTenantIDToContext(ctx context.Context, localTenantID string) context.Context { 91 return context.WithValue(ctx, LocalTenantIDContextKey, localTenantID) 92 }