Skip to content

Commit

Permalink
lxd/db: Update Functions to use StorageVolumeFilter.PoolID
Browse files Browse the repository at this point in the history
Signed-off-by: hamistao <[email protected]>
  • Loading branch information
hamistao committed Apr 18, 2024
1 parent 3190452 commit f90ec17
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lxd/db/storage_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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(")")
}

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f90ec17

Please sign in to comment.