实现域名、记录同步等API

This commit is contained in:
刘祥超
2021-06-01 16:43:00 +08:00
parent 2a3fbd080e
commit e9e4abff03
13 changed files with 379 additions and 11 deletions

View File

@@ -44,10 +44,16 @@ func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, id int64) error {
}
// DisableNSDomain 禁用条目
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, domainId int64) error {
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(domainId).
Set("state", NSDomainStateDisabled).
Set("version", version).
Update()
return err
}
@@ -74,26 +80,38 @@ func (this *NSDomainDAO) FindNSDomainName(tx *dbs.Tx, id int64) (string, error)
// CreateDomain 创建域名
func (this *NSDomainDAO) CreateDomain(tx *dbs.Tx, clusterId int64, userId int64, name string) (int64, error) {
version, err := this.IncreaseVersion(tx)
if err != nil {
return 0, err
}
op := NewNSDomainOperator()
op.ClusterId = clusterId
op.UserId = userId
op.Name = name
op.Version = version
op.IsOn = true
op.State = NSDomainStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateDomain 修改域名
func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int64, userId int64, name string, isOn bool) error {
func (this *NSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, clusterId int64, userId int64, isOn bool) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
op := NewNSDomainOperator()
op.Id = domainId
op.ClusterId = clusterId
op.UserId = userId
op.Name = name
op.IsOn = isOn
op.Version = version
return this.Save(tx, op)
}
@@ -146,3 +164,23 @@ func (this *NSDomainDAO) ListEnabledDomains(tx *dbs.Tx, clusterId int64, userId
FindAll()
return
}
// IncreaseVersion 增加版本
func (this *NSDomainDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
return models.SharedSysLockerDAO.Increase(tx, "NS_DOMAIN_VERSION", 1)
}
// ListDomainsAfterVersion 列出某个版本后的域名
func (this *NSDomainDAO) ListDomainsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSDomain, err error) {
if size <= 0 {
size = 10000
}
_, err = this.Query(tx).
Gte("version", version).
Limit(size).
Asc("version").
Slice(&result).
FindAll()
return
}

View File

@@ -8,6 +8,7 @@ type NSDomain struct {
IsOn uint8 `field:"isOn"` // 是否启用
Name string `field:"name"` // 域名
CreatedAt uint64 `field:"createdAt"` // 创建时间
Version uint64 `field:"version"` // 版本
State uint8 `field:"state"` // 状态
}
@@ -18,6 +19,7 @@ type NSDomainOperator struct {
IsOn interface{} // 是否启用
Name interface{} // 域名
CreatedAt interface{} // 创建时间
Version interface{} // 版本
State interface{} // 状态
}

View File

@@ -318,6 +318,18 @@ func (this *NSNodeDAO) UpdateNodeIsInstalled(tx *dbs.Tx, nodeId int64, isInstall
return err
}
// UpdateNodeStatus 更改节点状态
func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
if statusJSON == nil {
return nil
}
_, err := this.Query(tx).
Pk(nodeId).
Set("status", string(statusJSON)).
Update()
return err
}
// NotifyUpdate 通知更新
func (this *NSNodeDAO) NotifyUpdate(tx *dbs.Tx, nodeId int64) error {
// TODO 先什么都不做

View File

@@ -2,6 +2,7 @@ package nameservers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
_ "github.com/go-sql-driver/mysql"
@@ -47,9 +48,15 @@ func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, id uint64) error {
// DisableNSRecord 禁用条目
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(id).
Set("state", NSRecordStateDisabled).
Set("version", version).
Update()
return err
}
@@ -76,6 +83,11 @@ func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id int64) (string, error)
// CreateRecord 创建记录
func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64) (int64, error) {
version, err := this.IncreaseVersion(tx)
if err != nil {
return 0, err
}
op := NewNSRecordOperator()
op.DomainId = domainId
op.Description = description
@@ -96,6 +108,7 @@ func (this *NSRecordDAO) CreateRecord(tx *dbs.Tx, domainId int64, description st
op.IsOn = true
op.State = NSRecordStateEnabled
op.Version = version
return this.SaveInt64(tx, op)
}
@@ -104,6 +117,11 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
return errors.New("invalid recordId")
}
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
op := NewNSRecordOperator()
op.Id = recordId
op.Description = description
@@ -122,6 +140,8 @@ func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description st
op.RouteIds = routeIds
}
op.Version = version
return this.Save(tx, op)
}
@@ -164,3 +184,23 @@ func (this *NSRecordDAO) ListAllEnabledRecords(tx *dbs.Tx, domainId int64, dnsTy
FindAll()
return
}
// IncreaseVersion 增加版本
func (this *NSRecordDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
return models.SharedSysLockerDAO.Increase(tx, "NS_RECORD_VERSION", 1)
}
// ListRecordsAfterVersion 列出某个版本后的记录
func (this *NSRecordDAO) ListRecordsAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRecord, err error) {
if size <= 0 {
size = 10000
}
_, err = this.Query(tx).
Gte("version", version).
Limit(size).
Asc("version").
Slice(&result).
FindAll()
return
}

