LoadBalancerPoolCloudflareServer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/loadBalancerPool"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. "github.com/jinzhu/copier"
  7. "github.com/samber/lo"
  8. )
  9. type LoadBalancerPoolServer struct {
  10. loadBalancerPool.UnimplementedLoadBalancerPoolCloudflareServer
  11. }
  12. func (d LoadBalancerPoolServer) Get(ctx context.Context, request *loadBalancerPool.IdRequest) (*loadBalancerPool.LoadBalancerPool, error) {
  13. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  14. if err != nil {
  15. return nil, err
  16. }
  17. ctxAPI := context.Background()
  18. //TODO 需要提供AccountID才能获取到正确的Pool。
  19. pool, err := api.LoadBalancerPoolDetails(ctxAPI, request.Id)
  20. if err != nil {
  21. return nil, err
  22. }
  23. out := Go2ProtoLoadBalancerPool(&pool)
  24. return out, nil
  25. }
  26. func (d LoadBalancerPoolServer) List(ctx context.Context, request *loadBalancerPool.ListRequest) (*loadBalancerPool.LoadBalancerPoolList, error) {
  27. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  28. if err != nil {
  29. return nil, err
  30. }
  31. ctxAPI := context.Background()
  32. pools, err := api.ListLoadBalancerPools(ctxAPI)
  33. if err != nil {
  34. return nil, err
  35. }
  36. filterMap := lo.FilterMap[cloudflare.LoadBalancerPool, *loadBalancerPool.LoadBalancerPool](pools, func(pool cloudflare.LoadBalancerPool, _ int) (*loadBalancerPool.LoadBalancerPool, bool) {
  37. protoPool := Go2ProtoLoadBalancerPool(&pool)
  38. if protoPool == nil {
  39. return nil, false
  40. }
  41. return protoPool, true
  42. })
  43. return &loadBalancerPool.LoadBalancerPoolList{LoadBalancerPools: filterMap}, nil
  44. }
  45. func (d LoadBalancerPoolServer) Create(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
  46. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  47. if err != nil {
  48. return nil, err
  49. }
  50. ctxAPI := context.Background()
  51. in := Proto2GoLoadBalancerPool(request.LoadBalancerPool)
  52. pool, err := api.CreateLoadBalancerPool(ctxAPI, *in)
  53. if err != nil {
  54. return nil, err
  55. }
  56. out := Go2ProtoLoadBalancerPool(&pool)
  57. return out, nil
  58. }
  59. func (d LoadBalancerPoolServer) Update(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
  60. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  61. if err != nil {
  62. return nil, err
  63. }
  64. ctxAPI := context.Background()
  65. in := Proto2GoLoadBalancerPool(request.LoadBalancerPool)
  66. pool, err := api.ModifyLoadBalancerPool(ctxAPI, *in)
  67. if err != nil {
  68. return nil, err
  69. }
  70. out := Go2ProtoLoadBalancerPool(&pool)
  71. return out, nil
  72. }
  73. func (d LoadBalancerPoolServer) Delete(ctx context.Context, request *loadBalancerPool.IdRequest) (*loadBalancerPool.Empty, error) {
  74. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  75. if err != nil {
  76. return nil, err
  77. }
  78. ctxAPI := context.Background()
  79. err = api.DeleteLoadBalancerPool(ctxAPI, request.Id)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &loadBalancerPool.Empty{}, nil
  84. }
  85. func Go2ProtoLoadBalancerPool(goPool *cloudflare.LoadBalancerPool) (protoPool *loadBalancerPool.LoadBalancerPool) {
  86. protoPool = new(loadBalancerPool.LoadBalancerPool)
  87. err := copier.Copy(protoPool, goPool)
  88. if err != nil {
  89. return nil
  90. }
  91. protoPool.Id = goPool.ID
  92. return
  93. }
  94. func Proto2GoLoadBalancerPool(protoPool *loadBalancerPool.LoadBalancerPool) (goPool *cloudflare.LoadBalancerPool) {
  95. goPool = new(cloudflare.LoadBalancerPool)
  96. err := copier.Copy(goPool, protoPool)
  97. if err != nil {
  98. return nil
  99. }
  100. goPool.ID = protoPool.Id
  101. return
  102. }