-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
94 lines (86 loc) · 2.59 KB
/
routes.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
package amazon
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"k8s.io/apimachinery/pkg/util/uuid"
)
// RegisterAwsCustomDomain registers a wildcard ALIAS for the custom domain
// to point at the given ELB host name
func RegisterAwsCustomDomain(customDomain string, elbAddress string) error {
sess, err := NewAwsSessionWithoutOptions()
if err != nil {
return err
}
svc := route53.New(sess)
// find the hosted zone for the domain name
var hostedZoneId *string
listZonesInput := &route53.ListHostedZonesInput{}
err = svc.ListHostedZonesPages(listZonesInput, func(page *route53.ListHostedZonesOutput, hasNext bool) bool {
if page != nil {
for _, r := range page.HostedZones {
if r != nil && r.Name != nil && (*r.Name == customDomain || *r.Name == customDomain+".") {
hostedZoneId = r.Id
return false
}
}
}
return true
})
if err != nil {
return err
}
if hostedZoneId == nil {
// lets create the hosted zone!
callerRef := string(uuid.NewUUID())
createInput := &route53.CreateHostedZoneInput{
Name: aws.String(customDomain),
CallerReference: aws.String(callerRef),
}
results, err := svc.CreateHostedZone(createInput)
if err != nil {
return err
}
if results.HostedZone == nil {
return fmt.Errorf("No HostedZone created for name %s!", customDomain)
}
hostedZoneId = results.HostedZone.Id
if hostedZoneId == nil {
return fmt.Errorf("No HostedZone ID created for name %s!", customDomain)
}
}
upsert := route53.ChangeActionUpsert
ttl := int64(300)
recordType := "CNAME"
wildcard := "*." + customDomain
info := util.ColorInfo
log.Infof("About to insert/update DNS %s record into HostedZone %s with wildcard %s pointing to %s\n", info(recordType), info(*hostedZoneId), info(wildcard), info(elbAddress))
changeInput := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: hostedZoneId,
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{
&route53.Change{
Action: &upsert,
ResourceRecordSet: &route53.ResourceRecordSet{
Name: aws.String(wildcard),
Type: aws.String(recordType),
TTL: &ttl,
ResourceRecords: []*route53.ResourceRecord{
{
Value: aws.String(elbAddress),
},
},
},
},
},
},
}
_, err = svc.ChangeResourceRecordSets(changeInput)
if err != nil {
return fmt.Errorf("Failed to update record for hostedZoneID %s: %s", *hostedZoneId, err)
}
log.Infof("Updated HostZone ID %s successfully\n", info(*hostedZoneId))
return nil
}