From e3a809a8875ea8ca35442efe3744fd657acef9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=90=AF=E8=88=AA?= <101104760+ZhangSetSail@users.noreply.github.com> Date: Wed, 5 Jun 2024 17:00:10 +0800 Subject: [PATCH] fix: modify team query logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张启航 <101104760+ZhangSetSail@users.noreply.github.com> --- api/controller/resources.go | 28 +++++++++---------- api/handler/service_plugin.go | 16 +++++------ api/handler/tenant.go | 51 ++++++++++++++++++++--------------- api/handler/tenant_handler.go | 3 ++- db/dao/dao.go | 1 + db/mysql/dao/tenants.go | 9 +++++++ 6 files changed, 64 insertions(+), 44 deletions(-) diff --git a/api/controller/resources.go b/api/controller/resources.go index b6e2dabd7b..c3ad840e82 100644 --- a/api/controller/resources.go +++ b/api/controller/resources.go @@ -531,7 +531,15 @@ func (t *TenantStruct) GetTenants(w http.ResponseWriter, r *http.Request) { // schema: // "$ref": "#/responses/commandResponse" // description: 统一返回格式 - value := r.FormValue("eid") + jsonTenantIDs := r.FormValue("tenant_ids") + var tenantIDS []string + if jsonTenantIDs != "" { + err := json.Unmarshal([]byte(jsonTenantIDs), &tenantIDS) + if err != nil { + httputil.ReturnError(r, w, 500, fmt.Sprintf("json unmarshal failure %v", err)) + return + } + } page, _ := strconv.Atoi(r.FormValue("page")) if page == 0 { page = 1 @@ -540,21 +548,13 @@ func (t *TenantStruct) GetTenants(w http.ResponseWriter, r *http.Request) { if pageSize == 0 { pageSize = 10 } - queryName := r.FormValue("query") var tenants []*dbmodel.Tenants var err error - if len(value) == 0 { - tenants, err = handler.GetTenantManager().GetTenants(queryName) - if err != nil { - httputil.ReturnError(r, w, 500, "get tenant error") - return - } - } else { - tenants, err = handler.GetTenantManager().GetTenantsByEid(value, queryName) - if err != nil { - httputil.ReturnError(r, w, 500, "get tenant error") - return - } + + tenants, err = handler.GetTenantManager().GetTenantsByTenantIDs(tenantIDS) + if err != nil { + httputil.ReturnError(r, w, 500, "get tenant error") + return } list := handler.GetTenantManager().BindTenantsResource(tenants) re := list.Paging(page, pageSize) diff --git a/api/handler/service_plugin.go b/api/handler/service_plugin.go index 066d507e66..6ce4fd64b4 100644 --- a/api/handler/service_plugin.go +++ b/api/handler/service_plugin.go @@ -34,7 +34,7 @@ import ( "github.com/sirupsen/logrus" ) -//GetTenantServicePluginRelation GetTenantServicePluginRelation +// GetTenantServicePluginRelation GetTenantServicePluginRelation func (s *ServiceAction) GetTenantServicePluginRelation(serviceID string) ([]*dbmodel.TenantServicePluginRelation, *util.APIHandleError) { gps, err := db.GetManager().TenantServicePluginRelationDao().GetALLRelationByServiceID(serviceID) if err != nil { @@ -43,7 +43,7 @@ func (s *ServiceAction) GetTenantServicePluginRelation(serviceID string) ([]*dbm return gps, nil } -//TenantServiceDeletePluginRelation uninstall plugin for app +// TenantServiceDeletePluginRelation uninstall plugin for app func (s *ServiceAction) TenantServiceDeletePluginRelation(tenantID, serviceID, pluginID string) *util.APIHandleError { tx := db.GetManager().Begin() defer func() { @@ -85,7 +85,7 @@ func (s *ServiceAction) TenantServiceDeletePluginRelation(tenantID, serviceID, p return nil } -//SetTenantServicePluginRelation SetTenantServicePluginRelation +// SetTenantServicePluginRelation SetTenantServicePluginRelation func (s *ServiceAction) SetTenantServicePluginRelation(tenantID, serviceID string, pss *api_model.PluginSetStruct) (*dbmodel.TenantServicePluginRelation, *util.APIHandleError) { plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(pss.Body.PluginID, tenantID) if err != nil { @@ -182,7 +182,7 @@ func (s *ServiceAction) SetTenantServicePluginRelation(tenantID, serviceID strin return relation, nil } -//UpdateTenantServicePluginRelation UpdateTenantServicePluginRelation +// UpdateTenantServicePluginRelation UpdateTenantServicePluginRelation func (s *ServiceAction) UpdateTenantServicePluginRelation(serviceID string, pss *api_model.PluginSetStruct) (*dbmodel.TenantServicePluginRelation, *util.APIHandleError) { relation, err := db.GetManager().TenantServicePluginRelationDao().GetRelateionByServiceIDAndPluginID(serviceID, pss.Body.PluginID) if err != nil { @@ -221,7 +221,7 @@ func checkPluginHaveInbound(model string) bool { return model == dbmodel.InBoundNetPlugin || model == dbmodel.InBoundAndOutBoundNetPlugin } -//UpdateVersionEnv UpdateVersionEnv +// UpdateVersionEnv UpdateVersionEnv func (s *ServiceAction) UpdateVersionEnv(uve *api_model.SetVersionEnv) *util.APIHandleError { plugin, err := db.GetManager().TenantPluginDao().GetPluginByID(uve.PluginID, uve.Body.TenantID) if err != nil { @@ -290,7 +290,7 @@ func (s *ServiceAction) upNormalEnvs(tx *gorm.DB, uve *api_model.SetVersionEnv) return nil } -//SavePluginConfig save plugin dynamic discovery config +// SavePluginConfig save plugin dynamic discovery config func (s *ServiceAction) SavePluginConfig(serviceID, pluginID string, config *api_model.ResourceSpec) *util.APIHandleError { if config == nil { return nil @@ -327,7 +327,7 @@ func (s *ServiceAction) SavePluginConfig(serviceID, pluginID string, config *api return nil } -//DeletePluginConfig delete service plugin dynamic discovery config +// DeletePluginConfig delete service plugin dynamic discovery config func (s *ServiceAction) DeletePluginConfig(serviceID, pluginID string) *util.APIHandleError { tx := db.GetManager().Begin() err := s.deletePluginConfig(tx, serviceID, pluginID) @@ -343,7 +343,7 @@ func (s *ServiceAction) DeletePluginConfig(serviceID, pluginID string) *util.API return nil } -//DeletePluginConfig delete service plugin dynamic discovery config +// DeletePluginConfig delete service plugin dynamic discovery config func (s *ServiceAction) deletePluginConfig(tx *gorm.DB, serviceID, pluginID string) *util.APIHandleError { if tx != nil { if err := db.GetManager().TenantPluginVersionConfigDaoTransactions(tx).DeletePluginConfig(serviceID, pluginID); err != nil { diff --git a/api/handler/tenant.go b/api/handler/tenant.go index 268e6b1231..413abe3bb3 100644 --- a/api/handler/tenant.go +++ b/api/handler/tenant.go @@ -50,7 +50,7 @@ import ( k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) -//TenantAction tenant act +// TenantAction tenant act type TenantAction struct { MQClient mqclient.MQClient statusCli *client.AppRuntimeSyncClient @@ -63,7 +63,7 @@ type TenantAction struct { resources map[string]k8sclient.Object } -//CreateTenManager create Manger +// CreateTenManager create Manger func CreateTenManager(mqc mqclient.MQClient, statusCli *client.AppRuntimeSyncClient, optCfg *option.Config, kubeClient *kubernetes.Clientset, @@ -86,7 +86,7 @@ func CreateTenManager(mqc mqclient.MQClient, statusCli *client.AppRuntimeSyncCli } } -//BindTenantsResource query tenant resource used and sort +// BindTenantsResource query tenant resource used and sort func (t *TenantAction) BindTenantsResource(source []*dbmodel.Tenants) api_model.TenantList { var list api_model.TenantList var resources = make(map[string]*pb.TenantResource, len(source)) @@ -133,7 +133,7 @@ func (t *TenantAction) BindTenantsResource(source []*dbmodel.Tenants) api_model. return list } -//GetTenants get tenants +// GetTenants get tenants func (t *TenantAction) GetTenants(query string) ([]*dbmodel.Tenants, error) { tenants, err := db.GetManager().TenantDao().GetALLTenants(query) if err != nil { @@ -142,7 +142,7 @@ func (t *TenantAction) GetTenants(query string) ([]*dbmodel.Tenants, error) { return tenants, err } -//GetTenantsByEid GetTenantsByEid +// GetTenantsByEid GetTenantsByEid func (t *TenantAction) GetTenantsByEid(eid, query string) ([]*dbmodel.Tenants, error) { tenants, err := db.GetManager().TenantDao().GetTenantByEid(eid, query) if err != nil { @@ -151,7 +151,16 @@ func (t *TenantAction) GetTenantsByEid(eid, query string) ([]*dbmodel.Tenants, e return tenants, err } -//UpdateTenant update tenant info +// GetTenantsByTenantIDs GetTenantsByTenantIDs +func (t *TenantAction) GetTenantsByTenantIDs(tenantIDS []string) ([]*dbmodel.Tenants, error) { + tenants, err := db.GetManager().TenantDao().GetTenantByTenantIDs(tenantIDS) + if err != nil { + return nil, err + } + return tenants, err +} + +// UpdateTenant update tenant info func (t *TenantAction) UpdateTenant(tenant *dbmodel.Tenants) error { return db.GetManager().TenantDao().UpdateModel(tenant) } @@ -213,7 +222,7 @@ func (t *TenantAction) DeleteTenant(ctx context.Context, tenantID string) error return nil } -//TotalMemCPU StatsMemCPU +// TotalMemCPU StatsMemCPU func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) { cpus := 0 mem := 0 @@ -229,7 +238,7 @@ func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_mod return si, nil } -//GetTenantsName get tenants name +// GetTenantsName get tenants name func (t *TenantAction) GetTenantsName() ([]string, error) { tenants, err := db.GetManager().TenantDao().GetALLTenants("") if err != nil { @@ -242,7 +251,7 @@ func (t *TenantAction) GetTenantsName() ([]string, error) { return result, err } -//GetTenantsByName get tenants +// GetTenantsByName get tenants func (t *TenantAction) GetTenantsByName(name string) (*dbmodel.Tenants, error) { tenant, err := db.GetManager().TenantDao().GetTenantIDByName(name) if err != nil { @@ -251,7 +260,7 @@ func (t *TenantAction) GetTenantsByName(name string) (*dbmodel.Tenants, error) { return tenant, err } -//GetTenantsByUUID get tenants +// GetTenantsByUUID get tenants func (t *TenantAction) GetTenantsByUUID(uuid string) (*dbmodel.Tenants, error) { tenant, err := db.GetManager().TenantDao().GetTenantByUUID(uuid) if err != nil { @@ -261,7 +270,7 @@ func (t *TenantAction) GetTenantsByUUID(uuid string) (*dbmodel.Tenants, error) { return tenant, err } -//StatsMemCPU StatsMemCPU +// StatsMemCPU StatsMemCPU func (t *TenantAction) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) { cpus := 0 mem := 0 @@ -289,7 +298,7 @@ type QueryResult struct { Status string `json:"status"` } -//GetTenantsResources Gets the resource usage of the specified tenant. +// GetTenantsResources Gets the resource usage of the specified tenant. func (t *TenantAction) GetTenantsResources(ctx context.Context, tr *api_model.TenantResources) (map[string]map[string]interface{}, error) { ids, err := db.GetManager().TenantDao().GetTenantIDsByNames(tr.Body.TenantNames) if err != nil { @@ -373,7 +382,7 @@ func (t *TenantAction) GetTenantsResources(ctx context.Context, tr *api_model.Te return result, nil } -//TenantResourceStats tenant resource stats +// TenantResourceStats tenant resource stats type TenantResourceStats struct { TenantID string `json:"tenant_id,omitempty"` CPURequest int64 `json:"cpu_request,omitempty"` @@ -388,7 +397,7 @@ type TenantResourceStats struct { RunningApplications int64 `json:"running_applications"` } -//GetTenantResource get tenant resource +// GetTenantResource get tenant resource func (t *TenantAction) GetTenantResource(tenantID string) (ts TenantResourceStats, err error) { tr, err := t.statusCli.GetTenantResource(tenantID) if err != nil { @@ -415,7 +424,7 @@ type PodResourceInformation struct { StorageEphemeral int64 } -//ClusterResourceStats cluster resource stats +// ClusterResourceStats cluster resource stats type ClusterResourceStats struct { AllCPU int64 AllMemory int64 @@ -507,7 +516,7 @@ func (t *TenantAction) GetAllocatableResources(ctx context.Context) (*ClusterRes return re, nil } -//GetServicesResources Gets the resource usage of the specified service. +// GetServicesResources Gets the resource usage of the specified service. func (t *TenantAction) GetServicesResources(tr *api_model.ServicesResources) (re map[string]map[string]interface{}, err error) { status := t.statusCli.GetStatuss(strings.Join(tr.Body.ServiceIDs, ",")) var running, closed []string @@ -567,7 +576,7 @@ func (t *TenantAction) getPodNums(serviceID string) int { return len(pods.OldPods) + len(pods.NewPods) } -//TenantsSum TenantsSum +// TenantsSum TenantsSum func (t *TenantAction) TenantsSum() (int, error) { s, err := db.GetManager().TenantDao().GetALLTenants("") if err != nil { @@ -576,7 +585,7 @@ func (t *TenantAction) TenantsSum() (int, error) { return len(s), nil } -//GetProtocols GetProtocols +// GetProtocols GetProtocols func (t *TenantAction) GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHandleError) { return []*dbmodel.RegionProcotols{ { @@ -609,7 +618,7 @@ func (t *TenantAction) GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHand }, nil } -//TransPlugins TransPlugins +// TransPlugins TransPlugins func (t *TenantAction) TransPlugins(tenantID, tenantName, fromTenant string, pluginList []string) *util.APIHandleError { tenantInfo, err := db.GetManager().TenantDao().GetTenantIDByName(fromTenant) if err != nil { @@ -651,12 +660,12 @@ func (t *TenantAction) GetServicesStatus(ids string) map[string]string { return t.statusCli.GetStatuss(ids) } -//IsClosedStatus checks if the status is closed status. +// IsClosedStatus checks if the status is closed status. func (t *TenantAction) IsClosedStatus(status string) bool { return t.statusCli.IsClosedStatus(status) } -//GetClusterResource get cluster resource +// GetClusterResource get cluster resource func (t *TenantAction) GetClusterResource(ctx context.Context) *ClusterResourceStats { if t.initClusterResource(ctx) != nil { return nil diff --git a/api/handler/tenant_handler.go b/api/handler/tenant_handler.go index 95c2237360..ccfb1d8443 100644 --- a/api/handler/tenant_handler.go +++ b/api/handler/tenant_handler.go @@ -27,11 +27,12 @@ import ( dbmodel "github.com/goodrain/rainbond/db/model" ) -//TenantHandler tenant handler +// TenantHandler tenant handler type TenantHandler interface { GetTenants(query string) ([]*dbmodel.Tenants, error) GetTenantsByName(name string) (*dbmodel.Tenants, error) GetTenantsByEid(eid, query string) ([]*dbmodel.Tenants, error) + GetTenantsByTenantIDs(tenantIDS []string) ([]*dbmodel.Tenants, error) GetTenantsByUUID(uuid string) (*dbmodel.Tenants, error) GetTenantsName() ([]string, error) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) diff --git a/db/dao/dao.go b/db/dao/dao.go index 6ded09e4c2..66c4ab3952 100644 --- a/db/dao/dao.go +++ b/db/dao/dao.go @@ -53,6 +53,7 @@ type TenantDao interface { GetTenantByUUID(uuid string) (*model.Tenants, error) GetTenantIDByName(tenantName string) (*model.Tenants, error) GetALLTenants(query string) ([]*model.Tenants, error) + GetTenantByTenantIDs(tenantIDs []string) ([]*model.Tenants, error) GetTenantByEid(eid, query string) ([]*model.Tenants, error) GetPagedTenants(offset, len int) ([]*model.Tenants, error) GetTenantIDsByNames(names []string) ([]string, error) diff --git a/db/mysql/dao/tenants.go b/db/mysql/dao/tenants.go index cc77885a53..56f88bcf10 100644 --- a/db/mysql/dao/tenants.go +++ b/db/mysql/dao/tenants.go @@ -120,6 +120,15 @@ func (t *TenantDaoImpl) GetTenantByEid(eid, query string) ([]*model.Tenants, err return tenants, nil } +// GetTenantByTenantIDs get tenants by tenant ids +func (t *TenantDaoImpl) GetTenantByTenantIDs(tenantIDs []string) ([]*model.Tenants, error) { + var tenants []*model.Tenants + if err := t.DB.Where("uuid IN ?", tenantIDs).Find(&tenants).Error; err != nil { + return nil, err + } + return tenants, nil +} + // GetTenantIDsByNames get tenant ids by names func (t *TenantDaoImpl) GetTenantIDsByNames(names []string) (re []string, err error) { rows, err := t.DB.Raw("select uuid from tenants where name in (?)", names).Rows()