gatewayipadmin.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package admin
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. v1admin "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  9. "github.com/go-nunu/nunu-layout-advanced/internal/repository/admin"
  10. "github.com/go-nunu/nunu-layout-advanced/internal/service"
  11. "github.com/hashicorp/go-multierror"
  12. "github.com/spf13/viper"
  13. )
  14. type GatewayIpAdminService interface {
  15. GetGatewayIpAdmin(ctx context.Context, id int64) (*model.Gatewayip, error)
  16. GetGatewayGroupIpList(ctx context.Context,req v1admin.SearchGatewayIpParams) (*v1.PaginatedResponse[model.Gatewayip], error)
  17. AddGatewayIp(ctx context.Context,req model.Gatewayip) error
  18. EditGatewayIp(ctx context.Context,req model.Gatewayip) error
  19. DeleteGatewayIp(ctx context.Context,id int64) error
  20. DeleteGatewayIps(ctx context.Context, ids []int64) error
  21. }
  22. func NewGatewayIpAdminService(
  23. service *service.Service,
  24. gatewayIpAdminRepository admin.GatewayIpAdminRepository,
  25. config *viper.Viper,
  26. request service.RequestService,
  27. ) GatewayIpAdminService {
  28. return &gatewayIpAdminService{
  29. Service: service,
  30. gatewayIpAdminRepository: gatewayIpAdminRepository,
  31. request: request,
  32. config: config,
  33. }
  34. }
  35. type gatewayIpAdminService struct {
  36. *service.Service
  37. gatewayIpAdminRepository admin.GatewayIpAdminRepository
  38. config *viper.Viper
  39. request service.RequestService
  40. }
  41. func (s *gatewayIpAdminService) sendIp(ctx context.Context, ip string, action string,nodeArea string) error {
  42. serverIps := s.config.GetStringSlice("addServerIp." + nodeArea)
  43. for _, serverIp := range serverIps {
  44. apiUrl := "http://" + serverIp + ":3075/" + action
  45. formData := map[string]interface{}{
  46. "ip": ip,
  47. }
  48. resBody, err := s.request.Request(ctx, formData, apiUrl, "", "")
  49. if err != nil {
  50. return err
  51. }
  52. var res v1.GeneralResponse[any]
  53. err = json.Unmarshal(resBody, &res)
  54. if err != nil {
  55. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  56. }
  57. if res.Code != 0 {
  58. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  59. }
  60. }
  61. return nil
  62. }
  63. func (s *gatewayIpAdminService) GetGatewayIpAdmin(ctx context.Context, id int64) (*model.Gatewayip, error) {
  64. return s.gatewayIpAdminRepository.GetGatewayIpAdmin(ctx, id)
  65. }
  66. func (s *gatewayIpAdminService) GetGatewayGroupIpList(ctx context.Context,req v1admin.SearchGatewayIpParams) (*v1.PaginatedResponse[model.Gatewayip], error) {
  67. return s.gatewayIpAdminRepository.GetGatewayGroupIpList(ctx,req)
  68. }
  69. func (s *gatewayIpAdminService) AddGatewayIp(ctx context.Context,req model.Gatewayip) error {
  70. // 启动网关组IP
  71. if req.NodeArea != "" {
  72. err := s.sendIp(ctx,req.Ip,"addIp",req.NodeArea)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. return s.gatewayIpAdminRepository.AddGatewayIp(ctx,req)
  78. }
  79. func (s *gatewayIpAdminService) EditGatewayIp(ctx context.Context,req model.Gatewayip) error {
  80. oldData, err := s.GetGatewayIpAdmin(ctx, int64(req.Id))
  81. if err != nil {
  82. return err
  83. }
  84. if oldData.Ip != req.Ip || oldData.NodeArea != "" || req.NodeArea != "" {
  85. // 启动网关组IP
  86. err := s.sendIp(ctx,oldData.Ip,"deleteIp",oldData.NodeArea)
  87. if err != nil {
  88. return err
  89. }
  90. err = s.sendIp(ctx,req.Ip,"addIp",req.NodeArea)
  91. if err != nil {
  92. return err
  93. }
  94. }
  95. return s.gatewayIpAdminRepository.EditGatewayIp(ctx,req)
  96. }
  97. func (s *gatewayIpAdminService) DeleteGatewayIp(ctx context.Context,id int64) error {
  98. oldData, err := s.GetGatewayIpAdmin(ctx, id)
  99. if err != nil {
  100. return err
  101. }
  102. if oldData.HostId != 0 {
  103. return fmt.Errorf("该IP已被绑定,无法删除")
  104. }
  105. // 启动网关组IP
  106. if oldData.NodeArea != "" {
  107. err := s.sendIp(ctx,oldData.Ip,"deleteIp",oldData.NodeArea)
  108. if err != nil {
  109. return err
  110. }
  111. }
  112. return s.gatewayIpAdminRepository.DeleteGatewayIp(ctx,id)
  113. }
  114. func (s *gatewayIpAdminService) DeleteGatewayIps(ctx context.Context, ids []int64) error {
  115. var allErrors *multierror.Error
  116. for _, id := range ids {
  117. oldData, err := s.GetGatewayIpAdmin(ctx, id)
  118. if err != nil {
  119. return err
  120. }
  121. if oldData.HostId != 0 {
  122. return fmt.Errorf("该IP已被绑定,无法删除")
  123. }
  124. // 启动网关组IP
  125. if oldData.NodeArea != "" {
  126. err := s.sendIp(ctx,oldData.Ip,"deleteIp",oldData.NodeArea)
  127. if err != nil {
  128. allErrors = multierror.Append(allErrors, err)
  129. }
  130. }
  131. }
  132. if allErrors != nil {
  133. return allErrors.ErrorOrNil()
  134. }
  135. return s.gatewayIpAdminRepository.DeleteGatewayIps(ctx,ids)
  136. }