Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modify team query logic #1927

Open
wants to merge 1 commit into
base: V5.17
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions api/controller/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions api/handler/service_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
51 changes: 30 additions & 21 deletions api/handler/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
Expand All @@ -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 {
Expand All @@ -415,7 +424,7 @@ type PodResourceInformation struct {
StorageEphemeral int64
}

//ClusterResourceStats cluster resource stats
// ClusterResourceStats cluster resource stats
type ClusterResourceStats struct {
AllCPU int64
AllMemory int64
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion api/handler/tenant_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions db/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions db/mysql/dao/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading