Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

locations: patching the india location names #8217

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions azurerm/internal/clients/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) {
}

if features.EnhancedValidationEnabled() {
// e.g. https://management.azure.com/ but we need management.azure.com
endpoint := strings.TrimPrefix(env.ResourceManagerEndpoint, "https://")
endpoint = strings.TrimSuffix(endpoint, "/")
location.CacheSupportedLocations(ctx, endpoint)
location.CacheSupportedLocations(ctx, env)
}

// client declarations:
Expand Down
5 changes: 3 additions & 2 deletions azurerm/internal/location/supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"

"github.com/Azure/go-autorest/autorest/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/sdk"
)

Expand All @@ -12,8 +13,8 @@ var supportedLocations *[]string

// CacheSupportedLocations attempts to retrieve the supported locations from the Azure MetaData Service
// and caches them, for used in enhanced validation
func CacheSupportedLocations(ctx context.Context, endpoint string) {
locs, err := sdk.AvailableAzureLocations(ctx, endpoint)
func CacheSupportedLocations(ctx context.Context, env *azure.Environment) {
locs, err := sdk.AvailableAzureLocations(ctx, env)
if err != nil {
log.Printf("[DEBUG] error retrieving locations: %s. Enhanced validation will be unavailable", err)
return
Expand Down
36 changes: 35 additions & 1 deletion azurerm/internal/sdk/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"strings"

"github.com/Azure/go-autorest/autorest/azure"
)

type SupportedLocations struct {
Expand All @@ -25,7 +27,11 @@ type metaDataResponse struct {
}

// AvailableAzureLocations returns a list of the Azure Locations which are available on the specified endpoint
func AvailableAzureLocations(ctx context.Context, endpoint string) (*SupportedLocations, error) {
func AvailableAzureLocations(ctx context.Context, env *azure.Environment) (*SupportedLocations, error) {
// e.g. https://management.azure.com/ but we need management.azure.com
endpoint := strings.TrimPrefix(env.ResourceManagerEndpoint, "https://")
endpoint = strings.TrimSuffix(endpoint, "/")

uri := fmt.Sprintf("https://%s//metadata/endpoints?api-version=2018-01-01", endpoint)
client := http.Client{
Transport: &http.Transport{
Expand Down Expand Up @@ -54,7 +60,35 @@ func AvailableAzureLocations(ctx context.Context, endpoint string) (*SupportedLo
}
}

// TODO: remove this once Microsoft fixes the API
// the Azure API returns the india locations the wrong way around
// e.g. 'southindia' is returned as 'indiasouth'
// so we need to conditionally switch these out until Microsoft fixes the API
// $ az account list-locations -o table | grep india
// Central India centralindia (Asia Pacific) Central India
// South India southindia (Asia Pacific) South India
// West India westindia (Asia Pacific) West India
if env.Name == azure.PublicCloud.Name && locations != nil {
out := *locations
out = switchLocationIfExists("indiacentral", "centralindia", out)
out = switchLocationIfExists("indiasouth", "southindia", out)
out = switchLocationIfExists("indiawest", "westindia", out)
locations = &out
}

return &SupportedLocations{
Locations: locations,
}, nil
}

func switchLocationIfExists(find, replace string, locations []string) []string {
out := locations

for i, v := range out {
if v == find {
out[i] = replace
}
}

return locations
}