nodeGroup.go 4.1 KB
Newer Older
Y
Your Name 已提交
1
package console_sqlite3
E
eoLinker API Management 已提交
2 3

import (
Y
Your Name 已提交
4 5 6
	SQL "database/sql"

	"github.com/eolinker/goku-api-gateway/server/dao"
E
eoLinker API Management 已提交
7 8
)

Y
Your Name 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//NodeGroupDao NodeGroupDao
type NodeGroupDao struct {
	db *SQL.DB
}

//NewNodeGroupDao new NodeGroupDao
func NewNodeGroupDao() *NodeGroupDao {
	return &NodeGroupDao{}
}

//Create create
func (d *NodeGroupDao) Create(db *SQL.DB) (interface{}, error) {
	d.db = db
	var i dao.NodeGroupDao = d
	return &i, nil
}

Y
Your Name 已提交
26
//AddNodeGroup 新建节点分组
Y
Your Name 已提交
27 28
func (d *NodeGroupDao) AddNodeGroup(groupName string, clusterID int) (bool, interface{}, error) {
	db := d.db
29
	sql := "INSERT INTO goku_node_group (`groupName`,`clusterID`,`groupType`) VALUES (?,?,0);"
E
eoLinker API Management 已提交
30 31 32 33 34
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, err.Error(), err
	}
	defer stmt.Close()
Y
Your Name 已提交
35
	r, err := stmt.Exec(groupName, clusterID)
E
eoLinker API Management 已提交
36 37 38 39 40 41 42
	if err != nil {
		return false, "[ERROR]Fail to insert data!", err
	}
	groupID, _ := r.LastInsertId()
	return true, groupID, nil
}

Y
Your Name 已提交
43
//EditNodeGroup 修改节点分组信息
Y
Your Name 已提交
44 45
func (d *NodeGroupDao) EditNodeGroup(groupName string, groupID int) (bool, string, error) {
	db := d.db
E
eoLinker API Management 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
	sql := "UPDATE goku_node_group SET groupName = ? WHERE groupID = ?;"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return false, err.Error(), err
	}
	defer stmt.Close()
	_, err = stmt.Exec(groupName, groupID)
	if err != nil {
		return false, "[ERROR]Fail to update data!", err
	}
	return true, "", nil
}

Y
Your Name 已提交
59
//DeleteNodeGroup 删除节点分组
Y
Your Name 已提交
60 61
func (d *NodeGroupDao) DeleteNodeGroup(groupID int) (bool, string, error) {
	db := d.db
E
eoLinker API Management 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	Tx, _ := db.Begin()
	sql := "DELETE FROM goku_node_group WHERE groupID = ?;"
	_, err := Tx.Exec(sql, groupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
	sql = "DELETE FROM goku_node_info WHERE groupID = ?;"
	_, err = Tx.Exec(sql, groupID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to delete data!", err
	}
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
79
//GetNodeGroupInfo 获取节点分组信息
Y
Your Name 已提交
80 81
func (d *NodeGroupDao) GetNodeGroupInfo(groupID int) (bool, map[string]interface{}, error) {
	db := d.db
E
eoLinker API Management 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

	sql := "SELECT G.`groupName`,C.`name` FROM goku_node_group G left join `goku_cluster` C ON C.`id` = G.`clusterID` WHERE G.`groupID` = ?;"
	var groupName string
	var clusterName string
	err := db.QueryRow(sql, groupID).Scan(&groupName, &clusterName)
	if err != nil {
		return false, nil, err
	}
	groupInfo := map[string]interface{}{
		"groupID":   groupID,
		"groupName": groupName,
		"cluster":   clusterName,
	}
	return true, groupInfo, nil
}

Y
Your Name 已提交
98
//GetNodeGroupList 获取节点分组列表
Y
Your Name 已提交
99 100
func (d *NodeGroupDao) GetNodeGroupList(clusterID int) (bool, []map[string]interface{}, error) {
	db := d.db
E
eoLinker API Management 已提交
101
	sql := "SELECT G.`groupID`, G.groupName,C.`name` as cluster  FROM goku_node_group G left join `goku_cluster` C ON C.`id` = G.`clusterID` where G.`clusterID`=?;"
Y
Your Name 已提交
102
	rows, err := db.Query(sql, clusterID)
E
eoLinker API Management 已提交
103 104 105 106 107 108
	if err != nil {
		return false, nil, err
	}
	//延时关闭Rows
	defer rows.Close()
	//获取记录列
Y
Your Name 已提交
109

Y
Your Name 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
	nodeGroupList := make([]map[string]interface{}, 0)
	for rows.Next() {
		var groupID int
		var groupName string
		var clusterName string
		err = rows.Scan(&groupID, &groupName, &clusterName)
		if err != nil {
			return false, nil, err
		}
		groupInfo := map[string]interface{}{
			"groupID":   groupID,
			"groupName": groupName,
			"cluster":   clusterName,
E
eoLinker API Management 已提交
123
		}
Y
Your Name 已提交
124
		nodeGroupList = append(nodeGroupList, groupInfo)
E
eoLinker API Management 已提交
125
	}
Y
Your Name 已提交
126
	return true, nodeGroupList, nil
E
eoLinker API Management 已提交
127 128
}

Y
Your Name 已提交
129
//CheckNodeGroupIsExist 检查节点分组是否存在
Y
Your Name 已提交
130 131
func (d *NodeGroupDao) CheckNodeGroupIsExist(groupID int) (bool, error) {
	db := d.db
E
eoLinker API Management 已提交
132 133 134 135 136 137 138 139 140
	var id int
	sql := "SELECT groupID FROM goku_node_group WHERE groupID = ?;"
	err := db.QueryRow(sql, groupID).Scan(&id)
	if err != nil {
		return false, err
	}
	return true, nil
}

Y
Your Name 已提交
141
//GetRunningNodeCount 获取分组内启动节点数量
Y
Your Name 已提交
142 143
func (d *NodeGroupDao) GetRunningNodeCount(groupID int) (bool, interface{}, error) {
	db := d.db
E
eoLinker API Management 已提交
144 145 146 147 148 149 150 151
	var count int
	sql := "SELECT COUNT(0) FROM goku_node_info WHERE groupID = ? AND nodeStatus = 1"
	err := db.QueryRow(sql, groupID).Scan(&count)
	if err != nil {
		return false, "[ERROR]Can not find the avaliable node", err
	}
	return true, count, nil
}