[REV] optimize region api ,optimize unmarshal list obj

This commit is contained in:
bay1ts 2017-12-22 10:14:03 +08:00
parent 36bb33cb54
commit 3a46b8868f
5 changed files with 31 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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("..")

View File

@ -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)
}

View File

@ -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",