diff --git a/command/volume_register_host.go b/command/volume_register_host.go index b6cb213ca..f0f35a78a 100644 --- a/command/volume_register_host.go +++ b/command/volume_register_host.go @@ -16,6 +16,10 @@ func (c *VolumeRegisterCommand) hostVolumeRegister(client *api.Client, ast *ast. 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 + } req := &api.HostVolumeRegisterRequest{ Volume: vol, diff --git a/nomad/host_volume_endpoint.go b/nomad/host_volume_endpoint.go index e33b0e8a4..0c8f2ac4e 100644 --- a/nomad/host_volume_endpoint.go +++ b/nomad/host_volume_endpoint.go @@ -4,6 +4,7 @@ package nomad import ( + "errors" "fmt" "net/http" "regexp" @@ -316,6 +317,10 @@ func (v *HostVolume) Register(args *structs.HostVolumeRegisterRequest, reply *st return err } + if vol.NodeID == "" { + return errors.New("cannot register volume: node ID is required") + } + now := time.Now() vol, err = v.validateVolumeUpdate(vol, snap, now) if err != nil { diff --git a/nomad/host_volume_endpoint_test.go b/nomad/host_volume_endpoint_test.go index e523ae29a..544390aec 100644 --- a/nomad/host_volume_endpoint_test.go +++ b/nomad/host_volume_endpoint_test.go @@ -127,6 +127,10 @@ func TestHostVolumeEndpoint_CreateRegisterGetDelete(t *testing.T) { must.EqError(t, err, fmt.Sprintf( `validating volume "example" against state failed: node %q does not exist`, invalidNode.ID)) + + req.Volume.NodeID = "" + err = msgpackrpc.CallWithCodec(codec, "HostVolume.Register", req, &resp) + must.EqError(t, err, "cannot register volume: node ID is required") }) var expectIndex uint64 @@ -211,7 +215,7 @@ func TestHostVolumeEndpoint_CreateRegisterGetDelete(t *testing.T) { t.Run("invalid updates", func(t *testing.T) { invalidVol1 := vol1.Copy() - invalidVol2 := &structs.HostVolume{} + invalidVol2 := &structs.HostVolume{NodeID: uuid.Generate()} createReq := &structs.HostVolumeCreateRequest{ Volume: invalidVol2,