Skip to content

Commit

Permalink
refactor(dns): Use build-in function to find domain / domain-record f…
Browse files Browse the repository at this point in the history
…rom name or id
  • Loading branch information
mlec1 committed Sep 28, 2024
1 parent 0fece24 commit 1a8531f
Showing 1 changed file with 12 additions and 49 deletions.
61 changes: 12 additions & 49 deletions cmd/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"errors"
"fmt"

"github.com/spf13/cobra"

Expand All @@ -19,74 +18,38 @@ var dnsCmd = &cobra.Command{
func domainFromIdent(ident string) (*v3.DNSDomain, error) {
ctx := gContext

domainId, err := v3.ParseUUID(ident)
if err == nil {
// ident is a valid UUID
domain, err := globalstate.EgoscaleV3Client.GetDNSDomain(ctx, domainId)
if err != nil {
return nil, err
}

return domain, nil
}

// ident is not a UUID, trying finding domain by name
domains, err := globalstate.EgoscaleV3Client.ListDNSDomains(ctx)
domainsResp, err := globalstate.EgoscaleV3Client.ListDNSDomains(ctx)
if err != nil {
return nil, err
}

for _, domain := range domains.DNSDomains {
if domain.UnicodeName == ident {
return &domain, nil
}
domain, err := domainsResp.FindDNSDomain(ident)
if err != nil {
return nil, err
}

return nil, fmt.Errorf("domain %q not found", ident)
return &domain, err
}

// domainRecordFromIdent returns a DNS record from identifier (record name or ID) and optional type
func domainRecordFromIdent(domainID v3.UUID, ident string, rType *v3.DNSDomainRecordType) (*v3.DNSDomainRecord, error) {
ctx := gContext

RecordId, err := v3.ParseUUID(ident)
if err == nil {
// ident is a valid UUID
domainRecord, err := globalstate.EgoscaleV3Client.GetDNSDomainRecord(ctx, domainID, RecordId)
if err != nil {
return nil, err
}

return domainRecord, nil
}

// ident is not a UUID, trying finding domain by name
records, err := globalstate.EgoscaleV3Client.ListDNSDomainRecords(ctx, domainID)
domainRecordsResp, err := globalstate.EgoscaleV3Client.ListDNSDomainRecords(ctx, domainID)
if err != nil {
return nil, err
}

var foundRecord *v3.DNSDomainRecord

for _, r := range records.DNSDomainRecords {
if rType != nil && r.Type != *rType {
continue
}

if ident == r.Name {
if foundRecord != nil {
return nil, errors.New("more than one records were found")
}
t := r
foundRecord = &t
}
domainRecord, err := domainRecordsResp.FindDNSDomainRecord(ident)
if err != nil {
return nil, err
}

if foundRecord == nil {
return nil, fmt.Errorf("no records were found")
if rType != nil && domainRecord.Type != *rType {
return nil, errors.New("Record not found (Record Type doesn't match)")
}

return foundRecord, nil
return &domainRecord, nil
}

func init() {
Expand Down

0 comments on commit 1a8531f

Please sign in to comment.