From dba51f3976a1a4046c81601331a18d13cea7757d Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Wed, 1 Dec 2021 17:37:01 +0800 Subject: [PATCH] fix(region): host support list by os_arch --- pkg/apis/compute/host.go | 1 + pkg/cloudcommon/db/archs.go | 49 +++++++++++++++---------------------- pkg/compute/models/hosts.go | 3 +++ 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/pkg/apis/compute/host.go b/pkg/apis/compute/host.go index 87af968afa..cd6278738b 100644 --- a/pkg/apis/compute/host.go +++ b/pkg/apis/compute/host.go @@ -124,6 +124,7 @@ type HostListInput struct { ServerIdForNetwork string `json:"server_id_for_network"` // 宿主机 cpu 架构 CpuArchitecture string `json:"cpu_architecture"` + OsArch string `json:"os_arch"` // 按虚拟机数量排序 // enum: asc,desc diff --git a/pkg/cloudcommon/db/archs.go b/pkg/cloudcommon/db/archs.go index eef9f9d394..873874242d 100644 --- a/pkg/cloudcommon/db/archs.go +++ b/pkg/cloudcommon/db/archs.go @@ -12,20 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Code generated by model-api-gen. DO NOT EDIT. - package db import ( @@ -52,20 +38,25 @@ func (manager *SMultiArchResourceBaseManager) ListItemFilter( userCred mcclient.TokenCredential, query apis.MultiArchResourceBaseListInput, ) (*sqlchemy.SQuery, error) { - if len(query.OsArch) > 0 { - if query.OsArch == apis.OS_ARCH_X86 { - q = q.Filter(sqlchemy.OR( - sqlchemy.Startswith(q.Field("os_arch"), query.OsArch), - sqlchemy.IsNullOrEmpty(q.Field("os_arch")), - )) - } else if query.OsArch == apis.OS_ARCH_ARM { - q = q.Filter(sqlchemy.OR( - sqlchemy.Startswith(q.Field("os_arch"), query.OsArch), - sqlchemy.Equals(q.Field("os_arch"), apis.OS_ARCH_AARCH64), - )) - } else { - q = q.Startswith("os_arch", query.OsArch) - } + return ListQueryByArchitecture(q, "os_arch", query.OsArch), nil +} + +func ListQueryByArchitecture(q *sqlchemy.SQuery, fieldKey string, arch string) *sqlchemy.SQuery { + if len(arch) == 0 { + return q } - return q, nil + if arch == apis.OS_ARCH_X86 { + q = q.Filter(sqlchemy.OR( + sqlchemy.Startswith(q.Field(fieldKey), arch), + sqlchemy.IsNullOrEmpty(q.Field(fieldKey)), + )) + } else if arch == apis.OS_ARCH_ARM { + q = q.Filter(sqlchemy.OR( + sqlchemy.Startswith(q.Field(fieldKey), arch), + sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_AARCH64), + )) + } else { + q = q.Startswith(fieldKey, arch) + } + return q } diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 84bf77c644..4e117ec1ed 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -445,6 +445,9 @@ func (manager *SHostManager) ListItemFilter( if len(query.CpuArchitecture) > 0 { q = q.Equals("cpu_architecture", query.CpuArchitecture) } + if len(query.OsArch) > 0 { + q = db.ListQueryByArchitecture(q, "cpu_architecture", query.OsArch) + } // for provider onecloud if len(query.ServerIdForNetwork) > 0 {