mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
volume delete: allow prefix for ID (#24997)
The `volume delete` command doesn't allow using a prefix for the volume ID for either CSI or dynamic host volumes. Use a prefix search and wildcard namespace as we do for other CLI commands. Ref: https://hashicorp.atlassian.net/browse/NET-12057
This commit is contained in:
3
.changelog/24997.txt
Normal file
3
.changelog/24997.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
```release-note:improvement
|
||||
csi: Accept ID prefixes and wildcard namespace for the volume delete command
|
||||
```
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/nomad/api"
|
||||
"github.com/hashicorp/nomad/api/contexts"
|
||||
"github.com/hashicorp/nomad/helper"
|
||||
flaghelper "github.com/hashicorp/nomad/helper/flags"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
@@ -136,7 +137,31 @@ func (c *VolumeDeleteCommand) deleteCSIVolume(client *api.Client, volID string,
|
||||
}
|
||||
}
|
||||
|
||||
err := client.CSIVolumes().DeleteOpts(&api.CSIVolumeDeleteRequest{
|
||||
// get a CSI volume that matches the given prefix or a list of all matches
|
||||
// if an exact match is not found.
|
||||
stub, possible, err := getByPrefix[api.CSIVolumeListStub]("volumes", client.CSIVolumes().List,
|
||||
func(vol *api.CSIVolumeListStub, prefix string) bool { return vol.ID == prefix },
|
||||
&api.QueryOptions{
|
||||
Prefix: volID,
|
||||
Namespace: c.namespace,
|
||||
})
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Could not find existing volume to delete: %s", err))
|
||||
return 1
|
||||
}
|
||||
if len(possible) > 0 {
|
||||
out, err := csiFormatVolumes(possible, false, "")
|
||||
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
|
||||
}
|
||||
volID = stub.ID
|
||||
c.namespace = stub.Namespace
|
||||
|
||||
err = client.CSIVolumes().DeleteOpts(&api.CSIVolumeDeleteRequest{
|
||||
ExternalVolumeID: volID,
|
||||
Secrets: secrets,
|
||||
}, nil)
|
||||
@@ -150,6 +175,26 @@ func (c *VolumeDeleteCommand) deleteCSIVolume(client *api.Client, volID string,
|
||||
}
|
||||
|
||||
func (c *VolumeDeleteCommand) deleteHostVolume(client *api.Client, volID string) int {
|
||||
|
||||
if !helper.IsUUID(volID) {
|
||||
stub, possible, err := getHostVolumeByPrefix(client, volID, c.namespace)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Could not find existing volume to delete: %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
|
||||
}
|
||||
volID = stub.ID
|
||||
c.namespace = stub.Namespace
|
||||
}
|
||||
|
||||
_, err := client.HostVolumes().Delete(&api.HostVolumeDeleteRequest{ID: volID}, nil)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error deleting volume: %s", err))
|
||||
|
||||
@@ -83,11 +83,17 @@ capability {
|
||||
must.StrContains(t, ui.ErrorWriter.String(), "no such volume")
|
||||
ui.ErrorWriter.Reset()
|
||||
|
||||
// fix the namespace
|
||||
args = []string{"-address", url, "-type", "host", "-namespace", "prod", id}
|
||||
// missing the namespace, but use a prefix
|
||||
args = []string{"-address", url, "-type", "host", id[:12]}
|
||||
code = cmd.Run(args)
|
||||
must.Eq(t, 1, code)
|
||||
must.StrContains(t, ui.ErrorWriter.String(), "no volumes with prefix")
|
||||
ui.ErrorWriter.Reset()
|
||||
|
||||
// fix the namespace, and use a prefix
|
||||
args = []string{"-address", url, "-type", "host", "-namespace", "prod", id[:12]}
|
||||
code = cmd.Run(args)
|
||||
must.Eq(t, 0, code, must.Sprintf("got error: %s", ui.ErrorWriter.String()))
|
||||
out = ui.OutputWriter.String()
|
||||
must.StrContains(t, out, fmt.Sprintf("Successfully deleted volume %q!", id))
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user