diff --git a/cmd/climc/shell/compute/storages.go b/cmd/climc/shell/compute/storages.go index a57ef9a2b6..088f4dab8d 100644 --- a/cmd/climc/shell/compute/storages.go +++ b/cmd/climc/shell/compute/storages.go @@ -37,6 +37,7 @@ func init() { Schedtag string `help:"filter storage by schedtag"` HostSchedtagId string `help:"filter storage by host schedtag"` + ImageId string `help:"filter storage by image"` } R(&StorageListOptions{}, "storage-list", "List storages", func(s *mcclient.ClientSession, opts *StorageListOptions) error { params, err := options.ListStructToParams(opts) diff --git a/pkg/apis/compute/storage_const.go b/pkg/apis/compute/storage_const.go index 091e9667ec..527b763735 100644 --- a/pkg/apis/compute/storage_const.go +++ b/pkg/apis/compute/storage_const.go @@ -186,4 +186,7 @@ type StorageListInput struct { // filter by host schedtag HostSchedtagId string `json:"host_schedtag_id"` + + // filter by cachedimage + ImageId string `json:"image_id"` } diff --git a/pkg/compute/models/storages.go b/pkg/compute/models/storages.go index c0f0ada84c..e31c9d97ad 100644 --- a/pkg/compute/models/storages.go +++ b/pkg/compute/models/storages.go @@ -1399,6 +1399,17 @@ func (manager *SStorageManager) ListItemFilter( q = q.In("id", subq.SubQuery()) } + if len(query.ImageId) > 0 { + image, err := CachedimageManager.getImageInfo(ctx, userCred, query.ImageId, false) + if err != nil { + return nil, errors.Wrap(err, "CachedimageManager.getImageInfo") + } + subq := StorageManager.Query("id") + storagecaches := StoragecachedimageManager.Query("storagecache_id").Equals("cachedimage_id", image.Id).SubQuery() + subq = subq.Join(storagecaches, sqlchemy.Equals(subq.Field("storagecache_id"), storagecaches.Field("storagecache_id"))) + q = q.In("id", subq.SubQuery()) + } + return q, err }