1 /*
2  * Copyright 2013-2020 Amazon Technologies, 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://aws.amazon.com/apache2.0
9  *
10  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
11  * OR CONDITIONS OF ANY KIND, either express or implied. See the
12  * License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package com.amazonaws.regions;
16
17 import com.amazonaws.AmazonClientException;
18 import org.apache.commons.logging.LogFactory;
19
20 import com.amazonaws.util.EC2MetadataUtils;
21
22 /**
23  * Enumeration of region names
24  */

25 public enum Regions {
26
27     GovCloud("us-gov-west-1""AWS GovCloud (US)"),
28     US_GOV_EAST_1("us-gov-east-1""AWS GovCloud (US-East)"),
29     US_EAST_1("us-east-1""US East (N. Virginia)"),
30     US_EAST_2("us-east-2""US East (Ohio)"),
31     US_WEST_1("us-west-1""US West (N. California)"),
32     US_WEST_2("us-west-2""US West (Oregon)"),
33     EU_WEST_1("eu-west-1""EU (Ireland)"),
34     EU_WEST_2("eu-west-2""EU (London)"),
35     EU_WEST_3("eu-west-3""EU (Paris)"),
36     EU_CENTRAL_1("eu-central-1""EU (Frankfurt)"),
37     EU_NORTH_1("eu-north-1""EU (Stockholm)"),
38     EU_SOUTH_1("eu-south-1""EU (Milan)"),
39     AP_EAST_1("ap-east-1""Asia Pacific (Hong Kong)"),
40     AP_SOUTH_1("ap-south-1""Asia Pacific (Mumbai)"),
41     AP_SOUTHEAST_1("ap-southeast-1""Asia Pacific (Singapore)"),
42     AP_SOUTHEAST_2("ap-southeast-2""Asia Pacific (Sydney)"),
43     AP_NORTHEAST_1("ap-northeast-1""Asia Pacific (Tokyo)"),
44     AP_NORTHEAST_2("ap-northeast-2""Asia Pacific (Seoul)"),
45     SA_EAST_1("sa-east-1""South America (Sao Paulo)"),
46     CN_NORTH_1("cn-north-1""China (Beijing)"),
47     CN_NORTHWEST_1("cn-northwest-1""China (Ningxia)"),
48     CA_CENTRAL_1("ca-central-1""Canada (Central)"),
49     ME_SOUTH_1("me-south-1""Middle East (Bahrain)"),
50     AF_SOUTH_1("af-south-1""Africa (Cape Town)")
51     ;
52
53     /**
54      * The default region that new customers in the US are encouraged to use
55      * when using AWS services for the first time.
56      */

57     public static final Regions DEFAULT_REGION = US_WEST_2;
58
59     private final String name;
60     private final String description;
61
62     private Regions(String name, String description) {
63         this.name = name;
64         this.description = description;
65     }
66
67     /**
68      * The name of this region, used in the regions.xml file to identify it.
69      */

70     public String getName() {
71         return name;
72     }
73
74     /**
75      * Descriptive readable name for this region.
76      */

77     public String getDescription() {
78         return description;
79     }
80
81     /**
82      * Returns a region enum corresponding to the given region name.
83      *
84      * @param regionName
85      *            The name of the region. Ex.: eu-west-1
86      * @return Region enum representing the given region name.
87      */

88     public static Regions fromName(String regionName) {
89         for (Regions region : Regions.values()) {
90             if (region.getName().equals(regionName)) {
91                 return region;
92             }
93         }
94         throw new IllegalArgumentException("Cannot create enum from " + regionName + " value!");
95     }
96
97     /**
98      * Returns a Region object representing the region the application is
99      * running in, when running in EC2. If this method is called from a non-EC2
100      * environment, it will return null.
101      */

102     public static Region getCurrentRegion() {
103         try {
104             final String region = EC2MetadataUtils.getEC2InstanceRegion();
105             if (region != null)
106                 return RegionUtils.getRegion(region);
107         } catch (AmazonClientException e) {
108             LogFactory.getLog(Regions.class).debug(
109                 "Ignoring failure to retrieve the region: " + e.getMessage());
110         }
111         return null;
112     }
113 }
114