diff --git a/lxd/db/storage_volumes.go b/lxd/db/storage_volumes.go index 37c111c82b04..8eeafc0d8cb0 100644 --- a/lxd/db/storage_volumes.go +++ b/lxd/db/storage_volumes.go @@ -130,9 +130,9 @@ type StorageVolume struct { // If there are no volumes, it returns an empty list and no error. // Accepts filters for narrowing down the results returned. If memberSpecific is true, then the search is // restricted to volumes that belong to this member or belong to all members. -func (c *ClusterTx) GetStoragePoolVolumes(ctx context.Context, poolID int64, memberSpecific bool, filters ...StorageVolumeFilter) ([]*StorageVolume, error) { +func (c *ClusterTx) GetStoragePoolVolumes(ctx context.Context, memberSpecific bool, filters ...StorageVolumeFilter) ([]*StorageVolume, error) { var q = &strings.Builder{} - args := []any{poolID} + args := []any{} q.WriteString(` SELECT @@ -149,16 +149,10 @@ func (c *ClusterTx) GetStoragePoolVolumes(ctx context.Context, poolID int64, mem JOIN projects ON projects.id = storage_volumes_all.project_id LEFT JOIN nodes ON nodes.id = storage_volumes_all.node_id JOIN storage_pools ON storage_pools.id = storage_volumes_all.storage_pool_id - WHERE storage_volumes_all.storage_pool_id = ? `) - if memberSpecific { - q.WriteString("AND (storage_volumes_all.node_id = ? OR storage_volumes_all.node_id IS NULL) ") - args = append(args, c.nodeID) - } - if len(filters) > 0 { - q.WriteString("AND (") + q.WriteString("WHERE (") for i, filter := range filters { // Validate filter. @@ -177,6 +171,11 @@ func (c *ClusterTx) GetStoragePoolVolumes(ctx context.Context, poolID int64, mem args = append(args, *filter.Type) } + if filter.PoolID != nil { + qFilters = append(qFilters, "storage_volumes_all.storage_pool_id = ?") + args = append(args, *filter.PoolID) + } + if filter.Project != nil { qFilters = append(qFilters, "projects.name = ?") args = append(args, *filter.Project) @@ -198,6 +197,15 @@ func (c *ClusterTx) GetStoragePoolVolumes(ctx context.Context, poolID int64, mem q.WriteString(fmt.Sprintf("(%s)", strings.Join(qFilters, " AND "))) } + if memberSpecific { + if len(filters) > 0 { + q.WriteString("AND (storage_volumes_all.node_id = ? OR storage_volumes_all.node_id IS NULL) ") + } else { + q.WriteString("WHERE (storage_volumes_all.node_id = ? OR storage_volumes_all.node_id IS NULL) ") + } + args = append(args, c.nodeID) + } + q.WriteString(")") } @@ -249,9 +257,10 @@ func (c *ClusterTx) GetStoragePoolVolume(ctx context.Context, poolID int64, proj Project: &projectName, Type: &volumeType, Name: &volumeName, + PoolID: &poolID, }} - volumes, err := c.GetStoragePoolVolumes(ctx, poolID, memberSpecific, filters...) + volumes, err := c.GetStoragePoolVolumes(ctx, memberSpecific, filters...) volumesLen := len(volumes) if (err == nil && volumesLen <= 0) || errors.Is(err, sql.ErrNoRows) { return nil, api.StatusErrorf(http.StatusNotFound, "Storage volume not found")