[ADD] add interface for manage service ,get all tenants with alloc res and used res from region

This commit is contained in:
bay1ts 2017-12-06 11:13:56 +08:00
parent 478ac0dfca
commit ee983c788d
6 changed files with 90 additions and 0 deletions

View File

@ -29,6 +29,7 @@ type TenantInterface interface {
TenantResources(w http.ResponseWriter, r *http.Request) TenantResources(w http.ResponseWriter, r *http.Request)
Tenant(w http.ResponseWriter, r *http.Request) Tenant(w http.ResponseWriter, r *http.Request)
ServicesInfo(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) SumTenants(w http.ResponseWriter, r *http.Request)
SingleTenantResources(w http.ResponseWriter, r *http.Request) SingleTenantResources(w http.ResponseWriter, r *http.Request)
} }

View File

@ -185,6 +185,8 @@ func (v2 *V2) resourcesRouter() chi.Router {
r := chi.NewRouter() r := chi.NewRouter()
r.Post("/tenants", controller.GetManager().TenantResources) r.Post("/tenants", controller.GetManager().TenantResources)
r.Get("/tenants/sum", controller.GetManager().SumTenants) r.Get("/tenants/sum", controller.GetManager().SumTenants)
//tenants's resource
r.Get("/tenants/res", controller.GetManager().TenantsWithResource)
return r return r
} }

View File

@ -132,6 +132,59 @@ func (t *TenantStruct) TenantResources(w http.ResponseWriter, r *http.Request) {
return 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 统计租户数量 //SumTenants 统计租户数量
func (t *TenantStruct) SumTenants(w http.ResponseWriter, r *http.Request) { func (t *TenantStruct) SumTenants(w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /v2/resources/tenants/sum v2 sumTenants // swagger:operation GET /v2/resources/tenants/sum v2 sumTenants

View File

@ -29,6 +29,7 @@ import (
type TenantHandler interface { type TenantHandler interface {
GetTenants() ([]*dbmodel.Tenants, error) GetTenants() ([]*dbmodel.Tenants, error)
StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, 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) //QueryTsdb(md *api_model.MontiorData) (*tsdbClient.QueryResponse, error)
HTTPTsdb(md *api_model.MontiorData) ([]byte, error) HTTPTsdb(md *api_model.MontiorData) ([]byte, error)
GetTenantsResources(tr *api_model.TenantResources) ([]*map[string]interface{}, error) GetTenantsResources(tr *api_model.TenantResources) ([]*map[string]interface{}, error)

View File

@ -91,6 +91,24 @@ func (t *TenantAction) GetTenants() ([]*dbmodel.Tenants, error) {
return tenants, err 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 //StatsMemCPU StatsMemCPU
func (t *TenantAction) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) { func (t *TenantAction) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {
cpus := 0 cpus := 0

View File

@ -25,6 +25,21 @@ import (
dbmodel "github.com/goodrain/rainbond/pkg/db/model" 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参数 //ServiceGetCommon path参数
//swagger:parameters getVolumes getDepVolumes //swagger:parameters getVolumes getDepVolumes
type ServiceGetCommon struct { type ServiceGetCommon struct {