diff --git a/cmd/climc/shell/compute/snapshots.go b/cmd/climc/shell/compute/snapshots.go index 368bf8fd755..04016dab591 100644 --- a/cmd/climc/shell/compute/snapshots.go +++ b/cmd/climc/shell/compute/snapshots.go @@ -32,6 +32,7 @@ func init() { Share *bool `help:"Show shared snapshots"` DiskType string `help:"Filter by disk type" choices:"sys|data"` Server string `help:"Filter by server" json:"server_id"` + Unused bool OrderByGuest string OrderByDiskName string diff --git a/pkg/apis/compute/snapshot.go b/pkg/apis/compute/snapshot.go index ebfe4dfa4a8..360866bfcf6 100644 --- a/pkg/apis/compute/snapshot.go +++ b/pkg/apis/compute/snapshot.go @@ -74,6 +74,9 @@ type SnapshotListInput struct { // list server snapshots ServerId string `json:"server_id"` + // 未关联任何磁盘 + Unused bool `json:"unused"` + // 按虚拟机名称排序 // pattern:asc|desc OrderByGuest string `json:"order_by_guest"` diff --git a/pkg/compute/models/snapshots.go b/pkg/compute/models/snapshots.go index 0fa27f3147d..c87e8bb41b0 100644 --- a/pkg/compute/models/snapshots.go +++ b/pkg/compute/models/snapshots.go @@ -206,6 +206,11 @@ func (manager *SSnapshotManager) ListItemFilter( q = q.In("disk_id", gdq) } + if query.Unused { + sq := DiskManager.Query("id").Distinct().SubQuery() + q = q.NotIn("disk_id", sq) + } + return q, nil }