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

     1  package powervs
     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  
    12  	"github.com/openshift/installer/pkg/types"
    13  )
    14  
    15  // Zone represents a DNS Zone
    16  type Zone struct {
    17  	Name            string
    18  	InstanceCRN     string
    19  	ResourceGroupID string
    20  	Publish         types.PublishingStrategy
    21  }
    22  
    23  // GetDNSZone returns a DNS Zone chosen by survey.
    24  func GetDNSZone() (*Zone, error) {
    25  	client, err := NewClient()
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
    30  	defer cancel()
    31  
    32  	var options []string
    33  	var optionToZoneMap = make(map[string]*Zone, 10)
    34  	isInternal := ""
    35  	strategies := []types.PublishingStrategy{types.ExternalPublishingStrategy, types.InternalPublishingStrategy}
    36  	for _, s := range strategies {
    37  		zones, err := client.GetDNSZones(ctx, s)
    38  		if err != nil {
    39  			return nil, fmt.Errorf("could not retrieve base domains: %w", err)
    40  		}
    41  
    42  		for _, zone := range zones {
    43  			if s == types.InternalPublishingStrategy {
    44  				isInternal = " (Internal)"
    45  			}
    46  			option := fmt.Sprintf("%s%s", zone.Name, isInternal)
    47  			optionToZoneMap[option] = &Zone{
    48  				Name:            zone.Name,
    49  				InstanceCRN:     zone.InstanceCRN,
    50  				ResourceGroupID: zone.ResourceGroupID,
    51  				Publish:         s,
    52  			}
    53  			options = append(options, option)
    54  		}
    55  	}
    56  	sort.Strings(options)
    57  
    58  	var zoneChoice string
    59  	if err := survey.AskOne(&survey.Select{
    60  		Message: "Base Domain",
    61  		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.",
    62  		Options: options,
    63  	},
    64  		&zoneChoice,
    65  		survey.WithValidator(func(ans interface{}) error {
    66  			choice := ans.(core.OptionAnswer).Value
    67  			i := sort.SearchStrings(options, choice)
    68  			if i == len(options) || options[i] != choice {
    69  				return fmt.Errorf("invalid base domain %q", choice)
    70  			}
    71  			return nil
    72  		}),
    73  	); err != nil {
    74  		return nil, fmt.Errorf("failed UserInput: %w", err)
    75  	}
    76  
    77  	return optionToZoneMap[zoneChoice], nil
    78  }