github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/aws/region.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 aws 18 19 import ( 20 "fmt" 21 "strconv" 22 "strings" 23 ) 24 25 // IsCNRegion returns true if the region is an AWS China region. 26 func IsCNRegion(region string) bool { 27 return strings.HasPrefix(strings.ToLower(region), CNRegionPrefix) 28 } 29 30 // IsUSGovRegion returns true if the region is an AWS US GovCloud region. 31 func IsUSGovRegion(region string) bool { 32 return strings.HasPrefix(strings.ToLower(region), USGovRegionPrefix) 33 } 34 35 // ShortRegionToRegion converts short region codes to regular region names. For 36 // example, a short region "use1" maps to region "us-east-1". 37 // 38 // There is no official documentation on this mapping. Here is gist of others 39 // collecting these naming schemes: 40 // https://gist.github.com/colinvh/14e4b7fb6b66c29f79d3 41 // 42 // This function currently does not support regions in secert partitions. 43 func ShortRegionToRegion(shortRegion string) (string, bool) { 44 var prefix, direction string 45 46 // Determine region prefix. 47 remain := strings.ToLower(shortRegion) 48 switch { 49 case strings.HasPrefix(remain, "usg"): 50 prefix = USGovRegionPrefix 51 remain = remain[3:] 52 53 case strings.HasPrefix(remain, "cn"): 54 prefix = CNRegionPrefix 55 remain = remain[2:] 56 57 default: 58 // For regions in standard partition, the first two letters is the 59 // continent or country code (e.g. "eu" for Europe, "us" for US). 60 if len(remain) < 2 { 61 return "", false 62 } 63 64 prefix = remain[:2] + "-" 65 remain = remain[2:] 66 } 67 68 // Map direction codes. 69 switch { 70 case strings.HasPrefix(remain, "nw"): 71 direction = "northwest" 72 remain = remain[2:] 73 case strings.HasPrefix(remain, "ne"): 74 direction = "northeast" 75 remain = remain[2:] 76 case strings.HasPrefix(remain, "se"): 77 direction = "southeast" 78 remain = remain[2:] 79 case strings.HasPrefix(remain, "sw"): 80 direction = "southwest" 81 remain = remain[2:] 82 case strings.HasPrefix(remain, "n"): 83 direction = "north" 84 remain = remain[1:] 85 case strings.HasPrefix(remain, "e"): 86 direction = "east" 87 remain = remain[1:] 88 case strings.HasPrefix(remain, "w"): 89 direction = "west" 90 remain = remain[1:] 91 case strings.HasPrefix(remain, "s"): 92 direction = "south" 93 remain = remain[1:] 94 case strings.HasPrefix(remain, "c"): 95 direction = "central" 96 remain = remain[1:] 97 default: 98 return "", false 99 } 100 101 // Remain should be a number. 102 if _, err := strconv.Atoi(remain); err != nil { 103 return "", false 104 } 105 106 return fmt.Sprintf("%s%s-%s", prefix, direction, remain), true 107 } 108 109 const ( 110 // CNRegionPrefix is the prefix for all AWS China regions. 111 CNRegionPrefix = "cn-" 112 113 // USGovRegionPrefix is the prefix for all AWS US GovCloud regions. 114 USGovRegionPrefix = "us-gov-" 115 )