LoadBalancerCloudflareServer.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "strings"
  9. )
  10. type LoadBalancerServer struct {
  11. loadBalancer.UnimplementedLoadBalancerCloudflareServer
  12. }
  13. func Go2ProtoLoadBalancer(goBalancer *cloudflare.LoadBalancer) (protoBalancer *loadBalancer.LoadBalancer) {
  14. protoBalancer = new(loadBalancer.LoadBalancer)
  15. err := copier.Copy(protoBalancer, goBalancer)
  16. if err != nil {
  17. return nil
  18. }
  19. protoBalancer.Id = goBalancer.ID
  20. return
  21. }
  22. func Proto2GoLoadBalancer(protoBalancer *loadBalancer.LoadBalancer) (goBalancer *cloudflare.LoadBalancer) {
  23. goBalancer = new(cloudflare.LoadBalancer)
  24. err := copier.Copy(goBalancer, protoBalancer)
  25. if err != nil {
  26. return nil
  27. }
  28. goBalancer.ID = protoBalancer.Id
  29. return
  30. }
  31. func (d LoadBalancerServer) List(ctx context.Context, request *loadBalancer.ListRequest) (*loadBalancer.LoadBalancerList, error) {
  32. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  33. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  34. } else {
  35. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  36. }
  37. if err != nil {
  38. return nil, err
  39. }
  40. ctxAPI := context.Background()
  41. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  42. listLoadBalancerParams := cloudflare.ListLoadBalancerParams{
  43. PaginationOptions: cloudflare.PaginationOptions{}}
  44. balancers, err := api.ListLoadBalancers(ctxAPI, zoneIdentifier, listLoadBalancerParams)
  45. //balancers, err := api.ListLoadBalancers(ctxAPI, request.ZoneId)
  46. if err != nil {
  47. return nil, err
  48. }
  49. filterMap := lo.FilterMap[cloudflare.LoadBalancer, *loadBalancer.LoadBalancer](balancers, func(balancer cloudflare.LoadBalancer, _ int) (*loadBalancer.LoadBalancer, bool) {
  50. protoLoadBalancer := Go2ProtoLoadBalancer(&balancer)
  51. if protoLoadBalancer == nil {
  52. return nil, false
  53. }
  54. return protoLoadBalancer, true
  55. })
  56. return &loadBalancer.LoadBalancerList{LoadBalancers: filterMap}, nil
  57. }
  58. func (d LoadBalancerServer) Create(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  59. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  60. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  61. } else {
  62. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  63. }
  64. if err != nil {
  65. return nil, err
  66. }
  67. ctxAPI := context.Background()
  68. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  69. in := Proto2GoLoadBalancer(request.LoadBalancer)
  70. createLoadBalancerParams := cloudflare.CreateLoadBalancerParams{
  71. LoadBalancer: *in,
  72. }
  73. balancer, err := api.CreateLoadBalancer(ctxAPI, zoneIdentifier, createLoadBalancerParams)
  74. //balancer, err := api.CreateLoadBalancer(ctxAPI, request.ZoneId, *in)
  75. if err != nil {
  76. return nil, err
  77. }
  78. out := Go2ProtoLoadBalancer(&balancer)
  79. return out, nil
  80. }
  81. func (d LoadBalancerServer) Update(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
  82. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  83. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  84. } else {
  85. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  86. }
  87. if err != nil {
  88. return nil, err
  89. }
  90. ctxAPI := context.Background()
  91. in := Proto2GoLoadBalancer(request.LoadBalancer)
  92. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  93. updateLoadBalancerParams := cloudflare.UpdateLoadBalancerParams{
  94. LoadBalancer: *in,
  95. }
  96. balancer, err := api.UpdateLoadBalancer(ctxAPI, zoneIdentifier, updateLoadBalancerParams)
  97. //balancer, err := api.ModifyLoadBalancer(ctxAPI, request.ZoneId, *in)
  98. if err != nil {
  99. return nil, err
  100. }
  101. out := Go2ProtoLoadBalancer(&balancer)
  102. return out, nil
  103. }
  104. func (d LoadBalancerServer) Delete(ctx context.Context, request *loadBalancer.DeleteRequest) (*loadBalancer.Empty, error) {
  105. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  106. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  107. } else {
  108. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  109. }
  110. if err != nil {
  111. return nil, err
  112. }
  113. ctxAPI := context.Background()
  114. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  115. err = api.DeleteLoadBalancer(ctxAPI, zoneIdentifier, request.Id)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return &loadBalancer.Empty{}, nil
  120. }