diff --git a/pkg/api/apiFunc/api_interface.go b/pkg/api/apiFunc/api_interface.go index 3204c4ead..a0ffc5855 100644 --- a/pkg/api/apiFunc/api_interface.go +++ b/pkg/api/apiFunc/api_interface.go @@ -29,6 +29,7 @@ type TenantInterface interface { TenantResources(w http.ResponseWriter, r *http.Request) Tenant(w http.ResponseWriter, r *http.Request) ServicesInfo(w http.ResponseWriter, r *http.Request) + TenantsWithResource(w http.ResponseWriter, r *http.Request) SumTenants(w http.ResponseWriter, r *http.Request) SingleTenantResources(w http.ResponseWriter, r *http.Request) } diff --git a/pkg/api/apiRouters/version2/v2Routers.go b/pkg/api/apiRouters/version2/v2Routers.go index 0b049c2e7..99bb07265 100644 --- a/pkg/api/apiRouters/version2/v2Routers.go +++ b/pkg/api/apiRouters/version2/v2Routers.go @@ -185,6 +185,8 @@ func (v2 *V2) resourcesRouter() chi.Router { r := chi.NewRouter() r.Post("/tenants", controller.GetManager().TenantResources) r.Get("/tenants/sum", controller.GetManager().SumTenants) + //tenants's resource + r.Get("/tenants/res", controller.GetManager().TenantsWithResource) return r } diff --git a/pkg/api/controller/resources.go b/pkg/api/controller/resources.go index 06172062d..a13203428 100644 --- a/pkg/api/controller/resources.go +++ b/pkg/api/controller/resources.go @@ -132,6 +132,59 @@ func (t *TenantStruct) TenantResources(w http.ResponseWriter, r *http.Request) { return } +//TenantsWithResource TenantsWithResource +func (t *TenantStruct) TenantsWithResource(w http.ResponseWriter, r *http.Request) { + // swagger:operation GET /v2/tenants v2 tenants + // + // 租户带资源列表 + // + // get tenant resources + // + // --- + // produces: + // - application/json + // - application/xml + // + // responses: + // default: + // schema: + // "$ref": "#/responses/commandResponse" + // description: 统一返回格式 + var tr api_model.TenantResources + ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &tr.Body, nil) + if !ok { + return + } + rep, err := handler.GetTenantManager().GetTenants() + if err != nil { + httputil.ReturnError(r, w, 500, fmt.Sprintf("get tenants error, %v", err)) + return + } + var result []*api_model.TenantResource + for _,v:=range rep{ + services, err := handler.GetServiceManager().GetService(v.UUID) + if err != nil { + httputil.ReturnError(r, w, 500, fmt.Sprintf("get services by tenantID %s error, %v",v.UUID, err)) + return + } + totalResInfo, _ := handler.GetTenantManager().TotalMemCPU(services) + usedResInfo, _ := handler.GetTenantManager().StatsMemCPU(services) + var res api_model.TenantResource + res.UUID=v.UUID + res.Name=v.Name + res.EID=v.EID + res.AllocatedCPU=totalResInfo.CPU + res.AllocatedMEM=totalResInfo.MEM + res.UsedCPU=usedResInfo.CPU + res.UsedMEM=usedResInfo.MEM + result=append(result,&res) + } + + httputil.ReturnSuccess(r, w, result) + return +} + + //SumTenants 统计租户数量 func (t *TenantStruct) SumTenants(w http.ResponseWriter, r *http.Request) { // swagger:operation GET /v2/resources/tenants/sum v2 sumTenants diff --git a/pkg/api/handler/TenantHandler.go b/pkg/api/handler/TenantHandler.go index 6c170a369..dedb00139 100644 --- a/pkg/api/handler/TenantHandler.go +++ b/pkg/api/handler/TenantHandler.go @@ -29,6 +29,7 @@ import ( type TenantHandler interface { GetTenants() ([]*dbmodel.Tenants, error) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) + TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) //QueryTsdb(md *api_model.MontiorData) (*tsdbClient.QueryResponse, error) HTTPTsdb(md *api_model.MontiorData) ([]byte, error) GetTenantsResources(tr *api_model.TenantResources) ([]*map[string]interface{}, error) diff --git a/pkg/api/handler/tenant.go b/pkg/api/handler/tenant.go index 9505f11b8..01e5bd25c 100644 --- a/pkg/api/handler/tenant.go +++ b/pkg/api/handler/tenant.go @@ -91,6 +91,24 @@ func (t *TenantAction) GetTenants() ([]*dbmodel.Tenants, error) { return tenants, err } + +//StatsMemCPU StatsMemCPU +func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error){ + cpus := 0 + mem := 0 + for _, service := range services { + + logrus.Debugf("service is %s, cpus is %v, mem is %v", service.ID, service.ContainerCPU, service.ContainerMemory) + cpus += service.ContainerCPU + mem += service.ContainerMemory + } + si := &api_model.StatsInfo{ + CPU: cpus, + MEM: mem, + } + return si, nil +} + //StatsMemCPU StatsMemCPU func (t *TenantAction) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) { cpus := 0 diff --git a/pkg/api/model/model.go b/pkg/api/model/model.go index 0e5d81fa9..01b8c9061 100644 --- a/pkg/api/model/model.go +++ b/pkg/api/model/model.go @@ -25,6 +25,21 @@ import ( dbmodel "github.com/goodrain/rainbond/pkg/db/model" ) + +//TenantResource path参数 +//swagger:parameters getVolumes getDepVolumes +type TenantResource struct { + AllocatedCPU int `json:"alloc_cpu"` + AllocatedMEM int `json:"alloc_memory"` + + UsedCPU int `json:"used_cpu"` + UsedMEM int `json:"used_memory"` + Name string `json:"name"` + UUID string `json:"uuid"` + EID string `json:"eid"` +} + + //ServiceGetCommon path参数 //swagger:parameters getVolumes getDepVolumes type ServiceGetCommon struct {