From df71fdd3a81bbe4bd0c6151ecd33f7f8631b185a Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Thu, 30 Nov 2023 00:07:41 +0800 Subject: [PATCH] fix: filter networks by host type (#18818) Co-authored-by: Qiu Jian --- pkg/compute/models/networks.go | 84 +++++++++++++++------------------- 1 file changed, 38 insertions(+), 46 deletions(-) diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index e1f8b1d0f5..2d84ebbb5a 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -2097,56 +2097,48 @@ func (manager *SNetworkManager) ListItemFilter( q = q.In("wire_id", wires).Equals("status", api.NETWORK_STATUS_AVAILABLE) } - if len(input.HostId)+len(input.HostType) > 0 { - type sSimpleHost struct { - Id string - OvnVersion string + if len(input.HostType) > 0 || len(input.HostId) > 0 { + classicWiresIdQ := WireManager.Query("id").Equals("vpc_id", api.DEFAULT_VPC_ID) + netifs := NetInterfaceManager.Query("wire_id", "baremetal_id").SubQuery() + classicWiresIdQ = classicWiresIdQ.Join(netifs, sqlchemy.Equals(netifs.Field("wire_id"), classicWiresIdQ.Field("id"))) + hosts := HostManager.Query("id") + if len(input.HostType) > 0 { + hosts = hosts.Equals("host_type", input.HostType) } - hq := HostManager.Query("id", "ovn_version") - switch { - case len(input.HostId) > 0 && len(input.HostType) > 0: - hq = hq.Filter(sqlchemy.OR( - sqlchemy.Equals(hq.Field("id"), input.HostId), - sqlchemy.Equals(hq.Field("host_type"), input.HostType), - )) - case len(input.HostId) > 0: - hq = hq.Equals("id", input.HostId) - case len(input.HostType) > 0: - hq = hq.Equals("host_type", input.HostType) + if len(input.HostId) > 0 { + hosts = hosts.In("id", input.HostId) } - shs := make([]sSimpleHost, 0) - err := hq.All(&shs) - if err != nil { - return nil, errors.Wrap(err, "unable to filter all host from id and host type") - } - hostids := make([]string, len(shs)) - var ovnVersion bool - for i := range shs { - hostids[i] = shs[i].Id - if len(shs[i].OvnVersion) > 0 { - ovnVersion = true + hostsQ := hosts.SubQuery() + classicWiresIdQ = classicWiresIdQ.Join(hostsQ, sqlchemy.Equals(netifs.Field("baremetal_id"), hostsQ.Field("id"))) + + wireIdQ := classicWiresIdQ.SubQuery() + + if input.HostType == api.HOST_TYPE_HYPERVISOR { + // should consider VPC network + vpcHostQ := HostManager.Query().IsNotEmpty("ovn_version") + if len(input.HostType) > 0 { + vpcHostQ = vpcHostQ.Equals("host_type", input.HostType) + } + if len(input.HostId) > 0 { + vpcHostQ = vpcHostQ.In("id", input.HostId) + } + vpcHostCnt, err := vpcHostQ.CountWithError() + if err != nil { + return nil, errors.Wrap(err, "vpcHostQ.CountWithError") + } + if vpcHostCnt > 0 { + vpcWiresIdQ := WireManager.Query("id").NotEquals("vpc_id", api.DEFAULT_VPC_ID) + + wireIdUnion := sqlchemy.Union(classicWiresIdQ, vpcWiresIdQ) + wireIdQ = wireIdUnion.Query().SubQuery() } } - sq := NetInterfaceManager.Query("wire_id") - switch len(hostids) { - case 0: - // hack for empty hostwire - sq = sq.IsTrue("deleted") - case 1: - sq = sq.Equals("baremetal_id", hostids[0]) - default: - sq = sq.In("baremetal_id", hostids) - } - if ovnVersion { - vpcSub := VpcManager.Query("id").Equals("cloudregion_id", "default").NotEquals("id", api.DEFAULT_VPC_ID).SubQuery() - wireQuery := WireManager.Query("id").In("vpc_id", vpcSub) - q = q.Filter(sqlchemy.OR( - sqlchemy.In(q.Field("wire_id"), wireQuery.SubQuery()), - sqlchemy.In(q.Field("wire_id"), sq.SubQuery())), - ) - } else { - q = q.Filter(sqlchemy.In(q.Field("wire_id"), sq.SubQuery())) - } + additionalQ := NetworkAdditionalWireManager.Query("network_id") + additionalQ = additionalQ.Join(wireIdQ, sqlchemy.Equals(wireIdQ.Field("id"), additionalQ.Field("wire_id"))) + q = q.Filter(sqlchemy.OR( + sqlchemy.In(q.Field("wire_id"), wireIdQ), + sqlchemy.In(q.Field("id"), additionalQ.SubQuery()), + )) } storageStr := input.StorageId