Skip to content

Commit

Permalink
Bug 1786363: filter out unsupported regions in Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianofranz committed Feb 28, 2020
1 parent ec87e64 commit fba69c7
Show file tree
Hide file tree
Showing 13 changed files with 8,622 additions and 2 deletions.
42 changes: 40 additions & 2 deletions pkg/asset/installconfig/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"

azres "github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/resources"
azsub "github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/subscriptions"
)

Expand All @@ -27,12 +28,24 @@ func Platform() (*azure.Platform, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to get list of regions")
}

resourceCapableRegions, err := getResourceCapableRegions()
if err != nil {
return nil, errors.Wrap(err, "failed to get list of resources to check available regions")
}

longRegions := make([]string, 0, len(regions))
shortRegions := make([]string, 0, len(regions))
for id, location := range regions {
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
shortRegions = append(shortRegions, id)
for _, resourceCapableRegion := range resourceCapableRegions {
// filter our regions not capable of having resources created (we check for resource groups)
if resourceCapableRegion == location {
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
shortRegions = append(shortRegions, id)
}
}
}

regionTransform := survey.TransformString(func(s string) string {
return strings.SplitN(s, " ", 2)[0]
})
Expand Down Expand Up @@ -95,3 +108,28 @@ func getRegions() (map[string]string, error) {
}
return allLocations, nil
}

func getResourceCapableRegions() ([]string, error) {
session, err := GetSession()
if err != nil {
return nil, err
}

client := azres.NewProvidersClient(session.Credentials.SubscriptionID)
client.Authorizer = session.Authorizer
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer cancel()

provider, err := client.Get(ctx, "Microsoft.Resources", "")
if err != nil {
return nil, err
}

for _, resType := range *provider.ResourceTypes {
if *resType.ResourceType == "resourceGroups" {
return *resType.Locations, nil
}
}

return []string{}, nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fba69c7

Please sign in to comment.