AWSResourceMappingManager.h 5.7 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/JSON/document.h>
  10. #include <AzCore/std/containers/unordered_map.h>
  11. #include <ResourceMapping/AWSResourceMappingBus.h>
  12. namespace AWSCore
  13. {
  14. //! AWSResourceMappingManager is the manager holding AWS resource mapping data.
  15. //! The manager provides mapping key based AWS resource attributes lookups,
  16. //! and API to reload AWS resource mapping data synchronously.
  17. //! The manager doesn't support to modify or update AWS resource mapping data.
  18. class AWSResourceMappingManager
  19. : AWSResourceMappingRequestBus::Handler
  20. {
  21. //! AWSResourceMappingAttributes is a private data structure for holding
  22. //! AWS resource mapping attributes.
  23. //! Which includes AccountId, NameId, Region and Type
  24. struct AWSResourceMappingAttributes
  25. {
  26. AWSResourceMappingAttributes()
  27. : resourceAccountId("")
  28. , resourceNameId("")
  29. , resourceRegion("")
  30. , resourceType("")
  31. {}
  32. AZStd::string resourceAccountId;
  33. AZStd::string resourceNameId;
  34. AZStd::string resourceRegion;
  35. AZStd::string resourceType;
  36. };
  37. public:
  38. static constexpr const char AWSResourceMappingManagerName[] = "AWSResourceMappingManager";
  39. static constexpr const char ManagerUnexpectedStatusErrorMessage[] =
  40. "AWSResourceMappingManager is in unexpected status.";
  41. static constexpr const char ResourceMappingFileInvalidPathErrorMessage[] =
  42. "Failed to get resource mapping config file path.";
  43. static constexpr const char ResourceMappingKeyNotFoundErrorMessage[] =
  44. "Failed to find resource mapping key: %s";
  45. static constexpr const char ResourceMappingFileNotLoadedErrorMessage[] =
  46. "Resource mapping config file is not loaded, please confirm %s is setup correctly.";
  47. static constexpr const char ResourceMappingFileLoadFailureErrorMessage[] =
  48. "Resource mapping config file failed to load, please confirm file is present and in correct format.";
  49. static constexpr const char ResourceMappingRESTApiIdAndStageInconsistentErrorMessage[] =
  50. "Resource mapping %s and %s have inconsistent region value, return empty service url.";
  51. static constexpr const char ResourceMappingRESTApiInvalidServiceUrlErrorMessage[] =
  52. "Unable to format REST Api url with RESTApiId=%s, RESTApiRegion=%s, RESTApiStage=%s, return empty service url.";
  53. static constexpr const char ResourceMappingFileInvalidJsonFormatErrorMessage[] =
  54. "Failed to read resource mapping config file: %s";
  55. static constexpr const char ResourceMappingFileInvalidSchemaErrorMessage[] =
  56. "Failed to load resource mapping config file json schema.";
  57. static constexpr const char ResourceMappingFileInvalidContentErrorMessage[] =
  58. "Failed to parse resource mapping config file: %s";
  59. enum class Status : AZ::u8
  60. {
  61. NotLoaded = 0,
  62. Ready = 1,
  63. Error = 2
  64. };
  65. AWSResourceMappingManager();
  66. ~AWSResourceMappingManager() override = default;
  67. void ActivateManager();
  68. void DeactivateManager();
  69. // AWSResourceMappingRequestBus interface implementation
  70. AZStd::string GetDefaultAccountId() const override;
  71. AZStd::string GetDefaultRegion() const override;
  72. bool HasResource(const AZStd::string& resourceKeyName) const override;
  73. AZStd::string GetResourceAccountId(const AZStd::string& resourceKeyName) const override;
  74. AZStd::string GetResourceNameId(const AZStd::string& resourceKeyName) const override;
  75. AZStd::string GetResourceRegion(const AZStd::string& resourceKeyName) const override;
  76. AZStd::string GetResourceType(const AZStd::string& resourceKeyName) const override;
  77. AZStd::string GetServiceUrlByServiceName(const AZStd::string& serviceName) const override;
  78. AZStd::string GetServiceUrlByRESTApiIdAndStage(
  79. const AZStd::string& restApiIdKeyName, const AZStd::string& restApiStageKeyName) const override;
  80. void ReloadConfigFile(bool reloadConfigFileName = false) override;
  81. Status GetStatus() const;
  82. private:
  83. // Get resource attribute error message based on the status
  84. AZStd::string GetResourceAttributeErrorMessageByStatus(const AZStd::string& resourceKeyName) const;
  85. // Get resource attribute from resource mappings
  86. AZStd::string GetResourceAttribute(
  87. AZStd::function<AZStd::string(const AWSResourceMappingAttributes&)> getAttributeFunction,
  88. const AZStd::string& resourceKeyName) const;
  89. // Parse JSON document into manager internal data
  90. void ParseJsonDocument(const rapidjson::Document& jsonDocument);
  91. // Parse JSON object into manager internal data structure
  92. typedef rapidjson::GenericObject<true, rapidjson::Value> JsonObject;
  93. AWSResourceMappingAttributes ParseJsonObjectIntoResourceMappingAttributes(
  94. const JsonObject& jsonObject);
  95. // Reset manager internal data
  96. void ResetResourceMappingsData();
  97. // Validate JSON document against schema
  98. bool ValidateJsonDocumentAgainstSchema(const rapidjson::Document& jsonDocument);
  99. Status m_status;
  100. // Resource mapping related data
  101. AZStd::string m_defaultAccountId;
  102. AZStd::string m_defaultRegion;
  103. AZStd::unordered_map<AZStd::string, AWSResourceMappingAttributes> m_resourceMappings;
  104. };
  105. } // namespace AWSCore