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() //pool, err := api.LoadBalancerPoolDetails(ctxAPI, request.Id) //TODO 需要提供AccountID才能获取到正确的Pool。 AccountId := "" accountIdentifier := cloudflare.AccountIdentifier(AccountId) pool, err := api.GetLoadBalancerPool(ctxAPI, accountIdentifier, 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() //TODO 需要提供AccountID才能获取到正确的Pool。 AccountId := "" accountIdentifier := cloudflare.AccountIdentifier(AccountId) listLoadBalancerPoolParams := cloudflare.ListLoadBalancerPoolParams{ PaginationOptions: cloudflare.PaginationOptions{}} pools, err := api.ListLoadBalancerPools(ctxAPI, accountIdentifier, listLoadBalancerPoolParams) 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() //TODO 需要提供AccountID才能获取到正确的Pool。 AccountId := "" accountIdentifier := cloudflare.AccountIdentifier(AccountId) in := Proto2GoLoadBalancerPool(request.LoadBalancerPool) createLoadBalancerPoolParams := cloudflare.CreateLoadBalancerPoolParams{ LoadBalancerPool: *in, } pool, err := api.CreateLoadBalancerPool(ctxAPI, accountIdentifier, createLoadBalancerPoolParams) 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() //TODO 需要提供AccountID才能获取到正确的Pool。 AccountId := "" accountIdentifier := cloudflare.AccountIdentifier(AccountId) in := Proto2GoLoadBalancerPool(request.LoadBalancerPool) updateLoadBalancerPoolParams := cloudflare.UpdateLoadBalancerPoolParams{ LoadBalancer: *in, } pool, err := api.UpdateLoadBalancerPool(ctxAPI, accountIdentifier, updateLoadBalancerPoolParams) 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() //TODO 需要提供AccountID才能获取到正确的Pool。 AccountId := "" accountIdentifier := cloudflare.AccountIdentifier(AccountId) err = api.DeleteLoadBalancerPool(ctxAPI, accountIdentifier, 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 }