Files
EdgeAPI/internal/db/models/api_node_dao.go

278 lines
5.8 KiB
Go
Raw Normal View History

2020-09-06 16:19:54 +08:00
package models
import (
2020-10-13 20:05:13 +08:00
"encoding/json"
2020-09-06 16:19:54 +08:00
"errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
2020-10-13 20:05:13 +08:00
"github.com/iwind/TeaGo/maps"
2020-09-06 16:19:54 +08:00
"github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types"
2020-10-13 20:05:13 +08:00
"strconv"
2020-09-06 16:19:54 +08:00
)
const (
APINodeStateEnabled = 1 // 已启用
APINodeStateDisabled = 0 // 已禁用
)
type APINodeDAO dbs.DAO
func NewAPINodeDAO() *APINodeDAO {
return dbs.NewDAO(&APINodeDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
2020-09-23 10:12:57 +08:00
Table: "edgeAPINodes",
2020-09-06 16:19:54 +08:00
Model: new(APINode),
PkName: "id",
},
}).(*APINodeDAO)
}
2020-10-13 20:05:13 +08:00
var SharedAPINodeDAO *APINodeDAO
func init() {
dbs.OnReady(func() {
SharedAPINodeDAO = NewAPINodeDAO()
})
}
2020-09-06 16:19:54 +08:00
// 启用条目
func (this *APINodeDAO) EnableAPINode(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", APINodeStateEnabled).
Update()
return err
}
// 禁用条目
func (this *APINodeDAO) DisableAPINode(id int64) error {
_, err := this.Query().
Pk(id).
Set("state", APINodeStateDisabled).
Update()
return err
}
// 查找启用中的条目
func (this *APINodeDAO) FindEnabledAPINode(id int64) (*APINode, error) {
result, err := this.Query().
Pk(id).
Attr("state", APINodeStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*APINode), err
}
2020-10-04 14:27:14 +08:00
// 根据ID和Secret查找节点
func (this *APINodeDAO) FindEnabledAPINodeWithUniqueIdAndSecret(uniqueId string, secret string) (*APINode, error) {
one, err := this.Query().
State(APINodeStateEnabled).
Attr("uniqueId", uniqueId).
Attr("secret", secret).
Find()
if err != nil || one == nil {
return nil, err
}
return one.(*APINode), nil
}
2020-09-06 16:19:54 +08:00
// 根据主键查找名称
func (this *APINodeDAO) FindAPINodeName(id int64) (string, error) {
return this.Query().
Pk(id).
Result("name").
FindStringCol("")
}
// 创建API节点
2021-01-01 20:49:09 +08:00
func (this *APINodeDAO) CreateAPINode(name string, description string, httpJSON []byte, httpsJSON []byte, restIsOn bool, restHTTPJSON []byte, restHTTPSJSON []byte, accessAddrsJSON []byte, isOn bool) (nodeId int64, err error) {
2020-09-06 16:19:54 +08:00
uniqueId, err := this.genUniqueId()
if err != nil {
return 0, err
}
secret := rands.String(32)
2020-10-13 20:05:13 +08:00
err = NewApiTokenDAO().CreateAPIToken(uniqueId, secret, NodeRoleAPI)
2020-09-06 16:19:54 +08:00
if err != nil {
return
}
op := NewAPINodeOperator()
2020-10-04 14:27:14 +08:00
op.IsOn = isOn
2020-09-06 16:19:54 +08:00
op.UniqueId = uniqueId
op.Secret = secret
op.Name = name
op.Description = description
2020-10-04 14:27:14 +08:00
if len(httpJSON) > 0 {
op.Http = httpJSON
}
if len(httpsJSON) > 0 {
op.Https = httpsJSON
}
2021-01-01 20:49:09 +08:00
op.RestIsOn = restIsOn
if len(restHTTPJSON) > 0 {
op.RestHTTP = restHTTPJSON
}
if len(restHTTPSJSON) > 0 {
op.RestHTTPS = restHTTPSJSON
}
2020-10-04 14:27:14 +08:00
if len(accessAddrsJSON) > 0 {
op.AccessAddrs = accessAddrsJSON
}
2020-09-06 16:19:54 +08:00
op.State = NodeStateEnabled
2020-12-09 20:44:05 +08:00
err = this.Save(op)
2020-09-06 16:19:54 +08:00
if err != nil {
return
}
return types.Int64(op.Id), nil
}
// 修改API节点
2021-01-01 20:49:09 +08:00
func (this *APINodeDAO) UpdateAPINode(nodeId int64, name string, description string, httpJSON []byte, httpsJSON []byte, restIsOn bool, restHTTPJSON []byte, restHTTPSJSON []byte, accessAddrsJSON []byte, isOn bool) error {
2020-09-06 16:19:54 +08:00
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
op := NewAPINodeOperator()
op.Id = nodeId
op.Name = name
op.Description = description
2020-10-04 14:27:14 +08:00
op.IsOn = isOn
if len(httpJSON) > 0 {
op.Http = httpJSON
} else {
2021-01-01 20:49:09 +08:00
op.Http = "{}"
2020-10-04 14:27:14 +08:00
}
if len(httpsJSON) > 0 {
op.Https = httpsJSON
} else {
2021-01-01 20:49:09 +08:00
op.Https = "{}"
}
op.RestIsOn = restIsOn
if len(restHTTPJSON) > 0 {
op.RestHTTP = restHTTPJSON
} else {
op.RestHTTP = "{}"
}
if len(restHTTPSJSON) > 0 {
op.RestHTTPS = restHTTPSJSON
} else {
op.RestHTTPS = "{}"
2020-10-04 14:27:14 +08:00
}
if len(accessAddrsJSON) > 0 {
op.AccessAddrs = accessAddrsJSON
} else {
2021-01-01 20:49:09 +08:00
op.AccessAddrs = "[]"
2020-10-04 14:27:14 +08:00
}
2020-12-09 20:44:05 +08:00
err := this.Save(op)
2020-09-06 16:19:54 +08:00
return err
}
// 列出所有可用API节点
func (this *APINodeDAO) FindAllEnabledAPINodes() (result []*APINode, err error) {
_, err = this.Query().
Attr("clusterId", 0). // 非集群专用
State(APINodeStateEnabled).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
// 列出所有可用而且启用的API节点
func (this *APINodeDAO) FindAllEnabledAndOnAPINodes() (result []*APINode, err error) {
_, err = this.Query().
Attr("clusterId", 0). // 非集群专用
Attr("isOn", true).
State(APINodeStateEnabled).
Desc("order").
AscPk().
Slice(&result).
FindAll()
return
}
2020-09-06 16:19:54 +08:00
// 计算API节点数量
func (this *APINodeDAO) CountAllEnabledAPINodes() (int64, error) {
return this.Query().
State(APINodeStateEnabled).
Count()
}
// 列出单页的API节点
func (this *APINodeDAO) ListEnabledAPINodes(offset int64, size int64) (result []*APINode, err error) {
_, err = this.Query().
Attr("clusterId", 0). // 非集群专用
State(APINodeStateEnabled).
Offset(offset).
Limit(size).
Desc("order").
DescPk().
Slice(&result).
FindAll()
return
}
2020-10-13 20:05:13 +08:00
// 根据主机名和端口获取ID
func (this *APINodeDAO) FindEnabledAPINodeIdWithAddr(protocol string, host string, port int) (int64, error) {
addr := maps.Map{
"protocol": protocol,
"host": host,
"portRange": strconv.Itoa(port),
}
addrJSON, err := json.Marshal(addr)
if err != nil {
return 0, err
}
one, err := this.Query().
State(APINodeStateEnabled).
Where("JSON_CONTAINS(accessAddrs, :addr)").
Param("addr", string(addrJSON)).
ResultPk().
Find()
if err != nil {
return 0, err
}
if one == nil {
return 0, nil
}
return int64(one.(*APINode).Id), nil
}
// 设置API节点状态
func (this *APINodeDAO) UpdateAPINodeStatus(apiNodeId int64, statusJSON []byte) error {
_, err := this.Query().
Pk(apiNodeId).
Set("status", statusJSON).
Update()
return err
}
2020-09-06 16:19:54 +08:00
// 生成唯一ID
func (this *APINodeDAO) genUniqueId() (string, error) {
for {
uniqueId := rands.HexString(32)
ok, err := this.Query().
Attr("uniqueId", uniqueId).
Exist()
if err != nil {
return "", err
}
if ok {
continue
}
return uniqueId, nil
}
}