mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-29 17:26:35 +08:00
实现基本的集群DNS列表、设置、简单数据同步
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -133,32 +134,20 @@ func (this *DNSDomainDAO) UpdateDomainData(domainId int64, data string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新服务相关域名
|
||||
func (this *DNSDomainDAO) UpdateServerDomains(domainId int64, serverDomainsJSON []byte) error {
|
||||
// 更新域名解析记录
|
||||
func (this *DNSDomainDAO) UpdateDomainRecords(domainId int64, recordsJSON []byte) error {
|
||||
if domainId <= 0 {
|
||||
return errors.New("invalid domainId")
|
||||
}
|
||||
op := NewDNSDomainOperator()
|
||||
op.Id = domainId
|
||||
op.ServerDomains = serverDomainsJSON
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新集群相关域名
|
||||
func (this *DNSDomainDAO) UpdateClusterDomains(domainId int64, clusterDomainJSON []byte) error {
|
||||
if domainId <= 0 {
|
||||
return errors.New("invalid domainId")
|
||||
}
|
||||
op := NewDNSDomainOperator()
|
||||
op.Id = domainId
|
||||
op.ClusterDomains = clusterDomainJSON
|
||||
op.Records = recordsJSON
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新线路
|
||||
func (this *DNSDomainDAO) UpdateRoutes(domainId int64, routesJSON []byte) error {
|
||||
func (this *DNSDomainDAO) UpdateDomainRoutes(domainId int64, routesJSON []byte) error {
|
||||
if domainId <= 0 {
|
||||
return errors.New("invalid domainId")
|
||||
}
|
||||
@@ -168,3 +157,23 @@ func (this *DNSDomainDAO) UpdateRoutes(domainId int64, routesJSON []byte) error
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找域名线路
|
||||
func (this *DNSDomainDAO) FindDomainRoutes(domainId int64) ([]string, error) {
|
||||
routes, err := this.Query().
|
||||
Pk(domainId).
|
||||
Result("routes").
|
||||
FindStringCol("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(routes) == 0 || routes == "null" {
|
||||
return nil, nil
|
||||
}
|
||||
result := []string{}
|
||||
err = json.Unmarshal([]byte(routes), &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -2,33 +2,33 @@ package models
|
||||
|
||||
// 管理的域名
|
||||
type DNSDomain struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
ProviderId uint32 `field:"providerId"` // 服务商ID
|
||||
IsOn uint8 `field:"isOn"` // 是否可用
|
||||
Name string `field:"name"` // 域名
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
DataUpdatedAt uint64 `field:"dataUpdatedAt"` // 数据更新时间
|
||||
Data string `field:"data"` // 原始数据信息
|
||||
ServerDomains string `field:"serverDomains"` // 服务相关子域名
|
||||
ClusterDomains string `field:"clusterDomains"` // 集群相关域名
|
||||
Routes string `field:"routes"` // 线路数据
|
||||
State uint8 `field:"state"` // 状态
|
||||
Id uint32 `field:"id"` // ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
ProviderId uint32 `field:"providerId"` // 服务商ID
|
||||
IsOn uint8 `field:"isOn"` // 是否可用
|
||||
Name string `field:"name"` // 域名
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
DataUpdatedAt uint64 `field:"dataUpdatedAt"` // 数据更新时间
|
||||
DataError string `field:"dataError"` // 数据更新错误
|
||||
Data string `field:"data"` // 原始数据信息
|
||||
Records string `field:"records"` // 所有解析记录
|
||||
Routes string `field:"routes"` // 线路数据
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type DNSDomainOperator struct {
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
ProviderId interface{} // 服务商ID
|
||||
IsOn interface{} // 是否可用
|
||||
Name interface{} // 域名
|
||||
CreatedAt interface{} // 创建时间
|
||||
DataUpdatedAt interface{} // 数据更新时间
|
||||
Data interface{} // 原始数据信息
|
||||
ServerDomains interface{} // 服务相关子域名
|
||||
ClusterDomains interface{} // 集群相关域名
|
||||
Routes interface{} // 线路数据
|
||||
State interface{} // 状态
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
ProviderId interface{} // 服务商ID
|
||||
IsOn interface{} // 是否可用
|
||||
Name interface{} // 域名
|
||||
CreatedAt interface{} // 创建时间
|
||||
DataUpdatedAt interface{} // 数据更新时间
|
||||
DataError interface{} // 数据更新错误
|
||||
Data interface{} // 原始数据信息
|
||||
Records interface{} // 所有解析记录
|
||||
Routes interface{} // 线路数据
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewDNSDomainOperator() *DNSDomainOperator {
|
||||
|
||||
@@ -120,3 +120,14 @@ func (this *DNSProviderDAO) ListEnabledDNSProviders(offset int64, size int64) (r
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 查询某个类型下的所有服务商
|
||||
func (this *DNSProviderDAO) FindAllEnabledDNSProvidersWithType(providerType string) (result []*DNSProvider, err error) {
|
||||
_, err = this.Query().
|
||||
State(DNSProviderStateEnabled).
|
||||
Attr("type", providerType).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -297,6 +297,34 @@ func (this *NodeClusterDAO) CountAllEnabledClustersWithDNSDomainId(dnsDomainId i
|
||||
Count()
|
||||
}
|
||||
|
||||
// 查询使用某个DNS域名的集群ID列表
|
||||
func (this *NodeClusterDAO) FindAllEnabledClusterIdsWithDNSDomainId(dnsDomainId int64) ([]int64, error) {
|
||||
ones, err := this.Query().
|
||||
State(NodeClusterStateEnabled).
|
||||
Attr("dnsDomainId", dnsDomainId).
|
||||
ResultPk().
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []int64{}
|
||||
for _, one := range ones {
|
||||
result = append(result, int64(one.(*NodeCluster).Id))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 查询使用某个DNS域名的所有集群域名
|
||||
func (this *NodeClusterDAO) FindAllEnabledClustersWithDNSDomainId(dnsDomainId int64) (result []*NodeCluster, err error) {
|
||||
_, err = this.Query().
|
||||
State(NodeClusterStateEnabled).
|
||||
Attr("dnsDomainId", dnsDomainId).
|
||||
Result("id", "name", "dnsName").
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 查找集群的认证ID
|
||||
func (this *NodeClusterDAO) FindClusterGrantId(clusterId int64) (int64, error) {
|
||||
return this.Query().
|
||||
@@ -309,7 +337,7 @@ func (this *NodeClusterDAO) FindClusterGrantId(clusterId int64) (int64, error) {
|
||||
func (this *NodeClusterDAO) FindClusterDNSInfo(clusterId int64) (*NodeCluster, error) {
|
||||
one, err := this.Query().
|
||||
Pk(clusterId).
|
||||
Result("dnsName", "dnsDomain", "dnsProviderId").
|
||||
Result("dnsName", "dnsDomainId").
|
||||
Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -320,6 +348,29 @@ func (this *NodeClusterDAO) FindClusterDNSInfo(clusterId int64) (*NodeCluster, e
|
||||
return one.(*NodeCluster), nil
|
||||
}
|
||||
|
||||
// 检查某个子域名是否可用
|
||||
func (this *NodeClusterDAO) ExistClusterDNSName(dnsName string, excludeClusterId int64) (bool, error) {
|
||||
return this.Query().
|
||||
Attr("dnsName", dnsName).
|
||||
State(NodeClusterStateEnabled).
|
||||
Where("id!=:clusterId").
|
||||
Param("clusterId", excludeClusterId).
|
||||
Exist()
|
||||
}
|
||||
|
||||
// 修改集群DNS相关信息
|
||||
func (this *NodeClusterDAO) UpdateClusterDNS(clusterId int64, dnsName string, dnsDomainId int64) error {
|
||||
if clusterId <= 0 {
|
||||
return errors.New("invalid clusterId")
|
||||
}
|
||||
op := NewNodeClusterOperator()
|
||||
op.Id = clusterId
|
||||
op.DnsName = dnsName
|
||||
op.DnsDomainId = dnsDomainId
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
func (this *NodeClusterDAO) genUniqueId() (string, error) {
|
||||
for {
|
||||
|
||||
@@ -125,7 +125,7 @@ func (this *NodeIPAddressDAO) UpdateAddressNodeId(addressId int64, nodeId int64)
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找某个节点所有的IP地址
|
||||
// 查找节点的所有的IP地址
|
||||
func (this *NodeIPAddressDAO) FindAllEnabledAddressesWithNode(nodeId int64) (result []*NodeIPAddress, err error) {
|
||||
_, err = this.Query().
|
||||
Attr("nodeId", nodeId).
|
||||
@@ -136,3 +136,15 @@ func (this *NodeIPAddressDAO) FindAllEnabledAddressesWithNode(nodeId int64) (res
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 查找节点的第一个可访问的IP地址
|
||||
func (this *NodeIPAddressDAO) FindFirstNodeIPAddress(nodeId int64) (string, error) {
|
||||
return this.Query().
|
||||
Attr("nodeId", nodeId).
|
||||
State(NodeIPAddressStateEnabled).
|
||||
Attr("canAccess", true).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Result("ip").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
@@ -39,3 +39,17 @@ func (this *Node) DecodeStatus() (*nodeconfigs.NodeStatus, error) {
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
// DNS线路
|
||||
func (this *Node) DNSRoute(dnsDomainId int64) (string, error) {
|
||||
routes := map[int64]string{} // domainId => route
|
||||
if len(this.DnsRoutes) == 0 || this.DnsRoutes == "null" {
|
||||
return "", nil
|
||||
}
|
||||
err := json.Unmarshal([]byte(this.DnsRoutes), &routes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
route, _ := routes[dnsDomainId]
|
||||
return route, nil
|
||||
}
|
||||
|
||||
@@ -798,6 +798,56 @@ func (this *ServerDAO) CountAllEnabledServersWithGroupId(groupId int64) (int64,
|
||||
Count()
|
||||
}
|
||||
|
||||
// 查询使用某个DNS域名的所有服务域名
|
||||
func (this *ServerDAO) FindAllServerDNSNamesWithDNSDomainId(dnsDomainId int64) ([]string, error) {
|
||||
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledClusterIdsWithDNSDomainId(dnsDomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(clusterIds) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ones, err := this.Query().
|
||||
State(ServerStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Attr("clusterId", clusterIds).
|
||||
Result("dnsName").
|
||||
Reuse(false). // 避免因为IN语句造成内存占用过多
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []string{}
|
||||
for _, one := range ones {
|
||||
dnsName := one.(*Server).DnsName
|
||||
if len(dnsName) == 0 {
|
||||
continue
|
||||
}
|
||||
result = append(result, dnsName)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 查找DNS名称为空的所有服务
|
||||
func (this *ServerDAO) FindAllServersToFixWithDNSDomainId(dnsDomainId int64) (result []*Server, err error) {
|
||||
clusterIds, err := SharedNodeClusterDAO.FindAllEnabledClusterIdsWithDNSDomainId(dnsDomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(clusterIds) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
_, err = this.Query().
|
||||
State(ServerStateEnabled).
|
||||
Attr("isOn", true).
|
||||
Attr("clusterId", clusterIds).
|
||||
Result("dnsName").
|
||||
Reuse(false). // 避免因为IN语句造成内存占用过多
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 创建事件
|
||||
func (this *ServerDAO) createEvent() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
|
||||
@@ -52,3 +52,13 @@ func TestServerDAO_genDNSName(t *testing.T) {
|
||||
}
|
||||
t.Log("dnsName:", dnsName)
|
||||
}
|
||||
|
||||
func TestServerDAO_FindAllServerDNSNamesWithDNSDomainId(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
dnsNames, err := SharedServerDAO.FindAllServerDNSNamesWithDNSDomainId(2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("dnsNames:", dnsNames)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user