From c04e7f6f7437a693ae1940dd1e1fe1ae3d9da662 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Wed, 7 Oct 2020 11:14:57 -0400 Subject: [PATCH] csi: allow volume detach to take a node ID prefix (#9041) Fixes a bug where the `nomad volume detach` command would not accept a node ID prefix instead of a full node ID. The volume ID is already prefix matched server-side. --- CHANGELOG.md | 1 + command/volume_detach.go | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01bf9f07a615..aeee24203b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ BUG FIXES: * core: Fixed a bug where blocking queries would not include the query's maximum wait time when calculating whether it was safe to retry. [[GH-8921](https://github.com/hashicorp/nomad/issues/8921)] * consul: Fixed a bug to correctly validate task when using script-checks in group-level services [[GH-8952](https://github.com/hashicorp/nomad/issues/8952)] * csi: Fixed a bug where multi-writer volumes were allowed only 1 write claim. [[GH-9040](https://github.com/hashicorp/nomad/issues/9040)] + * csi: Fixed a bug where `nomad volume detach` would not accept prefixes for the node ID parameter. [[GH-9041](https://github.com/hashicorp/nomad/issues/9041)] ## 0.12.5 (September 17, 2020) diff --git a/command/volume_detach.go b/command/volume_detach.go index 1810d4d4d5a1..3ecb95139e73 100644 --- a/command/volume_detach.go +++ b/command/volume_detach.go @@ -87,7 +87,24 @@ func (c *VolumeDetachCommand) Run(args []string) int { return 1 } - err = client.CSIVolumes().Detach(volID, nodeID, nil) + nodeID = sanitizeUUIDPrefix(nodeID) + nodes, _, err := client.Nodes().PrefixList(nodeID) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error detaching volume: %s", err)) + return 1 + } + // Return error if no nodes are found + if len(nodes) == 0 { + c.Ui.Error(fmt.Sprintf("No node(s) with prefix or id %q found", nodeID)) + return 1 + } + if len(nodes) > 1 { + c.Ui.Error(fmt.Sprintf("Prefix matched multiple nodes\n\n%s", + formatNodeStubList(nodes, true))) + return 1 + } + + err = client.CSIVolumes().Detach(volID, nodes[0].ID, nil) if err != nil { c.Ui.Error(fmt.Sprintf("Error detaching volume: %s", err)) return 1