LoadBalancerCloudflareServer.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/loadBalancer"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. "github.com/jinzhu/copier"
  7. "github.com/samber/lo"
  8. "log"
  9. "strings"
  10. )
  11. type LoadBalancerServer struct {
  12. loadBalancer.UnimplementedLoadBalancerCloudflareServer
  13. }
  14. func Go2ProtoLoadBalancer(goBalancer *cloudflare.LoadBalancer) (protoBalancer *loadBalancer.LoadBalancer) {
  15. protoBalancer = new(loadBalancer.LoadBalancer)
  16. err := copier.Copy(protoBalancer, goBalancer)
  17. if err != nil {
  18. return nil
  19. }
  20. protoBalancer.Id = goBalancer.ID
  21. return
  22. }
  23. func Proto2GoLoadBalancer(protoBalancer *loadBalancer.LoadBalancer) (goBalancer *cloudflare.LoadBalancer) {
  24. goBalancer = new(cloudflare.LoadBalancer)
  25. err := copier.Copy(goBalancer, protoBalancer)
  26. if err != nil {
  27. return nil
  28. }
  29. goBalancer.ID = protoBalancer.Id
  30. return
  31. }
  32. func (d LoadBalancerServer) List(ctx context.Context, request *loadBalancer.ListRequest) (*loadBalancer.LoadBalancerList, error) {
  33. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  34. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  35. } else {
  36. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  37. }
  38. if err != nil {
  39. return nil, err
  40. }
  41. ctxAPI := context.Background()
  42. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  43. listLoadBalancerParams := cloudflare.ListLoadBalancerParams{
  44. PaginationOptions: cloudflare.PaginationOptions{}}
  45. balancers, err := api.ListLoadBalancers(ctxAPI, zoneIdentifier, listLoadBalancerParams)
  46. log.Printf("Cloudflare ListLoadBalancers API balancers: %v err:%s", balancers, err)
  47. //balancers, err := api.ListLoadBalancers(ctxAPI, request.ZoneId)
  48. if err != nil {
  49. return nil, err
  50. }
  51. filterMap := lo.FilterMap[cloudflare.LoadBalancer, *loadBalancer.LoadBalancer](balancers, func(balancer cloudflare.LoadBalancer, _ int) (*loadBalancer.LoadBalancer, bool) {
  52. protoLoadBalancer := Go2ProtoLoadBalancer(&balancer)
  53. if protoLoadBalancer == nil {
  54. return nil, false
  55. }
  56. return protoLoadBalancer, true
  57. })
  58. return &loadBalancer.LoadBalancerList{LoadBalancers: filterMap}, nil
  59. }
  60. func (d LoadBalancerServer) Create(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  61. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  62. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  63. } else {
  64. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  65. }
  66. if err != nil {
  67. return nil, err
  68. }
  69. ctxAPI := context.Background()
  70. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  71. in := Proto2GoLoadBalancer(request.LoadBalancer)
  72. createLoadBalancerParams := cloudflare.CreateLoadBalancerParams{
  73. LoadBalancer: *in,
  74. }
  75. balancer, err := api.CreateLoadBalancer(ctxAPI, zoneIdentifier, createLoadBalancerParams)
  76. log.Printf("Cloudflare CreateLoadBalancer API balancer: %v err:%s", balancer, err)
  77. //balancer, err := api.CreateLoadBalancer(ctxAPI, request.ZoneId, *in)
  78. if err != nil {
  79. return nil, err
  80. }
  81. out := Go2ProtoLoadBalancer(&balancer)
  82. return out, nil
  83. }
  84. func (d LoadBalancerServer) Update(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  85. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  86. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  87. } else {
  88. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  89. }
  90. if err != nil {
  91. return nil, err
  92. }
  93. ctxAPI := context.Background()
  94. in := Proto2GoLoadBalancer(request.LoadBalancer)
  95. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  96. updateLoadBalancerParams := cloudflare.UpdateLoadBalancerParams{
  97. LoadBalancer: *in,
  98. }
  99. balancer, err := api.UpdateLoadBalancer(ctxAPI, zoneIdentifier, updateLoadBalancerParams)
  100. log.Printf("Cloudflare UpdateLoadBalancer API balancer: %v err:%s", balancer, err)
  101. //balancer, err := api.ModifyLoadBalancer(ctxAPI, request.ZoneId, *in)
  102. if err != nil {
  103. return nil, err
  104. }
  105. out := Go2ProtoLoadBalancer(&balancer)
  106. return out, nil
  107. }
  108. func (d LoadBalancerServer) Delete(ctx context.Context, request *loadBalancer.DeleteRequest) (*loadBalancer.Empty, error) {
  109. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  110. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  111. } else {
  112. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  113. }
  114. if err != nil {
  115. return nil, err
  116. }
  117. ctxAPI := context.Background()
  118. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  119. err = api.DeleteLoadBalancer(ctxAPI, zoneIdentifier, request.Id)
  120. log.Printf("Cloudflare DeleteLoadBalancer API err:%s", err)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return &loadBalancer.Empty{}, nil
  125. }