-
Notifications
You must be signed in to change notification settings - Fork 20
/
dns.go
78 lines (62 loc) · 2.06 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
exo "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
var dnsCmd = &cobra.Command{
Use: "dns",
Short: "DNS cmd lets you host your zones and manage records",
}
// domainFromIdent returns a DNS domain from identifier (domain name or ID).
func domainFromIdent(ident string) (*exo.DNSDomain, error) {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone))
if exo.IsValidUUID(ident) {
return globalstate.EgoscaleClient.GetDNSDomain(ctx, account.CurrentAccount.DefaultZone, ident)
}
domains, err := globalstate.EgoscaleClient.ListDNSDomains(ctx, account.CurrentAccount.DefaultZone)
if err != nil {
return nil, err
}
for _, domain := range domains {
if *domain.UnicodeName == ident {
return &domain, nil
}
}
return nil, fmt.Errorf("domain %q not found", ident)
}
// domainRecordFromIdent returns a DNS record from identifier (record name or ID) and optional type
func domainRecordFromIdent(domainID, ident string, rType *string) (*exo.DNSDomainRecord, error) {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, account.CurrentAccount.DefaultZone))
if exo.IsValidUUID(ident) {
return globalstate.EgoscaleClient.GetDNSDomainRecord(ctx, account.CurrentAccount.DefaultZone, domainID, ident)
}
records, err := globalstate.EgoscaleClient.ListDNSDomainRecords(ctx, account.CurrentAccount.DefaultZone, domainID)
if err != nil {
return nil, err
}
var foundRecord *exo.DNSDomainRecord
for _, r := range records {
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
}
}
if foundRecord == nil {
return nil, fmt.Errorf("no records were found")
}
return foundRecord, nil
}
func init() {
RootCmd.AddCommand(dnsCmd)
}