View File

@@ -13,6 +13,7 @@ type NSRecord struct {
Weight uint32 `field:"weight"` // 权重
RouteIds string `field:"routeIds"` // 线路
CreatedAt uint64 `field:"createdAt"` // 创建时间
Version uint64 `field:"version"` //
State uint8 `field:"state"` // 状态
}
@@ -28,6 +29,7 @@ type NSRecordOperator struct {
Weight interface{} // 权重
RouteIds interface{} // 线路
CreatedAt interface{} // 创建时间
Version interface{} //
State interface{} // 状态
}

View File

@@ -1,6 +1,7 @@
package nameservers
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
@@ -43,10 +44,16 @@ func (this *NSRouteDAO) EnableNSRoute(tx *dbs.Tx, id int64) error {
}
// DisableNSRoute 禁用条目
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, routeId int64) error {
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(routeId).
Set("state", NSRouteStateDisabled).
Set("version", version).
Update()
return err
}
@@ -73,6 +80,11 @@ func (this *NSRouteDAO) FindNSRouteName(tx *dbs.Tx, id int64) (string, error) {
// CreateRoute 创建线路
func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64, userId int64, name string, rangesJSON []byte) (int64, error) {
version, err := this.IncreaseVersion(tx)
if err != nil {
return 0, err
}
op := NewNSRouteOperator()
op.ClusterId = clusterId
op.DomainId = domainId
@@ -85,6 +97,7 @@ func (this *NSRouteDAO) CreateRoute(tx *dbs.Tx, clusterId int64, domainId int64,
}
op.IsOn = true
op.State = NSRouteStateEnabled
op.Version = version
return this.SaveInt64(tx, op)
}
@@ -93,6 +106,12 @@ func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rang
if routeId <= 0 {
return errors.New("invalid routeId")
}
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
op := NewNSRouteOperator()
op.Id = routeId
op.Name = name
@@ -101,16 +120,25 @@ func (this *NSRouteDAO) UpdateRoute(tx *dbs.Tx, routeId int64, name string, rang
} else {
op.Ranges = "[]"
}
op.Version = version
return this.Save(tx, op)
}
// UpdateRouteOrders 修改线路排序
func (this *NSRouteDAO) UpdateRouteOrders(tx *dbs.Tx, routeIds []int64) error {
version, err := this.IncreaseVersion(tx)
if err != nil {
return err
}
order := len(routeIds)
for _, routeId := range routeIds {
_, err := this.Query(tx).
_, err = this.Query(tx).
Pk(routeId).
Set("order", order).
Set("version", version).
Update()
if err != nil {
return err
@@ -142,3 +170,23 @@ func (this *NSRouteDAO) FindAllEnabledRoutes(tx *dbs.Tx, clusterId int64, domain
_, err = query.FindAll()
return
}
// IncreaseVersion 增加版本
func (this *NSRouteDAO) IncreaseVersion(tx *dbs.Tx) (int64, error) {
return models.SharedSysLockerDAO.Increase(tx, "NS_ROUTE_VERSION", 1)
}
// ListRoutesAfterVersion 列出某个版本后的域名
func (this *NSRouteDAO) ListRoutesAfterVersion(tx *dbs.Tx, version int64, size int64) (result []*NSRoute, err error) {
if size <= 0 {
size = 10000
}
_, err = this.Query(tx).
Gte("version", version).
Limit(size).
Asc("version").
Slice(&result).
FindAll()
return
}

View File

@@ -10,6 +10,7 @@ type NSRoute struct {
Name string `field:"name"` // 名称
Ranges string `field:"ranges"` // 范围
Order uint32 `field:"order"` // 排序
Version uint64 `field:"version"` // 版本号
State uint8 `field:"state"` // 状态
}
@@ -22,6 +23,7 @@ type NSRouteOperator struct {
Name interface{} // 名称
Ranges interface{} // 范围
Order interface{} // 排序
Version interface{} // 版本号
State interface{} // 状态
}

View File

@@ -96,6 +96,8 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, ser
switch role {
case nodeconfigs.NodeRoleNode:
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
case nodeconfigs.NodeRoleDNS:
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
}
}
if serverId > 0 {
@@ -141,6 +143,8 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
switch role {
case nodeconfigs.NodeRoleNode:
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
case nodeconfigs.NodeRoleDNS:
query.Where("nodeId IN (SELECT id FROM edgeNSNodes WHERE state=1)") // 没有用 SharedNSNodeDAO() 因为有包循环引用的问题
}
}
if serverId > 0 {