forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basedomain.go
80 lines (69 loc) · 2.19 KB
/
basedomain.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
package aws
import (
"net/http"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
survey "gopkg.in/AlecAivazis/survey.v1"
)
// IsForbidden returns true if and only if the input error is an HTTP
// 403 error from the AWS API.
func IsForbidden(err error) bool {
requestError, ok := err.(awserr.RequestFailure)
return ok && requestError.StatusCode() == http.StatusForbidden
}
// GetBaseDomain returns a base domain chosen from among the account's
// public routes.
func GetBaseDomain() (string, error) {
session, err := getSession()
if err != nil {
return "", err
}
client := route53.New(session)
publicZoneMap := map[string]struct{}{}
exists := struct{}{}
input := route53.ListHostedZonesInput{}
for i := 0; true; i++ {
logrus.Debugf("listing AWS hosted zones (page %d)", i)
response, err := client.ListHostedZones(&input)
if err != nil {
return "", errors.Wrap(err, "list hosted zones")
}
for _, zone := range response.HostedZones {
if !*zone.Config.PrivateZone {
publicZoneMap[strings.TrimSuffix(*zone.Name, ".")] = exists
}
}
if !*response.IsTruncated {
break
}
input.Marker = response.Marker
}
publicZones := make([]string, 0, len(publicZoneMap))
for name := range publicZoneMap {
publicZones = append(publicZones, name)
}
sort.Strings(publicZones)
if len(publicZones) == 0 {
return "", errors.New("no public Route 53 hosted zones found")
}
var domain string
if err := survey.AskOne(&survey.Select{
Message: "Base Domain",
Help: "The base domain of the cluster. All DNS records will be sub-domains of this base and will also include the cluster name.\n\nIf you don't see you intended base-domain listed, create a new public Route53 hosted zone and rerun the installer.",
Options: publicZones,
}, &domain, func(ans interface{}) error {
choice := ans.(string)
i := sort.SearchStrings(publicZones, choice)
if i == len(publicZones) || publicZones[i] != choice {
return errors.Errorf("invalid base domain %q", choice)
}
return nil
}); err != nil {
return "", errors.Wrap(err, "failed UserInput for base domain")
}
return domain, nil
}