package server import ( "cfTest/cloudflareApi/dns" "context" "github.com/cloudflare/cloudflare-go" "github.com/jinzhu/copier" "github.com/samber/lo" ) type DnsServer struct { dns.UnimplementedDnsCloudflareServer } func Go2ProtoDnsRecord(goRecord *cloudflare.DNSRecord) (protoRecord *dns.DnsRecord) { protoRecord = new(dns.DnsRecord) err := copier.Copy(protoRecord, goRecord) if err != nil { return nil } protoRecord.Id = goRecord.ID protoRecord.ZoneId = goRecord.ZoneID protoRecord.Ttl = int64(goRecord.TTL) return } func Proto2GoDnsRecord(protoRecord *dns.DnsRecord) (goRecord *cloudflare.DNSRecord) { goRecord = new(cloudflare.DNSRecord) err := copier.Copy(goRecord, protoRecord) if err != nil { return nil } goRecord.TTL = int(protoRecord.Ttl) goRecord.ID = protoRecord.Id goRecord.ZoneID = protoRecord.ZoneId return } func goRecord2CreateDnsRecordParams(goRecord *cloudflare.DNSRecord) (createDnsRecordParams *cloudflare.CreateDNSRecordParams) { createDnsRecordParams = new(cloudflare.CreateDNSRecordParams) err := copier.Copy(createDnsRecordParams, goRecord) if err != nil { return nil } return } func goRecord2UpdateDnsRecordParams(goRecord *cloudflare.DNSRecord) (updateDnsRecordParams *cloudflare.UpdateDNSRecordParams) { updateDnsRecordParams = new(cloudflare.UpdateDNSRecordParams) err := copier.Copy(updateDnsRecordParams, goRecord) if err != nil { return nil } return } func (d DnsServer) List(ctx context.Context, request *dns.ListRequest) (*dns.DnsRecordList, error) { api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail()) if err != nil { return nil, err } ctxAPI := context.Background() zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId) records, _, err := api.ListDNSRecords(ctxAPI, zoneIdentifier, cloudflare.ListDNSRecordsParams{}) //records, err := api.DNSRecords(ctxAPI, zoneIdentifier, cloudflare.DNSRecord{}) if err != nil { return nil, err } filterMap := lo.FilterMap[cloudflare.DNSRecord, *dns.DnsRecord](records, func(record cloudflare.DNSRecord, _ int) (*dns.DnsRecord, bool) { protoRecord := Go2ProtoDnsRecord(&record) if protoRecord == nil { return nil, false } return protoRecord, true }) return &dns.DnsRecordList{DnsRecord: filterMap}, nil } func (d DnsServer) Create(ctx context.Context, request *dns.DnsRecordRequest) (*dns.DnsRecord, error) { api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail()) if err != nil { return nil, err } ctxAPI := context.Background() inRecord := Proto2GoDnsRecord(request.DnsRecord) zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId) createDNSRecordParams := goRecord2CreateDnsRecordParams(inRecord) response, err := api.CreateDNSRecord(ctxAPI, zoneIdentifier, *createDNSRecordParams) if err != nil { return nil, err } outRecord := Go2ProtoDnsRecord(&response) return outRecord, nil } func (d DnsServer) Update(ctx context.Context, request *dns.DnsRecordRequest) (*dns.Empty, error) { api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail()) if err != nil { return nil, err } ctxAPI := context.Background() inRecord := Proto2GoDnsRecord(request.DnsRecord) zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId) updateDNSRecordParams := goRecord2UpdateDnsRecordParams(inRecord) _, err = api.UpdateDNSRecord(ctxAPI, zoneIdentifier, *updateDNSRecordParams) if err != nil { return nil, err } return &dns.Empty{}, nil } func (d DnsServer) Delete(ctx context.Context, request *dns.DeleteRequest) (*dns.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.DeleteDNSRecord(ctxAPI, zoneIdentifier, request.Id) if err != nil { return nil, err } return &dns.Empty{}, nil }