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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	survey "github.com/AlecAivazis/survey/v2"
     9  	"github.com/AlecAivazis/survey/v2/core"
    10  	"github.com/sirupsen/logrus"
    11  
    12  	"github.com/openshift/installer/pkg/types/aws"
    13  	"github.com/openshift/installer/pkg/version"
    14  )
    15  
    16  // Platform collects AWS-specific configuration.
    17  func Platform() (*aws.Platform, error) {
    18  	architecture := version.DefaultArch()
    19  	regions := knownPublicRegions(architecture)
    20  	longRegions := make([]string, 0, len(regions))
    21  	shortRegions := make([]string, 0, len(regions))
    22  	for id, location := range regions {
    23  		longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
    24  		shortRegions = append(shortRegions, id)
    25  	}
    26  	var regionTransform survey.Transformer = func(ans interface{}) interface{} {
    27  		switch v := ans.(type) {
    28  		case core.OptionAnswer:
    29  			return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index}
    30  		case string:
    31  			return strings.SplitN(v, " ", 2)[0]
    32  		}
    33  		return ""
    34  	}
    35  
    36  	defaultRegion := "us-east-1"
    37  	if !IsKnownPublicRegion(defaultRegion, architecture) {
    38  		panic(fmt.Sprintf("installer bug: invalid default AWS region %q", defaultRegion))
    39  	}
    40  
    41  	ssn, err := GetSession()
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	defaultRegionPointer := ssn.Config.Region
    47  	if defaultRegionPointer != nil && *defaultRegionPointer != "" {
    48  		if IsKnownPublicRegion(*defaultRegionPointer, architecture) {
    49  			defaultRegion = *defaultRegionPointer
    50  		} else {
    51  			logrus.Warnf("Unrecognized AWS region %q, defaulting to %s", *defaultRegionPointer, defaultRegion)
    52  		}
    53  	}
    54  
    55  	sort.Strings(longRegions)
    56  	sort.Strings(shortRegions)
    57  
    58  	var region string
    59  	err = survey.Ask([]*survey.Question{
    60  		{
    61  			Prompt: &survey.Select{
    62  				Message: "Region",
    63  				Help:    "The AWS region to be used for installation.",
    64  				Default: fmt.Sprintf("%s (%s)", defaultRegion, regions[defaultRegion]),
    65  				Options: longRegions,
    66  			},
    67  			Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
    68  				choice := regionTransform(ans).(core.OptionAnswer).Value
    69  				i := sort.SearchStrings(shortRegions, choice)
    70  				if i == len(shortRegions) || shortRegions[i] != choice {
    71  					return fmt.Errorf("invalid region %q", choice)
    72  				}
    73  				return nil
    74  			}),
    75  			Transform: regionTransform,
    76  		},
    77  	}, &region)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	return &aws.Platform{
    83  		Region: region,
    84  	}, nil
    85  }