Merge pull request #11838 from rainzm/automated-cherry-pick-of-#11833-upstream-release-3.7

Automated cherry pick of #11833: Filter wires and networks from host_type of host
This commit is contained in:
Zexi Li
2021-08-07 14:29:01 +08:00
committed by GitHub
6 changed files with 59 additions and 14 deletions
+2
View File
@@ -126,6 +126,8 @@ type NetworkListInput struct {
// filter by BGP types
BgpType []string `json:"bgp_type"`
HostType string `json:"host_type"`
}
type NetworkResourceInfoBase struct {
+2 -1
View File
@@ -90,7 +90,8 @@ type WireListInput struct {
HostResourceInput
Bandwidth *int `json:"bandwidth"`
Bandwidth *int `json:"bandwidth"`
HostType string `json:"host_type"`
}
type WireMergeInput struct {
+42 -8
View File
@@ -2050,15 +2050,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())),
+6
View File
@@ -1301,6 +1301,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)
+2 -1
View File
@@ -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 {
+5 -4
View File
@@ -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 {