实现基本的域名、记录管理

This commit is contained in:
GoEdgeLab
2021-05-27 17:09:07 +08:00
parent 402f8dde4f
commit 02188e6863
14 changed files with 622 additions and 52 deletions

View File

@@ -1,6 +1,8 @@
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"
"github.com/iwind/TeaGo/dbs"
@@ -33,7 +35,7 @@ func init() {
}
// EnableNSDomain 启用条目
func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, id uint32) error {
func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSDomainStateEnabled).
@@ -42,7 +44,7 @@ func (this *NSDomainDAO) EnableNSDomain(tx *dbs.Tx, id uint32) error {
}
// DisableNSDomain 禁用条目
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, id uint32) error {
func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSDomainStateDisabled).
@@ -51,7 +53,7 @@ func (this *NSDomainDAO) DisableNSDomain(tx *dbs.Tx, id uint32) error {
}
// FindEnabledNSDomain 查找启用中的条目
func (this *NSDomainDAO) FindEnabledNSDomain(tx *dbs.Tx, id uint32) (*NSDomain, error) {
func (this *NSDomainDAO) FindEnabledNSDomain(tx *dbs.Tx, id int64) (*NSDomain, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSDomainStateEnabled).
@@ -63,9 +65,84 @@ func (this *NSDomainDAO) FindEnabledNSDomain(tx *dbs.Tx, id uint32) (*NSDomain,
}
// FindNSDomainName 根据主键查找名称
func (this *NSDomainDAO) FindNSDomainName(tx *dbs.Tx, id uint32) (string, error) {
func (this *NSDomainDAO) FindNSDomainName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreateDomain 创建域名
func (this *NSDomainDAO) CreateDomain(tx *dbs.Tx, clusterId int64, userId int64, name string) (int64, error) {
op := NewNSDomainOperator()
op.ClusterId = clusterId
op.UserId = userId
op.Name = name
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 {
if domainId <= 0 {
return errors.New("invalid domainId")
}
op := NewNSDomainOperator()
op.Id = domainId
op.ClusterId = clusterId
op.UserId = userId
op.Name = name
op.IsOn = isOn
return this.Save(tx, op)
}
// CountAllEnabledDomains 计算域名数量
func (this *NSDomainDAO) CountAllEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string) (int64, error) {
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
} else {
query.Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)")
}
if userId > 0 {
query.Attr("userId", userId)
} else {
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
return query.
State(NSDomainStateEnabled).
Count()
}
// ListEnabledDomains 列出单页域名
func (this *NSDomainDAO) ListEnabledDomains(tx *dbs.Tx, clusterId int64, userId int64, keyword string, offset int64, size int64) (result []*NSDomain, err error) {
query := this.Query(tx)
if clusterId > 0 {
query.Attr("clusterId", clusterId)
} else {
query.Where("clusterId IN (SELECT id FROM " + SharedNSClusterDAO.Table + " WHERE state=1)")
}
if userId > 0 {
query.Attr("userId", userId)
} else {
query.Where("(userId=0 OR userId IN (SELECT id FROM " + models.SharedUserDAO.Table + " WHERE state=1))")
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
_, err = query.
State(NSDomainStateEnabled).
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}

View File

@@ -1,6 +1,9 @@
package nameservers
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -42,7 +45,7 @@ func (this *NSRecordDAO) EnableNSRecord(tx *dbs.Tx, id uint64) error {
}
// DisableNSRecord 禁用条目
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id uint64) error {
func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", NSRecordStateDisabled).
@@ -51,7 +54,7 @@ func (this *NSRecordDAO) DisableNSRecord(tx *dbs.Tx, id uint64) error {
}
// FindEnabledNSRecord 查找启用中的条目
func (this *NSRecordDAO) FindEnabledNSRecord(tx *dbs.Tx, id uint64) (*NSRecord, error) {
func (this *NSRecordDAO) FindEnabledNSRecord(tx *dbs.Tx, id int64) (*NSRecord, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSRecordStateEnabled).
@@ -63,9 +66,100 @@ func (this *NSRecordDAO) FindEnabledNSRecord(tx *dbs.Tx, id uint64) (*NSRecord,
}
// FindNSRecordName 根据主键查找名称
func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id uint64) (string, error) {
func (this *NSRecordDAO) FindNSRecordName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// 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) {
op := NewNSRecordOperator()
op.DomainId = domainId
op.Description = description
op.Name = name
op.Type = dnsType
op.Value = value
op.Ttl = ttl
if len(routeIds) == 0 {
op.RouteIds = "[]"
} else {
routeIds, err := json.Marshal(routeIds)
if err != nil {
return 0, err
}
op.RouteIds = routeIds
}
op.IsOn = true
op.State = NSRecordStateEnabled
return this.SaveInt64(tx, op)
}
func (this *NSRecordDAO) UpdateRecord(tx *dbs.Tx, recordId int64, description string, name string, dnsType dnsconfigs.RecordType, value string, ttl int32, routeIds []int64) error {
if recordId <= 0 {
return errors.New("invalid recordId")
}
op := NewNSRecordOperator()
op.Id = recordId
op.Description = description
op.Name = name
op.Type = dnsType
op.Value = value
op.Ttl = ttl
if len(routeIds) == 0 {
op.RouteIds = "[]"
} else {
routeIds, err := json.Marshal(routeIds)
if err != nil {
return err
}
op.RouteIds = routeIds
}
return this.Save(tx, op)
}
func (this *NSRecordDAO) CountAllEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64) (int64, error) {
query := this.Query(tx).
Attr("domainId", domainId).
State(NSRecordStateEnabled)
if len(dnsType) > 0 {
query.Attr("type", dnsType)
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if routeId > 0 {
query.JSONContains("routeIds", routeId)
}
return query.Count()
}
func (this *NSRecordDAO) ListAllEnabledRecords(tx *dbs.Tx, domainId int64, dnsType dnsconfigs.RecordType, keyword string, routeId int64, offset int64, size int64) (result []*NSRecord, err error) {
query := this.Query(tx).
Attr("domainId", domainId).
State(NSRecordStateEnabled)
if len(dnsType) > 0 {
query.Attr("type", dnsType)
}
if len(keyword) > 0 {
query.Where("(name LIKE :keyword OR description LIKE :keyword)").
Param("keyword", "%"+keyword+"%")
}
if routeId > 0 {
query.JSONContains("routeIds", routeId)
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}

View File

@@ -4,13 +4,14 @@ package nameservers
type NSRecord struct {
Id uint64 `field:"id"` // ID
DomainId uint32 `field:"domainId"` // 域名ID
IsOn uint8 `field:"isOn"` // 是否启用
Description string `field:"description"` // 备注
Name string `field:"name"` // 记录名
Type string `field:"type"` // 类型
Value string `field:"value"` // 值
Ttl uint32 `field:"ttl"` // TTL
Weight uint32 `field:"weight"` // 权重
Routes string `field:"routes"` // 线路
RouteIds string `field:"routeIds"` // 线路
CreatedAt uint64 `field:"createdAt"` // 创建时间
State uint8 `field:"state"` // 状态
}
@@ -18,13 +19,14 @@ type NSRecord struct {
type NSRecordOperator struct {
Id interface{} // ID
DomainId interface{} // 域名ID
IsOn interface{} // 是否启用
Description interface{} // 备注
Name interface{} // 记录名
Type interface{} // 类型
Value interface{} // 值
Ttl interface{} // TTL
Weight interface{} // 权重
Routes interface{} // 线路
RouteIds interface{} // 线路
CreatedAt interface{} // 创建时间
State interface{} // 状态
}

View File

@@ -1 +1,11 @@
package nameservers
import "encoding/json"
func (this *NSRecord) DecodeRouteIds() []int64 {
routeIds := []int64{}
if len(this.RouteIds) > 0 {
_ = json.Unmarshal([]byte(this.RouteIds), &routeIds)
}
return routeIds
}

View File

@@ -51,7 +51,7 @@ func (this *NSRouteDAO) DisableNSRoute(tx *dbs.Tx, id uint32) error {
}
// FindEnabledNSRoute 查找启用中的条目
func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id uint32) (*NSRoute, error) {
func (this *NSRouteDAO) FindEnabledNSRoute(tx *dbs.Tx, id int64) (*NSRoute, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", NSRouteStateEnabled).

View File

@@ -4,6 +4,7 @@ package nameservers
type NSRoute struct {
Id uint32 `field:"id"` // ID
ClusterId uint32 `field:"clusterId"` // 集群ID
DomainId uint32 `field:"domainId"` // 域名ID
UserId uint32 `field:"userId"` // 用户ID
Name string `field:"name"` // 名称
Conds string `field:"conds"` // 条件定义
@@ -14,6 +15,7 @@ type NSRoute struct {
type NSRouteOperator struct {
Id interface{} // ID
ClusterId interface{} // 集群ID
DomainId interface{} // 域名ID
UserId interface{} // 用户ID
Name interface{} // 名称
Conds interface{} // 条件定义

View File

@@ -36,7 +36,7 @@ func init() {
})
}
// 启用条目
// EnableUser 启用条目
func (this *UserDAO) EnableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -44,7 +44,7 @@ func (this *UserDAO) EnableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err e
Update()
}
// 禁用条目
// DisableUser 禁用条目
func (this *UserDAO) DisableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err error) {
return this.Query(tx).
Pk(id).
@@ -52,7 +52,7 @@ func (this *UserDAO) DisableUser(tx *dbs.Tx, id int64) (rowsAffected int64, err
Update()
}
// 查找启用中的条目
// FindEnabledUser 查找启用中的条目
func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, id int64) (*User, error) {
result, err := this.Query(tx).
Pk(id).
@@ -64,7 +64,7 @@ func (this *UserDAO) FindEnabledUser(tx *dbs.Tx, id int64) (*User, error) {
return result.(*User), err
}
// 查找用户基本信息
// FindEnabledBasicUser 查找用户基本信息
func (this *UserDAO) FindEnabledBasicUser(tx *dbs.Tx, id int64) (*User, error) {
result, err := this.Query(tx).
Pk(id).
@@ -77,7 +77,7 @@ func (this *UserDAO) FindEnabledBasicUser(tx *dbs.Tx, id int64) (*User, error) {
return result.(*User), err
}
// 获取管理员名称
// FindUserFullname 获取管理员名称
func (this *UserDAO) FindUserFullname(tx *dbs.Tx, userId int64) (string, error) {
return this.Query(tx).
Pk(userId).
@@ -85,7 +85,7 @@ func (this *UserDAO) FindUserFullname(tx *dbs.Tx, userId int64) (string, error)
FindStringCol("")
}
// 创建用户
// CreateUser 创建用户
func (this *UserDAO) CreateUser(tx *dbs.Tx, username string, password string, fullname string, mobile string, tel string, email string, remark string, source string, clusterId int64) (int64, error) {
op := NewUserOperator()
op.Username = username
@@ -107,7 +107,7 @@ func (this *UserDAO) CreateUser(tx *dbs.Tx, username string, password string, fu
return types.Int64(op.Id), nil
}
// 修改用户
// UpdateUser 修改用户
func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, password string, fullname string, mobile string, tel string, email string, remark string, isOn bool, clusterId int64) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -129,7 +129,7 @@ func (this *UserDAO) UpdateUser(tx *dbs.Tx, userId int64, username string, passw
return err
}
// 修改用户基本信息
// UpdateUserInfo 修改用户基本信息
func (this *UserDAO) UpdateUserInfo(tx *dbs.Tx, userId int64, fullname string) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -140,7 +140,7 @@ func (this *UserDAO) UpdateUserInfo(tx *dbs.Tx, userId int64, fullname string) e
return this.Save(tx, op)
}
// 修改用户登录信息
// UpdateUserLogin 修改用户登录信息
func (this *UserDAO) UpdateUserLogin(tx *dbs.Tx, userId int64, username string, password string) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -155,7 +155,7 @@ func (this *UserDAO) UpdateUserLogin(tx *dbs.Tx, userId int64, username string,
return err
}
// 计算用户数量
// CountAllEnabledUsers 计算用户数量
func (this *UserDAO) CountAllEnabledUsers(tx *dbs.Tx, keyword string) (int64, error) {
query := this.Query(tx)
query.State(UserStateEnabled)
@@ -166,8 +166,8 @@ func (this *UserDAO) CountAllEnabledUsers(tx *dbs.Tx, keyword string) (int64, er
return query.Count()
}
// 列出单页用户
func (this *UserDAO) ListEnabledUsers(tx *dbs.Tx, keyword string) (result []*User, err error) {
// ListEnabledUsers 列出单页用户
func (this *UserDAO) ListEnabledUsers(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*User, err error) {
query := this.Query(tx)
query.State(UserStateEnabled)
if len(keyword) > 0 {
@@ -176,12 +176,14 @@ func (this *UserDAO) ListEnabledUsers(tx *dbs.Tx, keyword string) (result []*Use
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// 检查用户名是否存在
// ExistUser 检查用户名是否存在
func (this *UserDAO) ExistUser(tx *dbs.Tx, userId int64, username string) (bool, error) {
return this.Query(tx).
State(UserStateEnabled).
@@ -190,7 +192,7 @@ func (this *UserDAO) ExistUser(tx *dbs.Tx, userId int64, username string) (bool,
Exist()
}
// 列出单页的用户ID
// ListEnabledUserIds 列出单页的用户ID
func (this *UserDAO) ListEnabledUserIds(tx *dbs.Tx, offset, size int64) ([]int64, error) {
ones, _, err := this.Query(tx).
ResultPk().
@@ -209,7 +211,7 @@ func (this *UserDAO) ListEnabledUserIds(tx *dbs.Tx, offset, size int64) ([]int64
return result, nil
}
// 检查用户名、密码
// CheckUserPassword 检查用户名、密码
func (this *UserDAO) CheckUserPassword(tx *dbs.Tx, username string, encryptedPassword string) (int64, error) {
if len(username) == 0 || len(encryptedPassword) == 0 {
return 0, nil
@@ -223,7 +225,7 @@ func (this *UserDAO) CheckUserPassword(tx *dbs.Tx, username string, encryptedPas
FindInt64Col(0)
}
// 查找用户所在集群
// FindUserClusterId 查找用户所在集群
func (this *UserDAO) FindUserClusterId(tx *dbs.Tx, userId int64) (int64, error) {
return this.Query(tx).
Pk(userId).
@@ -231,7 +233,7 @@ func (this *UserDAO) FindUserClusterId(tx *dbs.Tx, userId int64) (int64, error)
FindInt64Col(0)
}
// 更新用户Features
// UpdateUserFeatures 更新用户Features
func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON []byte) error {
if userId <= 0 {
return errors.New("invalid userId")
@@ -249,7 +251,7 @@ func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON [
return nil
}
// 查找用户Features
// FindUserFeatures 查找用户Features
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*UserFeature, error) {
featuresJSON, err := this.Query(tx).
Pk(userId).