3
0

AWSScriptBehaviorDynamoDB.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include <AzCore/RTTI/BehaviorContext.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <aws/core/auth/AWSCredentials.h>
  11. #include <aws/dynamodb/DynamoDBClient.h>
  12. #include <aws/dynamodb/model/GetItemRequest.h>
  13. #include <aws/dynamodb/model/GetItemResult.h>
  14. #include <aws/core/utils/Outcome.h>
  15. #include <Framework/AWSApiRequestJob.h>
  16. #include <ResourceMapping/AWSResourceMappingBus.h>
  17. #include <ScriptCanvas/AWSScriptBehaviorDynamoDB.h>
  18. namespace AWSCore
  19. {
  20. void AWSScriptBehaviorDynamoDB::Reflect(AZ::ReflectContext* context)
  21. {
  22. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->Class<AWSScriptBehaviorDynamoDB>()
  25. ->Version(0);
  26. }
  27. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  28. {
  29. behaviorContext->Class<AWSScriptBehaviorDynamoDB>("AWSScriptBehaviorDynamoDB")
  30. ->Attribute(AZ::Script::Attributes::Category, "AWSCore")
  31. ->Method("GetItem", &AWSScriptBehaviorDynamoDB::GetItem,
  32. {{{"Table Resource KeyName", "The name of the table containing the requested item."},
  33. {"Key Map", "A map of attribute names to AttributeValue objects, representing the primary key of the item to retrieve."}}})
  34. ->Method("GetItemRaw", &AWSScriptBehaviorDynamoDB::GetItemRaw,
  35. {{{"Table Name", "The name of the table containing the requested item."},
  36. {"Key Map", "A map of attribute names to AttributeValue objects, representing the primary key of the item to retrieve."},
  37. {"Region Name", "The region of the table located in."}}});
  38. behaviorContext->EBus<AWSScriptBehaviorDynamoDBNotificationBus>("AWSDynamoDBBehaviorNotificationBus")
  39. ->Attribute(AZ::Script::Attributes::Category, "AWSCore")
  40. ->Handler<AWSScriptBehaviorDynamoDBNotificationBusHandler>();
  41. }
  42. }
  43. void AWSScriptBehaviorDynamoDB::GetItem(const AZStd::string& tableResourceKey, const DynamoDBAttributeValueMap& keyMap)
  44. {
  45. AZStd::string requestTableName = "";
  46. AWSResourceMappingRequestBus::BroadcastResult(requestTableName, &AWSResourceMappingRequests::GetResourceNameId, tableResourceKey);
  47. AZStd::string requestRegion = "";
  48. AWSResourceMappingRequestBus::BroadcastResult(requestRegion, &AWSResourceMappingRequests::GetResourceRegion, tableResourceKey);
  49. GetItemRaw(requestTableName, keyMap, requestRegion);
  50. }
  51. void AWSScriptBehaviorDynamoDB::GetItemRaw(const AZStd::string& table, const DynamoDBAttributeValueMap& keyMap, const AZStd::string& region)
  52. {
  53. if (!ValidateGetItemRequest(table, keyMap, region))
  54. {
  55. return;
  56. }
  57. using DynamoDBGetItemRequestJob = AWS_API_REQUEST_JOB(DynamoDB, GetItem);
  58. DynamoDBGetItemRequestJob::Config config(DynamoDBGetItemRequestJob::GetDefaultConfig());
  59. config.region = region.c_str();
  60. auto job = DynamoDBGetItemRequestJob::Create(
  61. [](DynamoDBGetItemRequestJob* job) // OnSuccess handler
  62. {
  63. auto item = job->result.GetItem();
  64. if (!item.empty())
  65. {
  66. DynamoDBAttributeValueMap result;
  67. for (const auto& itermPair : item)
  68. {
  69. Aws::String attributeValue = itermPair.second.SerializeAttribute();
  70. result.emplace(AZStd::string(itermPair.first.c_str()), AZStd::string(attributeValue.c_str()));
  71. }
  72. AWSScriptBehaviorDynamoDBNotificationBus::Broadcast(
  73. &AWSScriptBehaviorDynamoDBNotificationBus::Events::OnGetItemSuccess, result);
  74. }
  75. else
  76. {
  77. AWSScriptBehaviorDynamoDBNotificationBus::Broadcast(
  78. &AWSScriptBehaviorDynamoDBNotificationBus::Events::OnGetItemError, "No item was found with the key.");
  79. }
  80. },
  81. [](DynamoDBGetItemRequestJob* job) // OnError handler
  82. {
  83. Aws::String errorMessage = job->error.GetMessage();
  84. AWSScriptBehaviorDynamoDBNotificationBus::Broadcast(
  85. &AWSScriptBehaviorDynamoDBNotificationBus::Events::OnGetItemError, errorMessage.c_str());
  86. },
  87. &config);
  88. job->request.SetTableName(table.c_str());
  89. for (const auto& kepMapPair : keyMap)
  90. {
  91. Aws::Utils::Json::JsonValue keyJsonValue(kepMapPair.second.c_str());
  92. Aws::DynamoDB::Model::AttributeValue keyAttributeValue(keyJsonValue.View());
  93. job->request.AddKey(kepMapPair.first.c_str(), keyAttributeValue);
  94. }
  95. job->Start();
  96. }
  97. bool AWSScriptBehaviorDynamoDB::ValidateGetItemRequest(
  98. const AZStd::string& table, const DynamoDBAttributeValueMap& keyMap, const AZStd::string& region)
  99. {
  100. if (table.empty())
  101. {
  102. AZ_Warning("AWSScriptBehaviorDynamoDB", false, "Request validation failed, table name is required.");
  103. AWSScriptBehaviorDynamoDBNotificationBus::Broadcast(
  104. &AWSScriptBehaviorDynamoDBNotificationBus::Events::OnGetItemError, "Request validation failed, table name is required.");
  105. return false;
  106. }
  107. if (keyMap.empty())
  108. {
  109. AZ_Warning("AWSScriptBehaviorDynamoDB", false, "Request validation failed, key is required.");
  110. AWSScriptBehaviorDynamoDBNotificationBus::Broadcast(
  111. &AWSScriptBehaviorDynamoDBNotificationBus::Events::OnGetItemError, "Request validation failed, key is required.");
  112. return false;
  113. }
  114. else
  115. {
  116. for (const auto& kepMapPair : keyMap)
  117. {
  118. Aws::Utils::Json::JsonValue keyJsonValue(kepMapPair.second.c_str());
  119. if (!keyJsonValue.WasParseSuccessful())
  120. {
  121. AZ_Warning("AWSScriptBehaviorDynamoDB", false, "Request validation failed, key attribute value is invalid.");
  122. AWSScriptBehaviorDynamoDBNotificationBus::Broadcast(
  123. &AWSScriptBehaviorDynamoDBNotificationBus::Events::OnGetItemError, "Request validation failed, key attribute value is invalid.");
  124. return false;
  125. }
  126. }
  127. }
  128. if (region.empty())
  129. {
  130. AZ_Warning("AWSScriptBehaviorDynamoDB", false, "Request validation failed, region name is required.");
  131. AWSScriptBehaviorDynamoDBNotificationBus::Broadcast(
  132. &AWSScriptBehaviorDynamoDBNotificationBus::Events::OnGetItemError, "Request validation failed, region name is required.");
  133. return false;
  134. }
  135. return true;
  136. }
  137. } // namespace AWSCore