Skip to content

Commit

Permalink
validate region when provided by user
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiamac committed Jun 24, 2019
1 parent cfa6597 commit 0846b68
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/juju/commands/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/naturalsort"
"github.com/juju/schema"
"github.com/juju/utils"
"github.com/juju/utils/featureflag"
Expand Down Expand Up @@ -505,6 +506,23 @@ to create a new model to deploy k8s workloads.
if err != nil {
return errors.Trace(err)
}
// If region is specified by the user, validate it here.
// lp#1632735
if c.Region != "" {
_, err := jujucloud.RegionByName(cloud.Regions, c.Region)
if err != nil {
allRegions := make([]string, len(cloud.Regions))
for i, one := range cloud.Regions {
allRegions[i] = one.Name
}
if len(allRegions) > 0 {
naturalsort.Sort(allRegions)
ctx.Infof("Available cloud regions are %v", strings.Join(allRegions, ", "))
}
return errors.NotValidf("region %q for cloud %q", c.Region, c.Cloud)
}
}

isCAASController := jujucloud.CloudIsCAAS(cloud)

// Custom clouds may not have explicitly declared support for any auth-
Expand Down
8 changes: 8 additions & 0 deletions cmd/juju/commands/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,14 @@ sa-east-1
`[1:])
}

func (s *BootstrapSuite) TestBootstrapInvalidRegion(c *gc.C) {
resetJujuXDGDataHome(c)
ctx, err := cmdtesting.RunCommand(c, s.newBootstrapCommand(), "aws/eu-west")
c.Assert(err, gc.ErrorMatches, `region "eu-west" for cloud "aws" not valid`)
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Available cloud regions are ap-northeast-1, ap-northeast-2, ap-south-1, ap-southeast-1, ap-southeast-2, ca-central-1, eu-central-1, eu-west-1, eu-west-2, eu-west-3, sa-east-1, us-east-1, us-east-2, us-west-1, us-west-2\n")
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, ``)
}

func (s *BootstrapSuite) TestBootstrapPrintCloudRegionsNoSuchCloud(c *gc.C) {
resetJujuXDGDataHome(c)
_, err := cmdtesting.RunCommand(c, s.newBootstrapCommand(), "--regions", "foo")
Expand Down

0 comments on commit 0846b68

Please sign in to comment.