diff --git a/internal/pkg/service/templates/api/service/service.go b/internal/pkg/service/templates/api/service/service.go index 8d0a0ce58a..cfa00a6c45 100644 --- a/internal/pkg/service/templates/api/service/service.go +++ b/internal/pkg/service/templates/api/service/service.go @@ -3,6 +3,10 @@ package service import ( "context" "fmt" + "github.com/keboola/keboola-as-code/internal/pkg/encoding/jsonnet" + "github.com/keboola/keboola-as-code/internal/pkg/encoding/jsonnet/fsimporter" + "github.com/keboola/keboola-as-code/internal/pkg/filesystem/aferofs/mountfs" + "github.com/keboola/keboola-as-code/internal/pkg/template/jsonnet/function" "net/http" "path" "strconv" @@ -257,6 +261,23 @@ func (s *service) UseTemplateVersion(ctx context.Context, d dependencies.Project } defer unlockFn(ctx) + s.mapper + mountPoint := mountfs.NewMountPoint(repository.CommonDirectoryMountPoint, commonDir) + templateDir, err := aferofs.NewMountFs(templateDir, []mountfs.MountPoint{mountPoint}) + if err != nil { + return nil, err + } + srcDir, err := d.SubDirFs(SrcDirectory) + if err != nil { + return nil, err + } + srcDir, err = aferofs.NewMountFs(srcDir, []mountfs.MountPoint{mountPoint}) + if err != nil { + return nil, err + } + jsonnetCtx := jsonnet.NewContext().WithCtx(ctx).WithImporter(fsimporter.New(objectsRoot)) + + jsonnetCtx.NativeFunctionWithAlias(function.SnowflakeWriterComponentID(d.Components())) // Note: // A very strange code follows. // Since I did not manage to complete the refactoring - separation of remote and local state. diff --git a/internal/pkg/service/templates/dependencies/api.go b/internal/pkg/service/templates/dependencies/api.go index f8952e793f..3cd2ff81d9 100644 --- a/internal/pkg/service/templates/dependencies/api.go +++ b/internal/pkg/service/templates/dependencies/api.go @@ -27,6 +27,7 @@ const ( // apiScope implements APIScope interface. type apiScope struct { dependencies.BaseScope + dependencies.ProjectScope dependencies.PublicScope dependencies.EtcdClientScope dependencies.DistributionScope @@ -41,6 +42,7 @@ type apiScope struct { type parentScopes struct { dependencies.BaseScope + dependencies.ProjectScope dependencies.PublicScope dependencies.EtcdClientScope dependencies.DistributionScope @@ -130,6 +132,8 @@ func newAPIScope(ctx context.Context, p *parentScopes, cfg config.Config) (v *ap d.BaseScope = p.BaseScope + d.ProjectScope = p.ProjectScope + d.PublicScope = p.PublicScope d.EtcdClientScope = p.EtcdClientScope diff --git a/internal/pkg/service/templates/dependencies/dependencies.go b/internal/pkg/service/templates/dependencies/dependencies.go index 8088dac182..cd695195cb 100644 --- a/internal/pkg/service/templates/dependencies/dependencies.go +++ b/internal/pkg/service/templates/dependencies/dependencies.go @@ -44,6 +44,7 @@ type ctxKey string const ( PublicRequestScopeCtxKey = ctxKey("PublicRequestScope") ProjectRequestScopeCtxKey = ctxKey("ProjectRequestScope") + APIRequestScopeCtxKey = ctxKey("APIRequestScope") ) // APIScope interface provides dependencies for Templates API server. diff --git a/internal/pkg/template/context/load/context.go b/internal/pkg/template/context/load/context.go index 53bf563773..05030629f5 100644 --- a/internal/pkg/template/context/load/context.go +++ b/internal/pkg/template/context/load/context.go @@ -3,6 +3,7 @@ package load import ( "context" + "fmt" "github.com/keboola/keboola-as-code/internal/pkg/encoding/jsonnet" "github.com/keboola/keboola-as-code/internal/pkg/encoding/jsonnet/fsimporter" @@ -29,6 +30,7 @@ func NewContext(ctx context.Context, objectsRoot filesystem.Fs, components *mode projectBackends: projectBackends, } + fmt.Println("AAA: ", projectBackends) // Register Jsonnet functions c.registerJsonnetFunctions() @@ -41,6 +43,6 @@ func (c *Context) JsonnetContext() *jsonnet.Context { func (c *Context) registerJsonnetFunctions() { c.jsonnetCtx.NativeFunctionWithAlias(function.ComponentIsAvailable(c.components)) - c.jsonnetCtx.NativeFunctionWithAlias(function.SnowflakeWriterComponentID(c.components)) + //c.jsonnetCtx.NativeFunctionWithAlias(function.SnowflakeWriterComponentID(c.components)) c.jsonnetCtx.NativeFunctionWithAlias(function.HasProjectBackend(c.projectBackends)) } diff --git a/internal/pkg/template/repository/manager/manager.go b/internal/pkg/template/repository/manager/manager.go index 8cfb998937..00146fad90 100644 --- a/internal/pkg/template/repository/manager/manager.go +++ b/internal/pkg/template/repository/manager/manager.go @@ -17,6 +17,7 @@ package manager import ( "context" + "fmt" "sort" "sync" "time" @@ -59,6 +60,7 @@ type dependencies interface { Telemetry() telemetry.Telemetry Process() *servicectx.Process Components() *model.ComponentsMap + ProjectBackends() []string } func New(ctx context.Context, d dependencies, defaultRepositories []model.TemplateRepository) (*Manager, error) { @@ -72,7 +74,7 @@ func New(ctx context.Context, d dependencies, defaultRepositories []model.Templa repositoriesInit: &singleflight.Group{}, repositoriesLock: &sync.RWMutex{}, } - + fmt.Println("BB: ", d.ProjectBackends()) // Free all repositories on server shutdown d.Process().OnShutdown(func(ctx context.Context) { m.Free(ctx) diff --git a/internal/pkg/template/repository/manager/repository.go b/internal/pkg/template/repository/manager/repository.go index b50eec7002..c987a106b4 100644 --- a/internal/pkg/template/repository/manager/repository.go +++ b/internal/pkg/template/repository/manager/repository.go @@ -108,7 +108,7 @@ func (r *CachedRepository) Template(ctx context.Context, reference model.Templat // Load template r.d.Logger().Infof(ctx, r.repo.Fs().BasePath()) - tmpl, err := loadTemplateOp.Run(ctx, r.d, r.repo, reference, template.Option{}) + tmpl, err := loadTemplateOp.Run(ctx, r.d, r.repo, reference, template.Option{ProjectBackend: r.d.ProjectBackends()}) if err != nil { return nil, errors.Errorf(`cannot load template "%s": %w`, reference.FullName(), err) }