管理端全部功能跑通
This commit is contained in:
193
EdgeAPI/internal/rpc/services/httpdns/service_httpdns_rule.go
Normal file
193
EdgeAPI/internal/rpc/services/httpdns/service_httpdns_rule.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package httpdns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
// HTTPDNSRuleService HTTPDNS规则服务
|
||||
type HTTPDNSRuleService struct {
|
||||
services.BaseService
|
||||
pb.UnimplementedHTTPDNSRuleServiceServer
|
||||
}
|
||||
|
||||
func (this *HTTPDNSRuleService) CreateHTTPDNSCustomRule(ctx context.Context, req *pb.CreateHTTPDNSCustomRuleRequest) (*pb.CreateHTTPDNSCustomRuleResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.Rule == nil {
|
||||
return nil, errors.New("required 'rule'")
|
||||
}
|
||||
var ruleId int64
|
||||
err = this.RunTx(func(tx *dbs.Tx) error {
|
||||
rule := &models.HTTPDNSCustomRule{
|
||||
AppId: uint32(req.Rule.AppId),
|
||||
DomainId: uint32(req.Rule.DomainId),
|
||||
RuleName: req.Rule.RuleName,
|
||||
LineScope: req.Rule.LineScope,
|
||||
LineCarrier: req.Rule.LineCarrier,
|
||||
LineRegion: req.Rule.LineRegion,
|
||||
LineProvince: req.Rule.LineProvince,
|
||||
LineContinent: req.Rule.LineContinent,
|
||||
LineCountry: req.Rule.LineCountry,
|
||||
TTL: req.Rule.Ttl,
|
||||
IsOn: req.Rule.IsOn,
|
||||
Priority: req.Rule.Priority,
|
||||
}
|
||||
ruleId, err = models.SharedHTTPDNSCustomRuleDAO.CreateRule(tx, rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, record := range req.Rule.Records {
|
||||
_, err := models.SharedHTTPDNSCustomRuleRecordDAO.CreateRecord(tx, ruleId, record.RecordType, record.RecordValue, record.Weight, record.Sort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return notifyHTTPDNSAppTasksByAppDbId(tx, req.Rule.AppId, models.HTTPDNSNodeTaskTypeRuleChanged)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateHTTPDNSCustomRuleResponse{RuleId: ruleId}, nil
|
||||
}
|
||||
|
||||
func (this *HTTPDNSRuleService) UpdateHTTPDNSCustomRule(ctx context.Context, req *pb.UpdateHTTPDNSCustomRuleRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.Rule == nil || req.Rule.Id <= 0 {
|
||||
return nil, errors.New("invalid 'rule.id'")
|
||||
}
|
||||
err = this.RunTx(func(tx *dbs.Tx) error {
|
||||
oldRule, err := models.SharedHTTPDNSCustomRuleDAO.FindEnabledRule(tx, req.Rule.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if oldRule == nil {
|
||||
return errors.New("rule not found")
|
||||
}
|
||||
|
||||
rule := &models.HTTPDNSCustomRule{
|
||||
Id: uint32(req.Rule.Id),
|
||||
RuleName: req.Rule.RuleName,
|
||||
LineScope: req.Rule.LineScope,
|
||||
LineCarrier: req.Rule.LineCarrier,
|
||||
LineRegion: req.Rule.LineRegion,
|
||||
LineProvince: req.Rule.LineProvince,
|
||||
LineContinent: req.Rule.LineContinent,
|
||||
LineCountry: req.Rule.LineCountry,
|
||||
TTL: req.Rule.Ttl,
|
||||
IsOn: req.Rule.IsOn,
|
||||
Priority: req.Rule.Priority,
|
||||
}
|
||||
err = models.SharedHTTPDNSCustomRuleDAO.UpdateRule(tx, rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = models.SharedHTTPDNSCustomRuleRecordDAO.DisableRecordsWithRuleId(tx, req.Rule.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, record := range req.Rule.Records {
|
||||
_, err := models.SharedHTTPDNSCustomRuleRecordDAO.CreateRecord(tx, req.Rule.Id, record.RecordType, record.RecordValue, record.Weight, record.Sort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = notifyHTTPDNSAppTasksByAppDbId(tx, int64(oldRule.AppId), models.HTTPDNSNodeTaskTypeRuleChanged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetAppDbId := req.Rule.AppId
|
||||
if targetAppDbId <= 0 {
|
||||
targetAppDbId = int64(oldRule.AppId)
|
||||
}
|
||||
return notifyHTTPDNSAppTasksByAppDbId(tx, targetAppDbId, models.HTTPDNSNodeTaskTypeRuleChanged)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
func (this *HTTPDNSRuleService) DeleteHTTPDNSCustomRule(ctx context.Context, req *pb.DeleteHTTPDNSCustomRuleRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = this.RunTx(func(tx *dbs.Tx) error {
|
||||
rule, err := models.SharedHTTPDNSCustomRuleDAO.FindEnabledRule(tx, req.RuleId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rule == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = models.SharedHTTPDNSCustomRuleDAO.DisableRule(tx, req.RuleId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return notifyHTTPDNSAppTasksByAppDbId(tx, int64(rule.AppId), models.HTTPDNSNodeTaskTypeRuleChanged)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
func (this *HTTPDNSRuleService) UpdateHTTPDNSCustomRuleStatus(ctx context.Context, req *pb.UpdateHTTPDNSCustomRuleStatusRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = this.RunTx(func(tx *dbs.Tx) error {
|
||||
rule, err := models.SharedHTTPDNSCustomRuleDAO.FindEnabledRule(tx, req.RuleId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rule == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = models.SharedHTTPDNSCustomRuleDAO.UpdateRuleStatus(tx, req.RuleId, req.IsOn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return notifyHTTPDNSAppTasksByAppDbId(tx, int64(rule.AppId), models.HTTPDNSNodeTaskTypeRuleChanged)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
func (this *HTTPDNSRuleService) ListHTTPDNSCustomRulesWithDomainId(ctx context.Context, req *pb.ListHTTPDNSCustomRulesWithDomainIdRequest) (*pb.ListHTTPDNSCustomRulesWithDomainIdResponse, error) {
|
||||
_, _, validateErr := this.ValidateAdminAndUser(ctx, true)
|
||||
if validateErr != nil {
|
||||
if _, nodeErr := this.ValidateHTTPDNSNode(ctx); nodeErr != nil {
|
||||
return nil, validateErr
|
||||
}
|
||||
}
|
||||
rules, err := models.SharedHTTPDNSCustomRuleDAO.ListEnabledRulesWithDomainId(this.NullTx(), req.DomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbRules []*pb.HTTPDNSCustomRule
|
||||
for _, rule := range rules {
|
||||
records, err := models.SharedHTTPDNSCustomRuleRecordDAO.ListEnabledRecordsWithRuleId(this.NullTx(), int64(rule.Id))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbRules = append(pbRules, toPBRule(rule, records))
|
||||
}
|
||||
return &pb.ListHTTPDNSCustomRulesWithDomainIdResponse{Rules: pbRules}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user