123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package server
- import (
- "cfTest/cloudflareApi/loadBalancerPool"
- "context"
- "github.com/cloudflare/cloudflare-go"
- "github.com/jinzhu/copier"
- "github.com/samber/lo"
- )
- type LoadBalancerPoolServer struct {
- loadBalancerPool.UnimplementedLoadBalancerPoolCloudflareServer
- }
- func (d LoadBalancerPoolServer) Get(ctx context.Context, request *loadBalancerPool.IdRequest) (*loadBalancerPool.LoadBalancerPool, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- //TODO 需要提供AccountID才能获取到正确的Pool。
- pool, err := api.LoadBalancerPoolDetails(ctxAPI, request.Id)
- if err != nil {
- return nil, err
- }
- out := Go2ProtoLoadBalancerPool(&pool)
- return out, nil
- }
- func (d LoadBalancerPoolServer) List(ctx context.Context, request *loadBalancerPool.ListRequest) (*loadBalancerPool.LoadBalancerPoolList, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- pools, err := api.ListLoadBalancerPools(ctxAPI)
- if err != nil {
- return nil, err
- }
- filterMap := lo.FilterMap[cloudflare.LoadBalancerPool, *loadBalancerPool.LoadBalancerPool](pools, func(pool cloudflare.LoadBalancerPool, _ int) (*loadBalancerPool.LoadBalancerPool, bool) {
- protoPool := Go2ProtoLoadBalancerPool(&pool)
- if protoPool == nil {
- return nil, false
- }
- return protoPool, true
- })
- return &loadBalancerPool.LoadBalancerPoolList{LoadBalancerPools: filterMap}, nil
- }
- func (d LoadBalancerPoolServer) Create(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- in := Proto2GoLoadBalancerPool(request.LoadBalancerPool)
- pool, err := api.CreateLoadBalancerPool(ctxAPI, *in)
- if err != nil {
- return nil, err
- }
- out := Go2ProtoLoadBalancerPool(&pool)
- return out, nil
- }
- func (d LoadBalancerPoolServer) Update(ctx context.Context, request *loadBalancerPool.LoadBalancerPoolRequest) (*loadBalancerPool.LoadBalancerPool, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- in := Proto2GoLoadBalancerPool(request.LoadBalancerPool)
- pool, err := api.ModifyLoadBalancerPool(ctxAPI, *in)
- if err != nil {
- return nil, err
- }
- out := Go2ProtoLoadBalancerPool(&pool)
- return out, nil
- }
- func (d LoadBalancerPoolServer) Delete(ctx context.Context, request *loadBalancerPool.IdRequest) (*loadBalancerPool.Empty, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- err = api.DeleteLoadBalancerPool(ctxAPI, request.Id)
- if err != nil {
- return nil, err
- }
- return &loadBalancerPool.Empty{}, nil
- }
- func Go2ProtoLoadBalancerPool(goPool *cloudflare.LoadBalancerPool) (protoPool *loadBalancerPool.LoadBalancerPool) {
- protoPool = new(loadBalancerPool.LoadBalancerPool)
- err := copier.Copy(protoPool, goPool)
- if err != nil {
- return nil
- }
- protoPool.Id = goPool.ID
- return
- }
- func Proto2GoLoadBalancerPool(protoPool *loadBalancerPool.LoadBalancerPool) (goPool *cloudflare.LoadBalancerPool) {
- goPool = new(cloudflare.LoadBalancerPool)
- err := copier.Copy(goPool, protoPool)
- if err != nil {
- return nil
- }
- goPool.ID = protoPool.Id
- return
- }
|