Files
warewulf/internal/pkg/api/node/edit.go
Christian Goll 9ba1aa8d63 api changes to modify unexported fields
changes can now not be done directly but must
go to SetNode or SetProfile. Although its also
now possible to access the field direclty with
GetNodePtr

Signed-off-by: Christian Goll <cgoll@suse.com>
2024-10-17 15:30:54 -04:00

76 lines
1.8 KiB
Go

package apinode
import (
"os"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
"gopkg.in/yaml.v3"
)
/*
Returns the nodes as a yaml string
*/
func FindAllNodeConfs() *wwapiv1.NodeYaml {
nodeDB, err := node.New()
if err != nil {
wwlog.Error("Could not open nodeDB: %s\n", err)
os.Exit(1)
}
nodeMap, _ := nodeDB.FindAllNodes()
// ignore err as nodeDB should always be correct
buffer, _ := yaml.Marshal(nodeMap)
retVal := wwapiv1.NodeYaml{
NodeConfMapYaml: string(buffer),
Hash: nodeDB.StringHash(),
}
return &retVal
}
/*
Returns filtered list of nodes
*/
func FilteredNodes(nodeList *wwapiv1.NodeList) *wwapiv1.NodeYaml {
nodeDB, err := node.New()
if err != nil {
wwlog.Error("Could not open nodeDB: %s\n", err)
os.Exit(1)
}
nodeMap, _ := nodeDB.FindAllNodes()
nodeMap = node.FilterByName(nodeMap, nodeList.Output)
buffer, _ := yaml.Marshal(nodeMap)
retVal := wwapiv1.NodeYaml{
NodeConfMapYaml: string(buffer),
Hash: nodeDB.StringHash(),
}
return &retVal
}
/*
Add nodes from yaml
*/
func NodeAddFromYaml(nodeList *wwapiv1.NodeYaml) (err error) {
nodeDB, err := node.New()
if err != nil {
return errors.Wrap(err, "Could not open NodeDB: %s\n")
}
nodeMap := make(map[string]*node.NodeConf)
err = yaml.Unmarshal([]byte(nodeList.NodeConfMapYaml), nodeMap)
if err != nil {
return errors.Wrap(err, "Could not unmarshal Yaml: %s\n")
}
for nodeName, node := range nodeMap {
err = nodeDB.SetNode(nodeName, *node)
if err != nil {
return errors.Wrap(err, "couldn't set node")
}
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
}
return nil
}