resource_mappings.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import os
  7. import json
  8. import logging
  9. from AWS.common import constants
  10. logger = logging.getLogger(__name__)
  11. AWS_RESOURCE_MAPPINGS_KEY = 'AWSResourceMappings'
  12. AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY = 'AccountId'
  13. AWS_RESOURCE_MAPPINGS_REGION_KEY = 'Region'
  14. class ResourceMappings:
  15. """
  16. ResourceMappings class that handles writing Cloud formation outputs to resource mappings json file in a project.
  17. """
  18. def __init__(self, file_path: str, region: str, feature_name: str, account_id: str, cloud_formation_client):
  19. """
  20. :param file_path: Path for the resource mapping file.
  21. :param region: Region value for the resource mapping file.
  22. :param feature_name: Feature gem name to use to append name to mappings key.
  23. :param account_id: AWS account id value for the resource mapping file.
  24. :param cloud_formation_client: AWS cloud formation client.
  25. """
  26. self._resource_mapping_file_path = file_path
  27. self._region = region
  28. self._feature_name = feature_name
  29. self._account_id = account_id
  30. self._resource_mappings = {}
  31. assert os.path.exists(self._resource_mapping_file_path), \
  32. f'Invalid resource mapping file path {self._resource_mapping_file_path}'
  33. self._client = cloud_formation_client
  34. def populate_output_keys(self, stacks=None) -> None:
  35. """
  36. Calls describe stacks on cloud formation service and persists outputs to resource mappings file.
  37. :param stacks List of stack arns to describe and populate resource mappings with.
  38. """
  39. for stack_name in stacks:
  40. response = self._client.describe_stacks(
  41. StackName=stack_name
  42. )
  43. stacks = response.get('Stacks', [])
  44. assert len(stacks) == 1, f'{stack_name} is invalid.'
  45. self._write_resource_mappings(stacks[0].get('Outputs', []))
  46. def _write_resource_mappings(self, outputs, append_feature_name=True) -> None:
  47. with open(self._resource_mapping_file_path) as file_content:
  48. resource_mappings = json.load(file_content)
  49. resource_mappings[AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY] = self._account_id
  50. resource_mappings[AWS_RESOURCE_MAPPINGS_REGION_KEY] = self._region
  51. # Append new mappings.
  52. resource_mappings[AWS_RESOURCE_MAPPINGS_KEY] = resource_mappings.get(AWS_RESOURCE_MAPPINGS_KEY, {})
  53. for output in outputs:
  54. if append_feature_name:
  55. resource_key = f'{self._feature_name}.{output.get("OutputKey", "InvalidKey")}'
  56. else:
  57. resource_key = output.get("OutputKey", "InvalidKey")
  58. resource_mappings[AWS_RESOURCE_MAPPINGS_KEY][resource_key] = resource_mappings[
  59. AWS_RESOURCE_MAPPINGS_KEY].get(resource_key, {})
  60. resource_mappings[AWS_RESOURCE_MAPPINGS_KEY][resource_key]['Type'] = 'AutomationTestType'
  61. resource_mappings[AWS_RESOURCE_MAPPINGS_KEY][resource_key]['Name/ID'] = output.get('OutputValue',
  62. 'InvalidId')
  63. self._resource_mappings = resource_mappings
  64. with open(self._resource_mapping_file_path, 'w') as file_content:
  65. json.dump(resource_mappings, file_content, indent=4)
  66. def clear_output_keys(self) -> None:
  67. """
  68. Clears values of all resource mapping keys. Sets region to default to us-west-2
  69. """
  70. with open(self._resource_mapping_file_path) as file_content:
  71. resource_mappings = json.load(file_content)
  72. resource_mappings[AWS_RESOURCE_MAPPINGS_ACCOUNT_ID_KEY] = ''
  73. resource_mappings[AWS_RESOURCE_MAPPINGS_REGION_KEY] = constants.AWS_REGION
  74. # Append new mappings.
  75. resource_mappings[AWS_RESOURCE_MAPPINGS_KEY] = resource_mappings.get(AWS_RESOURCE_MAPPINGS_KEY, {})
  76. resource_mappings[AWS_RESOURCE_MAPPINGS_KEY] = {}
  77. with open(self._resource_mapping_file_path, 'w') as file_content:
  78. json.dump(resource_mappings, file_content, indent=4)
  79. self._resource_mapping_file_path = ''
  80. self._region = ''
  81. self._client = None
  82. def get_resource_name_id(self, resource_key: str):
  83. return self._resource_mappings[AWS_RESOURCE_MAPPINGS_KEY][resource_key]['Name/ID']
  84. def clear_select_keys(self, resource_keys=None) -> None:
  85. """
  86. Clears values from select resource mapping keys.
  87. :param resource_keys: list of keys to clear out
  88. """
  89. with open(self._resource_mapping_file_path) as file_content:
  90. resource_mappings = json.load(file_content)
  91. for key in resource_keys:
  92. resource_mappings[key] = ''
  93. with open(self._resource_mapping_file_path, 'w') as file_content:
  94. json.dump(resource_mappings, file_content, indent=4)