3
0

AWSApiJobConfig.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/std/optional.h>
  10. #include <AzCore/std/functional.h>
  11. #include <AzCore/std/smart_ptr/unique_ptr.h>
  12. #include <AzCore/Memory/SystemAllocator.h>
  13. #include <AWSCore_Traits_Platform.h>
  14. // The AWS Native SDK AWSAllocator triggers a warning due to accessing members of std::allocator directly.
  15. // AWSAllocator.h(70): warning C4996: 'std::allocator<T>::pointer': warning STL4010: Various members of std::allocator are deprecated in C++17.
  16. // Use std::allocator_traits instead of accessing these members directly.
  17. // You can define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received this warning.
  18. AZ_PUSH_DISABLE_WARNING(4251 4996, "-Wunknown-warning-option")
  19. #include <aws/core/client/ClientConfiguration.h>
  20. #include <aws/core/utils/memory/stl/AWSString.h>
  21. #include <aws/core/http/Scheme.h>
  22. #include <aws/core/Region.h>
  23. #include <aws/core/http/HttpTypes.h>
  24. AZ_POP_DISABLE_WARNING
  25. #include <AWSCoreBus.h>
  26. // Forward declarations
  27. namespace AZ
  28. {
  29. class JobContext;
  30. }
  31. namespace Aws
  32. {
  33. namespace Auth
  34. {
  35. class AWSCredentialsProvider;
  36. }
  37. namespace Utils
  38. {
  39. namespace RateLimits
  40. {
  41. class RateLimiterInterface;
  42. }
  43. namespace Threading
  44. {
  45. class Executor;
  46. }
  47. }
  48. namespace Client
  49. {
  50. class RetryStrategy;
  51. struct ClientConfiguration;
  52. }
  53. namespace Http
  54. {
  55. class HttpClient;
  56. }
  57. }
  58. namespace AWSCore
  59. {
  60. /// Provides configuration for an AwsApiJob.
  61. class IAwsApiJobConfig
  62. {
  63. public:
  64. virtual ~IAwsApiJobConfig() = default;
  65. virtual AZ::JobContext* GetJobContext() = 0;
  66. };
  67. /// Encapsulates all the properties that can be used to configured the
  68. /// operation of AWS jobs.
  69. class AwsApiJobConfig
  70. : public virtual IAwsApiJobConfig
  71. {
  72. public:
  73. AZ_CLASS_ALLOCATOR(AwsApiJobConfig, AZ::SystemAllocator);
  74. using InitializerFunction = AZStd::function<void(AwsApiJobConfig& config)>;
  75. /// Initialize an AwsApiClientJobConfig object.
  76. ///
  77. /// \param defaultConfig - the config object that provides values when
  78. /// no override has been set in this object. The default is nullptr, which
  79. /// will cause a default value to be used.
  80. ///
  81. /// \param initializer - a function called to initialize this object.
  82. /// This simplifies the initialization of static instances. The default
  83. /// value is nullptr, in which case no initializer will be called.
  84. AwsApiJobConfig(AwsApiJobConfig* defaultConfig = nullptr, InitializerFunction initializer = nullptr)
  85. : m_defaultConfig{ defaultConfig }
  86. {
  87. if (initializer)
  88. {
  89. initializer(*this);
  90. }
  91. }
  92. /// Type used to encapsulate override values.
  93. template<typename T>
  94. using Override = AZStd::optional<T>;
  95. // TODO: document the individual configuration settings
  96. Override<AZ::JobContext*> jobContext;
  97. Override<std::shared_ptr<Aws::Auth::AWSCredentialsProvider>> credentialsProvider;
  98. Override<Aws::String> userAgent;
  99. Override<Aws::Http::Scheme> scheme;
  100. Override<Aws::String> region;
  101. Override<unsigned> maxConnections;
  102. Override<long> requestTimeoutMs;
  103. Override<long> connectTimeoutMs;
  104. Override<std::shared_ptr<Aws::Client::RetryStrategy>> retryStrategy;
  105. Override<Aws::String> endpointOverride;
  106. Override<Aws::String> proxyHost;
  107. Override<unsigned> proxyPort;
  108. Override<Aws::String> proxyUserName;
  109. Override<Aws::String> proxyPassword;
  110. Override<std::shared_ptr<Aws::Utils::Threading::Executor>> executor;
  111. Override<bool> verifySSL;
  112. Override<std::shared_ptr<Aws::Utils::RateLimits::RateLimiterInterface>> writeRateLimiter;
  113. Override<std::shared_ptr<Aws::Utils::RateLimits::RateLimiterInterface>> readRateLimiter;
  114. Override<Aws::Http::TransferLibType> httpLibOverride;
  115. #if AWSCORE_BACKWARD_INCOMPATIBLE_CHANGE
  116. Override<Aws::Client::FollowRedirectsPolicy> followRedirects;
  117. #else
  118. Override<bool> followRedirects;
  119. #endif
  120. Override<Aws::String> caFile;
  121. /// Applies settings changes made after first use.
  122. virtual void ApplySettings();
  123. //////////////////////////////////////////////////////////////////////////
  124. // IAwsApiJobConfig implementations
  125. AZ::JobContext* GetJobContext() override;
  126. /// Get a ClientConfiguration object initialized using the current settings.
  127. /// The base settings object's overrides are applied first, then this objects
  128. /// overrides are applied. By default all ClientConfiguration members will
  129. /// have default values (as set by the
  130. Aws::Client::ClientConfiguration GetClientConfiguration() const;
  131. protected:
  132. /// Ensures that ApplySettings has been called.
  133. void EnsureSettingsApplied()
  134. {
  135. if (!m_settingsApplied)
  136. {
  137. ApplySettings();
  138. }
  139. }
  140. /// Helper function for applying Override typed members.
  141. template<typename T>
  142. static void CheckAndSet(const Override<T>& src, T& dst)
  143. {
  144. if (src.has_value())
  145. {
  146. dst = src.value();
  147. }
  148. }
  149. /// Call Visit for m_defaultConfig, then call visitor for this object. Is
  150. /// templated so that it can be used to visit derived types as
  151. /// long as they define an m_defaultConfig of that type.
  152. template<class ConfigType>
  153. void Visit(AZStd::function<void(const ConfigType&)> visitor) const
  154. {
  155. if (ConfigType::m_defaultConfig)
  156. {
  157. ConfigType::m_defaultConfig->Visit(visitor);
  158. }
  159. visitor(*this);
  160. }
  161. /// Get the CredentialsProvider from this settings object, if set, or
  162. /// from the base settings object. By default a nullptr is returned.
  163. std::shared_ptr<Aws::Auth::AWSCredentialsProvider> GetCredentialsProvider() const;
  164. private:
  165. /// The settings object with values overridden by this settings object, or
  166. /// nullptr if this settings object is the root.
  167. const AwsApiJobConfig* const m_defaultConfig;
  168. /// True after ApplySettings is called.
  169. bool m_settingsApplied{ false };
  170. /// Set when ApplySettings is called.
  171. AZ::JobContext* m_jobContext{ nullptr };
  172. // Copy and assignment not allowed
  173. AwsApiJobConfig(const AwsApiJobConfig& base) = delete;
  174. AwsApiJobConfig& operator=(AwsApiJobConfig& other) = delete;
  175. };
  176. template<class ConfigType>
  177. class AwsApiJobConfigHolder
  178. : protected AWSCoreNotificationsBus::Handler
  179. {
  180. public:
  181. ~AwsApiJobConfigHolder() override
  182. {
  183. AWSCoreNotificationsBus::Handler::BusDisconnect();
  184. }
  185. ConfigType* GetConfig(AwsApiJobConfig* defaultConfig = nullptr, typename ConfigType::InitializerFunction initializer = nullptr)
  186. {
  187. if (!m_config)
  188. {
  189. AWSCoreNotificationsBus::Handler::BusConnect();
  190. m_config.reset(aznew ConfigType(defaultConfig, initializer));
  191. }
  192. return m_config.get();
  193. }
  194. void OnSDKInitialized() override {}
  195. //! AWSCore is deactivating which allows the configuration
  196. //! objects to delete any cached clients or other data allocated using
  197. //! the AWS API's allocator.
  198. void OnSDKShutdownStarted() override
  199. {
  200. m_config.reset();
  201. }
  202. private:
  203. AZStd::unique_ptr<ConfigType> m_config;
  204. };
  205. } // namespace AWSCore