LoadBalancerPoolCloudflareServer.go 5.8 KB

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