From 3a46b8868f1d0c5d9567ec7d936935f594d48a7b Mon Sep 17 00:00:00 2001 From: bay1ts Date: Fri, 22 Dec 2017 10:14:03 +0800 Subject: [PATCH] [REV] optimize region api ,optimize unmarshal list obj --- pkg/api/region/node.go | 14 ++++++++------ pkg/api/region/region.go | 25 ++++++++++++++++--------- pkg/grctl/cmd/installStatus.go | 9 +++++---- pkg/grctl/cmd/node.go | 2 +- pkg/grctl/cmd/tenant.go | 2 +- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/pkg/api/region/node.go b/pkg/api/region/node.go index 21ec88be0..2bcd7a91e 100644 --- a/pkg/api/region/node.go +++ b/pkg/api/region/node.go @@ -57,11 +57,13 @@ type node struct { type TaskInterface interface { Get(name string) (*model.Task, *util.APIHandleError) Status(name string) (*TaskStatus, error) + HandleTaskStatus(task string) (*map[string]*model.TaskStatus,*util.APIHandleError) Add(task *model.Task) *util.APIHandleError AddGroup(group *model.TaskGroup) *util.APIHandleError Exec(name string, nodes []string) *util.APIHandleError List() ([]*model.Task, *util.APIHandleError) Refresh() *util.APIHandleError + } type NodeInterface interface { Rule(rule string) ([]*model.HostNode,*util.APIHandleError) @@ -150,7 +152,7 @@ func (n *node) Rule(rule string) ([]*model.HostNode,*util.APIHandleError) { } var res utilhttp.ResponseBody var gc []*model.HostNode - res.List = &gc + res.List = gc if err := ffjson.Unmarshal(body, &res); err != nil { return nil, util.CreateAPIHandleError(code,err) } @@ -169,7 +171,7 @@ func (n *node) List() ([]*model.HostNode,*util.APIHandleError) { } var res utilhttp.ResponseBody var gc []*model.HostNode - res.List = &gc + res.List = gc if err := ffjson.Unmarshal(body, &res); err != nil { return nil, util.CreateAPIHandleError(code,err) } @@ -256,7 +258,7 @@ func (t *task) List() ([]*model.Task, *util.APIHandleError) { } var res utilhttp.ResponseBody var gc []*model.Task - res.List = &gc + res.List = gc if err := ffjson.Unmarshal(body, &res); err != nil { return nil, util.CreateAPIHandleError(code,err) } @@ -309,7 +311,7 @@ func (t *task) Status(name string) (*TaskStatus, error) { return HandleTaskStatus(taskId) } -func (t *task)HandleTaskStatus(task string) (*map[string]TaskStatus,*util.APIHandleError) { +func (t *task)HandleTaskStatus(task string) (*map[string]*model.TaskStatus,*util.APIHandleError) { body, code, err := nodeclient.Request("/tasks/"+task+"/status", "GET", nil) if err != nil { return nil, util.CreateAPIHandleError(code,err) @@ -318,12 +320,12 @@ func (t *task)HandleTaskStatus(task string) (*map[string]TaskStatus,*util.APIHan return nil, util.CreateAPIHandleError(code,fmt.Errorf("get task with code %d", code)) } var res utilhttp.ResponseBody - var gc map[string]TaskStatus + var gc map[string]*model.TaskStatus res.Bean = &gc if err := ffjson.Unmarshal(body, &res); err != nil { return nil, util.CreateAPIHandleError(code,err) } - if gc, ok := res.Bean.(*map[string]TaskStatus); ok { + if gc, ok := res.Bean.(*map[string]*model.TaskStatus); ok { return gc, nil } return nil, nil diff --git a/pkg/api/region/region.go b/pkg/api/region/region.go index 1ba83737a..a68257b3d 100644 --- a/pkg/api/region/region.go +++ b/pkg/api/region/region.go @@ -32,6 +32,7 @@ import ( "github.com/goodrain/rainbond/pkg/api/util" "fmt" "github.com/pquerna/ffjson/ffjson" + "github.com/Sirupsen/logrus" ) var regionAPI, token string @@ -89,7 +90,7 @@ type ServiceInterface interface { } func (s *services) Pods(serviceAlisa string) ([]*dbmodel.K8sPod, *util.APIHandleError) { - body, code, err := request("/v2"+s.tenant.prefix+s.tenant.tenantID+"/"+serviceAlisa+"/pods", "GET", nil) + body, code, err := request("/v2"+s.tenant.prefix+"/"+s.tenant.tenantID+"/"+s.prefix+"/"+serviceAlisa+"/pods", "GET", nil) if err != nil { return nil, util.CreateAPIHandleError(code,err) } @@ -98,7 +99,7 @@ func (s *services) Pods(serviceAlisa string) ([]*dbmodel.K8sPod, *util.APIHandle } var res utilhttp.ResponseBody var gc []*dbmodel.K8sPod - res.List = &gc + res.List = gc if err := ffjson.Unmarshal(body, &res); err != nil { return nil, util.CreateAPIHandleError(code,err) } @@ -113,7 +114,7 @@ func (s *services) Get(name string) (map[string]string,*util.APIHandleError) { return nil, util.CreateAPIHandleError(code,err) } if code != 200 { - return nil, util.CreateAPIHandleError(code,fmt.Errorf("Get database center configs code %d", code)) + return nil, util.CreateAPIHandleError(code,fmt.Errorf("Get err with code %d", code)) } j, err := simplejson.NewJson(body) if err != nil { @@ -142,7 +143,7 @@ func (s *services) EventLog(serviceAlisa, eventID, level string) ([]*model.Messa } var res utilhttp.ResponseBody var gc []*model.MessageData - res.List = &gc + res.List = gc if err := ffjson.Unmarshal(body, &res); err != nil { return nil, util.CreateAPIHandleError(code,err) } @@ -153,37 +154,43 @@ func (s *services) EventLog(serviceAlisa, eventID, level string) ([]*model.Messa } func (s *services) List() ([]*model.ServiceStruct,*util.APIHandleError) { - body, code, err := request("/v2"+s.tenant.prefix+"/"+s.tenant.tenantID+s.prefix, "GET", nil) + body, code, err := request("/v2"+s.tenant.prefix+"/"+s.tenant.tenantID+"/"+s.prefix, "GET", nil) if err != nil { return nil, util.CreateAPIHandleError(code,err) } if code != 200 { - return nil, util.CreateAPIHandleError(code,fmt.Errorf("Get database center configs code %d", code)) + return nil, util.CreateAPIHandleError(code,fmt.Errorf("Get with code %d", code)) } var res utilhttp.ResponseBody var gc []*model.ServiceStruct - res.List = &gc + res.List = gc + logrus.Infof("res is %v",res) if err := ffjson.Unmarshal(body, &res); err != nil { return nil, util.CreateAPIHandleError(code,err) } + logrus.Infof("after unmarshal res is %v",res) if gc, ok := res.List.([]*model.ServiceStruct); ok { return gc, nil + }else{ + c:=res.List.([]*model.ServiceStruct) + logrus.Infof("response is %v",c) } return nil, nil } func (s *services) Stop(name, eventID string) *util.APIHandleError { data := []byte(`{"event_id":"` + eventID + `"}`) - _, code, err := request("/v2"+s.tenant.prefix+"/"+s.tenant.tenantID+s.prefix+name+"/stop", "POST", data) + _, code, err := request("/v2"+s.tenant.prefix+"/"+s.tenant.tenantID+"/"+s.prefix+"/"+name+"/stop", "POST", data) return handleErrAndCode(err,code) } func (s *services) Start(name, eventID string) *util.APIHandleError { data := []byte(`{"event_id":"` + eventID + `"}`) - _, code, err := request("/v2"+s.tenant.prefix+"/"+s.tenant.tenantID+s.prefix+name+"/start", "POST", data) + _, code, err := request("/v2"+s.tenant.prefix+"/"+s.tenant.tenantID+"/"+s.prefix+"/"+name+"/start", "POST", data) return handleErrAndCode(err,code) } func request(url, method string, body []byte) ([]byte, int, error) { + logrus.Infof("req url is %s",region.regionAPI+url) request, err := http.NewRequest(method, region.regionAPI+url, bytes.NewBuffer(body)) if err != nil { return nil, 500, err diff --git a/pkg/grctl/cmd/installStatus.go b/pkg/grctl/cmd/installStatus.go index 271b47023..3e3887ecf 100644 --- a/pkg/grctl/cmd/installStatus.go +++ b/pkg/grctl/cmd/installStatus.go @@ -204,13 +204,14 @@ func Status(task string, nodes []string) { checkFail += 1 continue } - status,error:=clients.NodeClient.Tasks().Status(task) - if error != nil||status==nil { - logrus.Warnf("error get task %s status,retry",task) + //status,error:=clients.NodeClient.Tasks().Status(task) + status,err:=clients.NodeClient.Tasks().HandleTaskStatus(task) + if err != nil||status==nil { + logrus.Warnf("error get task %s status,details %s,retry",task,err.String()) checkFail+=1 continue } - for k,v:=range status.Status{ + for k,v:=range *status{ //不是当前任务需要检测的status if !set[k] { fmt.Print("..") diff --git a/pkg/grctl/cmd/node.go b/pkg/grctl/cmd/node.go index aeb572022..516f37140 100644 --- a/pkg/grctl/cmd/node.go +++ b/pkg/grctl/cmd/node.go @@ -40,7 +40,7 @@ import ( ) func handleErr(err *util.APIHandleError) { - if err != nil { + if err != nil&&err.Err!=nil { fmt.Println(err.String()) os.Exit(1) } diff --git a/pkg/grctl/cmd/tenant.go b/pkg/grctl/cmd/tenant.go index ac2dbacdb..794a34bb9 100644 --- a/pkg/grctl/cmd/tenant.go +++ b/pkg/grctl/cmd/tenant.go @@ -47,7 +47,7 @@ func NewCmdTenant() cli.Command { }, cli.Command{ Name: "batchstop", - Usage: "批量停止租户应用。grctl service batchstop tenant_name", + Usage: "批量停止租户应用。grctl tenant batchstop tenant_name", Flags: []cli.Flag{ cli.BoolFlag{ Name: "f",