From 47da51e04e544d377eef3dd07c1d3ccc3380f551 Mon Sep 17 00:00:00 2001 From: rainzm Date: Fri, 6 Aug 2021 15:41:49 +0800 Subject: [PATCH 1/2] feat(region): add host_type filter for listing wires --- pkg/apis/compute/wire.go | 3 ++- pkg/compute/models/wires.go | 6 ++++++ pkg/mcclient/options/wire.go | 9 +++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/apis/compute/wire.go b/pkg/apis/compute/wire.go index 70b376c92f..c87fd79de4 100644 --- a/pkg/apis/compute/wire.go +++ b/pkg/apis/compute/wire.go @@ -90,7 +90,8 @@ type WireListInput struct { HostResourceInput - Bandwidth *int `json:"bandwidth"` + Bandwidth *int `json:"bandwidth"` + HostType string `json:"host_type"` } type WireMergeInput struct { diff --git a/pkg/compute/models/wires.go b/pkg/compute/models/wires.go index 23e34b3dc7..274459a6ea 100644 --- a/pkg/compute/models/wires.go +++ b/pkg/compute/models/wires.go @@ -1306,6 +1306,12 @@ func (manager *SWireManager) ListItemFilter( sq := HostwireManager.Query("wire_id").Equals("host_id", hostObj.GetId()) q = q.Filter(sqlchemy.In(q.Field("id"), sq.SubQuery())) } + if len(query.HostType) > 0 { + hs := HostManager.Query("id").Equals("host_type", query.HostType).SubQuery() + sq := HostwireManager.Query("wire_id") + sq = sq.Join(hs, sqlchemy.Equals(sq.Field("host_id"), hs.Field("id"))) + q = q.Filter(sqlchemy.In(q.Field("id"), sq.SubQuery())) + } if query.Bandwidth != nil { q = q.Equals("bandwidth", *query.Bandwidth) diff --git a/pkg/mcclient/options/wire.go b/pkg/mcclient/options/wire.go index a14051afd0..95b5a94d51 100644 --- a/pkg/mcclient/options/wire.go +++ b/pkg/mcclient/options/wire.go @@ -21,10 +21,11 @@ type WireListOptions struct { Bandwidth *int `help:"List wires by bandwidth"` - Region string `help:"List wires in region"` - Zone string `help:"list wires in zone" json:"-"` - Vpc string `help:"List wires in vpc"` - Host string `help:"List wires attached to a host"` + Region string `help:"List wires in region"` + Zone string `help:"list wires in zone" json:"-"` + Vpc string `help:"List wires in vpc"` + Host string `help:"List wires attached to a host"` + HostType string `help:"List wires attached to host with HostType"` } func (wo *WireListOptions) GetContextId() string { From 2d7fba494d856cecc4ac80aa2676226105f8c420 Mon Sep 17 00:00:00 2001 From: rainzm Date: Fri, 6 Aug 2021 15:43:37 +0800 Subject: [PATCH 2/2] 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 {