3
0

AWSApiClientJobConfig.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/smart_ptr/make_shared.h>
  10. #include <Framework/AWSApiJobConfig.h>
  11. // Forward declarations
  12. namespace Aws
  13. {
  14. namespace Client
  15. {
  16. struct ClientConfiguration;
  17. }
  18. namespace Http
  19. {
  20. class HttpClient;
  21. }
  22. namespace Auth
  23. {
  24. class AWSCredentials;
  25. }
  26. }
  27. namespace AWSCore
  28. {
  29. /// Provides configuration for AWS jobs using a specific client type.
  30. template<class ClientType>
  31. class IAwsApiClientJobConfig
  32. : public virtual IAwsApiJobConfig
  33. {
  34. public:
  35. //! Returns the created AWS client for the Job
  36. virtual std::shared_ptr<ClientType> GetClient() = 0;
  37. };
  38. // warning C4250: 'AWSCore::AwsApiClientJobConfig<ClientType>': inherits 'AWSCore::AwsApiJobConfig::AWSCore::AwsApiJobConfig::GetJobContext' via dominance
  39. // Thanks to http://stackoverflow.com/questions/11965596/diamond-inheritance-scenario-compiles-fine-in-g-but-produces-warnings-errors for the explanation
  40. // This is the expected and desired behavior. The warning is superfluous.
  41. AZ_PUSH_DISABLE_WARNING(4250, "-Wunknown-warning-option")
  42. /// Configuration for AWS jobs using a specific client type.
  43. template<class ClientType>
  44. class AwsApiClientJobConfig
  45. : public AwsApiJobConfig
  46. , public virtual IAwsApiClientJobConfig<ClientType>
  47. {
  48. public:
  49. AZ_CLASS_ALLOCATOR(AwsApiClientJobConfig, AZ::SystemAllocator);
  50. using AwsApiClientJobConfigType = AwsApiClientJobConfig<ClientType>;
  51. using InitializerFunction = AZStd::function<void(AwsApiClientJobConfigType& config)>;
  52. /// Initialize an AwsApiClientJobConfig object.
  53. ///
  54. /// \param defaultConfig - the config object that provides values when
  55. /// no override has been set in this object. The default is nullptr, which
  56. /// will cause a default value to be used.
  57. ///
  58. /// \param initializer - a function called to initialize this object.
  59. /// This simplifies the initialization of static instances. The default
  60. /// value is nullptr, in which case no initializer will be called.
  61. AwsApiClientJobConfig(AwsApiJobConfig* defaultConfig = nullptr, InitializerFunction initializer = nullptr)
  62. : AwsApiJobConfig{ defaultConfig }
  63. {
  64. if (initializer)
  65. {
  66. initializer(*this);
  67. }
  68. }
  69. ~AwsApiClientJobConfig() override = default;
  70. /// Gets a client initialized used currently applied settings. If
  71. /// any settings change after first use, code must call
  72. /// ApplySettings before those changes will take effect.
  73. std::shared_ptr<ClientType> GetClient() override
  74. {
  75. EnsureSettingsApplied();
  76. return m_client;
  77. }
  78. protected:
  79. void ApplySettings() override
  80. {
  81. AwsApiJobConfig::ApplySettings();
  82. m_client = CreateClient();
  83. }
  84. /// Create a client configured using this object's settings. ClientType
  85. /// can be any of the AWS API service clients (e.g. LambdaClient, etc.).
  86. std::shared_ptr<ClientType> CreateClient() const
  87. {
  88. auto provider = GetCredentialsProvider();
  89. if (provider != nullptr)
  90. {
  91. return std::make_shared<ClientType>(provider, GetClientConfiguration());
  92. }
  93. else
  94. {
  95. // If no explicit credentials are provided then AWS C++ SDK will perform standard search
  96. return std::make_shared<ClientType>(Aws::Auth::AWSCredentials(), GetClientConfiguration());
  97. }
  98. }
  99. private:
  100. /// Set by ApplySettings
  101. std::shared_ptr<ClientType> m_client;
  102. };
  103. AZ_POP_DISABLE_WARNING
  104. } // namespace AWSCore