Skip to content

Instantly share code, notes, and snippets.

@robbiet480
Last active November 16, 2020 10:00
Show Gist options
  • Select an option

  • Save robbiet480/e241c7b281a4a37d4a5e to your computer and use it in GitHub Desktop.

Select an option

Save robbiet480/e241c7b281a4a37d4a5e to your computer and use it in GitHub Desktop.
Extensions to AWSRegionType to turn a string into a region name and lookup simple information about a region
import AWSCore
extension AWSRegionType {
/**
Return an AWSRegionType for the given string
- Parameter regionString: The Region name (e.g. us-east-1) as a string
- Returns: A new AWSRegionType for the given string, Unknown if no region was found.
*/
static func regionTypeForString(regionString: String) -> AWSRegionType {
switch regionString {
case "us-east-1": return .USEast1
case "us-west-1": return .USWest1
case "us-west-2": return .USWest2
case "eu-west-1": return .EUWest1
case "eu-central-1": return .EUCentral1
case "ap-northeast-1": return .APNortheast1
case "ap-northeast-2": return .APNortheast2
case "ap-southeast-1": return .APSoutheast1
case "ap-southeast-2": return .APSoutheast2
case "sa-east-1": return .SAEast1
case "cn-north-1": return .CNNorth1
case "us-gov-west-1": return .USGovWest1
default: return .Unknown
}
}
/**
Return the string representation of the AWSRegionType
- Returns: The string representation of the AWSRegionType.
*/
var stringValue: String {
switch self {
case .USEast1: return "us-east-1"
case .USWest1: return "us-west-1"
case .USWest2: return "us-west-2"
case .EUWest1: return "eu-west-1"
case .EUCentral1: return "eu-central-1"
case .APNortheast1: return "ap-northeast-1"
case .APNortheast2: return "ap-northeast-2"
case .APSoutheast1: return "ap-southeast-1"
case .APSoutheast2: return "ap-southeast-2"
case .SAEast1: return "sa-east-1"
case .CNNorth1: return "cn-north-1"
case .USGovWest1: return "us-gov-west-1"
default: return "Unknown"
}
}
/**
Return the physical region (e.g. us) of the AWSRegionType
- Returns: The physical region of the AWSRegionType as a string.
*/
var physicalRegion: String {
switch self {
case .USEast1, .USWest1, .USWest2: return "us"
case .EUWest1, .EUCentral1: return "eu"
case .APNortheast1, .APNortheast2, .APSoutheast1, .APSoutheast2: return "ap"
case .SAEast1: return "sa"
case .CNNorth1: return "cn"
case .USGovWest1: return "us-gov"
default: return "Unknown"
}
}
/**
Return the physical location (e.g. N. Virginia) of the AWSRegionType
- Returns: The physical location of the AWSRegionType as a string.
*/
var physicalLocation: String {
switch self {
case .USEast1: return "N. Virginia"
case .USWest1: return "N. California"
case .USWest2: return "Oregon"
case .EUWest1: return "Ireland"
case .EUCentral1: return "Frankfurt"
case .APNortheast1: return "Tokyo"
case .APNortheast2: return "Seoul"
case .APSoutheast1: return "Singapore"
case .APSoutheast2: return "Sydney"
case .SAEast1: return "Sao Paulo"
case .CNNorth1: return "Beijing"
case .USGovWest1: return "US"
default: return "Unknown"
}
}
/**
Return the full name (e.g. US East (N. Virginia)) of the AWSRegionType
- Returns: The full name of the AWSRegionType as a string.
*/
var name: String {
switch self {
case .USEast1: return "US East (N. Virginia)"
case .USWest1: return "US West (N. California)"
case .USWest2: return "US West (Oregon)"
case .EUWest1: return "EU (Ireland)"
case .EUCentral1: return "EU (Frankfurt)"
case .APNortheast1: return "Asia Pacific (Tokyo)"
case .APNortheast2: return "Asia Pacific (Seoul)"
case .APSoutheast1: return "Asia Pacific (Singapore)"
case .APSoutheast2: return "Asia Pacific (Sydney)"
case .SAEast1: return "South America (Sao Paulo)"
case .CNNorth1: return "China (Beijing)"
case .USGovWest1: return "AWS GovCloud (US)"
default: return "Unknown"
}
}
/**
Return the cardinal direction (e.g. East) of the AWSRegionType
- Returns: The cardinal direction of the AWSRegionType as a string.
*/
var cardinalDirection: String {
switch self {
case .USEast1, .SAEast1: return "East"
case .USWest1, .USWest2, .EUWest1, .USGovWest1: return "West"
case .EUCentral1: return "Central"
case .APNortheast1, .APNortheast2: return "Northeast"
case .APSoutheast1, .APSoutheast2: return "Southeast"
case .CNNorth1: return "North"
default: return "Unknown"
}
}
/**
Return the number (e.g. 1) of the AWSRegionType
- Returns: The number of the AWSRegionType as an integer.
*/
var number: Int {
switch self {
case .USEast1, .USWest1, .EUWest1, .EUCentral1, .APNortheast1, .APSoutheast1, .SAEast1, .CNNorth1, .USGovWest1: return 1
case .USWest2, .APNortheast2, .APSoutheast2: return 2
default: return 0
}
}
}
@mikeCalvir
Copy link
Copy Markdown

