github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/azure/location.go (about)

     1  /*
     2  Copyright 2022 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package azure
    18  
    19  import "strings"
    20  
    21  // NormalizeLocation converts a Azure location in various formats to the same
    22  // simple format.
    23  //
    24  // This function assumes the input location is in one of the following formats:
    25  // - Name (the "simple" format): "northcentralusstage"
    26  // - Display name: "North Central US (Stage)"
    27  // - Regional display name: "(US) North Central US (Stage)"
    28  //
    29  // Note that the location list can be generated from `az account list-locations
    30  // -o table`. However, this CLI command only lists the locations for the
    31  // current active subscription so it may not show locations in other
    32  // parititions like Government or China.
    33  func NormalizeLocation(input string) string {
    34  	if input == "" {
    35  		return input
    36  	}
    37  
    38  	// Check if the input is a recognized simple name.
    39  	if _, found := locationsToDisplayNames[input]; found {
    40  		return input
    41  	}
    42  
    43  	// If input starts with '(', it should be the Regional display name. Then
    44  	// removes the first bracket and its content. The leftover will be a
    45  	// display name.
    46  	if input[0] == '(' {
    47  		if index := strings.IndexRune(input, ')'); index >= 0 {
    48  			input = input[index:]
    49  		}
    50  		input = strings.TrimSpace(input)
    51  	}
    52  
    53  	// Check if the input is a recognized display name. If so, return the
    54  	// simple name from the mapping.
    55  	if location, found := displayNamesToLocations[input]; found {
    56  		return location
    57  	}
    58  
    59  	// Try our best to convert an unregconized input:
    60  	// - Remove brackets and spaces.
    61  	// - To lower case.
    62  	replacer := strings.NewReplacer("(", "", ")", "", " ", "")
    63  	return strings.ToLower(replacer.Replace(input))
    64  }
    65  
    66  var (
    67  	// displayNamesToLocations maps a location's "Display Name" to its simple
    68  	// "Name".
    69  	displayNamesToLocations = map[string]string{
    70  		// Azure locations.
    71  		"East US":                  "eastus",
    72  		"East US 2":                "eastus2",
    73  		"South Central US":         "southcentralus",
    74  		"West US 2":                "westus2",
    75  		"West US 3":                "westus3",
    76  		"Australia East":           "australiaeast",
    77  		"Southeast Asia":           "southeastasia",
    78  		"North Europe":             "northeurope",
    79  		"Sweden Central":           "swedencentral",
    80  		"UK South":                 "uksouth",
    81  		"West Europe":              "westeurope",
    82  		"Central US":               "centralus",
    83  		"South Africa North":       "southafricanorth",
    84  		"Central India":            "centralindia",
    85  		"East Asia":                "eastasia",
    86  		"Japan East":               "japaneast",
    87  		"Korea Central":            "koreacentral",
    88  		"Canada Central":           "canadacentral",
    89  		"France Central":           "francecentral",
    90  		"Germany West Central":     "germanywestcentral",
    91  		"Norway East":              "norwayeast",
    92  		"Switzerland North":        "switzerlandnorth",
    93  		"UAE North":                "uaenorth",
    94  		"Brazil South":             "brazilsouth",
    95  		"East US 2 EUAP":           "eastus2euap",
    96  		"Qatar Central":            "qatarcentral",
    97  		"Central US (Stage)":       "centralusstage",
    98  		"East US (Stage)":          "eastusstage",
    99  		"East US 2 (Stage)":        "eastus2stage",
   100  		"North Central US (Stage)": "northcentralusstage",
   101  		"South Central US (Stage)": "southcentralusstage",
   102  		"West US (Stage)":          "westusstage",
   103  		"West US 2 (Stage)":        "westus2stage",
   104  		"Asia":                     "asia",
   105  		"Asia Pacific":             "asiapacific",
   106  		"Australia":                "australia",
   107  		"Brazil":                   "brazil",
   108  		"Canada":                   "canada",
   109  		"Europe":                   "europe",
   110  		"France":                   "france",
   111  		"Germany":                  "germany",
   112  		"Global":                   "global",
   113  		"India":                    "india",
   114  		"Japan":                    "japan",
   115  		"Korea":                    "korea",
   116  		"Norway":                   "norway",
   117  		"Singapore":                "singapore",
   118  		"South Africa":             "southafrica",
   119  		"Switzerland":              "switzerland",
   120  		"United Arab Emirates":     "uae",
   121  		"United Kingdom":           "uk",
   122  		"United States":            "unitedstates",
   123  		"United States EUAP":       "unitedstateseuap",
   124  		"East Asia (Stage)":        "eastasiastage",
   125  		"Southeast Asia (Stage)":   "southeastasiastage",
   126  		"East US STG":              "eastusstg",
   127  		"South Central US STG":     "southcentralusstg",
   128  		"North Central US":         "northcentralus",
   129  		"West US":                  "westus",
   130  		"Jio India West":           "jioindiawest",
   131  		"Central US EUAP":          "centraluseuap",
   132  		"West Central US":          "westcentralus",
   133  		"South Africa West":        "southafricawest",
   134  		"Australia Central":        "australiacentral",
   135  		"Australia Central 2":      "australiacentral2",
   136  		"Australia Southeast":      "australiasoutheast",
   137  		"Japan West":               "japanwest",
   138  		"Jio India Central":        "jioindiacentral",
   139  		"Korea South":              "koreasouth",
   140  		"South India":              "southindia",
   141  		"West India":               "westindia",
   142  		"Canada East":              "canadaeast",
   143  		"France South":             "francesouth",
   144  		"Germany North":            "germanynorth",
   145  		"Norway West":              "norwaywest",
   146  		"Switzerland West":         "switzerlandwest",
   147  		"UK West":                  "ukwest",
   148  		"UAE Central":              "uaecentral",
   149  		"Brazil Southeast":         "brazilsoutheast",
   150  
   151  		// Azure Government locations.
   152  		//
   153  		// https://learn.microsoft.com/en-us/azure/azure-government/documentation-government-get-started-connect-with-ps
   154  		"USDoD Central":      "usdodcentral",
   155  		"USDoD East":         "usdodeast",
   156  		"USGov Arizona":      "usgovarizona",
   157  		"USGov Iowa":         "usgoviowa",
   158  		"USGov Texas":        "usgovtexas",
   159  		"USGov Virginia":     "usgovvirginia",
   160  		"USSec East":         "usseceast",
   161  		"USSec West":         "ussecwest",
   162  		"USSec West Central": "ussecwestcentral",
   163  
   164  		// Azure China locations.
   165  		"China East":    "chinaeast",
   166  		"China East 2":  "chinaeast2",
   167  		"China North":   "chinanorth",
   168  		"China North 2": "chinanorth2",
   169  		"China North 3": "chinanorth3",
   170  	}
   171  	// locationsToDisplayNames maps Azure location names to their display
   172  	// names. This is the reverse lookup map of displayNamesToLocations.
   173  	locationsToDisplayNames = map[string]string{}
   174  )
   175  
   176  func initLocations() {
   177  	for displayName, location := range displayNamesToLocations {
   178  		locationsToDisplayNames[location] = displayName
   179  	}
   180  }
   181  
   182  func init() {
   183  	initLocations()
   184  }