Files
warewulf/internal/pkg/api/profile/delete.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

77 lines
1.9 KiB
Go

package apiprofile
import (
"fmt"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/warewulf/warewulf/internal/pkg/hostlist"
"github.com/warewulf/warewulf/internal/pkg/node"
"github.com/warewulf/warewulf/internal/pkg/warewulfd"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
// ProfileDelete adds profile deletion for management by Warewulf.
func ProfileDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
nodeDB, profileList, err := ProfileDeleteParameterCheck(ndp, false)
if err != nil {
return
}
if nodeDB.StringHash() != ndp.Hash && !ndp.Force {
return fmt.Errorf("got wrong hash, not modifying profile database")
}
for _, p := range profileList {
err = nodeDB.DelProfile(p)
if err != nil {
return
}
}
err = nodeDB.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist nodedb")
}
err = warewulfd.DaemonReload()
if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon")
}
return
}
// ProfileDeleteParameterCheck does error checking on ProfileDeleteParameter.
// Output to the console if console is true.
// Returns the profiles to delete.
func ProfileDeleteParameterCheck(ndp *wwapiv1.NodeDeleteParameter, console bool) (nodeDB node.NodeYaml, profileList []string, err error) {
if ndp == nil {
err = fmt.Errorf("profileDeleteParameter is nil")
return
}
nodeDB, err = node.New()
if err != nil {
wwlog.Error("failed to open node database: %s\n", err)
return
}
profileList = nodeDB.ListAllProfiles()
profileArgs := hostlist.Expand(ndp.NodeNames)
for _, r := range profileArgs {
match := false
for _, p := range profileList {
if p == r {
profileList = append(profileList, p)
match = true
}
}
if !match {
wwlog.Error("no match for profile: %s", r)
}
}
if len(profileList) == 0 {
wwlog.Warn("no profiles found\n")
}
return
}