Here's what I used for ObjC just in case useful to someone, first function only.

/**
 Return an AWSRegionType for the given string

 - Parameter regionString: The Region name (e.g. us-east-1) as a string

 - Returns: A AWSRegionType for the given string, Unknown if no region was found.
 */
- (AWSRegionType)regionTypeForString:(NSString *)regionString
{

    NSArray *names = @[
        @"us-east-1",
        @"us-west-1",
        @"us-west-2",
        @"eu-west-1",
        @"eu-central-1",
        @"ap-northeast-1",
        @"ap-northeast-2",
        @"ap-southeast-1",
        @"ap-southeast-2",
        @"sa-east-1",
        @"cn-north-1",
        @"us-gov-west-1"
    ];

    NSArray *codes = @[
        @(AWSRegionUSEast1),
        @(AWSRegionUSWest1),
        @(AWSRegionUSWest2),
        @(AWSRegionEUWest1),
        @(AWSRegionEUCentral1),
        @(AWSRegionAPNortheast1),
        @(AWSRegionAPNortheast2),
        @(AWSRegionAPSoutheast1),
        @(AWSRegionAPSoutheast2),
        @(AWSRegionSAEast1),
        @(AWSRegionCNNorth1),
        @(AWSRegionUSGovWest1)
    ];
    NSInteger index = [names indexOfObject:regionString];
    if (NSNotFound == index) {
        return AWSRegionUnknown;
    }
    return [codes[index] integerValue];
}

@mohammadshalhoob
Copy link
Copy Markdown

dont forgrt eu-west-2

@mohammadshalhoob
Copy link
Copy Markdown

update Extensions with more Region .

import AWSCore
extension AWSRegionType {
/**
Return an AWSRegionType for the given string

 - Parameter regionString: The Region name (e.g. us-east-1) as a string
 
 - Returns: A new AWSRegionType for the given string, Unknown if no region was found.
 */
static func regionTypeForString(regionString: String) -> AWSRegionType {
    switch regionString {
    case "us-east-1": return .USEast1
    case "us-east-2": return .USEast2
    case "us-west-1": return .USWest1
    case "us-west-2": return .USWest2
    case "eu-west-1": return .EUWest1
    case "eu-west-2": return .EUWest2
    case "eu-central-1": return .EUCentral1
    case "ap-northeast-1": return .APNortheast1
    case "ap-northeast-2": return .APNortheast2
    case "ap-southeast-1": return .APSoutheast1
    case "ap-southeast-2": return .APSoutheast2
    case "sa-east-1": return .SAEast1
    case "cn-north-1": return .CNNorth1
    case "ap-south-1": return .APSouth1
    case "us-gov-west-1": return .USGovWest1
    default: return .Unknown
    }

}
}

@sarah-j-smith
Copy link
Copy Markdown

sarah-j-smith commented Oct 20, 2017

import AWSCore

// AWS SDK has a bunch of useful categories
let regionString = "ap-southeast"
let regionCode = regionString.aws_regionTypeValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment