Rainbond/api/region/node.go

302 lines
8.9 KiB
Go
Raw Normal View History

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/>.
package region
import (
"bytes"
"encoding/json"
"fmt"
"github.com/goodrain/rainbond/node/api/model"
"github.com/goodrain/rainbond/node/nodem/client"
"github.com/pquerna/ffjson/ffjson"
//"github.com/goodrain/rainbond/grctl/cmd"
"github.com/goodrain/rainbond/api/util"
utilhttp "github.com/goodrain/rainbond/util/http"
)
func (r *regionImpl) Tasks() TaskInterface {
return &task{regionImpl: *r, prefix: "/v2/tasks"}
}
func (r *regionImpl) Nodes() NodeInterface {
return &node{regionImpl: *r, prefix: "/v2/nodes"}
}
func (r *regionImpl) Configs() ConfigsInterface {
return &configs{regionImpl: *r, prefix: "/v2/configs"}
}
2017-12-21 16:44:31 +08:00
type task struct {
regionImpl
2017-12-21 16:44:31 +08:00
prefix string
}
2017-12-21 16:44:31 +08:00
type node struct {
regionImpl
2017-12-21 16:44:31 +08:00
prefix string
}
func (n *node) Get(node string) (*client.HostNode, *util.APIHandleError) {
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc client.HostNode
2017-12-21 16:44:31 +08:00
res.Bean = &gc
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
}
return &gc, nil
}
func (n *node) GetNodeByRule(rule string) ([]*client.HostNode, *util.APIHandleError) {
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc []*client.HostNode
res.List = &gc
code, err := n.DoRequest(n.prefix+"/rule/"+rule, "GET", nil, &res)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
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))
}
return gc, nil
}
func (n *node) List() ([]*client.HostNode, *util.APIHandleError) {
2017-12-21 16:44:31 +08:00
var res utilhttp.ResponseBody
var gc []*client.HostNode
res.List = &gc
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)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("Get database center configs code %d", code))
}
return gc, nil
}
2017-12-21 16:44:31 +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
}
code, err := n.DoRequest(n.prefix, "POST", bytes.NewBuffer(body), 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) Label(nid string, label map[string]string) *util.APIHandleError {
2017-12-21 16:44:31 +08:00
body, err := json.Marshal(label)
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
}
code, err := n.DoRequest(n.prefix+"/"+nid+"/labels", "PUT", bytes.NewBuffer(body), nil)
if err != nil {
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 {
code, err := n.DoRequest(n.prefix+"/"+nid, "DELETE", nil, nil)
if err != nil {
return util.CreateAPIHandleError(code, err)
}
if code != 200 {
return util.CreateAPIHandleError(code, fmt.Errorf("delete node error"))
}
return nil
2017-12-21 16:44:31 +08:00
}
2017-12-21 16:44:31 +08:00
func (n *node) Up(nid string) *util.APIHandleError {
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 {
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 {
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 {
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
}
type configs struct {
regionImpl
prefix string
}
2017-12-21 16:44:31 +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)
if err != nil {
2017-12-22 18:16:16 +08:00
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
}
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)
}
if code != 200 {
return util.CreateAPIHandleError(code, fmt.Errorf("Put database center configs code %d", code))
}
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
//NodeInterface node api
type NodeInterface interface {
GetNodeByRule(rule string) ([]*client.HostNode, *util.APIHandleError)
Get(node string) (*client.HostNode, *util.APIHandleError)
List() ([]*client.HostNode, *util.APIHandleError)
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
Label(nid string, label map[string]string) *util.APIHandleError
}
//ConfigsInterface 数据中心配置API
type ConfigsInterface interface {
Get() (*model.GlobalConfig, *util.APIHandleError)
Put(*model.GlobalConfig) *util.APIHandleError
}
2017-12-21 16:44:31 +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)
if err != nil {
2017-12-22 18:16:16 +08:00
return nil, util.CreateAPIHandleError(code, err)
}
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))
}
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
res.List = &gc
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)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
}
return gc, nil
}
//Exec 执行任务
2017-12-21 16:44:31 +08:00
func (t *task) Exec(taskID string, nodes []string) *util.APIHandleError {
var nodesBody struct {
Nodes []string `json:"nodes"`
}
nodesBody.Nodes = nodes
2017-12-21 16:44:31 +08:00
body, err := json.Marshal(nodesBody)
if err != nil {
2017-12-22 18:16:16 +08:00
return util.CreateAPIHandleError(400, err)
}
2017-12-22 18:16:16 +08:00
url := t.prefix + "/" + taskID + "/exec"
code, err := t.DoRequest(url, "POST", bytes.NewBuffer(body), nil)
if err != nil {
return util.CreateAPIHandleError(code, err)
}
return nil
}
2017-12-21 16:44:31 +08:00
func (t *task) Add(task *model.Task) *util.APIHandleError {
body, _ := json.Marshal(task)
2017-12-21 16:44:31 +08:00
url := t.prefix
code, err := t.DoRequest(url, "POST", bytes.NewBuffer(body), nil)
if err != nil {
return util.CreateAPIHandleError(code, err)
}
return nil
}
2017-12-20 10:23:55 +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
}
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)
if err != nil {
return nil, util.CreateAPIHandleError(code, err)
}
if code != 200 {
return nil, util.CreateAPIHandleError(code, fmt.Errorf("get task with code %d", code))
}
return gc, nil
}