This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
route53.go
122 lines (109 loc) · 3.28 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
package main
import (
"errors"
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/route53/route53iface"
)
var (
errRoute53WaitWatchTimedOut = errors.New("timed out waiting for changes to be applied")
defaultRoute53RecordTTL int64 = 60
defaultRoute53ZoneWaitWatchInterval = 10 * time.Second
defaultRoute53ZoneWaitWatchTimeout = 2 * time.Minute
)
type route53Zone struct {
api route53iface.Route53API
Name string
ID string
Nameservers []string
}
func newRoute53Zone(zoneID string, route53session route53iface.Route53API) (*route53Zone, error) {
ret := &route53Zone{api: route53session}
if err := ret.setZone(zoneID); err != nil {
return nil, err
}
return ret, nil
}
func (z *route53Zone) UpsertCnames(records []cnameRecord) error {
return z.changeCnames(route53.ChangeActionUpsert, records)
}
func (z *route53Zone) DeleteCnames(records []cnameRecord) error {
return z.changeCnames(route53.ChangeActionDelete, records)
}
func (z *route53Zone) changeCnames(action string, records []cnameRecord) error {
changes := make([]*route53.Change, len(records))
for i, r := range records {
changes[i] = &route53.Change{
Action: aws.String(action),
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(r.Hostname),
TTL: aws.Int64(defaultRoute53RecordTTL),
Type: aws.String(route53.RRTypeCname),
ResourceRecords: []*route53.ResourceRecord{{Value: aws.String(r.Target)}},
},
}
}
resp, err := z.api.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{
ChangeBatch: &route53.ChangeBatch{
Changes: changes,
Comment: aws.String("updated by ingress53"),
},
HostedZoneId: aws.String(z.ID),
})
if err != nil {
return err
}
log.Println("[DEBUG] route53 changes have been submitted, waiting for nameservers to sync")
return z.waitForSync(*resp.ChangeInfo.Id)
}
func (z *route53Zone) waitForSync(changeID string) error {
timeout := time.NewTimer(defaultRoute53ZoneWaitWatchTimeout)
tick := time.NewTicker(defaultRoute53ZoneWaitWatchInterval)
defer func() {
timeout.Stop()
tick.Stop()
}()
var err error
var change *route53.GetChangeOutput
for {
select {
case <-tick.C:
change, err = z.api.GetChange(&route53.GetChangeInput{Id: aws.String(changeID)})
if err != nil {
return err
}
if *change.ChangeInfo.Status == route53.ChangeStatusInsync {
return nil
}
log.Printf("[DEBUG] route53 changes are still being applied, waiting for %s", defaultRoute53ZoneWaitWatchInterval.String())
case <-timeout.C:
return errRoute53WaitWatchTimedOut
}
}
}
func (z *route53Zone) setZone(id string) error {
zone, err := z.api.GetHostedZone(&route53.GetHostedZoneInput{Id: aws.String(id)})
if err != nil {
return err
}
z.Name = *zone.HostedZone.Name
z.ID = *zone.HostedZone.Id
z.Nameservers = make([]string, len(zone.DelegationSet.NameServers))
for i, ns := range zone.DelegationSet.NameServers {
z.Nameservers[i] = *ns
}
return nil
}
func (z *route53Zone) Domain() string {
return z.Name
}
func (z *route53Zone) ListNameservers() []string {
ret := make([]string, len(z.Nameservers))
for i, ns := range z.Nameservers {
ret[i] = fmt.Sprintf("%s:53", ns)
}
return ret
}