mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
fix(region): show worker and tasks list by service type
This commit is contained in:
@@ -15,15 +15,18 @@
|
||||
package compute
|
||||
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
|
||||
)
|
||||
|
||||
func init() {
|
||||
type WorkerListOptions struct {
|
||||
ServiceType string `choices:"image|cloudid|cloudevent|devtool|ansible|identity|notify|log|compute|compute_v2"`
|
||||
}
|
||||
R(&WorkerListOptions{}, "worker-list", "List workers", func(s *mcclient.ClientSession, args *WorkerListOptions) error {
|
||||
result, err := modules.Workers.List(s, nil)
|
||||
result, err := modules.Workers.List(s, jsonutils.Marshal(args))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -37,10 +37,10 @@ func init() {
|
||||
return nil
|
||||
})
|
||||
|
||||
type TaskShowOptions struct {
|
||||
type RegionTaskShowOptions struct {
|
||||
ID string `help:"ID or name of the task"`
|
||||
}
|
||||
R(&TaskShowOptions{}, "region-task-show", "Show details of a region task", func(s *mcclient.ClientSession, args *TaskShowOptions) error {
|
||||
R(&RegionTaskShowOptions{}, "region-task-show", "Show details of a region task", func(s *mcclient.ClientSession, args *RegionTaskShowOptions) error {
|
||||
result, err := compute.ComputeTasks.Get(s, args.ID, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -48,4 +48,38 @@ func init() {
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
|
||||
type TaskListOptions struct {
|
||||
ObjName string `help:"object name"`
|
||||
ObjId string `help:"object id"`
|
||||
TaskName string `help:"task name"`
|
||||
ServiceType string `choices:"image|cloudid|cloudevent|devtool|ansible|identity|notify|log|compute|compute_v2"`
|
||||
}
|
||||
R(&TaskListOptions{}, "task-list", "List tasks", func(s *mcclient.ClientSession, args *TaskListOptions) error {
|
||||
params := jsonutils.Marshal(args)
|
||||
man := compute.TasksManager{}
|
||||
result, err := man.List(s, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printList(result, man.GetColumns(s))
|
||||
return nil
|
||||
})
|
||||
|
||||
type TaskShowOptions struct {
|
||||
ID string `help:"ID or name of the task"`
|
||||
ServiceType string `choices:"image|cloudid|cloudevent|devtool|ansible|identity|notify|log|compute|compute_v2"`
|
||||
}
|
||||
|
||||
R(&TaskShowOptions{}, "task-show", "Show details of a task", func(s *mcclient.ClientSession, args *TaskShowOptions) error {
|
||||
man := compute.TasksManager{}
|
||||
params := jsonutils.Marshal(args)
|
||||
result, err := man.Get(s, args.ID, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(result)
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ package modulebase
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
)
|
||||
|
||||
@@ -33,3 +35,22 @@ func GetVersion(s *mcclient.ClientSession, serviceType string) (string, error) {
|
||||
}
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func ListWorkers(s *mcclient.ClientSession, serviceType string) (*ListResult, error) {
|
||||
man := NewBaseManager(serviceType, "", "", nil, nil)
|
||||
resp, err := man.rawBaseUrlRequest(s, "GET", "/worker_stats", nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := ListResult{}
|
||||
if workers, _ := jsonutils.Parse(body); workers != nil {
|
||||
workers.Unmarshal(&ret.Data, "workers")
|
||||
ret.Total = len(ret.Data)
|
||||
}
|
||||
return &ret, nil
|
||||
}
|
||||
@@ -17,47 +17,51 @@ package compute
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
"yunion.io/x/onecloud/pkg/httperrors"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
var (
|
||||
ComputeTasks ComputeTasksManager
|
||||
DevtoolTasks modulebase.ResourceManager
|
||||
ComputeTasks TasksManager
|
||||
DevtoolTasks TasksManager
|
||||
)
|
||||
|
||||
type ComputeTasksManager struct {
|
||||
type TasksManager struct {
|
||||
modulebase.ResourceManager
|
||||
}
|
||||
|
||||
func init() {
|
||||
ComputeTasks = ComputeTasksManager{
|
||||
ComputeTasks = TasksManager{
|
||||
ResourceManager: modules.NewComputeManager("task", "tasks",
|
||||
[]string{},
|
||||
[]string{"Id", "Obj_name", "Obj_Id", "Task_name", "Stage", "Created_at"}),
|
||||
}
|
||||
modules.RegisterCompute(&ComputeTasks)
|
||||
|
||||
DevtoolTasks = modules.NewDevtoolManager("task", "tasks",
|
||||
[]string{},
|
||||
[]string{"Id", "Obj_name", "Obj_Id", "Task_name", "Stage", "Created_at"},
|
||||
)
|
||||
DevtoolTasks = TasksManager{
|
||||
ResourceManager: modules.NewDevtoolManager("task", "tasks",
|
||||
[]string{},
|
||||
[]string{"Id", "Obj_name", "Obj_Id", "Task_name", "Stage", "Created_at"},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (man ComputeTasksManager) TaskComplete(session *mcclient.ClientSession, taskId string, params jsonutils.JSONObject) {
|
||||
func (man TasksManager) TaskComplete(session *mcclient.ClientSession, taskId string, params jsonutils.JSONObject) {
|
||||
modules.TaskComplete(&man, session, taskId, params)
|
||||
}
|
||||
|
||||
func (man ComputeTasksManager) TaskFailed(session *mcclient.ClientSession, taskId string, err error) {
|
||||
func (man TasksManager) TaskFailed(session *mcclient.ClientSession, taskId string, err error) {
|
||||
man.TaskFailed2(session, taskId, err.Error())
|
||||
}
|
||||
|
||||
func (man ComputeTasksManager) TaskFailed2(session *mcclient.ClientSession, taskId string, reason string) {
|
||||
func (man TasksManager) TaskFailed2(session *mcclient.ClientSession, taskId string, reason string) {
|
||||
man.TaskFailed3(session, taskId, reason, nil)
|
||||
}
|
||||
|
||||
func (man ComputeTasksManager) TaskFailed3(session *mcclient.ClientSession, taskId string, reason string, params *jsonutils.JSONDict) {
|
||||
func (man TasksManager) TaskFailed3(session *mcclient.ClientSession, taskId string, reason string, params *jsonutils.JSONDict) {
|
||||
if params == nil {
|
||||
params = jsonutils.NewDict()
|
||||
}
|
||||
@@ -65,3 +69,39 @@ func (man ComputeTasksManager) TaskFailed3(session *mcclient.ClientSession, task
|
||||
params.Add(jsonutils.NewString(reason), "__reason__")
|
||||
man.TaskComplete(session, taskId, params)
|
||||
}
|
||||
|
||||
func (self *TasksManager) getManager(session *mcclient.ClientSession, params jsonutils.JSONObject) (*modulebase.ResourceManager, error) {
|
||||
serviceType := apis.SERVICE_TYPE_REGION
|
||||
if params.Contains("service_type") {
|
||||
serviceType, _ = params.GetString("service_type")
|
||||
}
|
||||
|
||||
version := ""
|
||||
switch serviceType {
|
||||
case apis.SERVICE_TYPE_KEYSTONE:
|
||||
version = "v3"
|
||||
case apis.SERVICE_TYPE_REGION, apis.SERVICE_TYPE_NOTIFY:
|
||||
version = "v2"
|
||||
case apis.SERVICE_TYPE_IMAGE:
|
||||
version = "v1"
|
||||
}
|
||||
|
||||
session.SetApiVersion(version)
|
||||
_, err := session.GetServiceURL(serviceType, "")
|
||||
if err != nil {
|
||||
return nil, httperrors.NewNotFoundError("service %s not found error: %v", serviceType, err)
|
||||
}
|
||||
|
||||
return &modulebase.ResourceManager{
|
||||
BaseManager: *modulebase.NewBaseManager(serviceType, "", version, []string{}, []string{}),
|
||||
Keyword: "task", KeywordPlural: "tasks",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (this *TasksManager) List(session *mcclient.ClientSession, params jsonutils.JSONObject) (*modulebase.ListResult, error) {
|
||||
man, err := this.getManager(session, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return man.List(session, params)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package compute
|
||||
import (
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/apis"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modulebase"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
@@ -30,10 +31,6 @@ var (
|
||||
Workers WorkerManager
|
||||
)
|
||||
|
||||
func (this *WorkerManager) List(s *mcclient.ClientSession, params jsonutils.JSONObject) (*modulebase.ListResult, error) {
|
||||
return modulebase.List(this.ResourceManager, s, this.KeywordPlural, this.Keyword)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Workers = WorkerManager{modules.NewComputeManager("workers", "worker_stats",
|
||||
[]string{"name", "queue_cnt", "active_worker_cnt", "backlog", "detach_worker_cnt", "max_worker_cnt"},
|
||||
@@ -41,3 +38,11 @@ func init() {
|
||||
|
||||
modules.RegisterCompute(&Workers)
|
||||
}
|
||||
|
||||
func (this *WorkerManager) List(s *mcclient.ClientSession, params jsonutils.JSONObject) (*modulebase.ListResult, error) {
|
||||
serviceType := apis.SERVICE_TYPE_REGION + "_v2"
|
||||
if params.Contains("service_type") {
|
||||
serviceType, _ = params.GetString("service_type")
|
||||
}
|
||||
return modulebase.ListWorkers(s, serviceType)
|
||||
}
|
||||
|
||||
@@ -22,3 +22,7 @@ import (
|
||||
func GetVersion(s *mcclient.ClientSession, serviceType string) (string, error) {
|
||||
return modulebase.GetVersion(s, serviceType)
|
||||
}
|
||||
|
||||
func ListWorkers(s *mcclient.ClientSession, serviceType string) (*modulebase.ListResult, error) {
|
||||
return modulebase.ListWorkers(s, serviceType)
|
||||
}
|
||||
Reference in New Issue
Block a user