From a46a4025192174435cbc29c07e6459fbbdefb92c Mon Sep 17 00:00:00 2001
From: Tim Gross <tgross@hashicorp.com>
Date: Thu, 3 Mar 2022 16:00:13 +0000
Subject: [PATCH] backport of commit cfac3c982c38f239f408a939be12e1ea611ebc72

---
 .changelog/12178.txt          |  3 +++
 command/agent/csi_endpoint.go | 28 ++++++++++++++++++++++++----
 2 files changed, 27 insertions(+), 4 deletions(-)
 create mode 100644 .changelog/12178.txt

diff --git a/.changelog/12178.txt b/.changelog/12178.txt
new file mode 100644
index 00000000000..0261e523707
--- /dev/null
+++ b/.changelog/12178.txt
@@ -0,0 +1,3 @@
+```release-note:bug
+csi: Fixed a bug where fields were missing from the Read Volume API response
+```
diff --git a/command/agent/csi_endpoint.go b/command/agent/csi_endpoint.go
index 0a3bd043378..4ee256463d0 100644
--- a/command/agent/csi_endpoint.go
+++ b/command/agent/csi_endpoint.go
@@ -468,10 +468,11 @@ func structsCSIVolumeToApi(vol *structs.CSIVolume) *api.CSIVolume {
 	allocCount := len(vol.ReadAllocs) + len(vol.WriteAllocs)
 
 	out := &api.CSIVolume{
-		ID:             vol.ID,
-		Name:           vol.Name,
-		ExternalID:     vol.ExternalID,
-		Namespace:      vol.Namespace,
+		ID:         vol.ID,
+		Name:       vol.Name,
+		ExternalID: vol.ExternalID,
+		Namespace:  vol.Namespace,
+
 		Topologies:     structsCSITopolgiesToApi(vol.Topologies),
 		AccessMode:     structsCSIAccessModeToApi(vol.AccessMode),
 		AttachmentMode: structsCSIAttachmentModeToApi(vol.AttachmentMode),
@@ -479,6 +480,13 @@ func structsCSIVolumeToApi(vol *structs.CSIVolume) *api.CSIVolume {
 		Secrets:        structsCSISecretsToApi(vol.Secrets),
 		Parameters:     vol.Parameters,
 		Context:        vol.Context,
+		Capacity:       vol.Capacity,
+
+		RequestedCapacityMin:  vol.RequestedCapacityMin,
+		RequestedCapacityMax:  vol.RequestedCapacityMax,
+		RequestedCapabilities: structsCSICapabilityToApi(vol.RequestedCapabilities),
+		CloneID:               vol.CloneID,
+		SnapshotID:            vol.SnapshotID,
 
 		// Allocations is the collapsed list of both read and write allocs
 		Allocations: make([]*api.AllocationListStub, 0, allocCount),
@@ -756,6 +764,18 @@ func structsCSIAttachmentModeToApi(mode structs.CSIVolumeAttachmentMode) api.CSI
 	return api.CSIVolumeAttachmentModeUnknown
 }
 
+// structsCSICapabilityToApi converts capabilities, part of structsCSIVolumeToApi
+func structsCSICapabilityToApi(caps []*structs.CSIVolumeCapability) []*api.CSIVolumeCapability {
+	out := make([]*api.CSIVolumeCapability, len(caps))
+	for i, cap := range caps {
+		out[i] = &api.CSIVolumeCapability{
+			AccessMode:     api.CSIVolumeAccessMode(cap.AccessMode),
+			AttachmentMode: api.CSIVolumeAttachmentMode(cap.AttachmentMode),
+		}
+	}
+	return out
+}
+
 // structsCSIMountOptionsToApi converts mount options, part of structsCSIVolumeToApi
 func structsCSIMountOptionsToApi(opts *structs.CSIMountOptions) *api.CSIMountOptions {
 	if opts == nil {