Util.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/string/string.h>
  10. #include "AzCore/StringFunc/StringFunc.h"
  11. #include <aws/core/utils/memory/stl/AWSString.h>
  12. namespace AWSCore
  13. {
  14. namespace Util
  15. {
  16. //! An enum representing the available data provided in an AWS ARN (Amazon Resource Name)
  17. //! ARN formats:
  18. //! arn:partition:service:region:account-id:resource-id
  19. //! arn:partition:service:region:account-id:resource-type/resource-id
  20. //! arn:partition:service:region:account-id:resource-type:resource-id
  21. //! Be aware that the ARNs for some resources omit the Region, the account ID, or both the Region and the account ID.
  22. //! Example of GameLift Fleet ARN:
  23. //! "arn:aws:gamelift:us-west-2:<account id>:fleet/fleet-<fleet id>"
  24. enum ArnFormatDataIndex
  25. {
  26. Partition = 1,
  27. Service,
  28. Region,
  29. AccountId
  30. };
  31. //! Extracts information from an AWS ARN (Amazon Resource Name)
  32. //! @param awsArn An AWS ARN. ARNs are formatted as arn:partition:service:region:account-id:<resource-type/id>
  33. //! @param arnDataIndex The type of ARN data to extract. Possible values are Partition, Service, Region, or AccountId
  34. //! @return A string representing extracted ARN data. Empty string is returned if data type isn't found.
  35. //! Be aware that the ARNs for some resources omit the Region, the account ID, or both the Region and the account ID.
  36. inline AZStd::string ExtractArnData(AZStd::string_view awsArn, ArnFormatDataIndex arnDataIndex)
  37. {
  38. // ARNs that omit region or account id will leave an empty entry.
  39. // For example, IAM ARNs will omit the region (notice the 2 colons '::' specifying no region)
  40. // arn:aws:iam::123456789012:<user_data>
  41. constexpr bool keepEmptyStrings = true;
  42. AZStd::vector<AZStd::string> tokenizedGameLiftArn;
  43. AZ::StringFunc::Tokenize(awsArn, tokenizedGameLiftArn, ":", keepEmptyStrings);
  44. if (tokenizedGameLiftArn.size() >= arnDataIndex)
  45. {
  46. return tokenizedGameLiftArn[arnDataIndex];
  47. }
  48. return "";
  49. }
  50. //! Extracts the AWS region from a given ARN (Amazon Resource Name)
  51. //! @param awsArn An AWS ARN. ARNs are formatted as arn:partition:service:region:account-id:<resource-type/id>
  52. //! @return A string representing the AWS region or an empty string if no region is found.
  53. inline AZStd::string ExtractRegion(AZStd::string_view awsArn)
  54. {
  55. return ExtractArnData(awsArn, ArnFormatDataIndex::Region);
  56. }
  57. inline Aws::String ToAwsString(const AZStd::string& s)
  58. {
  59. return Aws::String(s.c_str(), s.length());
  60. }
  61. inline AZStd::string ToAZString(const Aws::String& s)
  62. {
  63. return AZStd::string(s.c_str(), s.length());
  64. }
  65. }
  66. } // namespace AWSCore