2018-03-14 14:12:26 +08:00
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2018-03-14 14:07:12 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2018-03-14 14:07:12 +08:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version. For any non-GPL usage of Rainbond,
|
|
|
|
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
|
|
|
|
// must be obtained first.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2018-03-14 14:07:12 +08:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2018-03-14 14:07:12 +08:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-11-22 11:40:14 +08:00
|
|
|
package region
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2017-11-23 15:05:46 +08:00
|
|
|
"fmt"
|
2017-12-21 17:09:17 +08:00
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/node/api/model"
|
2018-07-16 15:30:53 +08:00
|
|
|
"github.com/goodrain/rainbond/node/nodem/client"
|
2017-12-19 18:46:49 +08:00
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
2018-12-05 19:59:26 +08:00
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
//"github.com/goodrain/rainbond/grctl/cmd"
|
2017-12-21 17:09:17 +08:00
|
|
|
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/api/util"
|
|
|
|
utilhttp "github.com/goodrain/rainbond/util/http"
|
2017-11-22 11:40:14 +08:00
|
|
|
)
|
2017-12-19 18:46:49 +08:00
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
func (r *regionImpl) Tasks() TaskInterface {
|
|
|
|
return &task{regionImpl: *r, prefix: "/v2/tasks"}
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
func (r *regionImpl) Nodes() NodeInterface {
|
|
|
|
return &node{regionImpl: *r, prefix: "/v2/nodes"}
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
func (r *regionImpl) Configs() ConfigsInterface {
|
|
|
|
return &configs{regionImpl: *r, prefix: "/v2/configs"}
|
2017-12-20 13:48:49 +08:00
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
2017-12-21 16:44:31 +08:00
|
|
|
type task struct {
|
2018-07-17 16:18:19 +08:00
|
|
|
regionImpl
|
2017-12-21 16:44:31 +08:00
|
|
|
prefix string
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
type node struct {
|
2018-07-17 16:18:19 +08:00
|
|
|
regionImpl
|
2017-12-21 16:44:31 +08:00
|
|
|
prefix string
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-20 13:48:49 +08:00
|
|
|
|
2018-07-16 15:30:53 +08:00
|
|
|
func (n *node) Get(node string) (*client.HostNode, *util.APIHandleError) {
|
2017-12-21 16:44:31 +08:00
|
|
|
var res utilhttp.ResponseBody
|
2018-07-16 15:30:53 +08:00
|
|
|
var gc client.HostNode
|
2017-12-21 16:44:31 +08:00
|
|
|
res.Bean = &gc
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/"+node, "GET", nil, &res)
|
2017-11-22 18:46:17 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
if code != 200 {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return &gc, nil
|
|
|
|
}
|
2018-08-22 16:06:28 +08:00
|
|
|
|
2018-08-22 19:16:54 +08:00
|
|
|
func (n *node) GetNodeResource(node string) (*client.NodePodResource, *util.APIHandleError) {
|
2018-08-22 16:06:28 +08:00
|
|
|
var res utilhttp.ResponseBody
|
2018-08-22 19:16:54 +08:00
|
|
|
var gc client.NodePodResource
|
2018-08-22 16:06:28 +08:00
|
|
|
res.Bean = &gc
|
2018-08-22 19:16:54 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/"+node+"/resource", "GET", nil, &res)
|
2018-08-22 16:06:28 +08:00
|
|
|
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 &gc, nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
func (n *node) GetNodeByRule(rule string) ([]*client.HostNode, *util.APIHandleError) {
|
2017-12-21 16:44:31 +08:00
|
|
|
var res utilhttp.ResponseBody
|
2018-07-16 15:30:53 +08:00
|
|
|
var gc []*client.HostNode
|
2017-12-22 11:17:13 +08:00
|
|
|
res.List = &gc
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/rule/"+rule, "GET", nil, &res)
|
2017-11-27 15:18:05 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
if code != 200 {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return gc, nil
|
|
|
|
}
|
|
|
|
func (n *node) List() ([]*client.HostNode, *util.APIHandleError) {
|
2017-12-21 16:44:31 +08:00
|
|
|
var res utilhttp.ResponseBody
|
2018-07-16 15:30:53 +08:00
|
|
|
var gc []*client.HostNode
|
2017-12-22 11:17:13 +08:00
|
|
|
res.List = &gc
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix, "GET", nil, &res)
|
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
if code != 200 {
|
|
|
|
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return gc, nil
|
2017-11-27 15:18:05 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
|
2018-10-30 15:59:25 +08:00
|
|
|
func (n *node) GetAllNodeHealth() (map[string][]map[string]string, *util.APIHandleError) {
|
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
var gc map[string][]map[string]string
|
|
|
|
res.Bean = &gc
|
2018-12-05 19:59:26 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/all_node_health", "GET", nil, &res)
|
2018-10-30 15:59:25 +08:00
|
|
|
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 gc, nil
|
|
|
|
}
|
|
|
|
|
2018-07-16 15:30:53 +08:00
|
|
|
func (n *node) Add(node *client.APIHostNode) *util.APIHandleError {
|
2017-12-21 16:44:31 +08:00
|
|
|
body, err := json.Marshal(node)
|
2017-11-22 18:46:17 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(400, err)
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
2018-12-08 17:19:53 +08:00
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
code, err := n.DoRequest(n.prefix, "POST", bytes.NewBuffer(body), &res)
|
2018-07-17 16:18:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
2018-12-08 17:19:53 +08:00
|
|
|
return handleAPIResult(code, res)
|
2017-12-21 16:44:31 +08:00
|
|
|
}
|
2018-12-05 19:59:26 +08:00
|
|
|
func (n *node) Label(nid string) NodeLabelInterface {
|
|
|
|
return &nodeLabelImpl{nodeImpl: n, NodeID: nid}
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeLabelImpl struct {
|
|
|
|
nodeImpl *node
|
|
|
|
NodeID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nl *nodeLabelImpl) List() (map[string]string, *util.APIHandleError) {
|
|
|
|
var decode map[string]string
|
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
res.Bean = &decode
|
|
|
|
code, err := nl.nodeImpl.DoRequest(nl.nodeImpl.prefix+"/"+nl.NodeID+"/labels", "GET", nil, &res)
|
|
|
|
if err != nil || code != 200 {
|
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return decode, nil
|
|
|
|
}
|
|
|
|
func (nl *nodeLabelImpl) Delete(k string) *util.APIHandleError {
|
|
|
|
var decode map[string]string
|
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
res.Bean = &decode
|
|
|
|
code, err := nl.nodeImpl.DoRequest(nl.nodeImpl.prefix+"/"+nl.NodeID+"/labels", "GET", nil, &res)
|
|
|
|
if err != nil || code != 200 {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
delete(decode, k)
|
|
|
|
body, err := json.Marshal(decode)
|
2017-11-22 18:46:17 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(400, err)
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
2018-12-05 19:59:26 +08:00
|
|
|
code, err = nl.nodeImpl.DoRequest(nl.nodeImpl.prefix+"/"+nl.NodeID+"/labels", "PUT", bytes.NewBuffer(body), &res)
|
|
|
|
if err != nil || code != 200 {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (nl *nodeLabelImpl) Add(k, v string) *util.APIHandleError {
|
|
|
|
var decode map[string]string
|
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
res.Bean = &decode
|
2018-12-05 22:21:19 +08:00
|
|
|
code, err := nl.nodeImpl.DoRequest(nl.nodeImpl.prefix+"/"+nl.NodeID+"/labels", "GET", nil, &res)
|
2018-12-05 19:59:26 +08:00
|
|
|
if err != nil || code != 200 {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
decode[k] = v
|
|
|
|
body, err := json.Marshal(decode)
|
2018-07-17 16:18:19 +08:00
|
|
|
if err != nil {
|
2018-12-05 19:59:26 +08:00
|
|
|
return util.CreateAPIHandleError(400, err)
|
|
|
|
}
|
2018-12-05 22:21:19 +08:00
|
|
|
code, err = nl.nodeImpl.DoRequest(nl.nodeImpl.prefix+"/"+nl.NodeID+"/labels", "PUT", bytes.NewBuffer(body), &res)
|
2018-12-05 19:59:26 +08:00
|
|
|
if err != nil || code != 200 {
|
2018-07-17 16:18:19 +08:00
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-11-22 18:46:17 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
|
2017-12-22 18:16:16 +08:00
|
|
|
func (n *node) Delete(nid string) *util.APIHandleError {
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/"+nid, "DELETE", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
2018-07-17 17:56:27 +08:00
|
|
|
if code != 200 {
|
|
|
|
return util.CreateAPIHandleError(code, fmt.Errorf("delete node error"))
|
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return nil
|
2017-12-21 16:44:31 +08:00
|
|
|
}
|
2018-07-17 17:56:27 +08:00
|
|
|
|
2017-12-21 16:44:31 +08:00
|
|
|
func (n *node) Up(nid string) *util.APIHandleError {
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/"+nid+"/up", "POST", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-12-21 16:44:31 +08:00
|
|
|
}
|
|
|
|
func (n *node) Down(nid string) *util.APIHandleError {
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/"+nid+"/down", "POST", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-12-21 16:44:31 +08:00
|
|
|
}
|
2017-12-22 18:16:16 +08:00
|
|
|
func (n *node) UnSchedulable(nid string) *util.APIHandleError {
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/"+nid+"/unschedulable", "PUT", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-12-21 16:44:31 +08:00
|
|
|
}
|
2017-12-22 18:16:16 +08:00
|
|
|
func (n *node) ReSchedulable(nid string) *util.APIHandleError {
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := n.DoRequest(n.prefix+"/"+nid+"/reschedulable", "PUT", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-12-21 16:44:31 +08:00
|
|
|
}
|
2018-12-08 17:44:02 +08:00
|
|
|
func (n *node) Install(nid string) *util.APIHandleError {
|
|
|
|
code, err := n.DoRequest(n.prefix+"/"+nid+"/install", "POST", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
type configs struct {
|
|
|
|
regionImpl
|
|
|
|
prefix string
|
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
func (c *configs) Get() (*model.GlobalConfig, *util.APIHandleError) {
|
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
var gc = model.GlobalConfig{
|
|
|
|
Configs: make(map[string]*model.ConfigUnit),
|
|
|
|
}
|
|
|
|
res.Bean = &gc
|
|
|
|
code, err := c.DoRequest(c.prefix+"/datacenter", "GET", nil, &res)
|
2017-12-19 18:46:49 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-11-28 16:33:11 +08:00
|
|
|
}
|
2017-11-29 09:43:37 +08:00
|
|
|
if code != 200 {
|
2018-07-17 16:18:19 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
|
2017-11-28 16:33:11 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return &gc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *configs) Put(gc *model.GlobalConfig) *util.APIHandleError {
|
|
|
|
rebody, err := ffjson.Marshal(gc)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(400, err)
|
|
|
|
}
|
|
|
|
code, err := c.DoRequest(c.prefix+"/datacenter", "PUT", bytes.NewBuffer(rebody), nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
if code != 200 {
|
|
|
|
return util.CreateAPIHandleError(code, fmt.Errorf("Put database center configs code %d", code))
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//TaskInterface task api
|
|
|
|
type TaskInterface interface {
|
|
|
|
Get(name string) (*model.Task, *util.APIHandleError)
|
|
|
|
GetTaskStatus(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)
|
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
//NodeInterface node api
|
|
|
|
type NodeInterface interface {
|
|
|
|
GetNodeByRule(rule string) ([]*client.HostNode, *util.APIHandleError)
|
|
|
|
Get(node string) (*client.HostNode, *util.APIHandleError)
|
2018-08-22 19:16:54 +08:00
|
|
|
GetNodeResource(node string) (*client.NodePodResource, *util.APIHandleError)
|
2018-07-17 16:18:19 +08:00
|
|
|
List() ([]*client.HostNode, *util.APIHandleError)
|
2018-10-30 15:59:25 +08:00
|
|
|
GetAllNodeHealth() (map[string][]map[string]string, *util.APIHandleError)
|
2018-07-17 16:18:19 +08:00
|
|
|
Add(node *client.APIHostNode) *util.APIHandleError
|
|
|
|
Up(nid string) *util.APIHandleError
|
|
|
|
Down(nid string) *util.APIHandleError
|
|
|
|
UnSchedulable(nid string) *util.APIHandleError
|
|
|
|
ReSchedulable(nid string) *util.APIHandleError
|
|
|
|
Delete(nid string) *util.APIHandleError
|
2018-12-05 19:59:26 +08:00
|
|
|
Label(nid string) NodeLabelInterface
|
2018-12-08 17:44:02 +08:00
|
|
|
Install(nid string) *util.APIHandleError
|
2018-12-05 19:59:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//NodeLabelInterface node label interface
|
|
|
|
type NodeLabelInterface interface {
|
|
|
|
Add(k, v string) *util.APIHandleError
|
|
|
|
Delete(k string) *util.APIHandleError
|
|
|
|
List() (map[string]string, *util.APIHandleError)
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2017-11-29 18:44:39 +08:00
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
//ConfigsInterface 数据中心配置API
|
|
|
|
type ConfigsInterface interface {
|
|
|
|
Get() (*model.GlobalConfig, *util.APIHandleError)
|
|
|
|
Put(*model.GlobalConfig) *util.APIHandleError
|
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
func (t *task) Get(id string) (*model.Task, *util.APIHandleError) {
|
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
var gc model.Task
|
|
|
|
res.Bean = &gc
|
|
|
|
code, err := t.DoRequest(t.prefix+"/"+id, "GET", nil, &res)
|
2017-12-19 18:46:49 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
if code != 200 {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return &gc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//List list all task
|
|
|
|
func (t *task) List() ([]*model.Task, *util.APIHandleError) {
|
2017-12-21 16:44:31 +08:00
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
var gc []*model.Task
|
2017-12-22 11:17:13 +08:00
|
|
|
res.List = &gc
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := t.DoRequest(t.prefix, "GET", nil, &res)
|
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
if code != 200 {
|
|
|
|
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
|
2017-12-19 18:46:49 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return gc, nil
|
2017-11-29 18:44:39 +08:00
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
|
|
|
|
//Exec 执行任务
|
2017-12-21 16:44:31 +08:00
|
|
|
func (t *task) Exec(taskID string, nodes []string) *util.APIHandleError {
|
2017-11-22 11:40:14 +08:00
|
|
|
var nodesBody struct {
|
|
|
|
Nodes []string `json:"nodes"`
|
|
|
|
}
|
2017-12-19 18:46:49 +08:00
|
|
|
nodesBody.Nodes = nodes
|
2017-12-21 16:44:31 +08:00
|
|
|
body, err := json.Marshal(nodesBody)
|
2017-12-19 18:46:49 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(400, err)
|
2017-11-23 15:05:46 +08:00
|
|
|
}
|
2017-12-22 18:16:16 +08:00
|
|
|
url := t.prefix + "/" + taskID + "/exec"
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := t.DoRequest(url, "POST", bytes.NewBuffer(body), nil)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleError(code, err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-21 16:44:31 +08:00
|
|
|
func (t *task) Add(task *model.Task) *util.APIHandleError {
|
2017-11-30 14:43:24 +08:00
|
|
|
|
2017-12-19 18:46:49 +08:00
|
|
|
body, _ := json.Marshal(task)
|
2017-12-21 16:44:31 +08:00
|
|
|
url := t.prefix
|
2018-07-17 16:18:19 +08:00
|
|
|
code, err := t.DoRequest(url, "POST", bytes.NewBuffer(body), nil)
|
2017-11-22 11:40:14 +08:00
|
|
|
if err != nil {
|
2018-07-17 16:18:19 +08:00
|
|
|
return util.CreateAPIHandleError(code, err)
|
2017-12-21 17:09:17 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return nil
|
2017-11-22 11:40:14 +08:00
|
|
|
}
|
2017-12-20 10:23:55 +08:00
|
|
|
|
2018-07-17 16:18:19 +08:00
|
|
|
func (t *task) AddGroup(group *model.TaskGroup) *util.APIHandleError {
|
|
|
|
body, _ := json.Marshal(group)
|
|
|
|
url := "/v2/taskgroups"
|
|
|
|
code, err := t.DoRequest(url, "POST", bytes.NewBuffer(body), nil)
|
2017-12-21 16:44:31 +08:00
|
|
|
if err != nil {
|
2017-12-22 18:16:16 +08:00
|
|
|
return util.CreateAPIHandleError(code, err)
|
2017-12-21 16:44:31 +08:00
|
|
|
}
|
|
|
|
return nil
|
2017-12-22 18:16:16 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
func (t *task) GetTaskStatus(task string) (map[string]*model.TaskStatus, *util.APIHandleError) {
|
|
|
|
var res utilhttp.ResponseBody
|
|
|
|
var gc = make(map[string]*model.TaskStatus)
|
|
|
|
res.Bean = &gc
|
|
|
|
code, err := t.DoRequest("/tasks/"+task+"/status", "GET", nil, &res)
|
2017-12-27 18:20:52 +08:00
|
|
|
if err != nil {
|
2018-07-17 16:18:19 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, err)
|
2017-12-27 18:20:52 +08:00
|
|
|
}
|
|
|
|
if code != 200 {
|
2018-07-17 16:18:19 +08:00
|
|
|
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
|
2017-12-27 18:20:52 +08:00
|
|
|
}
|
2018-07-17 16:18:19 +08:00
|
|
|
return gc, nil
|
2017-12-27 18:20:52 +08:00
|
|
|
}
|