实现缓存策略的部分功能

This commit is contained in:
刘祥超
2020-10-04 14:27:14 +08:00
parent 9c6efad81f
commit 97f9bb27e7
13 changed files with 762 additions and 197 deletions

View File

@@ -59,6 +59,19 @@ func (this *APINodeDAO) FindEnabledAPINode(id int64) (*APINode, error) {
return result.(*APINode), err
}
// 根据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
}
// 根据主键查找名称
func (this *APINodeDAO) FindAPINodeName(id int64) (string, error) {
return this.Query().
@@ -68,7 +81,7 @@ func (this *APINodeDAO) FindAPINodeName(id int64) (string, error) {
}
// 创建API节点
func (this *APINodeDAO) CreateAPINode(name string, description string, host string, port int) (nodeId int64, err error) {
func (this *APINodeDAO) CreateAPINode(name string, description string, httpJSON []byte, httpsJSON []byte, accessAddrsJSON []byte, isOn bool) (nodeId int64, err error) {
uniqueId, err := this.genUniqueId()
if err != nil {
return 0, err
@@ -80,13 +93,22 @@ func (this *APINodeDAO) CreateAPINode(name string, description string, host stri
}
op := NewAPINodeOperator()
op.IsOn = true
op.IsOn = isOn
op.UniqueId = uniqueId
op.Secret = secret
op.Name = name
op.Description = description
op.Host = host
op.Port = port
if len(httpJSON) > 0 {
op.Http = httpJSON
}
if len(httpsJSON) > 0 {
op.Https = httpsJSON
}
if len(accessAddrsJSON) > 0 {
op.AccessAddrs = accessAddrsJSON
}
op.State = NodeStateEnabled
_, err = this.Save(op)
if err != nil {
@@ -97,7 +119,7 @@ func (this *APINodeDAO) CreateAPINode(name string, description string, host stri
}
// 修改API节点
func (this *APINodeDAO) UpdateAPINode(nodeId int64, name string, description string, host string, port int) error {
func (this *APINodeDAO) UpdateAPINode(nodeId int64, name string, description string, httpJSON []byte, httpsJSON []byte, accessAddrsJSON []byte, isOn bool) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
@@ -106,8 +128,24 @@ func (this *APINodeDAO) UpdateAPINode(nodeId int64, name string, description str
op.Id = nodeId
op.Name = name
op.Description = description
op.Host = host
op.Port = port
op.IsOn = isOn
if len(httpJSON) > 0 {
op.Http = httpJSON
} else {
op.Http = "null"
}
if len(httpsJSON) > 0 {
op.Https = httpsJSON
} else {
op.Https = "null"
}
if len(accessAddrsJSON) > 0 {
op.AccessAddrs = accessAddrsJSON
} else {
op.AccessAddrs = "null"
}
_, err := this.Save(op)
return err
}

View File

@@ -9,8 +9,9 @@ type APINode struct {
Secret string `field:"secret"` // 密钥
Name string `field:"name"` // 名称
Description string `field:"description"` // 描述
Host string `field:"host"` // 主机
Port uint32 `field:"port"` // 端口
Http string `field:"http"` // 监听的HTTP配置
Https string `field:"https"` // 监听的HTTPS配置
AccessAddrs string `field:"accessAddrs"` // 外部访问地址
Order uint32 `field:"order"` // 排序
State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间
@@ -26,8 +27,9 @@ type APINodeOperator struct {
Secret interface{} // 密钥
Name interface{} // 名称
Description interface{} // 描述
Host interface{} // 主机
Port interface{} // 端口
Http interface{} // 监听的HTTP配置
Https interface{} // 监听的HTTPS配置
AccessAddrs interface{} // 外部访问地址
Order interface{} // 排序
State interface{} // 状态
CreatedAt interface{} // 创建时间

View File

@@ -1,8 +1,95 @@
package models
import "strconv"
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
)
// 地址
func (this *APINode) Address() string {
return this.Host + ":" + strconv.Itoa(int(this.Port))
// 解析HTTP配置
func (this *APINode) DecodeHTTP() (*serverconfigs.HTTPProtocolConfig, error) {
if !IsNotNull(this.Http) {
return nil, nil
}
config := &serverconfigs.HTTPProtocolConfig{}
err := json.Unmarshal([]byte(this.Http), config)
if err != nil {
return nil, err
}
err = config.Init()
if err != nil {
return nil, err
}
return config, nil
}
// 解析HTTPS配置
func (this *APINode) DecodeHTTPS() (*serverconfigs.HTTPSProtocolConfig, error) {
if !IsNotNull(this.Https) {
return nil, nil
}
config := &serverconfigs.HTTPSProtocolConfig{}
err := json.Unmarshal([]byte(this.Https), config)
if err != nil {
return nil, err
}
err = config.Init()
if err != nil {
return nil, err
}
if config.SSLPolicyRef != nil {
policyId := config.SSLPolicyRef.SSLPolicyId
if policyId > 0 {
sslPolicy, err := SharedSSLPolicyDAO.ComposePolicyConfig(policyId)
if err != nil {
return nil, err
}
if sslPolicy != nil {
config.SSLPolicy = sslPolicy
}
}
}
err = config.Init()
if err != nil {
return nil, err
}
return config, nil
}
// 解析访问地址
func (this *APINode) DecodeAccessAddrs() ([]*serverconfigs.NetworkAddressConfig, error) {
if !IsNotNull(this.AccessAddrs) {
return nil, nil
}
addrConfigs := []*serverconfigs.NetworkAddressConfig{}
err := json.Unmarshal([]byte(this.AccessAddrs), &addrConfigs)
if err != nil {
return nil, err
}
for _, addrConfig := range addrConfigs {
err = addrConfig.Init()
if err != nil {
return nil, err
}
}
return addrConfigs, nil
}
// 解析访问地址,并返回字符串形式
func (this *APINode) DecodeAccessAddrStrings() ([]string, error) {
addrs, err := this.DecodeAccessAddrs()
if err != nil {
return nil, err
}
result := []string{}
for _, addr := range addrs {
result = append(result, addr.FullAddresses()...)
}
return result, nil
}

View File

@@ -2,7 +2,7 @@ package models
import (
"encoding/json"
"errors"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
@@ -275,6 +275,17 @@ func (this *NodeDAO) FindAllNodeIdsMatch(clusterId int64) (result []int64, err e
return
}
// 获取一个集群的所有节点
func (this *NodeDAO) FindAllEnabledNodesWithClusterId(clusterId int64) (result []*Node, err error) {
_, err = this.Query().
State(NodeStateEnabled).
Attr("clusterId", clusterId).
DescPk().
Slice(&result).
FindAll()
return
}
// 计算节点数量
func (this *NodeDAO) CountAllEnabledNodesMatch(clusterId int64, installState configutils.BoolState, activeState configutils.BoolState) (int64, error) {
query := this.Query()
@@ -422,6 +433,28 @@ func (this *NodeDAO) ComposeNodeConfig(nodeId int64) (*nodeconfigs.NodeConfig, e
return config, nil
}
// 修改当前连接的API节点
func (this *NodeDAO) UpdateNodeConnectedAPINodes(nodeId int64, apiNodeIds []int64) error {
if nodeId <= 0 {
return errors.New("invalid nodeId")
}
op := NewNodeOperator()
op.Id = nodeId
if len(apiNodeIds) > 0 {
apiNodeIdsJSON, err := json.Marshal(apiNodeIds)
if err != nil {
return errors.Wrap(err)
}
op.ConnectedAPINodes = apiNodeIdsJSON
} else {
op.ConnectedAPINodes = "[]"
}
_, err := this.Save(op)
return err
}
// 生成唯一ID
func (this *NodeDAO) genUniqueId() (string, error) {
for {

View File

@@ -2,47 +2,49 @@ package models
// 节点
type Node struct {
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
Name string `field:"name"` // 节点名
Code string `field:"code"` // 代号
ClusterId uint32 `field:"clusterId"` // 集群ID
RegionId uint32 `field:"regionId"` // 区域ID
GroupId uint32 `field:"groupId"` // 分组ID
CreatedAt uint64 `field:"createdAt"` // 创建时间
Status string `field:"status"` // 最新的状态
Version uint32 `field:"version"` // 当前版本号
LatestVersion uint32 `field:"latestVersion"` // 最后版本号
InstallDir string `field:"installDir"` // 安装目录
IsInstalled uint8 `field:"isInstalled"` // 是否已安装
InstallStatus string `field:"installStatus"` // 安装状态
State uint8 `field:"state"` // 状态
Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用
UniqueId string `field:"uniqueId"` // 节点ID
Secret string `field:"secret"` // 密钥
Name string `field:"name"` // 节点名
Code string `field:"code"` // 代号
ClusterId uint32 `field:"clusterId"` // 集群ID
RegionId uint32 `field:"regionId"` // 区域ID
GroupId uint32 `field:"groupId"` // 分组ID
CreatedAt uint64 `field:"createdAt"` // 创建时间
Status string `field:"status"` // 最新的状态
Version uint32 `field:"version"` // 当前版本号
LatestVersion uint32 `field:"latestVersion"` // 最后版本号
InstallDir string `field:"installDir"` // 安装目录
IsInstalled uint8 `field:"isInstalled"` // 是否已安装
InstallStatus string `field:"installStatus"` // 安装状态
State uint8 `field:"state"` // 状态
ConnectedAPINodes string `field:"connectedAPINodes"` // 当前连接的API节点
}
type NodeOperator struct {
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
Name interface{} // 节点名
Code interface{} // 代号
ClusterId interface{} // 集群ID
RegionId interface{} // 区域ID
GroupId interface{} // 分组ID
CreatedAt interface{} // 创建时间
Status interface{} // 最新的状态
Version interface{} // 当前版本号
LatestVersion interface{} // 最后版本号
InstallDir interface{} // 安装目录
IsInstalled interface{} // 是否已安装
InstallStatus interface{} // 安装状态
State interface{} // 状态
Id interface{} // ID
AdminId interface{} // 管理员ID
UserId interface{} // 用户ID
IsOn interface{} // 是否启用
UniqueId interface{} // 节点ID
Secret interface{} // 密钥
Name interface{} // 节点名
Code interface{} // 代号
ClusterId interface{} // 集群ID
RegionId interface{} // 区域ID
GroupId interface{} // 分组ID
CreatedAt interface{} // 创建时间
Status interface{} // 最新的状态
Version interface{} // 当前版本号
LatestVersion interface{} // 最后版本号
InstallDir interface{} // 安装目录
IsInstalled interface{} // 是否已安装
InstallStatus interface{} // 安装状态
State interface{} // 状态
ConnectedAPINodes interface{} // 当前连接的API节点
}
func NewNodeOperator() *NodeOperator {