-
Notifications
You must be signed in to change notification settings - Fork 19
/
dns.go
78 lines (63 loc) · 1.91 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/exoscale/egoscale"
exo "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
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(gCurrentAccount.Environment, gCurrentAccount.DefaultZone))
_, err := egoscale.ParseUUID(ident)
if err == nil {
return cs.GetDNSDomain(ctx, gCurrentAccount.DefaultZone, ident)
}
domains, err := cs.ListDNSDomains(ctx, gCurrentAccount.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(gCurrentAccount.Environment, gCurrentAccount.DefaultZone))
_, err := egoscale.ParseUUID(ident)
if err == nil {
return cs.GetDNSDomainRecord(ctx, gCurrentAccount.DefaultZone, domainID, ident)
}
records, err := cs.ListDNSDomainRecords(ctx, gCurrentAccount.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)
}