From f04762918000b18fb3e6599aeb63a8c305b5e0b4 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Wed, 6 Sep 2023 16:52:41 +0800 Subject: [PATCH] fix: util func to convert id or name of tenants to ids (#17950) Co-authored-by: Qiu Jian --- pkg/cloudcommon/db/tenantcache.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/cloudcommon/db/tenantcache.go b/pkg/cloudcommon/db/tenantcache.go index dd9f40ebf5a..b38c4de441e 100644 --- a/pkg/cloudcommon/db/tenantcache.go +++ b/pkg/cloudcommon/db/tenantcache.go @@ -552,3 +552,29 @@ func (tenant *STenant) GetAllClassMetadata() (map[string]string, error) { } return ret, nil } + +func (manager *STenantCacheManager) ConvertIds(ids []string, isDomain bool) ([]string, error) { + var q *sqlchemy.SQuery + if isDomain { + q = manager.GetDomainQuery("id") + } else { + q = manager.GetTenantQuery("id") + } + q = q.Filter(sqlchemy.OR( + sqlchemy.In(q.Field("id"), ids), + sqlchemy.In(q.Field("name"), ids), + )) + q = q.Distinct() + results := []struct { + Id string + }{} + err := q.All(&results) + if err != nil { + return nil, errors.Wrap(err, "query") + } + ret := make([]string, len(results)) + for i := range results { + ret[i] = results[i].Id + } + return ret, nil +}