LoadBalancerCloudflareServer.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  37. listLoadBalancerParams := cloudflare.ListLoadBalancerParams{
  38. PaginationOptions: cloudflare.PaginationOptions{}}
  39. balancers, err := api.ListLoadBalancers(ctxAPI, zoneIdentifier, listLoadBalancerParams)
  40. //balancers, err := api.ListLoadBalancers(ctxAPI, request.ZoneId)
  41. if err != nil {
  42. return nil, err
  43. }
  44. filterMap := lo.FilterMap[cloudflare.LoadBalancer, *loadBalancer.LoadBalancer](balancers, func(balancer cloudflare.LoadBalancer, _ int) (*loadBalancer.LoadBalancer, bool) {
  45. protoLoadBalancer := Go2ProtoLoadBalancer(&balancer)
  46. if protoLoadBalancer == nil {
  47. return nil, false
  48. }
  49. return protoLoadBalancer, true
  50. })
  51. return &loadBalancer.LoadBalancerList{LoadBalancers: filterMap}, nil
  52. }
  53. func (d LoadBalancerServer) Create(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  54. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  55. if err != nil {
  56. return nil, err
  57. }
  58. ctxAPI := context.Background()
  59. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  60. in := Proto2GoLoadBalancer(request.LoadBalancer)
  61. createLoadBalancerParams := cloudflare.CreateLoadBalancerParams{
  62. LoadBalancer: *in,
  63. }
  64. balancer, err := api.CreateLoadBalancer(ctxAPI, zoneIdentifier, createLoadBalancerParams)
  65. //balancer, err := api.CreateLoadBalancer(ctxAPI, request.ZoneId, *in)
  66. if err != nil {
  67. return nil, err
  68. }
  69. out := Go2ProtoLoadBalancer(&balancer)
  70. return out, nil
  71. }
  72. func (d LoadBalancerServer) Update(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  73. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  74. if err != nil {
  75. return nil, err
  76. }
  77. ctxAPI := context.Background()
  78. in := Proto2GoLoadBalancer(request.LoadBalancer)
  79. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  80. updateLoadBalancerParams := cloudflare.UpdateLoadBalancerParams{
  81. LoadBalancer: *in,
  82. }
  83. balancer, err := api.UpdateLoadBalancer(ctxAPI, zoneIdentifier, updateLoadBalancerParams)
  84. //balancer, err := api.ModifyLoadBalancer(ctxAPI, request.ZoneId, *in)
  85. if err != nil {
  86. return nil, err
  87. }
  88. out := Go2ProtoLoadBalancer(&balancer)
  89. return out, nil
  90. }
  91. func (d LoadBalancerServer) Delete(ctx context.Context, request *loadBalancer.DeleteRequest) (*loadBalancer.Empty, error) {
  92. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  93. if err != nil {
  94. return nil, err
  95. }
  96. ctxAPI := context.Background()
  97. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  98. err = api.DeleteLoadBalancer(ctxAPI, zoneIdentifier, request.Id)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return &loadBalancer.Empty{}, nil
  103. }