From 2d7fba494d856cecc4ac80aa2676226105f8c420 Mon Sep 17 00:00:00 2001 From: rainzm Date: Fri, 6 Aug 2021 15:43:37 +0800 Subject: [PATCH] feat(region): add host_type filter for listing network Also fixed the previous problem of using host to filter the network of yunioncloud --- pkg/apis/compute/network.go | 2 ++ pkg/compute/models/networks.go | 50 +++++++++++++++++++++++++++------ pkg/mcclient/options/network.go | 3 +- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/pkg/apis/compute/network.go b/pkg/apis/compute/network.go index 4a341e7c30..e5ee5f573f 100644 --- a/pkg/apis/compute/network.go +++ b/pkg/apis/compute/network.go @@ -126,6 +126,8 @@ type NetworkListInput struct { // filter by BGP types BgpType []string `json:"bgp_type"` + + HostType string `json:"host_type"` } type NetworkResourceInfoBase struct { diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index d39ee6d7ce..27271f6a63 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -2060,15 +2060,49 @@ func (manager *SNetworkManager) ListItemFilter( } hostStr := input.HostId - if len(hostStr) > 0 { - hostObj, err := HostManager.FetchByIdOrName(userCred, hostStr) - if err != nil { - return nil, httperrors.NewResourceNotFoundError2(HostManager.Keyword(), hostStr) + if len(hostStr)+len(input.HostType) > 0 { + type sSimpleHost struct { + Id string + OvnVersion string } - host := hostObj.(*SHost) - sq := HostwireManager.Query("wire_id").Equals("host_id", hostObj.GetId()) - if len(host.OvnVersion) > 0 { - wireQuery := WireManager.Query("id").IsNotNull("vpc_id") + hq := HostManager.Query("id", "ovn_version") + switch { + case len(hostStr) > 0 && len(input.HostType) > 0: + hq = hq.Filter(sqlchemy.OR( + sqlchemy.Equals(hq.Field("id"), hostStr), + sqlchemy.Equals(hq.Field("host_type"), input.HostType), + )) + case len(hostStr) > 0: + hq = hq.Equals("id", hostStr) + case len(input.HostType) > 0: + hq = hq.Equals("host_type", input.HostType) + } + 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 + } + } + sq := HostwireManager.Query("wire_id") + switch len(hostids) { + case 0: + // hack for empty hostwire + sq = sq.IsTrue("deleted") + case 1: + sq = sq.Equals("host_id", hostids[0]) + default: + sq = sq.In("host_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())), diff --git a/pkg/mcclient/options/network.go b/pkg/mcclient/options/network.go index 5894358fe3..70c15536d9 100644 --- a/pkg/mcclient/options/network.go +++ b/pkg/mcclient/options/network.go @@ -44,7 +44,8 @@ type NetworkListOptions struct { GuestIpStart []string `help:"search by guest_ip_start"` GuestIpEnd []string `help:"search by guest_ip_end"` - BgpType []string `help:"filter by bgp_type"` + BgpType []string `help:"filter by bgp_type"` + HostType string `help:"filter by host_type"` } func (opts *NetworkListOptions) GetContextId() string {