Restore idempotency of PUT /api/nodes/{id}

HTTP PUT is supposed to be idempotent, so PUT for an existing node or
profile should replace the node or profile, not throw an error.

Generating an error on duplicate can now be requested using the
If-None-Match header with a `*` value.

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-07-12 11:43:59 -06:00
parent a0752a5bd0
commit 949dc9d46b
5 changed files with 147 additions and 9 deletions

View File

@@ -170,18 +170,20 @@ func getNodeFields() usecase.Interactor {
func addNode() usecase.Interactor {
type addNodeInput struct {
ID string `path:"id" required:"true" description:"ID of node to be added"`
Node node.Node `json:"node" required:"true" description:"Field values in JSON format for added node"`
ID string `path:"id" required:"true" description:"ID of node to be added"`
Node node.Node `json:"node" required:"true" description:"Field values in JSON format for added node"`
IfNoneMatch string `header:"If-None-Match" description:"Set to '*' to indicate that the node should only be created if it does not already exist"`
}
u := usecase.NewInteractor(func(ctx context.Context, input addNodeInput, output *node.Node) error {
wwlog.Debug("api.addNode(ID:%v, Node:%+v)", input.ID, input.Node)
// registry is the warewulf node "db" yaml file.
if registry, err := node.New(); err != nil {
return err
} else {
if _, ok := registry.Nodes[input.ID]; ok {
return status.Wrap(fmt.Errorf("node name '%s' already exists", input.ID), status.InvalidArgument)
if input.IfNoneMatch == "*" {
if _, ok := registry.Nodes[input.ID]; ok {
return status.Wrap(fmt.Errorf("node '%s' already exists", input.ID), status.InvalidArgument)
}
}
for _, profile := range input.Node.Profiles {
if _, ok := registry.NodeProfiles[profile]; !ok {