123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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 (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()
- records, err := api.DNSRecords(ctxAPI, request.ZoneId, 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)
- response, err := api.CreateDNSRecord(ctxAPI, request.ZoneId, *inRecord)
- if err != nil {
- return nil, err
- }
- outRecord := Go2ProtoDnsRecord(&response.Result)
- 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)
- err = api.UpdateDNSRecord(ctxAPI, request.ZoneId, inRecord.ID, *inRecord)
- 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()
- err = api.DeleteDNSRecord(ctxAPI, request.ZoneId, request.Id)
- if err != nil {
- return nil, err
- }
- return &dns.Empty{}, nil
- }
|