libqtaws  0.1.0
UnofficialAWSlibraryforQt-InternalDocumentation
awsregion.cpp
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 AwsRegion::AwsRegion(const Region region) : d_ptr(new AwsRegionPrivate(this))
49 {
50  Q_D(AwsRegion);
51  d->region = region;
52 }
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 AwsRegion::AwsRegion(const QString &regionName) : d_ptr(new AwsRegionPrivate(this))
63 {
64  Q_D(AwsRegion);
65  d->region = fromName(regionName);
66 }
67 
68 /**
69  * @brief AwsRegion destructor.
70  */
72 {
73  delete d_ptr;
74 }
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  */
91 {
92  Q_D(const AwsRegion);
93  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 &regionName) constructor, then the constructed object will
104  * be considered invalid.
105  *
106  * @return `true` if this object is valid, `false` otherwise.
107  */
108 bool AwsRegion::isValid() const
109 {
110  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 QString AwsRegion::name() const
121 {
122  Q_D(const AwsRegion);
123  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 QString AwsRegion::name(const Region &region)
134 {
135  switch (region) {
136  case AP_Northeast_1: return QLatin1String("ap-northeast-1");
137  case AP_Southeast_1: return QLatin1String("ap-southeast-1");
138  case AP_Southeast_2: return QLatin1String("ap-southeast-2");
139  case EU_West_1: return QLatin1String("eu-west-1");
140  case SA_East_1: return QLatin1String("sa-east-1");
141  case US_East_1: return QLatin1String("us-east-1");
142  case US_Gov_West_1: return QLatin1String("us-gov-west-1");
143  case US_West_1: return QLatin1String("us-west-1");
144  case US_West_2: return QLatin1String("us-west-2");
145  default:
146  Q_ASSERT_X(false, Q_FUNC_INFO, qPrintable(QString::fromLatin1("invalid region: %1").arg(region)));
147  }
148  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 QString AwsRegion::fullName() const
168 {
169  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 QString AwsRegion::fullName(const Region &region)
191 {
192  switch (region) {
193  case AP_Northeast_1: return QLatin1String("Asia Pacific (Tokyo) Region");
194  case AP_Southeast_1: return QLatin1String("Asia Pacific (Singapore) Region");
195  case AP_Southeast_2: return QLatin1String("Asia Pacific (Sydney) Region");
196  case EU_West_1: return QLatin1String("EU (Ireland) Region");
197  case SA_East_1: return QLatin1String("São Paulo Region");
198  case US_East_1: return QLatin1String("US East (Northern Virginia) Region");
199  case US_Gov_West_1: return QLatin1String("AWS GovCloud (US) Region");
200  case US_West_1: return QLatin1String("US West (Northern California) Region");
201  case US_West_2: return QLatin1String("US West (Oregon) Region");
202  default:
203  Q_ASSERT_X(false, Q_FUNC_INFO, qPrintable(QString::fromLatin1("invalid region: %1").arg(region)));
204  }
205  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 QString AwsRegion::hostName(const QString &serviceName) const
217 {
218  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 bool AwsRegion::isSupported(const QString &serviceName, const AwsEndpoint::Transports transports) const
232 {
233  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 QStringList AwsRegion::supportedServices(const AwsEndpoint::Transports transports) const
245 {
246  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 AwsRegion::Region AwsRegion::fromName(const QString &regionName)
261 {
262  const QString lowerName(regionName.toLower());
263  if (lowerName == QLatin1String("ap-northeast-1")) return AP_Northeast_1;
264  if (lowerName == QLatin1String("ap-southeast-1")) return AP_Southeast_1;
265  if (lowerName == QLatin1String("ap-southeast-2")) return AP_Southeast_2;
266  if (lowerName == QLatin1String("eu-west-1")) return EU_West_1;
267  if (lowerName == QLatin1String("sa-east-1")) return SA_East_1;
268  if (lowerName == QLatin1String("us-east-1")) return US_East_1;
269  if (lowerName == QLatin1String("us-gov-west-1")) return US_Gov_West_1;
270  if (lowerName == QLatin1String("us-west-1")) return US_West_1;
271  if (lowerName == QLatin1String("us-west-2")) return US_West_2;
272  Q_ASSERT_X(false, Q_FUNC_INFO, qPrintable(QString::fromLatin1("invalid region name: %1").arg(regionName)));
273  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  */
292  : q_ptr(q)
293 {
294 
295 }
296 
297 QTAWS_END_NAMESPACE
Private implementation for AwsRegion.
Definition: awsregion_p.h:29
QString hostName(const QString &serviceName) const
Get name of the host the supports a given service for this region.
Definition: awsregion.cpp:216
QStringList supportedServices(const AwsEndpoint::Transports transports=AwsEndpoint::AnyTransport) const
Get a list of services supported by this region.
Definition: awsregion.cpp:244
bool isSupported(const QString &serviceName, const AwsEndpoint::Transports transports=AwsEndpoint::AnyTransport) const
Is a given service supported for this region?
Definition: awsregion.cpp:231
static QStringList supportedServices(const QString &regionName, const Transports transport=AnyTransport)
Get a list of services that support a given region.
QString name() const
Get the canonical AWS name for this region.
Definition: awsregion.cpp:120
QString fullName() const
Get the full name for this region.
Definition: awsregion.cpp:167
Region
AWS regions.
Definition: awsregion.h:67
Region region() const
Get the region this object represents.
Definition: awsregion.cpp:90
Provides AWS region information.
Definition: awsregion.h:33
static bool isSupported(const QString &regionName, const QString &serviceName, const Transports transport=AnyTransport)
Is a region / service / transport combination supported by Amazon?
~AwsRegion()
AwsRegion destructor.
Definition: awsregion.cpp:71
static QUrl getEndpoint(const QString &regionName, const QString &serviceName, const Transports transport=AnyTransport)
Get a QUrl for an AWS endpoint.
bool isValid() const
Is this AWS region object valid?
Definition: awsregion.cpp:108
AwsRegion(const Region region)
Constructs a new AwsRegion object.
Definition: awsregion.cpp:48
AwsRegionPrivate(AwsRegion *const q)
Constructs a new AwsRegionPrivate object.
Definition: awsregion.cpp:291
AwsRegionPrivate *const d_ptr
Internal d-pointer.
Definition: awsregion.h:105
static Region fromName(const QString &regionName)
Get an AwsRegion::Region enumeration value from a region name.
Definition: awsregion.cpp:260