Line data Source code
1 : /*
2 : Copyright 2013-2015 Paul Colby
3 :
4 : This file is part of libqtaws.
5 :
6 : Libqtaws is free software: you can redistribute it and/or modify
7 : it under the terms of the GNU Lesser General Public License as published by
8 : the Free Software Foundation, either version 3 of the License, or
9 : (at your option) any later version.
10 :
11 : Libqtaws is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public License
17 : along with libqtaws. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "awsregion.h"
21 : #include "awsregion_p.h"
22 :
23 : #include <QDebug>
24 :
25 : QTAWS_BEGIN_NAMESPACE
26 :
27 : /**
28 : * @class AwsRegion
29 : *
30 : * @brief Provides AWS region information.
31 : *
32 : * Basic usage example:
33 : * @code
34 : * const AwsRegion region(AwsRegion::AP_Northeast_1);
35 : * region.name(); // "ap-northeast-1"
36 : * region.fullName(); // "Asia Pacific (Tokyo) Region"
37 : * region.hostName("iam"); // "iam.amazonaws.com"
38 : * @endcode
39 : *
40 : * @see http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
41 : */
42 :
43 : /**
44 : * @brief Constructs a new AwsRegion object.
45 : *
46 : * @param region AWS region for this object to represent.
47 : */
48 1882 : AwsRegion::AwsRegion(const Region region) : d_ptr(new AwsRegionPrivate(this))
49 : {
50 1882 : Q_D(AwsRegion);
51 1882 : d->region = region;
52 1882 : }
53 :
54 : /**
55 : * @brief Constructs a new AwsRegion object.
56 : *
57 : * If \p regionName is not recognised as a valid AWS region, AwsRegion::isValid
58 : * will return `false` for the created object.
59 : *
60 : * @param regionName Name of the AWS region for this object to represent.
61 : */
62 60 : AwsRegion::AwsRegion(const QString ®ionName) : d_ptr(new AwsRegionPrivate(this))
63 : {
64 60 : Q_D(AwsRegion);
65 60 : d->region = fromName(regionName);
66 60 : }
67 :
68 : /**
69 : * @brief AwsRegion destructor.
70 : */
71 1942 : AwsRegion::~AwsRegion()
72 : {
73 1942 : delete d_ptr;
74 1942 : }
75 :
76 : /**
77 : * @brief Get the region this object represents.
78 : *
79 : * If this object was given an AwsRegion::Region value during construction, this
80 : * function will simply return that enumeration value.
81 : *
82 : * If this object was constructed from an AWS region name, then this function will
83 : * return the AwsRegion::Region enumeration value corresponding to that AWS region
84 : * name, or AwsRegion::InvalidRegion if the given region name did not name a known,
85 : * valid AWS region.
86 : *
87 : * @return The region this object represents, or AwsRegion::InvalidRegion if this
88 : * object does not represent a valid region.
89 : */
90 90 : AwsRegion::Region AwsRegion::region() const
91 : {
92 90 : Q_D(const AwsRegion);
93 90 : return static_cast<AwsRegion::Region>(d->region);
94 : }
95 :
96 : /**
97 : * @brief Is this AWS region object valid?
98 : *
99 : * Any AwsRegion object created using one of the valid AwsRegion::Region enumeration
100 : * values (ie any but AwsRegion::InvalidRegion) will be considered valid.
101 : *
102 : * However, if an unknown region name is supplied to the overloaded
103 : * AwsRegion(const QString ®ionName) constructor, then the constructed object will
104 : * be considered invalid.
105 : *
106 : * @return `true` if this object is valid, `false` otherwise.
107 : */
108 40 : bool AwsRegion::isValid() const
109 : {
110 40 : return (region() != AwsRegion::InvalidRegion);
111 : }
112 :
113 : /**
114 : * @brief Get the canonical AWS name for this region.
115 : *
116 : * @return The canonical AWS name for this region, or null string if this object is invalid.
117 : *
118 : * @see isValid
119 : */
120 1852 : QString AwsRegion::name() const
121 : {
122 1852 : Q_D(const AwsRegion);
123 1852 : return name(static_cast<AwsRegion::Region>(d->region));
124 : }
125 :
126 : /**
127 : * @brief Get the canonical AWS name for a region.
128 : *
129 : * @param region AWS region to get the canonical name of.
130 : *
131 : * @return The canonical AWS name for \p region, or null string if \p region is invalid.
132 : */
133 1862 : QString AwsRegion::name(const Region ®ion)
134 : {
135 1862 : switch (region) {
136 212 : case AP_Northeast_1: return QLatin1String("ap-northeast-1");
137 212 : case AP_Southeast_1: return QLatin1String("ap-southeast-1");
138 204 : case AP_Southeast_2: return QLatin1String("ap-southeast-2");
139 220 : case EU_West_1: return QLatin1String("eu-west-1");
140 188 : case SA_East_1: return QLatin1String("sa-east-1");
141 268 : case US_East_1: return QLatin1String("us-east-1");
142 116 : case US_Gov_West_1: return QLatin1String("us-gov-west-1");
143 212 : case US_West_1: return QLatin1String("us-west-1");
144 228 : case US_West_2: return QLatin1String("us-west-2");
145 : default:
146 2 : Q_ASSERT_X(false, Q_FUNC_INFO, qPrintable(QString::fromLatin1("invalid region: %1").arg(region)));
147 : }
148 2 : return QString();
149 : }
150 :
151 : /**
152 : * @brief Get the full name for this region.
153 : *
154 : * This returns a humand-readable name for this object's region. For example,
155 : * for the AwsRegion::US_East_1 region, this will return something like
156 : * "US East (Northern Virginia) Region".
157 : *
158 : * These full names were gleaned from
159 : * http://aws.amazon.com/about-aws/globalinfrastructure/ and
160 : * http://docs.aws.amazon.com/general/latest/gr/rande.html.
161 : *
162 : * @return The full name for this region.
163 : *
164 : * @see http://aws.amazon.com/about-aws/globalinfrastructure/
165 : * @see http://docs.aws.amazon.com/general/latest/gr/rande.html
166 : */
167 10 : QString AwsRegion::fullName() const
168 : {
169 10 : return fullName(region());
170 : }
171 :
172 : /**
173 : * @brief Get the full name for an AWS region.
174 : *
175 : * This returns a humand-readable name for the specified AWS region. For example,
176 : * for the AwsRegion::US_East_1 region, this will return something like
177 : * "US East (Northern Virginia) Region".
178 : *
179 : * These full names were gleaned from
180 : * http://aws.amazon.com/about-aws/globalinfrastructure/ and
181 : * http://docs.aws.amazon.com/general/latest/gr/rande.html.
182 : *
183 : * @param region AWS region to get the full name of.
184 : *
185 : * @return The full name for the specified AWS region.
186 : *
187 : * @see http://aws.amazon.com/about-aws/globalinfrastructure/
188 : * @see http://docs.aws.amazon.com/general/latest/gr/rande.html
189 : */
190 20 : QString AwsRegion::fullName(const Region ®ion)
191 : {
192 20 : switch (region) {
193 2 : case AP_Northeast_1: return QLatin1String("Asia Pacific (Tokyo) Region");
194 2 : case AP_Southeast_1: return QLatin1String("Asia Pacific (Singapore) Region");
195 2 : case AP_Southeast_2: return QLatin1String("Asia Pacific (Sydney) Region");
196 2 : case EU_West_1: return QLatin1String("EU (Ireland) Region");
197 2 : case SA_East_1: return QLatin1String("São Paulo Region");
198 2 : case US_East_1: return QLatin1String("US East (Northern Virginia) Region");
199 2 : case US_Gov_West_1: return QLatin1String("AWS GovCloud (US) Region");
200 2 : case US_West_1: return QLatin1String("US West (Northern California) Region");
201 2 : case US_West_2: return QLatin1String("US West (Oregon) Region");
202 : default:
203 2 : Q_ASSERT_X(false, Q_FUNC_INFO, qPrintable(QString::fromLatin1("invalid region: %1").arg(region)));
204 : }
205 2 : return QString();
206 : }
207 :
208 : /**
209 : * @brief Get name of the host the supports a given service for this region.
210 : *
211 : * @param serviceName The service to get the hostname for.
212 : *
213 : * @return The name of the host that supports the given service for this region,
214 : * or a null string if the this service is not supported for this region.
215 : */
216 246 : QString AwsRegion::hostName(const QString &serviceName) const
217 : {
218 246 : return AwsEndpoint::getEndpoint(name(), serviceName).host();
219 : }
220 :
221 : /**
222 : * @brief Is a given service supported for this region?
223 : *
224 : * @param serviceName The service to check support for.
225 : * @param transports An optional set of transports to test for support.
226 : *
227 : * @return The name of the host that supports the given service for this region for
228 : * _at least one_ of the specified transports, or a null string if the this
229 : * service is not supported for this region with any such transports.
230 : */
231 1533 : bool AwsRegion::isSupported(const QString &serviceName, const AwsEndpoint::Transports transports) const
232 : {
233 1533 : return AwsEndpoint::isSupported(name(), serviceName, transports);
234 : }
235 :
236 : /**
237 : * @brief Get a list of services supported by this region.
238 : *
239 : * @param transports Optional set of transports to test for support.
240 : *
241 : * @return A list of the names of all AWS services support by this region for
242 : * _at least one_ of the specified transports. The list may be empty.
243 : */
244 63 : QStringList AwsRegion::supportedServices(const AwsEndpoint::Transports transports) const
245 : {
246 63 : return AwsEndpoint::supportedServices(name(), transports);
247 : }
248 :
249 : /**
250 : * @brief Get an AwsRegion::Region enumeration value from a region name.
251 : *
252 : * @note The region name is evaluated in a case-insensitve manner.
253 : *
254 : * @param regionName AWS region name to get an enumeration value for.
255 : *
256 : * @return An AwsRegion::Region enumeration value corresponding to the specified
257 : * AWS region name, or AwsRegion::InvalidRegion if the region name is not
258 : * valid or not recognised.
259 : */
260 1932 : AwsRegion::Region AwsRegion::fromName(const QString ®ionName)
261 : {
262 1932 : const QString lowerName(regionName.toLower());
263 1932 : if (lowerName == QLatin1String("ap-northeast-1")) return AP_Northeast_1;
264 1713 : if (lowerName == QLatin1String("ap-southeast-1")) return AP_Southeast_1;
265 1494 : if (lowerName == QLatin1String("ap-southeast-2")) return AP_Southeast_2;
266 1283 : if (lowerName == QLatin1String("eu-west-1")) return EU_West_1;
267 1056 : if (lowerName == QLatin1String("sa-east-1")) return SA_East_1;
268 861 : if (lowerName == QLatin1String("us-east-1")) return US_East_1;
269 586 : if (lowerName == QLatin1String("us-gov-west-1")) return US_Gov_West_1;
270 463 : if (lowerName == QLatin1String("us-west-1")) return US_West_1;
271 244 : if (lowerName == QLatin1String("us-west-2")) return US_West_2;
272 9 : Q_ASSERT_X(false, Q_FUNC_INFO, qPrintable(QString::fromLatin1("invalid region name: %1").arg(regionName)));
273 9 : return InvalidRegion;
274 : }
275 :
276 : /**
277 : * @internal
278 : *
279 : * @class AwsRegionPrivate
280 : *
281 : * @brief Private implementation for AwsRegion.
282 : */
283 :
284 : /**
285 : * @internal
286 : *
287 : * @brief Constructs a new AwsRegionPrivate object.
288 : *
289 : * @param q Pointer to this object's public AwsRegion instance.
290 : */
291 1942 : AwsRegionPrivate::AwsRegionPrivate(AwsRegion * const q)
292 1942 : : q_ptr(q)
293 : {
294 :
295 1942 : }
296 :
297 : QTAWS_END_NAMESPACE
|