Files
nomad/command/volume_register_host.go
Tim Gross cc99e8f0a2 dynamic host volumes: add -id arg for updates of existing volumes (#24996)
If you create a volume via `volume create/register` and want to update it later,
you need to change the volume spec to add the ID that was returned. This isn't a
very nice UX, so let's add an `-id` argument that allows you to update existing
volumes that have that ID.

Ref: https://hashicorp.atlassian.net/browse/NET-12083
2025-02-03 10:26:30 -05:00

67 lines
1.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"fmt"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/nomad/api"
)
func (c *VolumeRegisterCommand) hostVolumeRegister(client *api.Client, ast *ast.File, override bool, volID string) int {
vol, err := decodeHostVolume(ast)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error decoding the volume definition: %s", err))
return 1
}
if vol.NodeID == "" {
c.Ui.Error("Node ID is required for registering")
return 1
}
if volID != "" {
ns := c.namespace
if vol.Namespace != "" {
ns = vol.Namespace
}
stub, possible, err := getHostVolumeByPrefix(client, volID, ns)
if err != nil {
c.Ui.Error(fmt.Sprintf("Could not update existing volume: %s", err))
return 1
}
if len(possible) > 0 {
out, err := formatHostVolumes(possible, formatOpts{short: true})
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting: %s", err))
return 1
}
c.Ui.Error(fmt.Sprintf("Prefix matched multiple volumes\n\n%s", out))
return 1
}
vol.ID = stub.ID
}
req := &api.HostVolumeRegisterRequest{
Volume: vol,
PolicyOverride: override,
}
resp, _, err := client.HostVolumes().Register(req, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error registering volume: %s", err))
return 1
}
vol = resp.Volume
if resp.Warnings != "" {
c.Ui.Output(
c.Colorize().Color(
fmt.Sprintf("[bold][yellow]Volume Warnings:\n%s[reset]\n", resp.Warnings)))
}
c.Ui.Output(fmt.Sprintf(
"Registered host volume %s with ID %s", vol.Name, vol.ID))
return 0
}