diff --git a/cmd/climc/shell/compute/snapshots.go b/cmd/climc/shell/compute/snapshots.go index 04016dab59..6d362aa4b2 100644 --- a/cmd/climc/shell/compute/snapshots.go +++ b/cmd/climc/shell/compute/snapshots.go @@ -33,6 +33,7 @@ func init() { DiskType string `help:"Filter by disk type" choices:"sys|data"` Server string `help:"Filter by server" json:"server_id"` Unused bool + StorageId string `help:"Filter by storage id"` OrderByGuest string OrderByDiskName string diff --git a/pkg/apis/compute/snapshot.go b/pkg/apis/compute/snapshot.go index 360866bfcf..7ee10b9b5c 100644 --- a/pkg/apis/compute/snapshot.go +++ b/pkg/apis/compute/snapshot.go @@ -77,6 +77,9 @@ type SnapshotListInput struct { // 未关联任何磁盘 Unused bool `json:"unused"` + // 按存储过滤 + StorageId string `json:"storage_id"` + // 按虚拟机名称排序 // pattern:asc|desc OrderByGuest string `json:"order_by_guest"` diff --git a/pkg/compute/models/snapshots.go b/pkg/compute/models/snapshots.go index f4952b5132..8afb2e4934 100644 --- a/pkg/compute/models/snapshots.go +++ b/pkg/compute/models/snapshots.go @@ -212,6 +212,10 @@ func (manager *SSnapshotManager) ListItemFilter( q = q.NotIn("disk_id", sq) } + if len(query.StorageId) > 0 { + q = q.Equals("storage_id", query.StorageId) + } + return q, nil } @@ -297,6 +301,19 @@ func (manager *SSnapshotManager) QueryDistinctExtraField(q *sqlchemy.SQuery, fie return q, httperrors.ErrNotFound } +func (manager *SSnapshotManager) QueryDistinctExtraFields(q *sqlchemy.SQuery, resource string, fields []string) (*sqlchemy.SQuery, error) { + switch resource { + case StorageManager.Keyword(): + storages := StorageManager.Query().SubQuery() + for _, field := range fields { + q = q.AppendField(storages.Field(field)) + } + q = q.Join(storages, sqlchemy.Equals(q.Field("storage_id"), storages.Field("id"))) + return q, nil + } + return q, httperrors.ErrNotFound +} + type sSnapshotGuest struct { DiskId string GuestId string