diff --git a/pkg/cloudcommon/db/domain.go b/pkg/cloudcommon/db/domain.go index f458668c5b..f4ac19763f 100644 --- a/pkg/cloudcommon/db/domain.go +++ b/pkg/cloudcommon/db/domain.go @@ -107,12 +107,7 @@ func (manager *SDomainizedResourceBaseManager) ListItemFilter( ) (*sqlchemy.SQuery, error) { if len(query.ProjectDomainIds) > 0 { // make sure ids are not utf8 string - idList := make([]string, 0) - for _, pid := range query.ProjectDomainIds { - if !stringutils2.IsUtf8(pid) { - idList = append(idList, pid) - } - } + idList := stringutils2.RemoveUtf8Strings(query.ProjectDomainIds) tenants := TenantCacheManager.GetDomainQuery().SubQuery() subq := tenants.Query(tenants.Field("id")).Filter(sqlchemy.OR( sqlchemy.In(tenants.Field("id"), idList), diff --git a/pkg/cloudcommon/db/project.go b/pkg/cloudcommon/db/project.go index c36ee14aeb..02c86b3ee3 100644 --- a/pkg/cloudcommon/db/project.go +++ b/pkg/cloudcommon/db/project.go @@ -102,12 +102,7 @@ func (manager *SProjectizedResourceBaseManager) ListItemFilter( } if len(query.ProjectIds) > 0 { // make sure ids are not utf8 string - idList := make([]string, 0) - for _, pid := range query.ProjectIds { - if !stringutils2.IsUtf8(pid) { - idList = append(idList, pid) - } - } + idList := stringutils2.RemoveUtf8Strings(query.ProjectIds) tenants := TenantCacheManager.GetTenantQuery().SubQuery() subq := tenants.Query(tenants.Field("id")).Filter(sqlchemy.OR( sqlchemy.In(tenants.Field("id"), idList), diff --git a/pkg/compute/models/managedresource.go b/pkg/compute/models/managedresource.go index 07d118cc1c..75d4e13b85 100644 --- a/pkg/compute/models/managedresource.go +++ b/pkg/compute/models/managedresource.go @@ -518,7 +518,7 @@ func _managedResourceFilterByAccount(managerIdFieldName string, q *sqlchemy.SQue if len(cloudaccountArr) > 0 { cpq := CloudaccountManager.Query().SubQuery() subcpq := cpq.Query(cpq.Field("id")).Filter(sqlchemy.OR( - sqlchemy.In(cpq.Field("id"), cloudaccountArr), + sqlchemy.In(cpq.Field("id"), stringutils2.RemoveUtf8Strings(cloudaccountArr)), sqlchemy.In(cpq.Field("name"), cloudaccountArr), )).SubQuery() subq := CloudproviderManager.Query("id").In("cloudaccount_id", subcpq).SubQuery() diff --git a/pkg/keystone/models/identitybase.go b/pkg/keystone/models/identitybase.go index 2c31ebb3a1..ef804ba628 100644 --- a/pkg/keystone/models/identitybase.go +++ b/pkg/keystone/models/identitybase.go @@ -133,12 +133,7 @@ func (manager *SIdentityBaseResourceManager) ListItemFilter( // override manager.SDomainizedResourceBaseManager.ListItemFilter() if len(query.ProjectDomainIds) > 0 { // make sure ids are not utf8 string - idList := make([]string, 0) - for _, pid := range query.ProjectDomainIds { - if !stringutils2.IsUtf8(pid) { - idList = append(idList, pid) - } - } + idList := stringutils2.RemoveUtf8Strings(query.ProjectDomainIds) domains := DomainManager.Query().SubQuery() subq := domains.Query(domains.Field("id")).Filter(sqlchemy.OR( sqlchemy.In(domains.Field("id"), idList), diff --git a/pkg/util/stringutils2/i18n.go b/pkg/util/stringutils2/i18n.go index cb906c2c6c..3f709450ab 100644 --- a/pkg/util/stringutils2/i18n.go +++ b/pkg/util/stringutils2/i18n.go @@ -23,6 +23,16 @@ func IsUtf8(str string) bool { return false } +func RemoveUtf8Strings(idOrNames []string) []string { + ids := make([]string, 0) + for _, idOrName := range idOrNames { + if !IsUtf8(idOrName) { + ids = append(ids, idOrName) + } + } + return ids +} + func IsPrintableAscii(b byte) bool { if b >= 32 && b <= 126 { return true diff --git a/pkg/util/stringutils2/i18n_test.go b/pkg/util/stringutils2/i18n_test.go index 828af7fe87..40526089ae 100644 --- a/pkg/util/stringutils2/i18n_test.go +++ b/pkg/util/stringutils2/i18n_test.go @@ -15,6 +15,7 @@ package stringutils2 import ( + "reflect" "testing" ) @@ -34,6 +35,31 @@ func TestIsUtf8(t *testing.T) { } } +func TestRemoveUtf8Strings(t *testing.T) { + cases := []struct { + in []string + want []string + }{ + { + in: []string{}, + want: []string{}, + }, + { + in: []string{"en", "中文"}, + want: []string{"en"}, + }, + { + in: []string{"中文"}, + want: []string{}, + }, + } + for _, c := range cases { + if got := RemoveUtf8Strings(c.in); !reflect.DeepEqual(got, c.want) { + t.Errorf("RemoveUtf8Strings %s got %v want %v", c.in, got, c.want) + } + } +} + func TestIsPrintableAscii(t *testing.T) { cases := []struct { in string