LoadBalancerCloudflareServer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. )
  9. type LoadBalancerServer struct {
  10. loadBalancer.UnimplementedLoadBalancerCloudflareServer
  11. }
  12. func Go2ProtoLoadBalancer(goBalancer *cloudflare.LoadBalancer) (protoBalancer *loadBalancer.LoadBalancer) {
  13. protoBalancer = new(loadBalancer.LoadBalancer)
  14. err := copier.Copy(protoBalancer, goBalancer)
  15. if err != nil {
  16. return nil
  17. }
  18. protoBalancer.Id = goBalancer.ID
  19. return
  20. }
  21. func Proto2GoLoadBalancer(protoBalancer *loadBalancer.LoadBalancer) (goBalancer *cloudflare.LoadBalancer) {
  22. goBalancer = new(cloudflare.LoadBalancer)
  23. err := copier.Copy(goBalancer, protoBalancer)
  24. if err != nil {
  25. return nil
  26. }
  27. goBalancer.ID = protoBalancer.Id
  28. return
  29. }
  30. func (d LoadBalancerServer) List(ctx context.Context, request *loadBalancer.ListRequest) (*loadBalancer.LoadBalancerList, error) {
  31. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  32. if err != nil {
  33. return nil, err
  34. }
  35. ctxAPI := context.Background()
  36. balancers, err := api.ListLoadBalancers(ctxAPI, request.ZoneId)
  37. if err != nil {
  38. return nil, err
  39. }
  40. filterMap := lo.FilterMap[cloudflare.LoadBalancer, *loadBalancer.LoadBalancer](balancers, func(balancer cloudflare.LoadBalancer, _ int) (*loadBalancer.LoadBalancer, bool) {
  41. protoLoadBalancer := Go2ProtoLoadBalancer(&balancer)
  42. if protoLoadBalancer == nil {
  43. return nil, false
  44. }
  45. return protoLoadBalancer, true
  46. })
  47. return &loadBalancer.LoadBalancerList{LoadBalancers: filterMap}, nil
  48. }
  49. func (d LoadBalancerServer) Create(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  50. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  51. if err != nil {
  52. return nil, err
  53. }
  54. ctxAPI := context.Background()
  55. in := Proto2GoLoadBalancer(request.LoadBalancer)
  56. balancer, err := api.CreateLoadBalancer(ctxAPI, request.ZoneId, *in)
  57. if err != nil {
  58. return nil, err
  59. }
  60. out := Go2ProtoLoadBalancer(&balancer)
  61. return out, nil
  62. }
  63. func (d LoadBalancerServer) Update(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  64. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  65. if err != nil {
  66. return nil, err
  67. }
  68. ctxAPI := context.Background()
  69. in := Proto2GoLoadBalancer(request.LoadBalancer)
  70. balancer, err := api.ModifyLoadBalancer(ctxAPI, request.ZoneId, *in)
  71. if err != nil {
  72. return nil, err
  73. }
  74. out := Go2ProtoLoadBalancer(&balancer)
  75. return out, nil
  76. }
  77. func (d LoadBalancerServer) Delete(ctx context.Context, request *loadBalancer.DeleteRequest) (*loadBalancer.Empty, error) {
  78. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  79. if err != nil {
  80. return nil, err
  81. }
  82. ctxAPI := context.Background()
  83. err = api.DeleteLoadBalancer(ctxAPI, request.ZoneId, request.Id)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &loadBalancer.Empty{}, nil
  88. }