From 88d365155f9582d8e85e47e071ed30ec962969b9 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Tue, 7 Jan 2025 12:16:08 -0700 Subject: [PATCH] generator: fixed issue with templates not being generated --- pkg/generator/generator.go | 9 +++++++-- pkg/generator/templates.go | 3 +++ pkg/server/server.go | 12 +++++++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pkg/generator/generator.go b/pkg/generator/generator.go index dfc39d7..3397e36 100644 --- a/pkg/generator/generator.go +++ b/pkg/generator/generator.go @@ -163,7 +163,7 @@ func LoadPlugins(dirpath string, opts ...Option) (map[string]Generator, error) { // be used. This function will only load the plugin on-demand and fetch resources as needed. // // This function requires that a target and plugin path be set at minimum. -func Generate(plugin string, params Params) (FileMap, error) { +func Generate(config *config.Config, plugin string, params Params) (FileMap, error) { var ( gen Generator ok bool @@ -182,7 +182,7 @@ func Generate(plugin string, params Params) (FileMap, error) { } } - return gen.Generate(nil, params) + return gen.Generate(config, params) } // Main function to generate a collection of files as a map with the path as the key and @@ -232,6 +232,11 @@ func GenerateWithTarget(config *config.Config, target string) (FileMap, error) { } } + // check if there's at least one template available + if len(targetInfo.TemplatePaths) <= 0 { + return nil, fmt.Errorf("expects at least one template to be available") + } + // prepare params to pass into generator params.Templates = map[string]Template{} for _, templatePath := range targetInfo.TemplatePaths { diff --git a/pkg/generator/templates.go b/pkg/generator/templates.go index 6d4ae5d..321076c 100644 --- a/pkg/generator/templates.go +++ b/pkg/generator/templates.go @@ -8,6 +8,7 @@ import ( "github.com/OpenCHAMI/configurator/pkg/util" "github.com/nikolalohinski/gonja/v2" "github.com/nikolalohinski/gonja/v2/exec" + "github.com/rs/zerolog/log" ) type Template struct { @@ -65,6 +66,8 @@ func ApplyTemplates(mappings Mappings, templates map[string]Template) (FileMap, outputs[path] = b.Bytes() } + log.Debug().Any("templates", templates).Any("outputs", outputs).Any("mappings", mappings).Msg("apply templates") + return outputs, nil } diff --git a/pkg/server/server.go b/pkg/server/server.go index 7da26d7..a89e04a 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -93,6 +93,7 @@ func (s *Server) Serve() error { // create client with opts to use to fetch data from SMD opts := []client.Option{ + client.WithHost(s.Config.SmdClient.Host), client.WithAccessToken(s.Config.AccessToken), client.WithCertPoolFile(s.Config.CertPath), } @@ -146,7 +147,7 @@ func (s *Server) Generate(opts ...client.Option) func(w http.ResponseWriter, r * outputs generator.FileMap err error ) - s.GeneratorParams = parseGeneratorParams(r, opts...) + s.GeneratorParams = parseGeneratorParams(r, target, opts...) if targetParam == "" { err = writeErrorResponse(w, "must specify a target") log.Error().Err(err).Msg("failed to parse generator params") @@ -156,7 +157,8 @@ func (s *Server) Generate(opts ...client.Option) func(w http.ResponseWriter, r * // try to generate with target supplied by client first if target != nil { log.Debug().Any("target", target).Msg("target for Generate()") - outputs, err = generator.Generate(target.PluginPath, s.GeneratorParams) + outputs, err = generator.Generate(s.Config, target.PluginPath, s.GeneratorParams) + log.Debug().Any("outputs map", outputs).Msgf("after generate") if err != nil { log.Error().Err(err).Msg("failed to generate file") return @@ -299,9 +301,13 @@ func writeErrorResponse(w http.ResponseWriter, format string, a ...any) error { return fmt.Errorf(errmsg) } -func parseGeneratorParams(r *http.Request, opts ...client.Option) generator.Params { +func parseGeneratorParams(r *http.Request, target *Target, opts ...client.Option) generator.Params { var params = generator.Params{ ClientOpts: opts, + Templates: make(map[string]generator.Template, len(target.Templates)), + } + for i, template := range target.Templates { + params.Templates[fmt.Sprintf("%s_%d", target.Name, i)] = template } return params }