From d10645a664fcd4ddf778dcbb7173eb1ffe288996 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Tue, 18 Jun 2019 16:54:44 +0800 Subject: [PATCH] filter storage not attached --- cmd/climc/shell/hosts.go | 2 ++ pkg/compute/models/hosts.go | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/climc/shell/hosts.go b/cmd/climc/shell/hosts.go index 7b9a3118e7..95e98ffbca 100644 --- a/cmd/climc/shell/hosts.go +++ b/cmd/climc/shell/hosts.go @@ -48,6 +48,8 @@ func init() { Hypervisor string `help:"filter hosts by hypervisor"` + StorageNotAttached bool `help:"List hosts not attach specified storage"` + options.BaseListOptions } R(&HostListOptions{}, "host-list", "List hosts", func(s *mcclient.ClientSession, opts *HostListOptions) error { diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 33a586308b..83fcfe7000 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -295,6 +295,7 @@ func (manager *SHostManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQu } storageStr := jsonutils.GetAnyString(query, []string{"storage", "storage_id"}) + notAttached := jsonutils.QueryBoolean(query, "storage_not_attached", false) if len(storageStr) > 0 { storage, _ := StorageManager.FetchByIdOrName(nil, storageStr) if storage == nil { @@ -302,7 +303,11 @@ func (manager *SHostManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQu } hoststorages := HoststorageManager.Query().SubQuery() scopeQuery := hoststorages.Query(hoststorages.Field("host_id")).Equals("storage_id", storage.GetId()).SubQuery() - q = q.In("id", scopeQuery) + if !notAttached { + q = q.In("id", scopeQuery) + } else { + q = q.NotIn("id", scopeQuery) + } } q, err = managedResourceFilterByZone(q, query, "", nil)