gatewaygroupip.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  9. )
  10. type GateWayGroupIpService interface {
  11. GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error)
  12. GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (*[]model.GateWayGroupIp, error)
  13. AddGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error
  14. EditGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error
  15. DeleteGateWayGroupIp(ctx context.Context, req *v1.DeleteGateWayGroupIpRequest) error
  16. GetGateWayGroupIpAdmin(ctx context.Context,req *v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error)
  17. //hostid获取网关组ip']\\
  18. GetGateWayGroupIpByHostId(ctx context.Context, hostId int) ([]string, error)
  19. }
  20. func NewGateWayGroupIpService(
  21. service *Service,
  22. gateWayGroupIpRepository repository.GateWayGroupIpRepository,
  23. gateWayGroupRep repository.GatewayGroupRepository,
  24. request RequestService,
  25. ) GateWayGroupIpService {
  26. return &gateWayGroupIpService{
  27. Service: service,
  28. gateWayGroupIpRepository: gateWayGroupIpRepository,
  29. request: request,
  30. gateWayGroupRep: gateWayGroupRep,
  31. }
  32. }
  33. type gateWayGroupIpService struct {
  34. *Service
  35. gateWayGroupIpRepository repository.GateWayGroupIpRepository
  36. request RequestService
  37. gateWayGroupRep repository.GatewayGroupRepository
  38. }
  39. func (s *gateWayGroupIpService) GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error) {
  40. res, err := s.gateWayGroupIpRepository.GetGateWayGroupIp(ctx, id)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return res, nil
  45. }
  46. func (s *gateWayGroupIpService) GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (*[]model.GateWayGroupIp, error) {
  47. res, err := s.gateWayGroupIpRepository.GetGateWayGroupIpByGatewayGroupId(ctx, gatewayGroupId)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &res, nil
  52. }
  53. func (s *gateWayGroupIpService) AddGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
  54. if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
  55. return err
  56. }
  57. if err := s.gateWayGroupIpRepository.AddGateWayGroupIp(ctx, &model.GateWayGroupIp{
  58. GatewayGroupId: req.GatewayGroupId,
  59. Ip: req.Ip,
  60. Tag: req.Tag,
  61. Comment: req.Comment,
  62. OriginPlace: req.OriginPlace,
  63. }); err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. func (s *gateWayGroupIpService) EditGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
  69. oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
  70. if err != nil {
  71. return err
  72. }
  73. if oldIp.Ip != req.Ip {
  74. if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
  75. return err
  76. }
  77. if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
  78. return err
  79. }
  80. }
  81. if err := s.gateWayGroupIpRepository.EditGateWayGroupIp(ctx, &model.GateWayGroupIp{
  82. Id: req.Id,
  83. GatewayGroupId: req.GatewayGroupId,
  84. Ip: req.Ip,
  85. Tag: req.Tag,
  86. Comment: req.Comment,
  87. OriginPlace: req.OriginPlace,
  88. }); err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. func (s *gateWayGroupIpService) DeleteGateWayGroupIp(ctx context.Context, req *v1.DeleteGateWayGroupIpRequest) error {
  94. oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
  95. if err != nil {
  96. return err
  97. }
  98. if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
  99. return err
  100. }
  101. if err := s.gateWayGroupIpRepository.DeleteGateWayGroupIp(ctx, &model.GateWayGroupIp{
  102. Id: req.Id,
  103. }); err != nil {
  104. return err
  105. }
  106. return nil
  107. }
  108. func (s *gateWayGroupIpService) GetGateWayGroupIpAdmin(ctx context.Context,req *v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error) {
  109. res, err := s.gateWayGroupIpRepository.GetGatewayGroupIpList(ctx, *req)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return res, nil
  114. }
  115. func (s *gateWayGroupIpService) sendIp(ctx context.Context, ip string, action string) error {
  116. var apiUrl string
  117. switch action {
  118. case "add":
  119. apiUrl = ""
  120. case "delete":
  121. apiUrl = ""
  122. }
  123. formData := map[string]interface{}{
  124. "ip": ip,
  125. }
  126. resBody, err := s.request.Request(ctx, formData, apiUrl, "", "")
  127. if err != nil {
  128. return err
  129. }
  130. var res v1.GeneralResponse[string]
  131. err = json.Unmarshal(resBody, &res)
  132. if err != nil {
  133. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  134. }
  135. if res.Code != 0 {
  136. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  137. }
  138. return nil
  139. }
  140. func (s *gateWayGroupIpService) GetGateWayGroupIpByHostId(ctx context.Context, hostId int) ([]string, error) {
  141. gatewayGroup, err := s.gateWayGroupRep.GetGatewayGroupByHostId(ctx, int64(hostId))
  142. if err != nil {
  143. return nil, err
  144. }
  145. res, err := s.gateWayGroupIpRepository.GetGateWayGroupAllIpByGatewayGroupId(ctx, gatewayGroup.Id)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return res, nil
  150. }