diff --git a/pkg/apis/compute/host.go b/pkg/apis/compute/host.go index ed284f197d..894f6bfcb6 100644 --- a/pkg/apis/compute/host.go +++ b/pkg/apis/compute/host.go @@ -73,7 +73,7 @@ type HostListInput struct { // filter by mac of any network interface AnyMac string `json:"any_mac"` // filter by ip of any network interface - AnyIp string `json:"any_ip"` + AnyIp []string `json:"any_ip"` // filter storages not attached to this host StorageNotAttached *bool `json:"storage_not_attached"` // filter by Hypervisor diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 0524e243a2..dfb6bdbace 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -270,14 +270,24 @@ func (manager *SHostManager) ListItemFilter( } } if len(query.AnyIp) > 0 { - hn := HostnetworkManager.Query("baremetal_id").Contains("ip_addr", query.AnyIp).SubQuery() + hnQ := HostnetworkManager.Query("baremetal_id") //.Contains("ip_addr", query.AnyIp).SubQuery() + conditions := []sqlchemy.ICondition{} + for _, ip := range query.AnyIp { + conditions = append(conditions, sqlchemy.Contains(hnQ.Field("ip_addr"), ip)) + } + hn := hnQ.Filter( + sqlchemy.OR(conditions...), + ) + conditions = []sqlchemy.ICondition{} + for _, ip := range query.AnyIp { + conditions = append(conditions, sqlchemy.Contains(q.Field("access_ip"), ip)) + conditions = append(conditions, sqlchemy.Contains(q.Field("ipmi_ip"), ip)) + } + conditions = append(conditions, sqlchemy.In(q.Field("id"), hn)) q = q.Filter(sqlchemy.OR( - sqlchemy.Contains(q.Field("access_ip"), query.AnyIp), - sqlchemy.Contains(q.Field("ipmi_ip"), query.AnyIp), - sqlchemy.In(q.Field("id"), hn), + conditions..., )) } - // var scopeQuery *sqlchemy.SSubQuery schedTagStr := query.SchedtagId if len(schedTagStr) > 0 { diff --git a/pkg/mcclient/options/compute/host.go b/pkg/mcclient/options/compute/host.go index 2283f2c6fd..1c90d70771 100644 --- a/pkg/mcclient/options/compute/host.go +++ b/pkg/mcclient/options/compute/host.go @@ -21,20 +21,20 @@ import ( ) type HostListOptions struct { - Schedtag string `help:"List hosts in schedtag"` - Zone string `help:"List hosts in zone"` - Region string `help:"List hosts in region"` - Wire string `help:"List hosts in wire"` - Image string `help:"List hosts cached images" json:"cachedimage"` - Storage string `help:"List hosts attached to storages"` - Baremetal string `help:"List hosts that is managed by baremetal system" choices:"true|false"` - Empty bool `help:"show empty host" json:"-"` - Occupied bool `help:"show occupid host" json:"-"` - Enabled bool `help:"Show enabled host only" json:"-"` - Disabled bool `help:"Show disabled host only" json:"-"` - HostType string `help:"Host type filter" choices:"baremetal|hypervisor|esxi|kubelet|hyperv|aliyun|azure|qcloud|aws|huawei|ucloud|google|ctyun"` - AnyMac string `help:"Mac matches one of the host's interface"` - AnyIp string `help:"IP matches one of the host's interface"` + Schedtag string `help:"List hosts in schedtag"` + Zone string `help:"List hosts in zone"` + Region string `help:"List hosts in region"` + Wire string `help:"List hosts in wire"` + Image string `help:"List hosts cached images" json:"cachedimage"` + Storage string `help:"List hosts attached to storages"` + Baremetal string `help:"List hosts that is managed by baremetal system" choices:"true|false"` + Empty bool `help:"show empty host" json:"-"` + Occupied bool `help:"show occupid host" json:"-"` + Enabled bool `help:"Show enabled host only" json:"-"` + Disabled bool `help:"Show disabled host only" json:"-"` + HostType string `help:"Host type filter" choices:"baremetal|hypervisor|esxi|kubelet|hyperv|aliyun|azure|qcloud|aws|huawei|ucloud|google|ctyun"` + AnyMac string `help:"Mac matches one of the host's interface"` + AnyIp []string `help:"IP matches one of the host's interface"` IsBaremetal *bool `help:"filter host list by is_baremetal=true|false"`