github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/gcp/dns.go (about)

     1  package gcp
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"sort"
     7  	"time"
     8  
     9  	survey "github.com/AlecAivazis/survey/v2"
    10  	"github.com/AlecAivazis/survey/v2/core"
    11  	"github.com/pkg/errors"
    12  	"google.golang.org/api/googleapi"
    13  )
    14  
    15  // GetBaseDomain returns a base domain chosen from among the project's public DNS zones.
    16  func GetBaseDomain(project string) (string, error) {
    17  	client, err := NewClient(context.TODO())
    18  	if err != nil {
    19  		return "", err
    20  	}
    21  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
    22  	defer cancel()
    23  
    24  	publicZones, err := client.GetPublicDomains(ctx, project)
    25  	if err != nil {
    26  		return "", errors.Wrap(err, "could not retrieve base domains")
    27  	}
    28  	if len(publicZones) == 0 {
    29  		return "", errors.New("no domain names found in project")
    30  	}
    31  	sort.Strings(publicZones)
    32  
    33  	var domain string
    34  	if err := survey.AskOne(
    35  		&survey.Select{
    36  			Message: "Base Domain",
    37  			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 hosted zone and rerun the installer.",
    38  			Options: publicZones,
    39  		},
    40  		&domain,
    41  		survey.WithValidator(func(ans interface{}) error {
    42  			choice := ans.(core.OptionAnswer).Value
    43  			i := sort.SearchStrings(publicZones, choice)
    44  			if i == len(publicZones) || publicZones[i] != choice {
    45  				return errors.Errorf("invalid base domain %q", choice)
    46  			}
    47  			return nil
    48  		}),
    49  	); err != nil {
    50  		return "", errors.Wrap(err, "failed UserInput")
    51  	}
    52  
    53  	return domain, nil
    54  }
    55  
    56  // IsForbidden checks whether a response from the GPC API was forbidden,
    57  // indicating that a given service account cannot access the specified project.
    58  func IsForbidden(err error) bool {
    59  	gErr, ok := err.(*googleapi.Error)
    60  	return ok && gErr.Code == http.StatusForbidden
    61  }
    62  
    63  // IsThrottled checks whether a response from the GPC API returns Too Many Requests
    64  func IsThrottled(err error) bool {
    65  	gErr, ok := err.(*googleapi.Error)
    66  	return ok && gErr.Code == http.StatusTooManyRequests
    67  }
    68  
    69  // IsNotFound checks whether a response from the GPC API was not found.
    70  func IsNotFound(err error) bool {
    71  	gErr, ok := err.(*googleapi.Error)
    72  	return ok && gErr.Code == http.StatusNotFound
    73  }