Skip to content

Commit

Permalink
Merge pull request coreos#1102 from bgilbert/regions
Browse files Browse the repository at this point in the history
cmd/ore/aws: let list-regions list disabled regions, or all regions
  • Loading branch information
bgilbert authored Oct 18, 2019
2 parents 39ff346 + 3bf1464 commit 1da599e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
15 changes: 14 additions & 1 deletion cmd/ore/aws/list-regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"

"github.com/spf13/cobra"

"github.com/coreos/mantle/platform/api/aws"
)

var (
Expand All @@ -29,14 +31,25 @@ var (
specified credentials file, profile, and region.`,
RunE: runListRegions,
}
disabledRegions bool
allRegions bool
)

func init() {
AWS.AddCommand(cmdListRegions)
cmdListRegions.Flags().BoolVar(&disabledRegions, "disabled", false, "list disabled regions")
cmdListRegions.Flags().BoolVar(&allRegions, "all", false, "list all regions")
}

func runListRegions(cmd *cobra.Command, args []string) error {
regions, err := API.ListRegions()
var kind = aws.RegionEnabled
if allRegions {
kind = aws.RegionAny
} else if disabledRegions {
kind = aws.RegionDisabled
}

regions, err := API.ListRegions(kind)
if err != nil {
fmt.Fprintf(os.Stderr, "could not list regions: %v\n", err)
os.Exit(1)
Expand Down
25 changes: 23 additions & 2 deletions platform/api/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,31 @@ import (
"github.com/coreos/mantle/util"
)

type RegionKind int

const (
RegionEnabled RegionKind = iota
RegionDisabled
RegionAny
)

// ListRegions lists the enabled regions in the AWS partition specified
// implicitly by the CredentialsFile, Profile, and Region options.
func (a *API) ListRegions() ([]string, error) {
output, err := a.ec2.DescribeRegions(nil)
func (a *API) ListRegions(kind RegionKind) ([]string, error) {
input := ec2.DescribeRegionsInput{}
switch kind {
case RegionDisabled:
input.AllRegions = aws.Bool(true)
input.Filters = []*ec2.Filter{
{
Name: aws.String("opt-in-status"),
Values: []*string{aws.String("not-opted-in")},
},
}
case RegionAny:
input.AllRegions = aws.Bool(true)
}
output, err := a.ec2.DescribeRegions(&input)
if err != nil {
return nil, fmt.Errorf("describing regions: %v", err)
}
Expand Down

0 comments on commit 1da599e

Please sign in to comment.