Example #1
0
func (s *cloudSuite) TestRegionNames(c *gc.C) {
	regions := []cloud.Region{
		{Name: "mars"},
		{Name: "earth"},
		{Name: "jupiter"},
	}

	names := cloud.RegionNames(regions)
	c.Assert(names, gc.DeepEquals, []string{"earth", "jupiter", "mars"})

	c.Assert(cloud.RegionNames([]cloud.Region{}), gc.HasLen, 0)
	c.Assert(cloud.RegionNames(nil), gc.HasLen, 0)
}
Example #2
0
// queryRegion asks the user to pick a region of the ones passed in. The first
// region in the list will be the default.
func queryRegion(cloud string, regions []jujucloud.Region, scanner *bufio.Scanner, w io.Writer) (string, error) {
	fmt.Fprintf(w, "Regions in %s:\n", cloud)
	names := jujucloud.RegionNames(regions)
	// add an empty string to allow for a default value. Also gives us an extra
	// line return after the list of names.
	names = append(names, "")
	if _, err := fmt.Fprintln(w, strings.Join(names, "\n")); err != nil {
		return "", errors.Trace(err)
	}
	verify := interact.MatchOptions(names, errors.Errorf("Invalid region."))
	defaultRegion := regions[0].Name
	query := fmt.Sprintf("Select a region in %s [%s]: ", cloud, defaultRegion)
	region, err := interact.QueryVerify([]byte(query), scanner, w, verify)
	if err != nil {
		return "", errors.Trace(err)
	}
	if region == "" {
		return defaultRegion, nil
	}
	regionName, ok := interact.FindMatch(region, names)
	if !ok {
		// should be impossible
		return "", errors.Errorf("invalid region name chosen: %s", region)
	}

	return regionName, nil
}