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

     1  package ibmcloud
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     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  
    13  	"github.com/openshift/installer/pkg/types"
    14  )
    15  
    16  // Zone represents a DNS Zone
    17  type Zone struct {
    18  	Name            string
    19  	ID              string
    20  	InstanceCRN     string
    21  	ResourceGroupID string
    22  }
    23  
    24  // GetDNSZone returns a DNS Zone chosen by survey.
    25  func GetDNSZone() (*Zone, error) {
    26  	// A pre-existing installConfig with potential serviceEndpoints would be required,
    27  	// but doesn't exist at this time (generating an installConfig), so we pass nil
    28  	client, err := NewClient(nil)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
    33  	defer cancel()
    34  
    35  	// IBM Cloud defaults to External (CIS) publish strategy during domain query
    36  	// TODO(cjschaef): Consider also offering Internal (DNS) based domains as well
    37  	publicZones, err := client.GetDNSZones(ctx, types.ExternalPublishingStrategy)
    38  	if err != nil {
    39  		return nil, errors.Wrap(err, "could not retrieve base domains")
    40  	}
    41  	if len(publicZones) == 0 {
    42  		return nil, errors.New("no domain names found in project")
    43  	}
    44  
    45  	var options []string
    46  	var optionToZoneMap = make(map[string]*Zone, len(publicZones))
    47  	for _, zone := range publicZones {
    48  		option := fmt.Sprintf("%s (%s)", zone.Name, zone.InstanceName)
    49  		optionToZoneMap[option] = &Zone{
    50  			Name:            zone.Name,
    51  			ID:              zone.ID,
    52  			InstanceCRN:     zone.InstanceCRN,
    53  			ResourceGroupID: zone.ResourceGroupID,
    54  		}
    55  		options = append(options, option)
    56  	}
    57  	sort.Strings(options)
    58  
    59  	var zoneChoice string
    60  	if err := survey.AskOne(&survey.Select{
    61  		Message: "Base Domain",
    62  		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 your intended base-domain listed, create a new public hosted zone and rerun the installer.",
    63  		Options: options,
    64  	},
    65  		&zoneChoice,
    66  		survey.WithValidator(func(ans interface{}) error {
    67  			choice := ans.(core.OptionAnswer).Value
    68  			i := sort.SearchStrings(options, choice)
    69  			if i == len(publicZones) || options[i] != choice {
    70  				return errors.Errorf("invalid base domain %q", choice)
    71  			}
    72  			return nil
    73  		}),
    74  	); err != nil {
    75  		return nil, errors.Wrap(err, "failed UserInput")
    76  	}
    77  
    78  	return optionToZoneMap[zoneChoice], nil
    79  }