LoadBalancerPoolCloudflareServer.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //pool, err := api.LoadBalancerPoolDetails(ctxAPI, request.Id)
  19. //TODO 需要提供AccountID才能获取到正确的Pool。
  20. AccountId := ""
  21. accountIdentifier := cloudflare.AccountIdentifier(AccountId)
  22. pool, err := api.GetLoadBalancerPool(ctxAPI, accountIdentifier, request.Id)
  23. if err != nil {
  24. return nil, err
  25. }
  26. out := Go2ProtoLoadBalancerPool(&pool)
  27. return out, nil
  28. }
  29. func (d LoadBalancerPoolServer) List(ctx context.Context, request *loadBalancerPool.ListRequest) (*loadBalancerPool.LoadBalancerPoolList, error) {
  30. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  31. if err != nil {
  32. return nil, err
  33. }
  34. ctxAPI := context.Background()
  35. //TODO 需要提供AccountID才能获取到正确的Pool。
  36. AccountId := ""
  37. accountIdentifier := cloudflare.AccountIdentifier(AccountId)
  38. listLoadBalancerPoolParams := cloudflare.ListLoadBalancerPoolParams{
  39. PaginationOptions: cloudflare.PaginationOptions{}}
  40. pools, err := api.ListLoadBalancerPools(ctxAPI, accountIdentifier, listLoadBalancerPoolParams)
  41. if err != nil {
  42. return nil, err
  43. }
  44. filterMap := lo.FilterMap[cloudflare.LoadBalancerPool, *loadBalancerPool.LoadBalancerPool](pools, func(pool cloudflare.LoadBalancerPool, _ int) (*loadBalancerPool.LoadBalancerPool, bool) {
  45. protoPool := Go2ProtoLoadBalancerPool(&pool)
  46. if protoPool == nil {
  47. return nil, false
  48. }
  49. return protoPool, true
  50. })
  51. return &loadBalancerPool.LoadBalancerPoolList{LoadBalancerPools: filterMap}, nil
  52. }
  53. func (d LoadBalancerPoolServer) Create(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
  54. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  55. if err != nil {
  56. return nil, err
  57. }
  58. ctxAPI := context.Background()
  59. //TODO 需要提供AccountID才能获取到正确的Pool。
  60. AccountId := ""
  61. accountIdentifier := cloudflare.AccountIdentifier(AccountId)
  62. in := Proto2GoLoadBalancerPool(request.LoadBalancerPool)
  63. createLoadBalancerPoolParams := cloudflare.CreateLoadBalancerPoolParams{
  64. LoadBalancerPool: *in,
  65. }
  66. pool, err := api.CreateLoadBalancerPool(ctxAPI, accountIdentifier, createLoadBalancerPoolParams)
  67. if err != nil {
  68. return nil, err
  69. }
  70. out := Go2ProtoLoadBalancerPool(&pool)
  71. return out, nil
  72. }
  73. func (d LoadBalancerPoolServer) Update(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
  74. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  75. if err != nil {
  76. return nil, err
  77. }
  78. ctxAPI := context.Background()
  79. //TODO 需要提供AccountID才能获取到正确的Pool。
  80. AccountId := ""
  81. accountIdentifier := cloudflare.AccountIdentifier(AccountId)
  82. in := Proto2GoLoadBalancerPool(request.LoadBalancerPool)
  83. updateLoadBalancerPoolParams := cloudflare.UpdateLoadBalancerPoolParams{
  84. LoadBalancer: *in,
  85. }
  86. pool, err := api.UpdateLoadBalancerPool(ctxAPI, accountIdentifier, updateLoadBalancerPoolParams)
  87. if err != nil {
  88. return nil, err
  89. }
  90. out := Go2ProtoLoadBalancerPool(&pool)
  91. return out, nil
  92. }
  93. func (d LoadBalancerPoolServer) Delete(ctx context.Context, request *loadBalancerPool.IdRequest) (*loadBalancerPool.Empty, error) {
  94. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  95. if err != nil {
  96. return nil, err
  97. }
  98. ctxAPI := context.Background()
  99. //TODO 需要提供AccountID才能获取到正确的Pool。
  100. AccountId := ""
  101. accountIdentifier := cloudflare.AccountIdentifier(AccountId)
  102. err = api.DeleteLoadBalancerPool(ctxAPI, accountIdentifier, request.Id)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return &loadBalancerPool.Empty{}, nil
  107. }
  108. func Go2ProtoLoadBalancerPool(goPool *cloudflare.LoadBalancerPool) (protoPool *loadBalancerPool.LoadBalancerPool) {
  109. protoPool = new(loadBalancerPool.LoadBalancerPool)
  110. err := copier.Copy(protoPool, goPool)
  111. if err != nil {
  112. return nil
  113. }
  114. protoPool.Id = goPool.ID
  115. return
  116. }
  117. func Proto2GoLoadBalancerPool(protoPool *loadBalancerPool.LoadBalancerPool) (goPool *cloudflare.LoadBalancerPool) {
  118. goPool = new(cloudflare.LoadBalancerPool)
  119. err := copier.Copy(goPool, protoPool)
  120. if err != nil {
  121. return nil
  122. }
  123. goPool.ID = protoPool.Id
  124. return
  125. }