123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package server
- import (
- "cfTest/cloudflareApi/loadBalancer"
- "context"
- "github.com/cloudflare/cloudflare-go"
- "github.com/jinzhu/copier"
- "github.com/samber/lo"
- )
- type LoadBalancerServer struct {
- loadBalancer.UnimplementedLoadBalancerCloudflareServer
- }
- func Go2ProtoLoadBalancer(goBalancer *cloudflare.LoadBalancer) (protoBalancer *loadBalancer.LoadBalancer) {
- protoBalancer = new(loadBalancer.LoadBalancer)
- err := copier.Copy(protoBalancer, goBalancer)
- if err != nil {
- return nil
- }
- protoBalancer.Id = goBalancer.ID
- return
- }
- func Proto2GoLoadBalancer(protoBalancer *loadBalancer.LoadBalancer) (goBalancer *cloudflare.LoadBalancer) {
- goBalancer = new(cloudflare.LoadBalancer)
- err := copier.Copy(goBalancer, protoBalancer)
- if err != nil {
- return nil
- }
- goBalancer.ID = protoBalancer.Id
- return
- }
- func (d LoadBalancerServer) List(ctx context.Context, request *loadBalancer.ListRequest) (*loadBalancer.LoadBalancerList, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
- listLoadBalancerParams := cloudflare.ListLoadBalancerParams{
- PaginationOptions: cloudflare.PaginationOptions{}}
- balancers, err := api.ListLoadBalancers(ctxAPI, zoneIdentifier, listLoadBalancerParams)
- //balancers, err := api.ListLoadBalancers(ctxAPI, request.ZoneId)
- if err != nil {
- return nil, err
- }
- filterMap := lo.FilterMap[cloudflare.LoadBalancer, *loadBalancer.LoadBalancer](balancers, func(balancer cloudflare.LoadBalancer, _ int) (*loadBalancer.LoadBalancer, bool) {
- protoLoadBalancer := Go2ProtoLoadBalancer(&balancer)
- if protoLoadBalancer == nil {
- return nil, false
- }
- return protoLoadBalancer, true
- })
- return &loadBalancer.LoadBalancerList{LoadBalancers: filterMap}, nil
- }
- func (d LoadBalancerServer) Create(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
- in := Proto2GoLoadBalancer(request.LoadBalancer)
- createLoadBalancerParams := cloudflare.CreateLoadBalancerParams{
- LoadBalancer: *in,
- }
- balancer, err := api.CreateLoadBalancer(ctxAPI, zoneIdentifier, createLoadBalancerParams)
- //balancer, err := api.CreateLoadBalancer(ctxAPI, request.ZoneId, *in)
- if err != nil {
- return nil, err
- }
- out := Go2ProtoLoadBalancer(&balancer)
- return out, nil
- }
- func (d LoadBalancerServer) Update(ctx context.Context, request *loadBalancer.LoadBalancerRequest) (*loadBalancer.LoadBalancer, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- in := Proto2GoLoadBalancer(request.LoadBalancer)
- zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
- updateLoadBalancerParams := cloudflare.UpdateLoadBalancerParams{
- LoadBalancer: *in,
- }
- balancer, err := api.UpdateLoadBalancer(ctxAPI, zoneIdentifier, updateLoadBalancerParams)
- //balancer, err := api.ModifyLoadBalancer(ctxAPI, request.ZoneId, *in)
- if err != nil {
- return nil, err
- }
- out := Go2ProtoLoadBalancer(&balancer)
- return out, nil
- }
- func (d LoadBalancerServer) Delete(ctx context.Context, request *loadBalancer.DeleteRequest) (*loadBalancer.Empty, error) {
- api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
- if err != nil {
- return nil, err
- }
- ctxAPI := context.Background()
- zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
- err = api.DeleteLoadBalancer(ctxAPI, zoneIdentifier, request.Id)
- if err != nil {
- return nil, err
- }
- return &loadBalancer.Empty{}, nil
- }
|