diff --git a/internal/db/models/nameservers/ns_domain_dao.go b/internal/db/models/nameservers/ns_domain_dao.go index 9af61c11..21983fbd 100644 --- a/internal/db/models/nameservers/ns_domain_dao.go +++ b/internal/db/models/nameservers/ns_domain_dao.go @@ -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 +} diff --git a/internal/db/models/nameservers/ns_record_dao.go b/internal/db/models/nameservers/ns_record_dao.go index 9ebae981..782c556d 100644 --- a/internal/db/models/nameservers/ns_record_dao.go +++ b/internal/db/models/nameservers/ns_record_dao.go @@ -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 +} diff --git a/internal/db/models/nameservers/ns_record_model.go b/internal/db/models/nameservers/ns_record_model.go index 3433f5f0..44b09723 100644 --- a/internal/db/models/nameservers/ns_record_model.go +++ b/internal/db/models/nameservers/ns_record_model.go @@ -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{} // 状态 } diff --git a/internal/db/models/nameservers/ns_record_model_ext.go b/internal/db/models/nameservers/ns_record_model_ext.go index e0f8e7e2..ccb05483 100644 --- a/internal/db/models/nameservers/ns_record_model_ext.go +++ b/internal/db/models/nameservers/ns_record_model_ext.go @@ -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 +} diff --git a/internal/db/models/nameservers/ns_route_dao.go b/internal/db/models/nameservers/ns_route_dao.go index 537ec38a..e03d4638 100644 --- a/internal/db/models/nameservers/ns_route_dao.go +++ b/internal/db/models/nameservers/ns_route_dao.go @@ -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). diff --git a/internal/db/models/nameservers/ns_route_model.go b/internal/db/models/nameservers/ns_route_model.go index bb0c44b5..2f5bc8d8 100644 --- a/internal/db/models/nameservers/ns_route_model.go +++ b/internal/db/models/nameservers/ns_route_model.go @@ -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{} // 条件定义 diff --git a/internal/db/models/user_dao.go b/internal/db/models/user_dao.go index 02df7da4..9c650531 100644 --- a/internal/db/models/user_dao.go +++ b/internal/db/models/user_dao.go @@ -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). diff --git a/internal/nodes/api_node_services.go b/internal/nodes/api_node_services.go index 760e741b..7a00262c 100644 --- a/internal/nodes/api_node_services.go +++ b/internal/nodes/api_node_services.go @@ -4,6 +4,7 @@ package nodes import ( "github.com/TeaOSLab/EdgeAPI/internal/rpc/services" + "github.com/TeaOSLab/EdgeAPI/internal/rpc/services/nameservers" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "google.golang.org/grpc" ) @@ -89,6 +90,8 @@ func (this *APINode) registerServices(server *grpc.Server) { pb.RegisterLatestItemServiceServer(server, &services.LatestItemService{}) pb.RegisterNodeThresholdServiceServer(server, &services.NodeThresholdService{}) pb.RegisterHTTPFastcgiServiceServer(server, &services.HTTPFastcgiService{}) - pb.RegisterNSClusterServiceServer(server, &services.NSClusterService{}) - pb.RegisterNSNodeServiceServer(server, &services.NSNodeService{}) + pb.RegisterNSClusterServiceServer(server, &nameservers.NSClusterService{}) + pb.RegisterNSNodeServiceServer(server, &nameservers.NSNodeService{}) + pb.RegisterNSDomainServiceServer(server, &nameservers.NSDomainService{}) + pb.RegisterNSRecordServiceServer(server, &nameservers.NSRecordService{}) } diff --git a/internal/rpc/services/service_ns_cluster.go b/internal/rpc/services/nameservers/service_ns_cluster.go similarity index 97% rename from internal/rpc/services/service_ns_cluster.go rename to internal/rpc/services/nameservers/service_ns_cluster.go index c31aebd3..7b9290ef 100644 --- a/internal/rpc/services/service_ns_cluster.go +++ b/internal/rpc/services/nameservers/service_ns_cluster.go @@ -1,16 +1,17 @@ // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. -package services +package nameservers import ( "context" "github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers" + "github.com/TeaOSLab/EdgeAPI/internal/rpc/services" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" ) // NSClusterService 域名服务集群相关服务 type NSClusterService struct { - BaseService + services.BaseService } // CreateNSCluster 创建集群 diff --git a/internal/rpc/services/nameservers/service_ns_domain.go b/internal/rpc/services/nameservers/service_ns_domain.go new file mode 100644 index 00000000..02981109 --- /dev/null +++ b/internal/rpc/services/nameservers/service_ns_domain.go @@ -0,0 +1,191 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package nameservers + +import ( + "context" + "github.com/TeaOSLab/EdgeAPI/internal/db/models" + "github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers" + "github.com/TeaOSLab/EdgeAPI/internal/rpc/services" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" +) + +// NSDomainService 域名相关服务 +type NSDomainService struct { + services.BaseService +} + +// CreateNSDomain 创建域名 +func (this *NSDomainService) CreateNSDomain(ctx context.Context, req *pb.CreateNSDomainRequest) (*pb.CreateNSDomainResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + domainId, err := nameservers.SharedNSDomainDAO.CreateDomain(tx, req.NsClusterId, req.UserId, req.Name) + if err != nil { + return nil, err + } + return &pb.CreateNSDomainResponse{NsDomainId: domainId}, nil +} + +// UpdateNSDomain 修改域名 +func (this *NSDomainService) UpdateNSDomain(ctx context.Context, req *pb.UpdateNSDomainRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + err = nameservers.SharedNSDomainDAO.UpdateDomain(tx, req.NsDomainId, req.NsClusterId, req.UserId, req.Name, req.IsOn) + if err != nil { + return nil, err + } + return this.Success() +} + +// DeleteNSDomain 删除域名 +func (this *NSDomainService) DeleteNSDomain(ctx context.Context, req *pb.DeleteNSDomainRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + err = nameservers.SharedNSDomainDAO.DisableNSDomain(tx, req.NsDomainId) + if err != nil { + return nil, err + } + return this.Success() +} + +// FindEnabledNSDomain 查找单个域名 +func (this *NSDomainService) FindEnabledNSDomain(ctx context.Context, req *pb.FindEnabledNSDomainRequest) (*pb.FindEnabledNSDomainResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, req.NsDomainId) + if err != nil { + return nil, err + } + if domain == nil { + return &pb.FindEnabledNSDomainResponse{NsDomain: nil}, nil + } + + // 集群 + cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(domain.ClusterId)) + if err != nil { + return nil, err + } + if cluster == nil { + return &pb.FindEnabledNSDomainResponse{NsDomain: nil}, nil + } + + // 用户 + var pbUser *pb.User + if domain.UserId > 0 { + user, err := models.SharedUserDAO.FindEnabledUser(tx, int64(domain.UserId)) + if err != nil { + return nil, err + } + if user == nil { + return &pb.FindEnabledNSDomainResponse{NsDomain: nil}, nil + } + pbUser = &pb.User{ + Id: int64(user.Id), + Username: user.Username, + Fullname: user.Fullname, + } + } + + return &pb.FindEnabledNSDomainResponse{ + NsDomain: &pb.NSDomain{ + Id: int64(domain.Id), + Name: domain.Name, + IsOn: domain.IsOn == 1, + CreatedAt: int64(domain.CreatedAt), + NsCluster: &pb.NSCluster{ + Id: int64(cluster.Id), + IsOn: cluster.IsOn == 1, + Name: cluster.Name, + }, + User: pbUser, + }, + }, nil +} + +// CountAllEnabledNSDomains 计算域名数量 +func (this *NSDomainService) CountAllEnabledNSDomains(ctx context.Context, req *pb.CountAllEnabledNSDomainsRequest) (*pb.RPCCountResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + count, err := nameservers.SharedNSDomainDAO.CountAllEnabledDomains(tx, req.NsClusterId, req.UserId, req.Keyword) + if err != nil { + return nil, err + } + return this.SuccessCount(count) +} + +// ListEnabledNSDomains 列出单页域名 +func (this *NSDomainService) ListEnabledNSDomains(ctx context.Context, req *pb.ListEnabledNSDomainsRequest) (*pb.ListEnabledNSDomainsResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + domains, err := nameservers.SharedNSDomainDAO.ListEnabledDomains(tx, req.NsClusterId, req.UserId, req.Keyword, req.Offset, req.Size) + if err != nil { + return nil, err + } + pbDomains := []*pb.NSDomain{} + for _, domain := range domains { + // 集群 + cluster, err := nameservers.SharedNSClusterDAO.FindEnabledNSCluster(tx, int64(domain.ClusterId)) + if err != nil { + return nil, err + } + if cluster == nil { + continue + } + + // 用户 + var pbUser *pb.User + if domain.UserId > 0 { + user, err := models.SharedUserDAO.FindEnabledUser(tx, int64(domain.UserId)) + if err != nil { + return nil, err + } + if user == nil { + continue + } + pbUser = &pb.User{ + Id: int64(user.Id), + Username: user.Username, + Fullname: user.Fullname, + } + } + + pbDomains = append(pbDomains, &pb.NSDomain{ + Id: int64(domain.Id), + Name: domain.Name, + IsOn: domain.IsOn == 1, + CreatedAt: int64(domain.CreatedAt), + NsCluster: &pb.NSCluster{ + Id: int64(cluster.Id), + IsOn: cluster.IsOn == 1, + Name: cluster.Name, + }, + User: pbUser, + }) + } + + return &pb.ListEnabledNSDomainsResponse{NsDomains: pbDomains}, nil +} diff --git a/internal/rpc/services/service_ns_node.go b/internal/rpc/services/nameservers/service_ns_node.go similarity index 98% rename from internal/rpc/services/service_ns_node.go rename to internal/rpc/services/nameservers/service_ns_node.go index 9b07e724..40f33a0b 100644 --- a/internal/rpc/services/service_ns_node.go +++ b/internal/rpc/services/nameservers/service_ns_node.go @@ -1,19 +1,20 @@ // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. -package services +package nameservers import ( "context" "github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers" "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/installers" + "github.com/TeaOSLab/EdgeAPI/internal/rpc/services" "github.com/TeaOSLab/EdgeCommon/pkg/configutils" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" ) // NSNodeService 域名服务器节点服务 type NSNodeService struct { - BaseService + services.BaseService } // FindAllEnabledNSNodesWithNSClusterId 根据集群查找所有节点 diff --git a/internal/rpc/services/nameservers/service_ns_record.go b/internal/rpc/services/nameservers/service_ns_record.go new file mode 100644 index 00000000..44bdc405 --- /dev/null +++ b/internal/rpc/services/nameservers/service_ns_record.go @@ -0,0 +1,182 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package nameservers + +import ( + "context" + "github.com/TeaOSLab/EdgeAPI/internal/db/models/nameservers" + "github.com/TeaOSLab/EdgeAPI/internal/rpc/services" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/types" +) + +// NSRecordService 域名记录相关服务 +type NSRecordService struct { + services.BaseService +} + +// CreateNSRecord 创建记录 +func (this *NSRecordService) CreateNSRecord(ctx context.Context, req *pb.CreateNSRecordRequest) (*pb.CreateNSRecordResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + recordId, err := nameservers.SharedNSRecordDAO.CreateRecord(tx, req.NsDomainId, req.Description, req.Name, req.Type, req.Value, req.Ttl, req.NsRouteIds) + if err != nil { + return nil, err + } + return &pb.CreateNSRecordResponse{NsRecordId: recordId}, nil +} + +// UpdateNSRecord 修改记录 +func (this *NSRecordService) UpdateNSRecord(ctx context.Context, req *pb.UpdateNSRecordRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + err = nameservers.SharedNSRecordDAO.UpdateRecord(tx, req.NsRecordId, req.Description, req.Name, req.Type, req.Value, req.Ttl, req.NsRouteIds) + if err != nil { + return nil, err + } + return this.Success() +} + +// DeleteNSRecord 删除记录 +func (this *NSRecordService) DeleteNSRecord(ctx context.Context, req *pb.DeleteNSRecordRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + err = nameservers.SharedNSRecordDAO.DisableNSRecord(tx, req.NsRecordId) + if err != nil { + return nil, err + } + return this.Success() +} + +// CountAllEnabledNSRecords 计算记录数量 +func (this *NSRecordService) CountAllEnabledNSRecords(ctx context.Context, req *pb.CountAllEnabledNSRecordsRequest) (*pb.RPCCountResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + count, err := nameservers.SharedNSRecordDAO.CountAllEnabledRecords(tx, req.NsDomainId, req.Type, req.Keyword, req.NsRouteId) + if err != nil { + return nil, err + } + return this.SuccessCount(count) +} + +// ListEnabledNSRecords 读取单页记录 +func (this *NSRecordService) ListEnabledNSRecords(ctx context.Context, req *pb.ListEnabledNSRecordsRequest) (*pb.ListEnabledNSRecordsResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + records, err := nameservers.SharedNSRecordDAO.ListAllEnabledRecords(tx, req.NsDomainId, req.Type, req.Keyword, req.NsRouteId, req.Offset, req.Size) + if err != nil { + return nil, err + } + var pbRecords = []*pb.NSRecord{} + for _, record := range records { + // 线路 + var pbRoutes = []*pb.NSRoute{} + for _, recordId := range record.DecodeRouteIds() { + route, err := nameservers.SharedNSRouteDAO.FindEnabledNSRoute(tx, recordId) + if err != nil { + return nil, err + } + if route == nil { + continue + } + pbRoutes = append(pbRoutes, &pb.NSRoute{ + Id: int64(route.Id), + Name: route.Name, + }) + } + + pbRecords = append(pbRecords, &pb.NSRecord{ + Id: int64(record.Id), + Description: record.Description, + Name: record.Name, + Type: record.Type, + Value: record.Value, + Ttl: types.Int32(record.Ttl), + Weight: types.Int32(record.Weight), + CreatedAt: int64(record.CreatedAt), + NsDomain: nil, + NsRoutes: pbRoutes, + }) + } + return &pb.ListEnabledNSRecordsResponse{NsRecords: pbRecords}, nil +} + +// FindEnabledNSRecord 查询单个记录信息 +func (this *NSRecordService) FindEnabledNSRecord(ctx context.Context, req *pb.FindEnabledNSRecordRequest) (*pb.FindEnabledNSRecordResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + record, err := nameservers.SharedNSRecordDAO.FindEnabledNSRecord(tx, req.NsRecordId) + if err != nil { + return nil, err + } + if record == nil { + return &pb.FindEnabledNSRecordResponse{NsRecord: nil}, nil + } + + // 域名 + domain, err := nameservers.SharedNSDomainDAO.FindEnabledNSDomain(tx, int64(record.DomainId)) + if err != nil { + return nil, err + } + if domain == nil { + return &pb.FindEnabledNSRecordResponse{NsRecord: nil}, nil + } + var pbDomain = &pb.NSDomain{ + Id: int64(domain.Id), + Name: domain.Name, + IsOn: domain.IsOn == 1, + } + + // 线路 + var pbRoutes = []*pb.NSRoute{} + for _, recordId := range record.DecodeRouteIds() { + route, err := nameservers.SharedNSRouteDAO.FindEnabledNSRoute(tx, recordId) + if err != nil { + return nil, err + } + if route == nil { + continue + } + pbRoutes = append(pbRoutes, &pb.NSRoute{ + Id: int64(route.Id), + Name: route.Name, + }) + } + + return &pb.FindEnabledNSRecordResponse{NsRecord: &pb.NSRecord{ + Id: int64(record.Id), + Description: record.Description, + Name: record.Name, + Type: record.Type, + Value: record.Value, + Ttl: types.Int32(record.Ttl), + Weight: types.Int32(record.Weight), + CreatedAt: int64(record.CreatedAt), + NsDomain: pbDomain, + NsRoutes: pbRoutes, + }}, nil +} diff --git a/internal/rpc/services/service_user.go b/internal/rpc/services/service_user.go index af646d38..baa8f3a5 100644 --- a/internal/rpc/services/service_user.go +++ b/internal/rpc/services/service_user.go @@ -12,12 +12,12 @@ import ( "time" ) -// 用户相关服务 +// UserService 用户相关服务 type UserService struct { BaseService } -// 创建用户 +// CreateUser 创建用户 func (this *UserService) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { @@ -33,7 +33,7 @@ func (this *UserService) CreateUser(ctx context.Context, req *pb.CreateUserReque return &pb.CreateUserResponse{UserId: userId}, nil } -// 修改用户 +// UpdateUser 修改用户 func (this *UserService) UpdateUser(ctx context.Context, req *pb.UpdateUserRequest) (*pb.RPCSuccess, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { @@ -62,7 +62,7 @@ func (this *UserService) UpdateUser(ctx context.Context, req *pb.UpdateUserReque return this.Success() } -// 删除用户 +// DeleteUser 删除用户 func (this *UserService) DeleteUser(ctx context.Context, req *pb.DeleteUserRequest) (*pb.RPCSuccess, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { @@ -90,7 +90,7 @@ func (this *UserService) DeleteUser(ctx context.Context, req *pb.DeleteUserReque return this.Success() } -// 计算用户数量 +// CountAllEnabledUsers 计算用户数量 func (this *UserService) CountAllEnabledUsers(ctx context.Context, req *pb.CountAllEnabledUsersRequest) (*pb.RPCCountResponse, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { @@ -106,7 +106,7 @@ func (this *UserService) CountAllEnabledUsers(ctx context.Context, req *pb.Count return this.SuccessCount(count) } -// 列出单页用户 +// ListEnabledUsers 列出单页用户 func (this *UserService) ListEnabledUsers(ctx context.Context, req *pb.ListEnabledUsersRequest) (*pb.ListEnabledUsersResponse, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { @@ -115,7 +115,7 @@ func (this *UserService) ListEnabledUsers(ctx context.Context, req *pb.ListEnabl tx := this.NullTx() - users, err := models.SharedUserDAO.ListEnabledUsers(tx, req.Keyword) + users, err := models.SharedUserDAO.ListEnabledUsers(tx, req.Keyword, req.Offset, req.Size) if err != nil { return nil, err } @@ -152,7 +152,7 @@ func (this *UserService) ListEnabledUsers(ctx context.Context, req *pb.ListEnabl return &pb.ListEnabledUsersResponse{Users: result}, nil } -// 查询单个用户信息 +// FindEnabledUser 查询单个用户信息 func (this *UserService) FindEnabledUser(ctx context.Context, req *pb.FindEnabledUserRequest) (*pb.FindEnabledUserResponse, error) { _, _, err := this.ValidateAdminAndUser(ctx, 0, 0) if err != nil { @@ -196,7 +196,7 @@ func (this *UserService) FindEnabledUser(ctx context.Context, req *pb.FindEnable }}, nil } -// 检查用户名是否存在 +// CheckUserUsername 检查用户名是否存在 func (this *UserService) CheckUserUsername(ctx context.Context, req *pb.CheckUserUsernameRequest) (*pb.CheckUserUsernameResponse, error) { userType, userId, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin, rpcutils.UserTypeUser) if err != nil { @@ -217,7 +217,7 @@ func (this *UserService) CheckUserUsername(ctx context.Context, req *pb.CheckUse return &pb.CheckUserUsernameResponse{Exists: b}, nil } -// 登录 +// LoginUser 登录 func (this *UserService) LoginUser(ctx context.Context, req *pb.LoginUserRequest) (*pb.LoginUserResponse, error) { _, _, err := rpcutils.ValidateRequest(ctx) if err != nil { @@ -254,7 +254,7 @@ func (this *UserService) LoginUser(ctx context.Context, req *pb.LoginUserRequest }, nil } -// 修改用户基本信息 +// UpdateUserInfo 修改用户基本信息 func (this *UserService) UpdateUserInfo(ctx context.Context, req *pb.UpdateUserInfoRequest) (*pb.RPCSuccess, error) { userId, err := this.ValidateUser(ctx) if err != nil { @@ -274,7 +274,7 @@ func (this *UserService) UpdateUserInfo(ctx context.Context, req *pb.UpdateUserI return this.Success() } -// 修改用户登录信息 +// UpdateUserLogin 修改用户登录信息 func (this *UserService) UpdateUserLogin(ctx context.Context, req *pb.UpdateUserLoginRequest) (*pb.RPCSuccess, error) { userId, err := this.ValidateUser(ctx) if err != nil { @@ -294,7 +294,7 @@ func (this *UserService) UpdateUserLogin(ctx context.Context, req *pb.UpdateUser return this.Success() } -// 取得用户Dashboard数据 +// ComposeUserDashboard 取得用户Dashboard数据 func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.ComposeUserDashboardRequest) (*pb.ComposeUserDashboardResponse, error) { userId, err := this.ValidateUser(ctx) if err != nil { @@ -371,7 +371,7 @@ func (this *UserService) ComposeUserDashboard(ctx context.Context, req *pb.Compo }, nil } -// 获取用户所在的集群ID +// FindUserNodeClusterId 获取用户所在的集群ID func (this *UserService) FindUserNodeClusterId(ctx context.Context, req *pb.FindUserNodeClusterIdRequest) (*pb.FindUserNodeClusterIdResponse, error) { _, _, err := this.ValidateAdminAndUser(ctx, 0, req.UserId) if err != nil { @@ -387,7 +387,7 @@ func (this *UserService) FindUserNodeClusterId(ctx context.Context, req *pb.Find return &pb.FindUserNodeClusterIdResponse{NodeClusterId: clusterId}, nil } -// 设置用户能使用的功能 +// UpdateUserFeatures 设置用户能使用的功能 func (this *UserService) UpdateUserFeatures(ctx context.Context, req *pb.UpdateUserFeaturesRequest) (*pb.RPCSuccess, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { @@ -408,7 +408,7 @@ func (this *UserService) UpdateUserFeatures(ctx context.Context, req *pb.UpdateU return this.Success() } -// 获取用户所有的功能列表 +// FindUserFeatures 获取用户所有的功能列表 func (this *UserService) FindUserFeatures(ctx context.Context, req *pb.FindUserFeaturesRequest) (*pb.FindUserFeaturesResponse, error) { _, userId, err := this.ValidateAdminAndUser(ctx, 0, req.UserId) if err != nil { @@ -435,7 +435,7 @@ func (this *UserService) FindUserFeatures(ctx context.Context, req *pb.FindUserF return &pb.FindUserFeaturesResponse{Features: result}, nil } -// 获取所有的功能定义 +// FindAllUserFeatureDefinitions 获取所有的功能定义 func (this *UserService) FindAllUserFeatureDefinitions(ctx context.Context, req *pb.FindAllUserFeatureDefinitionsRequest) (*pb.FindAllUserFeatureDefinitionsResponse, error) { _, err := this.ValidateAdmin(ctx, 0) if err != nil { diff --git a/internal/tasks/node_log_cleaner_task_test.go b/internal/tasks/node_log_cleaner_task_test.go index 0d81449a..e7d32a47 100644 --- a/internal/tasks/node_log_cleaner_task_test.go +++ b/internal/tasks/node_log_cleaner_task_test.go @@ -1,8 +1,13 @@ package tasks -import "testing" +import ( + "github.com/iwind/TeaGo/dbs" + "testing" +) func TestNodeLogCleaner_loop(t *testing.T) { + dbs.NotifyReady() + cleaner := &NodeLogCleanerTask{} err := cleaner.loop() if err != nil {