LoadBalancerPoolCloudflareServer.go 5.3 KB

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