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

     1  package powervs
     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  
    11  	"github.com/openshift/installer/pkg/types/powervs"
    12  )
    13  
    14  func knownRegions() map[string]string {
    15  
    16  	regions := make(map[string]string)
    17  
    18  	for name, region := range powervs.Regions {
    19  		regions[name] = region.Description
    20  	}
    21  	return regions
    22  }
    23  
    24  // IsKnownRegion return true is a specified region is Known to the installer.
    25  func IsKnownRegion(region string) bool {
    26  	if _, ok := knownRegions()[region]; ok {
    27  		return true
    28  	}
    29  	return false
    30  }
    31  
    32  func knownZones(region string) []string {
    33  	return powervs.Regions[region].Zones
    34  }
    35  
    36  // IsKnownZone return true is a specified zone is Known to the installer.
    37  func IsKnownZone(region string, zone string) bool {
    38  	if _, ok := knownRegions()[region]; ok {
    39  		zones := knownZones(region)
    40  		for _, z := range zones {
    41  			if z == zone {
    42  				return true
    43  			}
    44  		}
    45  		return false
    46  	}
    47  	return false
    48  }
    49  
    50  // GetRegion prompts the user to select a region and returns that region.
    51  func GetRegion(defaultRegion string) (string, error) {
    52  	regions := knownRegions()
    53  
    54  	longRegions := make([]string, 0, len(regions))
    55  	shortRegions := make([]string, 0, len(regions))
    56  	for id, location := range regions {
    57  		longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
    58  		shortRegions = append(shortRegions, id)
    59  	}
    60  	sort.Strings(longRegions)
    61  	sort.Strings(shortRegions)
    62  
    63  	var regionTransform survey.Transformer = func(ans interface{}) interface{} {
    64  		switch v := ans.(type) {
    65  		case core.OptionAnswer:
    66  			return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index}
    67  		case string:
    68  			return strings.SplitN(v, " ", 2)[0]
    69  		}
    70  		return ""
    71  	}
    72  
    73  	var region string
    74  	li := sort.SearchStrings(shortRegions, defaultRegion)
    75  	if li == len(shortRegions) || shortRegions[li] != defaultRegion {
    76  		defaultRegion = "dal"
    77  	} else {
    78  		defaultRegion = longRegions[li]
    79  	}
    80  
    81  	err := survey.Ask([]*survey.Question{
    82  		{
    83  			Prompt: &survey.Select{
    84  				Message: "Region",
    85  				Help:    "The Power VS region to be used for installation.",
    86  				Default: defaultRegion,
    87  				Options: longRegions,
    88  			},
    89  			Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
    90  				choice := regionTransform(ans).(core.OptionAnswer).Value
    91  				i := sort.SearchStrings(shortRegions, choice)
    92  				if i == len(shortRegions) || shortRegions[i] != choice {
    93  					return fmt.Errorf("invalid region %q", choice)
    94  				}
    95  				return nil
    96  			}),
    97  			Transform: regionTransform,
    98  		},
    99  	}, &region)
   100  	if err != nil {
   101  		return "", err
   102  	}
   103  
   104  	return region, nil
   105  }
   106  
   107  // GetZone prompts the user for a zone given a zone.
   108  func GetZone(region string, defaultZone string) (string, error) {
   109  	zones := knownZones(region)
   110  	if len(defaultZone) == 0 {
   111  		defaultZone = zones[0]
   112  	}
   113  
   114  	var zoneTransform survey.Transformer = func(ans interface{}) interface{} {
   115  		switch v := ans.(type) {
   116  		case core.OptionAnswer:
   117  			return core.OptionAnswer{Value: strings.SplitN(v.Value, " ", 2)[0], Index: v.Index}
   118  		case string:
   119  			return strings.SplitN(v, " ", 2)[0]
   120  		}
   121  		return ""
   122  	}
   123  
   124  	var zone string
   125  	err := survey.Ask([]*survey.Question{
   126  		{
   127  			Prompt: &survey.Select{
   128  				Message: "Zone",
   129  				Help:    "The Power VS zone within the region to be used for installation.",
   130  				Default: fmt.Sprintf("%s", defaultZone),
   131  				Options: zones,
   132  			},
   133  			Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
   134  				choice := zoneTransform(ans).(core.OptionAnswer).Value
   135  				i := sort.SearchStrings(zones, choice)
   136  				if i == len(zones) || zones[i] != choice {
   137  					return fmt.Errorf("invalid zone %q", choice)
   138  				}
   139  				return nil
   140  			}),
   141  			Transform: zoneTransform,
   142  		},
   143  	}, &zone)
   144  	if err != nil {
   145  		return "", err
   146  	}
   147  	return zone, err
   148  }