/
route53.go
139 lines (113 loc) · 2.75 KB
/
route53.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package aws
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"text/tabwriter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
)
// Route53 client struct
type Route53 struct {
Client *route53.Client
}
// NewRoute53Sess return Route53 struct initialized
func NewRoute53Client(profile, region string) *Route53 {
return &Route53{
Client: route53.NewFromConfig(GetSession(profile, region)),
}
}
// Record route53 set record struct
type Record struct {
ZoneId string
DomainName string
Type string
TTL string
DomainValue string
}
// Records Record struct slice
type Records []Record
// ListHostedZones return Records
// input route53.ListHostedZonesInput
func (c *Route53) ListHostedZones(input *route53.ListHostedZonesInput) (Records, error) {
zones, err := c.Client.ListHostedZones(context.TODO(), input)
if err != nil {
return nil, fmt.Errorf("list hostedzones: %v", err)
}
list := Records{}
for _, h := range zones.HostedZones {
s := strings.Split(*h.Id, "/")
zoneid := s[2]
rinput := &route53.ListResourceRecordSetsInput{
HostedZoneId: h.Id,
}
paginator := route53.NewListResourceRecordSetsPaginator(c.Client, rinput)
for paginator.HasMorePages() {
output, err := paginator.NextPage(context.Background())
if err != nil {
return nil, fmt.Errorf("list resource record sets: %v", err)
}
for _, r := range output.ResourceRecordSets {
if r.TTL == nil {
r.TTL = aws.Int64(0000)
}
ttl := strconv.FormatInt(*r.TTL, 10)
var value string
if r.AliasTarget == nil {
var values []string
for _, rr := range r.ResourceRecords {
values = append(values, *rr.Value)
}
value = strings.Join(values[:], ",")
} else if r.ResourceRecords == nil {
value = *r.AliasTarget.DNSName
}
list = append(list, Record{
ZoneId: zoneid,
DomainName: *r.Name,
Type: string(r.Type),
TTL: ttl,
DomainValue: value,
})
}
}
}
if len(list) == 0 {
return nil, fmt.Errorf("no resources")
}
return list, nil
}
func PrintRecords(wrt io.Writer, resources Records) error {
w := tabwriter.NewWriter(wrt, 0, 8, 1, ' ', 0)
header := []string{
"ZoneId",
"DomainName",
"Type",
"TTL",
"DomainValue",
}
if _, err := fmt.Fprintln(w, strings.Join(header, "\t")); err != nil {
return fmt.Errorf("%v", err)
}
for _, r := range resources {
if _, err := fmt.Fprintln(w, r.RecordTabString()); err != nil {
return fmt.Errorf("%v", err)
}
}
if err := w.Flush(); err != nil {
return fmt.Errorf("%v", err)
}
return nil
}
func (i *Record) RecordTabString() string {
fields := []string{
i.ZoneId,
i.DomainName,
i.Type,
i.TTL,
i.DomainValue,
}
return strings.Join(fields, "\t")
}