refactor: more code cleanup and simplification

This commit is contained in:
David Allen 2024-11-21 14:14:44 -07:00
parent 32065dc163
commit a7b8fb0de5
Signed by: towk
GPG key ID: 793B2924A49B3A3F
11 changed files with 268 additions and 398 deletions

View file

@ -3,9 +3,9 @@ package generator
import (
"fmt"
"maps"
"strings"
configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/client"
"github.com/OpenCHAMI/configurator/pkg/config"
"github.com/OpenCHAMI/configurator/pkg/util"
)
@ -23,22 +23,15 @@ func (g *Warewulf) GetDescription() string {
return "Configurator generator plugin for 'warewulf' config files."
}
func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (FileMap, error) {
func (g *Warewulf) Generate(config *config.Config, params Params) (FileMap, error) {
var (
params = GetParams(opts...)
client = GetClient(params)
targetKey = params["target"].(string)
target = config.Targets[targetKey]
outputs = make(FileMap, len(target.FilePaths)+len(target.TemplatePaths))
smdClient = client.NewSmdClient(params.ClientOpts...)
outputs = make(FileMap, len(params.Templates))
nodeEntries = ""
)
// check if our client is included and is valid
if client == nil {
return nil, fmt.Errorf("invalid client (client is nil)")
}
// if we have a client, try making the request for the ethernet interfaces
eths, err := client.FetchEthernetInterfaces(opts...)
eths, err := smdClient.FetchEthernetInterfaces(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %v", err)
}
@ -51,15 +44,8 @@ func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (F
return nil, fmt.Errorf("no ethernet interfaces found")
}
// print message if verbose param found
if verbose, ok := params["verbose"].(bool); ok {
if verbose {
fmt.Printf("template: \n%s\n ethernet interfaces found: %v\n", strings.Join(target.TemplatePaths, "\n\t"), len(eths))
}
}
// fetch redfish endpoints and handle errors
eps, err := client.FetchRedfishEndpoints(opts...)
eps, err := smdClient.FetchRedfishEndpoints(params.Verbose)
if err != nil {
return nil, fmt.Errorf("failed to fetch redfish endpoints: %v", err)
}
@ -67,31 +53,21 @@ func (g *Warewulf) Generate(config *configurator.Config, opts ...util.Option) (F
return nil, fmt.Errorf("no redfish endpoints found")
}
// format output for template substitution
nodeEntries := ""
// load files and templates and copy to outputs
files, err := LoadFiles(target.FilePaths...)
if err != nil {
return nil, fmt.Errorf("failed to load files: %v", err)
}
templates, err := ApplyTemplateFromFiles(Mappings{
templates, err := ApplyTemplates(Mappings{
"node_entries": nodeEntries,
}, target.TemplatePaths...)
}, params.Templates)
if err != nil {
return nil, fmt.Errorf("failed to load templates: %v", err)
}
maps.Copy(outputs, files)
maps.Copy(outputs, params.Files)
maps.Copy(outputs, templates)
// print message if verbose param is found
if verbose, ok := params["verbose"].(bool); ok {
if verbose {
fmt.Printf("templates and files loaded: \n")
for path, _ := range outputs {
fmt.Printf("\t%s", path)
}
if params.Verbose {
fmt.Printf("templates and files loaded: \n")
for path, _ := range outputs {
fmt.Printf("\t%s", path)
}
}